A diffusion model starts from random noise and improves the whole image at once over a series of steps, guided by the prompt at every step. One real Stable Diffusion run shows how the prompt shapes the image, which areas of the image each prompt word influences, and how a new prompt changes a finished image.
In plain wordsLike an illustrator who keeps a written description on the desk and looks at it again on every pass over the sketch. First they decide where everything goes, then refine the shapes and add the details. The more you insist that they follow the description, the more faithfully they do so, but the stiffer and more garish the drawing becomes.
From noise to an image in 20 steps
startstep 1step 4step 20gridpreviewnone
The model denoises a grid, not pixels: an array of 64×64 cells with 4 numbers each, which a separate network, the autoencoder’s decoder, turns into a 512×512 image. The top row is the grid after each step, shown through the decoder, which is why the start looks like coloured noise. The sampler, the algorithm that performs a step, removes only part of the noise at each step. The bottom row is the preview: the finished image the model expects at that step. After the first step the grid is still noise, but the preview already has a layout: sky at the top, a dark cliff at the bottom and the red blob of a lighthouse in the middle. The preview is blurred because from pure noise the model can predict only an averaged image, and that is why one step is not enough. After the fourth step the shapes are clear, and the last steps add detail such as the texture of the rocks and the foam.
Same prompt, different settings
The guidance scale controls how strongly the prompt influences the image, and the seed is the number that determines the starting noise; both are covered in the section “Guidance and seed” below.
Guidance 1 the prompt is not amplified: a pale image and a different layoutGuidance 3 natural coloursGuidance 7.5 the default for SD 1.5 in the diffusers library, the same image as at step 20Guidance 20 oversaturated colours, harsh contrastSeed 8 different starting noise, different layoutSeed 9 another layout again
Real Stable Diffusion 1.5 (2022) images. Prompt: “a red lighthouse on a rocky cliff above a stormy sea, photograph”, in English because the model was trained on English captions. The model generated a 512×512-pixel image, shown here at 256×256; 20 steps of the DDIM sampler, guidance 7.5 and seed 7 unless the caption says otherwise. The basics shared by images, video and audio, including the grid and the autoencoder, are covered in “How models generate images, video and audio”.
The denoising loop and how the generator is trained
What happens to the prompt and the noise
Text encoder. In Stable Diffusion 1.5 it is the CLIP model (OpenAI, 2021). Unlike in an LLM, the text is a separate input and the image is not a continuation of it. The CLIP tokeniser splits this prompt into 13 tokens, adds start and end markers and pads the result to 77 positions; it cuts a longer prompt off after 75 content tokens. The encoder turns each position into a vector of 768 numbers. These vectors are computed once and fed into every step.
aredlighthouseonarockycliffaboveastormysea,photograph+ start, end and 62 padding positions
The loop, 20 times. At the start the 64×64×4 grid is random noise. The grid has 64×64 cells with 4 numbers each, and each cell corresponds to a square of 8×8 pixels. It can also be described as 4 channels of 64×64 numbers. An RGB image has 3 channels, one per colour, but the grid’s channels do not stand for colours. Each channel is shown here as a grey square: the brighter the point, the larger the number. At every step the generator receives the grid, the noise level and the prompt vectors, and predicts the noise. In SD 1.5 the generator is a U-Net, described below. With guidance it does this twice per step, and the sampler combines the two predictions (section “Guidance and seed”). Subtracting all of the predicted noise and rescaling leaves a grid that decodes to the preview of the finished image, the bottom row in the figure above. The preview is only a visualisation: what goes into the next step is the grid with part of the noise removed by the sampler.
the grid entering step 4: still mostly noise→ previewthe preview from step 4
The end. After 20 steps the grid is denoised and the shape of the image is visible in it. The autoencoder’s decoder turns it into a 512×512 image.
the grid after 20 stepsdecoder →the finished image
Stable Diffusion 1.5 (2022) is an old model. It is the example here because it is small enough for the whole run to be computed on an ordinary CPU and shown step by step. Newer open models such as SD3 and FLUX have the same loop and differ in three places: the generator, the training target and the text encoder.
Generator: newer models use a transformer in place of the U-Net, an older convolutional network that processes the grid through small windows and uses attention in only some of its layers. Training target: they predict the direction in which the grid should change, not the noise (flow matching, described below). Text encoder: a larger language model (T5 in SD3 and FLUX.1) works alongside CLIP and its 77 positions, so the model accepts longer prompts, up to 512 tokens for FLUX.1 dev in the diffusers library. FLUX.2 (2025) replaces both encoders with a single Mistral model, also limited to 512 tokens. OpenAI has not published the architecture of GPT Image, nor Google that of Nano Banana Pro; OpenAI has said only that image generation in GPT-4o is autoregressive.
Which parts of the image the model ties each prompt word to
At every step the generator computes, for each patch of the grid, attention weights over the prompt vectors (cross-attention: the mechanism from “Attention”, with queries coming from the grid and keys and values from the prompt). Brighter areas have a higher attention weight for the given word: “lighthouse” and “red” land on the lighthouse, “cliff” on the rocks, “sea” on the water, “stormy” on the sky and the sea. This is the path by which the prompt affects what appears where.
The maps come from the U-Net layers that work on the grid reduced to 16×16, which is 256 patches (inside, the U-Net shrinks the grid and then enlarges it again), and are averaged over all steps. Each map is one column of the attention matrix: the rows are those 256 patches, the columns are the 77 prompt positions. Most of the weight, 88% on average, goes to the start marker, like the start token in “Attention” (an attention sink); each of the five words shown gets between 0.1 and 2%, and two to three times more in steps 1–4 than later. Each map has its own brightness scale, so brightness cannot be compared between the maps of different words. A map is a hint about where the model used a word, not proof of which pixel depends on it. SD3 and FLUX have no separate cross-attention: the prompt tokens sit in one sequence with the image tokens and ordinary attention connects them.
During training a model predicts a known target: in an LLM it is the next token, and here it is noise. The prediction error is used to adjust the weights (see “Training”). Noise at a random level is added to the grid of a real image, and the Stable Diffusion 1.5 generator learns to predict which noise was added. Each training example covers one noise level, and after hundreds of millions of examples the generator can denoise from any level, so at generation time it can be called step by step starting from pure noise.
Training the generator: a real image and its caption
image512×512 pixels with 3 numbers each
→
autoencoder’s encoderturns the image into a grid of 64×64 cells with 4 numbers each
→
noisy gridrandom noise level
image caption→text encoderturns the caption into vectors
↓ both inputs ↓
generatorreceives the noisy grid, the noise level and the caption vectors; it predicts the added noise, and the weights are adjusted according to the prediction error
The numbers and the image are those of Stable Diffusion 1.5 from the run above; the noisy grid is shown through the decoder. FLUX.1 from “How models generate images, video and audio” uses a grid of 128×128 cells with 16 numbers each for a 1024×1024 image. The autoencoder is trained separately, before the generator, and then frozen. Generating from scratch needs only the decoder, not the autoencoder’s encoder; the encoder comes back when a finished image is edited.
The newer SD3, FLUX and most video models use flow matching, a variant of the same training with noise. In training the noisy grid is a linear interpolation: (1 − t) × image grid + t × noise, for a random t between 0 and 1. The generator learns to predict the difference noise − image grid, which for every number in the grid is the direction from the image to the noise. At generation time the sampler (also called a solver in these models) moves the grid a small step the opposite way, towards the image, and calls the generator again; the diffusers library does this 28 times by default for FLUX.1 dev.Flow matching: the noisy grid lies on the line segment between the image grid and the noise. The generator predicts the direction towards the noise, and the sampler moves the grid a small step the opposite way.
The training data consists of images paired with captions. Increasingly, another model writes those captions: 95% of the captions used to train DALL·E 3 (2023) were model-generated, because internet captions are short and leave out which object is where. Training usually starts on small images and their resolution is then raised in stages, for example from 256 to 1024 pixels. After that, as with an LLM, the model is tuned on selected examples and on human preferences (see “Training”).
Guidance and seed
In a diffusion model the guidance scale controls how strongly the prompt influences the image. In the classic version, classifier-free guidance (CFG), which Stable Diffusion 1.5 uses, the generator predicts the noise twice at every step, without the prompt and with it, and the sampler amplifies the difference between the two predictions.
Guidance: two predictions and their amplified difference
Without the prompt grey-beige fogWith the prompt a pale outline of a cliff and a lighthouse on the rightGuidance 7.5 a clear red lighthouse
The sampler uses the noise given by the formula: prediction without the prompt + 7.5 × (prediction with the prompt − prediction without the prompt). Each picture is the preview computed from one of these noise predictions. The figure shows the first step: the prompt alone changes the preview only slightly, while the amplified difference already gives a clear outline of the lighthouse. At scale 1 the formula reduces to the prediction with the prompt.
A real first step of Stable Diffusion 1.5, prompt “a red lighthouse on a rocky cliff above a stormy sea, photograph”. The whole run is above, in the figure “From noise to an image in 20 steps”.
For the generator to be able to predict without a prompt, the caption is dropped from some of the training examples. The method’s authors got their best results when dropping the condition from 10–20% of examples (2022, an ImageNet 64×64 model, where the condition is a class label), and Stable Diffusion 1.5 was trained with captions dropped from 10% of examples. A higher guidance scale gives less varied results, and one that is too high ruins the colours, as in the pictures under “Same prompt, different settings” at the top. The effect resembles lowering the temperature (see “The next token”), but the mechanism is different and doubles the number of generator calls, so it almost doubles generation time. Models with distilled guidance, such as FLUX.1 dev, learn this effect without the second prediction. Scale values do not carry over between models: FLUX.1 dev defaults to 3.5 in diffusers.
The seed is the number that determines the starting noise. With the other settings unchanged, the seed decides the layout of the image. Changing the prompt or the guidance scale changes the layout too, because the prompt affects it from the first steps. An LLM samples at every token (see “The next token”), while the DDIM sampler shown here draws only once, at the start. The same prompt, seed and settings on the same model version usually give the same result; after a version change they no longer do, so save the finished files.
Editing and reference images
Same image, new prompt
New prompt: “…on a snowy cliff above a frozen sea at night”. Strength says what fraction of the denoising loop to run on the original: 0 means no change, and 1 means the whole loop from pure noise, which is generating from scratch.
OriginalStrength 0.3 little noise: almost unchangedStrength 0.5 ice appears on the water, the rest staysStrength 0.7 snow on the cliff, same layoutStrength 0.9 a new lighthouse and sky, original layout; the prompt’s request for a night scene has little effect
Inpainting: a mask marks the area to edit
New prompt: “a white windmill on a rocky cliff above a stormy sea, photograph”.
OriginalMask a rectangle around the lighthouseResult a windmill in place of the lighthouse, small changes outside the mask
Edits: Stable Diffusion 1.5, guidance 7.5. Strength picks the starting point in the 20-step loop. Noise at that point’s level is added to the original’s grid (noise accounts for about 33% of the grid’s variance at strength 0.3 and about 98% at 0.9), and then the remaining steps run: six at 0.3 and eighteen at 0.9. The request for a night scene has little effect even at strength 0.9, most likely because the image’s average brightness survived under the noise. Inpainting: a version of Stable Diffusion 1.5 trained with the mask as an extra input, 20 steps.
Editing a whole image (image-to-image) and inpainting also need the autoencoder’s encoder, which turns the original into a grid. In image-to-image, noise is added to the original image’s grid at the level selected by the strength setting, and the grid is then denoised with the new prompt. In inpainting the generator also receives the mask and the original’s grid with the masked area hidden, and creates new content inside the mask (in the example above, from pure noise) with the rest of the image as context. Cells outside the mask go through the loop and the decoder as well, so pixels outside the mask can change slightly, as the example above shows. In GPT Image the mask is only a hint, and OpenAI notes that the model does not always follow it exactly. When pixels outside the mask must stay untouched, paste the generated area into the original in your own code.
In FLUX.1 Kontext (2025) a reference image, for example a face or a product, goes through the same autoencoder as the generated image, and its tokens are placed next to the tokens of the generated image. It is context for one call, not training. Unintended changes accumulate over successive edits. The Kontext authors tracked face similarity to the original across successive edits; similarity declined more slowly for Kontext than for the other models they tested. Pass the original as the reference in every edit and compare the result with it, and don’t keep correcting only the previous version.
Resolution, errors and cost
The counterpart of the context window is the number of image tokens, and it follows from the size fixed before the start; an image can later be extended past its edge, but that is a separate generation (outpainting). Doubling the side means four times as many tokens and, for FLUX.1, an estimated 5.6 times as much computation per step: at 1024 pixels attention is about 13% of a step’s computation and grows 16 times, while the rest grows 4 times (see the calculator in “How models generate images, video and audio”). APIs have limits: the GPT Image models generate images with a side of up to 3840 pixels and at most 8.3 million pixels in total (as of November 2026).
Fitting a larger grid into GPU memory is not enough: a model generates well only at sizes close to those it was trained on. Stable Diffusion 1.5, trained on 512×512 images, often repeats objects and breaks their structure when asked for 1024×1024. A large image can be assembled from overlapping tiles that are denoised in the same steps and averaged where they overlap: seams usually do not show, but each tile sees only the prompt and its neighbours, so the whole can be inconsistent, for example with a repeating landscape. Generate the image first at a size the model handles, then upscale it and run it through image-to-image in tiles at a low strength. This order is more reliable: the layout is already fixed, so the tiles repeat objects less often. A low strength limits changes to the content but does not rule them out.
Hands and the number of objects: the model learns what parts of photos look like, but nothing forces a generated hand to have five fingers. Newer models get this wrong less often.
Text in images was the weakest point for a long time: Stable Diffusion 1.5 got letters wrong, and the autoencoder distorted small text. Models from 2025–2026 such as GPT Image and Nano Banana Pro usually render short text correctly, but OpenAI still warns of problems with the exact placement and legibility of text. When text, a logo or a chart has to be exact to the character, add it in code after generation.
Generate images at the smallest size the model handles well, using several different seeds. Pick a layout, then upscale the chosen image. Using the same seed at a larger size produces different starting noise and therefore a different layout. Add the rejected attempts to the cost of every accepted image.
Check yourself
How does a diffusion model turn a prompt into an image, and how does that differ from an LLM generating text?
A text encoder turns the prompt into vectors. The model works on a smaller grid of numbers that an autoencoder can turn into an image, and generation starts from random noise in that grid. At every step the generator predicts the noise (or the direction of change) for the whole grid at once and the sampler updates the grid; at the end the decoder turns it into pixels. An LLM appends one token from its vocabulary at a time, while here the generator improves the whole grid at once; the grid’s size depends on the resolution, not on the content. Randomness comes mainly from the starting noise, which the seed determines (some samplers add noise at every step), not from a draw at every token. A separate parameter, the guidance scale, controls how strongly the prompt influences the image.
Po polsku
Koder tekstu zamienia prompt w wektory. Model pracuje na mniejszej siatce liczb, którą autoenkoder umie zamienić w obraz, a generowanie startuje od losowego szumu w tej siatce. W każdym kroku generator przewiduje szum (albo kierunek zmiany) dla całej siatki naraz, a sampler aktualizuje siatkę; na końcu dekoder zamienia ją w piksele. LLM dopisuje po jednym tokenie ze słownika, a tu generator poprawia całą siatkę naraz; jej rozmiar zależy od rozdzielczości, nie od treści. Losowość pochodzi głównie z szumu startowego, który wyznacza seed (część samplerów dodaje szum w każdym kroku), a nie z losowania przy każdym tokenie. Siłę promptu ustawia osobny parametr, skala guidance.
Follow-up questions (4)
After you raise the guidance scale, the image has oversaturated colours and harsh contrast. What do you change?
Lower the guidance scale and leave the seed and the other settings alone, so that you see the effect of one parameter. A scale that is too high pushes the result outside what the model has learned. If the image drifts from the prompt at the lower scale, fix the prompt or pass a reference image, and don’t raise the guidance scale again.
You need a 4096×4096 poster and the model handles 1024×1024. How do you do it?
Generate several versions at 1024×1024 with different seeds and pick a layout. Then upscale the chosen version and run it through image-to-image in tiles at a low strength, which adds detail. Generating directly at 4096×4096 means sixteen times as many tokens and a size the model never saw in training, so it often produces repeated objects. Add text and logos in code at the end.
The same character has to appear in ten illustrations. How do you set that up?
Don’t describe the character in words alone, because one description fits many different faces and outfits. If the model supports it, pass the same reference image of the character with every illustration. Generate from that image, not from the previous illustration, and compare the character in every result with it. If that is not enough, you can fine-tune the model on images of the character, for example using LoRA (see “Fine-tuning and LoRA”).
You set the same seed and prompt, and the image from the API came out different from a month ago. Why?
The seed reproduces the starting noise, but the result also depends on the model version, the sampler, the libraries and the hardware. The provider may have changed any of these. Many services rewrite the prompt with an LLM before generating, and the way they rewrite it can change as well. If you will need the image later, save the file, not just the settings.