Search

Search papers and lessons

Diffusion Transformer (DiT)

Level 4 · 12 min read

A diffusion transformer runs the denoising network of a diffusion model as a plain transformer over patches of a compressed image, with the timestep and class fed in through adaptive normalisation.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Neural network basics
  • Linear algebra
  • Architectures
  • Generative models
  • Sign in to see your progress

TL;DR

A diffusion transformer, or DiT, is a Diffusion Model whose denoising network is a standard transformer instead of a U-Net. The image is compressed into a small latent grid, cut into patches, and each patch becomes a token. The timestep and the class label are injected through adaptive layer normalisation, so the network can change its behaviour at each noise level without any convolutions.

Where you'll see it: as the backbone of large class-conditional and text-to-image generators, where its quality keeps improving as you add more transformer depth, width and tokens.

Intuition

Same job, different machinery

A diffusion model needs one network that looks at a noisy input and says what the noise is. For years that network was a U-Net: a stack of convolutions with skip connections, built around the assumption that nearby pixels matter most. A Vision Transformer (ViT) makes a different assumption. It cuts the picture into patches and lets every patch look at every other patch through attention.

DiT asks a simple question: if the architecture is only a sequence of tokens, does the diffusion recipe still work? The answer from Peebles and Xie is yes, and it gets better as the transformer gets bigger. The diffusion process, the loss and the sampler stay the same. Only the network that makes the prediction changes.

Why bother? A transformer uses one design everywhere. The same blocks that model language can be scaled with the same rules, and the same hardware and tooling apply. Scaling is the point of the paper: DiT reuses the recipe that already worked for language and vision transformers.

flowchart LR
    Z[noisy latent z_t] --> P[patchify into tokens]
    P --> B1[DiT block 1]
    B1 --> B2[DiT block 2]
    B2 --> BN[DiT block N]
    BN --> D[linear decode and unpatchify]
    D --> E[predicted noise]
    C[timestep t and class y] -.->|scale, shift, gate| B1
    C -.-> B2
    C -.-> BN

The solid path is the data. The dashed lines are the conditioning signal, which reaches every block.

Work on a compressed image

A raw 256 by 256 colour image has 196,608 numbers. Running a transformer over it directly would be far too expensive. DiT therefore works in the latent space of a pretrained Variational Autoencoder, as in the Latent Diffusion Model. The encoder shrinks each image by a factor of 8 in each direction, so a 256 by 256 image becomes a 32 by 32 grid with 4 channels, which is 4,096 numbers. Diffusion runs on that small grid, and a decoder turns the final sample back into pixels.

The math

Setup

Let z0z_0 be the clean latent of an image, a tensor of shape I×I×CI \times I \times C. Diffusion adds noise over TT steps. Using the notation of the Diffusion Model lesson, the noisy latent at step tt is

zt=αˉt z0+1−αˉt ε,ε∼N(0,I).z_t = \sqrt{\bar\alpha_t}\, z_0 + \sqrt{1-\bar\alpha_t}\, \varepsilon, \qquad \varepsilon \sim \mathcal{N}(0, I).

Here αˉt\bar\alpha_t is the running product of the noise schedule, and ε\varepsilon is standard Gaussian noise. The network εθ\varepsilon_\theta is trained to predict ε\varepsilon from ztz_t, the timestep tt, and a class label yy:

Lsimple=Ez0, t, ε[ ∥ε−εθ(zt,t,y)∥2].\mathcal{L}_{\text{simple}} = \mathbb{E}_{z_0,\, t,\, \varepsilon}\left[\, \left\| \varepsilon - \varepsilon_\theta(z_t, t, y) \right\|^2 \right].

This is the same Mean Squared Error objective as before. DiT changes only the form of εθ\varepsilon_\theta.

Patchify

The latent grid is cut into non-overlapping patches of side pp. Each patch is flattened into a vector of length p2Cp^2 C and multiplied by a learned matrix to give a token of width dd. The number of tokens is

T=(Ip)2.T = \left(\frac{I}{p}\right)^2.

A fixed sine-cosine Positional Encoding is added to each token, so the transformer knows where each patch sat in the grid. The patch size pp is the main knob. Smaller patches mean more tokens, which means more computation but finer detail.

Conditioning with adaptive layer norm

The timestep tt is turned into a vector by a sinusoidal embedding followed by a small MLP. The class label yy is turned into a vector by an embedding table. The two are added to give one conditioning vector cc.

Inside each block, a normal Layer Normalization step is applied to the tokens with no learned scale or shift. Then cc is passed through a small linear layer that outputs three vectors for each block: a scale γ\gamma, a shift β\beta, and a gate α\alpha. For a token xx with a sub-layer ff (attention or the MLP), the block computes

x←x+α⋅f ⁣(γ⊙LN(x)+β).x \leftarrow x + \alpha \cdot f\!\left(\gamma \odot \mathrm{LN}(x) + \beta\right).

Here ⊙\odot is element-wise multiplication. The gate α\alpha is the key choice. The authors call the best variant adaLN-Zero: the linear layer that produces α\alpha is initialised to output zeros. At the start of training every block is therefore the identity map, and the network begins as a simple pass-through. In the paper's comparison, adaLN-Zero gave lower FID than plain adaLN, cross-attention and in-context conditioning.

Output

After the last block, a final adaptive layer norm is applied, and a linear layer maps each token back to p2⋅2Cp^2 \cdot 2C numbers. The extra factor of 2 is there because the network also predicts a per-dimension variance, following the learned-variance idea of Nichol and Dhariwal. The tokens are then rearranged back into a grid of shape I×I×2CI \times I \times 2C, and the noise estimate is the first half.

Sampling with guidance

To generate a sample, the reverse process of Diffusion Model is run from pure noise. DiT is usually sampled with Classifier-Free Guidance. The network is also trained with the class label replaced by a null token some fraction of the time. At sampling, the two predictions are combined:

ε^=εθ(zt,t,∅)+s(εθ(zt,t,y)−εθ(zt,t,∅)).\hat\varepsilon = \varepsilon_\theta(z_t, t, \varnothing) + s \left( \varepsilon_\theta(z_t, t, y) - \varepsilon_\theta(z_t, t, \varnothing) \right).

The scale ss above 1 pushes samples toward the class at the cost of diversity. The paper's best ImageNet 256 by 256 result, an FID of 2.27, uses a guidance scale of 1.5.

Worked examples

Example
Counting tokens for three patch sizes

Take a 256 by 256 image, which gives a latent grid of I=32I = 32 with C=4C = 4 channels.

  1. Patch size p=2p = 2: T=(32/2)2=162=256T = (32/2)^2 = 16^2 = 256 tokens. Each raw patch has 2⋅2⋅4=162 \cdot 2 \cdot 4 = 16 numbers.
  2. Patch size p=4p = 4: T=(32/4)2=82=64T = (32/4)^2 = 8^2 = 64 tokens. Each raw patch has 4⋅4⋅4=644 \cdot 4 \cdot 4 = 64 numbers.
  3. Patch size p=8p = 8: T=(32/8)2=42=16T = (32/8)^2 = 4^2 = 16 tokens. Each raw patch has 8⋅8⋅4=2568 \cdot 8 \cdot 4 = 256 numbers.
  4. Attention compares every pair of tokens, so its cost grows with T2T^2. Going from p=4p = 4 to p=2p = 2 multiplies the token count by 4 and the pairwise score count by 42=164^2 = 16.
  5. Interpretation: halving the patch size is the most expensive way to add detail. The paper finds that, at a fixed model size, smaller patches (more tokens) improve quality even though the parameter count barely changes, so the extra compute is what buys the gain.
Example
Why adaLN-Zero starts as the identity

Take one feature of one token, with layer-normalised value h=0.5h = 0.5. Suppose the conditioning gives scale γ=0.2\gamma = 0.2, shift β=−0.1\beta = -0.1, and the sub-layer output is f=0.8f = 0.8 for this input. The residual input is x=1.0x = 1.0.

  1. Modulated input: γh+β=0.2⋅0.5−0.1=0.0\gamma h + \beta = 0.2 \cdot 0.5 - 0.1 = 0.0. Note that this is the input the sub-layer sees, before the gate.
  2. Case A, gate at initialisation: α=0\alpha = 0. The update is x←1.0+0⋅0.8=1.0x \leftarrow 1.0 + 0 \cdot 0.8 = 1.0. The block passes the input through unchanged.
  3. Case B, gate after some training: α=0.3\alpha = 0.3. The update is x←1.0+0.3⋅0.8=1.24x \leftarrow 1.0 + 0.3 \cdot 0.8 = 1.24.
  4. Interpretation: at the start, every block does nothing, so the gradient flows cleanly through all the layers. The gate then opens gradually as the network learns to use each block.
Example
One guided prediction

At one denoising step, suppose the unconditional prediction for a single noise value is εθ(zt,t,∅)=0.4\varepsilon_\theta(z_t, t, \varnothing) = 0.4 and the class-conditional prediction is εθ(zt,t,y)=0.7\varepsilon_\theta(z_t, t, y) = 0.7. Use guidance scale s=1.5s = 1.5.

  1. Difference between the two predictions: 0.7−0.4=0.30.7 - 0.4 = 0.3.
  2. Scaled difference: 1.5⋅0.3=0.451.5 \cdot 0.3 = 0.45.
  3. Guided prediction: 0.4+0.45=0.850.4 + 0.45 = 0.85.
  4. Interpretation: the guided step moves further in the direction the class label points than the plain conditional model does. Setting s=1s = 1 recovers the conditional model, and s=0s = 0 gives the unconditional one.

Tradeoffs and failure modes

DiT is a good default when you have enough compute to train a large transformer and want the architecture to scale predictably. It is a poorer fit when the data is small, when you need very cheap sampling on a phone, or when you already have a strong convolutional pipeline.

SituationProblemBetter choice
Small dataset, few GPUsA large transformer overfits or never trains wellA U-Net with fewer parameters
Very high resolutionToken count grows with the square of the side lengthPatchify a stronger latent, or use a hierarchical model
Interactive generationMany network calls per sample, each with full attentionFewer sampling steps through a faster sampler, or distillation
No class or text conditioning, only the timestepThe class half of the conditioning vector is unusedKeep adaLN driven by the timestep alone, or use a simpler conditioning path
Plain adaLN without zero-initialised gatesIn the paper's comparison it reached a worse FID than adaLN-Zero at the same computeadaLN-Zero, as in the paper

Two failure modes are common. First, forgetting the patch size: the same architecture at a different pp is a different model, with a different cost and a different quality. Second, comparing models at different sampling steps or guidance scales. The paper's headline numbers depend on the sampler and the guidance scale, so a fair comparison fixes both.

History

Ho, Jain and Abbeel's 2020 work made diffusion models practical for images, using a U-Net as the denoiser. Rombach and colleagues then moved the diffusion into the latent space of an autoencoder, which made high-resolution generation affordable.

William Peebles and Saining Xie published the Diffusion Transformer in late 2022, and the paper appeared at ICCV in 2023. Their central claim was that the diffusion recipe is not tied to convolutions. Replacing the U-Net with a transformer that is patchified and conditioned with adaptive normalisation gave consistent gains as the model grew. Their largest model, DiT-XL/2, reached a Fréchet Inception Distance of 2.27 with classifier-free guidance on class-conditional ImageNet at 256 by 256, beating the previous best diffusion result of 3.60 from the latent diffusion model. The paper also showed that the forward-pass compute of a model, measured in Gflops, is strongly correlated with its sample quality across different sizes and token counts.

The idea caught on because it made the diffusion field use the same architecture as the rest of deep learning. Later text-to-image systems adopted transformer denoisers in the same spirit.

Check yourself

QuestionRecall: what are the three things DiT's adaLN produces from the conditioning vector for each block?Show answer

A scale γ\gamma and a shift β\beta, which modulate the normalised tokens before the sub-layer, and a gate α\alpha, which scales the sub-layer's output before it is added back to the residual stream.

QuestionComputation: a latent grid is 64 by 64 with 4 channels. With patch size p=4p = 4, how many tokens are there, and how many numbers does each raw patch hold?Show answer

The number of tokens is T=(64/4)2=162=256T = (64/4)^2 = 16^2 = 256. Each raw patch holds 4⋅4⋅4=644 \cdot 4 \cdot 4 = 64 numbers before the linear embedding projects it to width dd.

QuestionWhy: why is the gate α\alpha initialised to zero in adaLN-Zero rather than to a random value?Show answer

With α=0\alpha = 0 every block is the identity at the start of training, because the residual update α⋅f(⋅)\alpha \cdot f(\cdot) vanishes. The whole network begins as a simple pass-through, so gradients reach every layer cleanly. A random gate would add random sub-layer outputs at every depth, which makes deep models unstable early in training. The gate opens only as the network learns that the block is useful.

QuestionApplied: you are building a DiT for 512 by 512 images with a latent grid of 64 by 64 and patch size 2. Roughly how much more attention work is this than the 256 by 256 model with patch size 2, and why?Show answer

The 512 model has (64/2)2=1024(64/2)^2 = 1024 tokens, and the 256 model has (32/2)2=256(32/2)^2 = 256 tokens. Attention compares every pair of tokens, so its cost scales with T2T^2. The ratio is (1024/256)2=42=16(1024/256)^2 = 4^2 = 16. Each attention layer does about 16 times the pairwise work, even though the image has only four times as many pixels. The cost grows faster than the image area, which is why high-resolution transformer diffusion usually relies on a larger patch size or a hierarchical design.

Sources