# Prompt-to-Prompt Image Editing with Cross Attention Control

Prompt-to-prompt editing changes images by rewriting only the text prompt

Amir Hertz, Ron Mokady, Jay Tenenbaum, Kfir Aberman, Yael Pritch, Daniel Cohen-Or · arXiv · 2022-8-2

- **Problem** Text-conditioned generators change the whole picture when a prompt changes slightly. Existing text-driven editors need a user-drawn mask, which discards the structure inside it.
- **Method** Prompt-to-prompt editing runs the source and edited prompts with the same seed. It injects the source cross-attention maps into the edited run, instead of asking for a spatial mask.
- **Results** Injecting the source maps keeps the lemon cake layout when lemon is replaced by pumpkin. Regenerating with the same seed and the new prompt gives a different geometry.

## Why text edits break generated images

A text-conditioned generator can produce a completely different picture when only a few words of the prompt change, yet an editing tool is expected to keep most of the original image. Existing approaches sidestep this by asking the user for a spatial mask around the region to change, so the content inside the mask is discarded and the editor works only on the masked area.

Inpainting-based methods of this kind keep the background of the original image while altering the masked region, but the mask has to be drawn by hand and the masked content no longer informs the result. Edits that depend on the internal structure of an object, such as changing its texture, fall outside what inpainting can express.

This work instead edits an image through the prompt alone, with no mask, no model training and no extra data. The approach relies on the internal attention of the generator, so the user changes words rather than pixels.

## Cross-attention as the handle on layout

The internal cross-attention maps are high-dimensional tensors that link pixels to the tokens of the prompt, and they carry rich semantic relations that strongly affect the generated image. The central observation is that the composition and geometry of an image depend on these maps, not only on the random seed.

Keeping the seed fixed and changing the prompt is not sufficient: the regenerated image has a different structure and composition from the original. This points to the pixel-to-text interaction inside the cross-attention layers as the place where layout is decided.

Averaged over timesteps, the maps for the words "bear" and "bird" light up the pixels that show those objects, so each pixel attends most to the words that describe it. The averaging is only a visualization; the method keeps the maps separate for each head. The overall structure of the image is already settled in the early diffusion steps, which is why the maps of early steps matter most for editing.

## How the attention maps are computed in Imagen

The backbone is Imagen, a text-guided synthesis model. The method adapts only the text-to-image diffusion stage, because composition and geometry are mostly fixed at the 64× 64 resolution, and the super-resolution stages are used as they are. At each diffusion step, a U-shaped network predicts the noise from the noisy image and the text embedding, and the visual and textual features are fused inside cross-attention layers.

The deep spatial features of the noisy image give the queries, while the text embedding gives the keys and the values, all through learned linear projections. The attention map is a softmax over the scaled product of queries and keys, as the following formula gives, and each entry is the weight that a pixel places on the value of one token.

\\\[ M = \\mathrm{Softmax}\\left(\\frac{QK^{T}}{\\sqrt{d}}\\right) \\\] Here \\(d\\) is the projection dimension of the keys and queries, and the cross-attention output is \\(MV\\), which updates the spatial features.

The model also uses hybrid attention, which concatenates the text embedding to the keys and values of self-attention layers. The method modifies only the channels that correspond to text tokens inside these hybrid layers, so the same treatment as ordinary cross-attention applies.

## Running two prompts through one denoising loop

One diffusion step takes a noisy latent and a prompt and returns the latent of the next step together with the attention map of that step; the method can also override that map with a supplied one while keeping the values taken from the prompt. An edit function receives the map of the source run and the map of the edited run at the same step and returns the map that the edited run will use.

The two generations run together from the same initial noise, which must be held fixed, since even the same prompt with two different seeds yields very different images. At every step the source map and the edited map are computed, the edit function combines them, and the edited latent is advanced with the combined map. The edit function can be applied inside the diffusion forward call, and both latents can be processed as one batch, so the overhead is about one extra step.

Because the loop itself is shared, switching between editing tasks only changes the edit function. The next section gives the edit function for each of the three tasks.

## Word swap, new phrases and re-weighting

For a word swap, the source maps are injected into the run with the modified prompt, so the layout of the original is kept while the new word fills the content. Injecting throughout all steps can over-constrain the geometry when the swap is structural, for example car to bicycle, so the injection is limited to the steps before a timestamp τ.

Tokens that span a different number of subword tokens in the two prompts have their maps duplicated or averaged through an alignment step.

When a phrase is added, an alignment function maps each token index of the edited prompt to a matching index of the source prompt, or to nothing when no match exists. Matched tokens take the source map at their aligned index, while unmatched new tokens keep the map of the edited run, so the added words can add new content.

For re-weighting, the attention map of one chosen token is multiplied by a scalar c, and all other maps stay unchanged. The scalar is taken from the range \[−2, 2\], so the effect of that word can be strengthened or weakened.

## Replacing words and the number of injection steps

In the lemon cake example, replacing lemon with pumpkin keeps the spatial layout and background, and the lemons at the top left turn into pumpkins. Regenerating with the prompt pumpkin cake and the same seed gives a completely different geometry.

Injecting only the attention of the word butterfly keeps that object while the rest of the scene changes, and the butterfly sits on each new object in a plausible manner.

Without injection, changing one word leads to an entirely different outcome, and the fidelity to the source image increases with the number of steps that receive injection. The authors observe that the best result is not necessarily obtained with injection in all steps, so the number of injected steps gives the user a further control. Injection in every step over-constrains the car-to-bicycle case, where the car becomes a bicycle.

## Adding specifications, styles and fader control

When a specification is added, the attention of the original prompt is kept and the new words are free to act, so the car gains details such as being crushed while the background stays. Global edits affect every part of the image but keep its composition, as when snow is added or the lighting changes. Stylization applies the same logic: a style description added to the prompt, with the source maps injected, changes the look while the structure of the original image is kept.

Some controls are hard to express in words, such as how much snow a mountain carries, so re-scaling the attention of the specified word offers a fader. Reducing or increasing the cross-attention of a marked word changes the extent to which that word influences the generated image.

## Extending the loop to real images through inversion

Editing a real photograph requires an initial noise vector that reproduces the input when fed to the diffusion process, a step called inversion. A naive inversion adds Gaussian noise to the input and runs a set number of denoising steps, but this produces significant distortions.

This often gives satisfying results, but in other cases the inversion is not accurate enough.

To restore the unedited regions, a mask is extracted from the attention maps, with no guidance from the user. In the cat example, this works even with the naive DDPM inversion scheme, and the cat's identity is kept under various editing operations.

## Limitations

The current inversion of real images shows visible distortion on some test images. The user also has to come up with a suitable prompt for the image, which may be hard for complicated compositions.

Lowering the classifier-free guidance parameter improves reconstruction, but it also reduces the prompt's influence and limits significant manipulations, which the authors describe as a distortion-editability tradeoff.

Cross-attention sits in the network's bottleneck, so the attention maps are of low resolution, which bounds how precise localized editing can be. The authors suggest adding cross-attention to higher-resolution layers as a future step, which they did not test. The method cannot spatially move existing objects across the image, and this control is left for future work.

Showing these edits relies on example images, and the text reports no quantitative metric for edit quality.

## Paper info

- **Authors**: Amir Hertz, Ron Mokady, Jay Tenenbaum, Kfir Aberman, Yael Pritch, Daniel Cohen-Or
- **Venue**: arXiv · Preprint
- **First published**: 2022-8-2
- **Added**: 2026-11-12
- **Updated**: 2022-8-2
- **Version**: v1
- **arXiv**: 2208.01626
- **Category**: cs.CV · cs.CL · cs.GR · cs.LG
- **Original paper terms**: [arXiv non-exclusive 1.0](https://arxiv.org/licenses/nonexclusive-distrib/1.0/)
- **What we show**: Paper info and commentary

[Original paper ↗](https://arxiv.org/abs/2208.01626v1) · [PDF ↗](https://arxiv.org/pdf/2208.01626v1)

AI commentary is a separate explanation from the original paper and may contain errors. The original paper and figures follow each source's terms of use and any rights noted separately.

## Quote from the paper

Amir Hertz, Ron Mokady, Jay Tenenbaum, Kfir Aberman, Yael Pritch, Daniel Cohen-Or (2022). Prompt-to-Prompt Image Editing with Cross Attention Control. arXiv:2208.01626v1. https://arxiv.org/abs/2208.01626v1

**BibTeX**

```
@misc{hertz2022prompt,
  title = {{Prompt-to-Prompt Image Editing with Cross Attention Control}},
  author = {Amir Hertz and Ron Mokady and Jay Tenenbaum and Kfir Aberman and Yael Pritch and Daniel Cohen-Or},
  year = {2022},
  eprint = {2208.01626},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2208.01626v1}
}
```

## Related papers

- [InstructPix2Pix: Learning to Follow Image Editing Instructions](https://refivu.com/en/papers/b87997ee-e5f2-4a2e-9b66-e15df3a71be7-instructpix2pix-learning-to-follow-image-editing.md)
- [Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding](https://refivu.com/en/papers/eaa0c3ba-e0e4-4517-8ef7-d6ec6ba261b6-photorealistic-text-to-image-diffusion-models-with-deep.md)
- [GLIDE: Towards Photorealistic Image Generation and Editing with Text-Guided Diffusion Models](https://refivu.com/en/papers/85a30c3b-b6e0-4eaf-8fb4-fe6a473a1c05-glide-towards-photorealistic-image-generation-and-editing.md)
- [DreamFusion: Text-to-3D using 2D Diffusion](https://refivu.com/en/papers/8748788c-c1b0-47b9-a624-78c1f27630e6-dreamfusion-text-to-3d-using-2d-diffusion.md)
- [DreamBooth: Fine Tuning Text-to-Image Diffusion Models for Subject-Driven Generation](https://refivu.com/en/papers/be406a52-c06c-4d4e-849c-ca0a795fcb8a-dreambooth-fine-tuning-text-to-image-diffusion-models.md)
- [Diffusion Models Beat GANs on Image Synthesis](https://refivu.com/en/papers/c4296a34-b538-4422-b11f-4a805621a24f-diffusion-models-beat-gans-on-image-synthesis.md)

## Other paper commentary

- [TotalSegmentator: robust segmentation of 104 anatomical structures in CT images](https://refivu.com/en/papers/38d65e87-91c3-4285-bca2-9755328de70d-totalsegmentator-robust-segmentation-of-104-anatomical.md)
- [RMPE: Regional Multi-person Pose Estimation](https://refivu.com/en/papers/a258675a-c286-4c65-9760-3d72305d7120-rmpe-regional-multi-person-pose-estimation.md)
- [Memorizing Normality to Detect Anomaly: Memory-augmented Deep Autoencoder for Unsupervised Anomaly Detection](https://refivu.com/en/papers/b6435fab-e010-4a4f-aeb8-b125ca06e94a-memorizing-normality-to-detect-anomaly-memory-augmented-deep.md)
- [LLNet: A Deep Autoencoder Approach to Natural Low-light Image Enhancement](https://refivu.com/en/papers/1b58b4f0-48b5-40db-be28-ecd8cf270c6f-llnet-a-deep-autoencoder-approach-to-natural-low.md)
- [MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer](https://refivu.com/en/papers/c76255ba-9c87-4edc-80cc-28d59c4ccfc5-mobilevit-light-weight-general-purpose-and-mobile-friendly.md)
- [FairMOT: On the Fairness of Detection and Re-Identification in Multiple Object Tracking](https://refivu.com/en/papers/081a3a22-90c6-4c70-a7df-d2e70b35ad22-fairmot-on-the-fairness-of-detection-and-re.md)
- [Named Entity Recognition with Bidirectional LSTM-CNNs](https://refivu.com/en/papers/454922d0-d836-4e69-a3cd-d40cf940d456-named-entity-recognition-with-bidirectional-lstm-cnns.md)
- [Transferability in Machine Learning: from Phenomena to Black-Box Attacks using Adversarial Samples](https://refivu.com/en/papers/0070f617-bee7-4459-b94e-a20f7c1d2de5-transferability-in-machine-learning-from-phenomena-to-black.md)
- [Conditional Adversarial Domain Adaptation](https://refivu.com/en/papers/a233380e-bb76-4fca-bc73-df4e86a220e3-conditional-adversarial-domain-adaptation.md)
- [Generalized Cross Entropy Loss for Training Deep Neural Networks with Noisy Labels](https://refivu.com/en/papers/41c59d55-611c-4abf-a4e2-459eb900711f-generalized-cross-entropy-loss-for-training-deep-neural.md)
- [Speech Commands: A Dataset for Limited-Vocabulary Speech Recognition](https://refivu.com/en/papers/4809d645-7dd0-42ce-ba15-4c5208cfa877-speech-commands-a-dataset-for-limited-vocabulary-speech.md)

## Terms in this paper

- [classifier-free guidance](https://refivu.com/en/terms/7f72cec70f8e-classifier-free-guidance)
- [diffusion models](https://refivu.com/en/terms/39b044617fcd-diffusion-models)
- [DDIM](https://refivu.com/en/terms/6c24451d09a5-ddim)
- [DDPM](https://refivu.com/en/terms/f18692fc86b6-ddpm)
- [Imagen](https://refivu.com/en/terms/9082d2027b0a-imagen)
- [inversion](https://refivu.com/en/terms/21403fae189b-inversion)
- [Prompt-to-Prompt](https://refivu.com/en/terms/f6e66b1809fb-prompt-to-prompt)

---

AI-written text can contain mistakes. [Refivu](https://refivu.com/en/papers/a18cb3bd-2a1b-4adf-9a2a-f866c7f83edf-prompt-to-prompt-image-editing-with-cross-attention)
