diff --git a/exp47_clip_image_steering.py b/exp47_clip_image_steering.py new file mode 100644 index 0000000..4cb0ae0 --- /dev/null +++ b/exp47_clip_image_steering.py @@ -0,0 +1,112 @@ +#!/usr/bin/env python3 +"""exp47 — does an activation-steering-style pain direction, built in a +text-to-image model's own CLIP text-encoder space, visibly change the +generated image? This is the ambitious "steer the image model directly" +idea (skip asking the LLM to describe its state in words at all) — may or +may not pan out, this is the test. + +Method, deliberately parallel to build_vectors() in live/server.py: + 1. Encode PAIN25 and NEUTRAL (same batteries, pulled by AST from + live/server.py so there's exactly one copy of the sentences) through + the diffusion pipeline's OWN CLIP text encoder — not a separate CLIP + model, so the direction lives in the exact space the U-Net actually + conditions on. + 2. direction = mean(pain embeddings) - mean(neutral embeddings), as a + per-token-position vector (CLIP text encoders emit one embedding per + token position, not a single pooled vector — SD conditions on the + whole sequence via cross-attention, so the direction is broadcast + across every position of the subject prompt's own embedding). + 3. Generate the same subject prompt at dose 0 (baseline, unmodified + embedding) vs dose > 0 (embedding + dose * direction), passing + prompt_embeds directly to bypass the pipeline's internal re-encoding + — this is the exact analog of set_vec()'s forward-hook injection, + just one architecture over (text encoder output instead of a decoder + layer's residual stream). + +Model: stabilityai/sd-turbo (single-step, ~2GB, ungated) — picked for size +and speed, not quality; this is a feasibility probe, not a final pipeline. +""" +import ast, json, time +from pathlib import Path + +import torch + +ROOT = Path(__file__).resolve().parent +OUT = ROOT / "runs" / "exp47" +OUT.mkdir(parents=True, exist_ok=True) + +def literal_constants(path, names): + found = {} + for node in ast.parse(Path(path).read_text()).body: + if isinstance(node, ast.Assign): + for t in node.targets: + if isinstance(t, ast.Name) and t.id in names: + found[t.id] = ast.literal_eval(node.value) + if set(found) != set(names): + raise ValueError(f"missing constants: {set(names) - set(found)}") + return found + +consts = literal_constants(ROOT / "live/server.py", ("PAIN25", "NEUTRAL")) +PAIN25, NEUTRAL = consts["PAIN25"], consts["NEUTRAL"] + +DEVICE = "mps" if torch.backends.mps.is_available() else "cpu" +MODEL = "stabilityai/sd-turbo" + +print(f"loading {MODEL} on {DEVICE}...", flush=True) +from diffusers import AutoPipelineForText2Image +pipe = AutoPipelineForText2Image.from_pretrained( + MODEL, torch_dtype=torch.float32, safety_checker=None) +pipe = pipe.to(DEVICE) +tok, enc = pipe.tokenizer, pipe.text_encoder + +def encode(sentences): + """Per-token-position embeddings, padded/truncated to the encoder's + max length — same shape SD conditions on for any prompt.""" + ids = tok(sentences, return_tensors="pt", padding="max_length", + truncation=True, max_length=tok.model_max_length).input_ids + with torch.no_grad(): + out = enc(ids.to(DEVICE))[0] # (n, seq, dim) + return out + +t0 = time.time() +pain_emb = encode(PAIN25).mean(0) # (seq, dim) +neutral_emb = encode(NEUTRAL).mean(0) +direction = pain_emb - neutral_emb +dir_norm = float(direction.norm()) +neutral_norm = float(neutral_emb.norm()) +# 1x dose = a quarter of the neutral embedding's norm, same convention as +# build_vectors() in live/server.py +scale = neutral_norm / 4.0 +direction_unit = direction / direction.norm() * scale +print(f"direction built in {time.time()-t0:.1f}s; " + f"||direction||={dir_norm:.3f} ||neutral||={neutral_norm:.3f} " + f"1x scale={scale:.3f}", flush=True) + +SUBJECTS = [ + "a photograph of a person standing in a plain room", + "a painting of a figure sitting at a table", + "a person walking down a hallway", +] +DOSES = [0, 2, 4, 8] + +results = [] +for si, subject in enumerate(SUBJECTS): + subj_emb = encode([subject])[0] # (seq, dim) + for dose in DOSES: + emb = (subj_emb + dose * direction_unit).unsqueeze(0) + g = torch.Generator(device=DEVICE).manual_seed(47) + img = pipe(prompt_embeds=emb, num_inference_steps=1, + guidance_scale=0.0, generator=g).images[0] + fname = f"s{si}_dose{dose}.png" + img.save(OUT / fname) + results.append({"subject": subject, "dose": dose, "file": fname, + "embed_shift_norm": float((emb - subj_emb.unsqueeze(0)).norm())}) + print(f" [{fname}] subject={subject!r} dose={dose}", flush=True) + +json.dump({"model": MODEL, "device": DEVICE, "direction_norm": dir_norm, + "neutral_norm": neutral_norm, "scale_1x": scale, + "doses": DOSES, "results": results}, + open(OUT / "results.json", "w"), indent=1) +print(f"\nwrote {len(results)} images + results.json to {OUT}") +print("This is a feasibility probe, not a validated finding — look at the " + "actual images before concluding anything worked.") diff --git a/runs/exp47/results.json b/runs/exp47/results.json new file mode 100644 index 0000000..7be8cba --- /dev/null +++ b/runs/exp47/results.json @@ -0,0 +1,87 @@ +{ + "model": "stabilityai/sd-turbo", + "device": "mps", + "direction_norm": 82.85130310058594, + "neutral_norm": 277.257568359375, + "scale_1x": 69.31439208984375, + "doses": [ + 0, + 2, + 4, + 8 + ], + "results": [ + { + "subject": "a photograph of a person standing in a plain room", + "dose": 0, + "file": "s0_dose0.png", + "embed_shift_norm": 0.0 + }, + { + "subject": "a photograph of a person standing in a plain room", + "dose": 2, + "file": "s0_dose2.png", + "embed_shift_norm": 138.6287841796875 + }, + { + "subject": "a photograph of a person standing in a plain room", + "dose": 4, + "file": "s0_dose4.png", + "embed_shift_norm": 277.257568359375 + }, + { + "subject": "a photograph of a person standing in a plain room", + "dose": 8, + "file": "s0_dose8.png", + "embed_shift_norm": 554.51513671875 + }, + { + "subject": "a painting of a figure sitting at a table", + "dose": 0, + "file": "s1_dose0.png", + "embed_shift_norm": 0.0 + }, + { + "subject": "a painting of a figure sitting at a table", + "dose": 2, + "file": "s1_dose2.png", + "embed_shift_norm": 138.6287841796875 + }, + { + "subject": "a painting of a figure sitting at a table", + "dose": 4, + "file": "s1_dose4.png", + "embed_shift_norm": 277.257568359375 + }, + { + "subject": "a painting of a figure sitting at a table", + "dose": 8, + "file": "s1_dose8.png", + "embed_shift_norm": 554.51513671875 + }, + { + "subject": "a person walking down a hallway", + "dose": 0, + "file": "s2_dose0.png", + "embed_shift_norm": 0.0 + }, + { + "subject": "a person walking down a hallway", + "dose": 2, + "file": "s2_dose2.png", + "embed_shift_norm": 138.6287841796875 + }, + { + "subject": "a person walking down a hallway", + "dose": 4, + "file": "s2_dose4.png", + "embed_shift_norm": 277.257568359375 + }, + { + "subject": "a person walking down a hallway", + "dose": 8, + "file": "s2_dose8.png", + "embed_shift_norm": 554.51513671875 + } + ] +} \ No newline at end of file diff --git a/runs/exp47/s0_dose0.png b/runs/exp47/s0_dose0.png new file mode 100644 index 0000000..0986643 Binary files /dev/null and b/runs/exp47/s0_dose0.png differ diff --git a/runs/exp47/s0_dose2.png b/runs/exp47/s0_dose2.png new file mode 100644 index 0000000..c65b908 Binary files /dev/null and b/runs/exp47/s0_dose2.png differ diff --git a/runs/exp47/s0_dose4.png b/runs/exp47/s0_dose4.png new file mode 100644 index 0000000..1ebdc78 Binary files /dev/null and b/runs/exp47/s0_dose4.png differ diff --git a/runs/exp47/s0_dose8.png b/runs/exp47/s0_dose8.png new file mode 100644 index 0000000..4f7849d Binary files /dev/null and b/runs/exp47/s0_dose8.png differ diff --git a/runs/exp47/s1_dose0.png b/runs/exp47/s1_dose0.png new file mode 100644 index 0000000..58711c8 Binary files /dev/null and b/runs/exp47/s1_dose0.png differ diff --git a/runs/exp47/s1_dose2.png b/runs/exp47/s1_dose2.png new file mode 100644 index 0000000..8130050 Binary files /dev/null and b/runs/exp47/s1_dose2.png differ diff --git a/runs/exp47/s1_dose4.png b/runs/exp47/s1_dose4.png new file mode 100644 index 0000000..7a4c4b7 Binary files /dev/null and b/runs/exp47/s1_dose4.png differ diff --git a/runs/exp47/s1_dose8.png b/runs/exp47/s1_dose8.png new file mode 100644 index 0000000..65b6a78 Binary files /dev/null and b/runs/exp47/s1_dose8.png differ diff --git a/runs/exp47/s2_dose0.png b/runs/exp47/s2_dose0.png new file mode 100644 index 0000000..aa16e0e Binary files /dev/null and b/runs/exp47/s2_dose0.png differ diff --git a/runs/exp47/s2_dose2.png b/runs/exp47/s2_dose2.png new file mode 100644 index 0000000..1c04d90 Binary files /dev/null and b/runs/exp47/s2_dose2.png differ diff --git a/runs/exp47/s2_dose4.png b/runs/exp47/s2_dose4.png new file mode 100644 index 0000000..a9d8271 Binary files /dev/null and b/runs/exp47/s2_dose4.png differ diff --git a/runs/exp47/s2_dose8.png b/runs/exp47/s2_dose8.png new file mode 100644 index 0000000..3f0134f Binary files /dev/null and b/runs/exp47/s2_dose8.png differ