Prompt-to-Prompt Image Editing with Cross Attention Control
Edit images in text-to-image diffusion models without fine-tuning by injecting and manipulating cross-attention maps during the generation process.
Paper: Prompt-to-Prompt Image Editing with Cross Attention Control
Authors: Amir Hertz, Ron Mokady, Jay Tenenbaum, Kfir Aberman, Yael Pritch, Daniel Cohen-Or · 2022
Read the paperThe Problem
Text-to-image diffusion models generate stunning images from text, but making a localized edit to an already generated image is notoriously difficult. If you generate "A fluffy cat riding a bicycle" and then simply change the prompt to "A fluffy dog riding a bicycle", the random seed combined with the new prompt results in a completely different image layout, background, and composition. Prior approaches to localized editing required expensive model fine-tuning, masked inpainting (which requires manual user effort), or complex optimization processes per image.
The Idea
Researchers at Google and Tel Aviv University found that the spatial layout and structure of a generated image are deeply entangled with the model's cross-attention maps—the internal matrices that bind specific tokens in the text prompt to specific spatial regions in the image. They realized that by simply replacing the cross-attention maps of a new, edited prompt with the maps recorded from the original prompt, they could force the new generation to preserve the original image's structural layout while manifesting the new text.
How It Works
The method operates directly inside the diffusion model's generation loop without any training or fine-tuning.
Cross-Attention Extraction During the generation of the original image, the text encoder converts the prompt (e.g., "A fluffy cat") into embeddings. At each denoising step, the U-Net's cross-attention layers compute maps that represent the attention each pixel pays to each text token. These maps dictate the structure (where the "cat" appears). P2P records these maps at every step.
Cross-Attention Injection When generating the edited image with a modified prompt (e.g., "A fluffy dog"), P2P intervenes in the U-Net. Instead of letting the model compute new attention maps organically, P2P overwrites the new maps with the saved maps from the original run.
Targeted Map Editing To ensure the new concept ("dog") is properly rendered while keeping the rest of the image identical, P2P uses specific injection strategies:
- For tokens shared between the two prompts, it injects the original maps exactly.
- For a replaced word (Word Swap), it aligns the new token's attention map with the original token's map, ensuring the "dog" appears exactly where the "cat" was.
- It also supports localized blending and global style editing by weighting how many steps the original maps are injected before letting the model take over.
Why It Mattered
Prompt-to-Prompt proved that massive diffusion models inherently possess zero-shot editing capabilities hidden within their attention layers. It enabled users to perform complex, localized, and global edits using only text, fundamentally shifting image editing from pixel manipulation to attention manipulation. It provided a mathematically elegant, compute-free way to edit images without touching the model's weights.
What Came After
P2P sparked an entire sub-field of attention-based editing in diffusion models. It directly laid the foundation for Null-Text Inversion (which allowed P2P to be used on real, user-provided photos instead of just generated ones) and inspired instruct-based editing models like InstructPix2Pix. The core mechanic of cross-attention control became a staple in generative editing pipelines.