← Back
Diffusion Detective: Interpretability Tooling for a Generative Model
Real-time extraction of internal representations (cross-attention), causal steering of behavior via representation-space interventions (94% success), and automated natural-language explanation of what each intervention did, mechanically a precursor to LLM activation steering and patchscopes-style self-explanation.
Stable Diffusion 2.1 PyTorch FastAPI React CLIP GPT-4o-mini Python 67% / JS 31%
Diffusion Detective interface
100%
Attention extraction accuracy (MAE < 10⁻⁶)
94%
Semantic steering success rate
2.7%
Overhead from attention extraction
250ms
Narrative generation (GPT-4o-mini)

What It Does

Diffusion Detective makes Stable Diffusion interpretable. For any generated image it provides three capabilities that standard diffusion pipelines lack entirely:

Zero-Approximation Attention Extraction

Cross-attention probabilities are extracted directly from the UNet's transformer layers at every denoising timestep: no approximation, validated via probability sum = 1.0 (MAE < 10⁻⁶). Shows exactly which tokens the model attends to while drawing each part of the image.

Semantic Steering via CLIP Algebra

Edit images after the prompt, without rerunning: by adding or subtracting CLIP embedding vectors to the latent space during denoising. E.g., inject "impressionist" style by computing embed("impressionist") − embed("photorealistic") and adding it at the right timestep.

LLM-Powered Explanations

GPT-4o-mini reads the raw attention logs and writes a three-stage narrative: Setup (what the model focused on first), Comparison (how intervention changed attention), Insight (what it means). Human-readable transparency.

How It Works

Attention extraction hooks into the UNet's attention computation during each denoising step:

# During denoising step t attention_probs = softmax(Q @ K.T / sqrt(d_k)) # [H, W, num_tokens] # Hooked and stored at every timestep: zero approximation

Semantic steering uses CLIP embedding arithmetic to compute a steering vector applied mid-generation:

steering_vector = clip_encode(attribute) - clip_encode(concept) latents_t = latents_t + steering_vector * strength # Optimal window: steps 40–20 (semantic attributes form before fine details)

The React frontend shows side-by-side comparison of the baseline and steered image, with the attention heatmap and narrative overlaid.

Architecture

Connection to LLM Interpretability

The three capabilities in this system map directly onto core LLM interpretability methods:

Standard diffusion models are black boxes. Diffusion Detective is a working prototype of an interpretability toolkit for such a system, and the transferable skill is exactly the pipeline that LLM interpretability research builds on: hook into internals → intervene causally → explain in natural language.