Search

Search papers and lessons

Transfusion: Predict the Next Token and Diffuse Images

Chunting Zhou, Lili Yu, Arun Babu et al. · 2024

Transfusion trains one transformer on text with next-token prediction and on images with diffusion, and matches similar-size diffusion models on image generation at 7B scale.

On this page

  • Colour = topic mix
  • Probability
  • Calculus
  • Discrete math
  • Neural network basics
  • Linear algebra
  • Generative models
  • ReadReady to readNot yet

TL;DR

Transfusion is a training recipe for one transformer that handles text and images together. Text is split into discrete tokens and learned with the next-token loss; images stay as continuous latent vectors and are learned with the diffusion loss. The two losses are added and the same network learns both. The authors pretrained models up to 7B parameters on a mixture of text and images, reaching image quality close to similar-size diffusion models and text ability close to Llama.

Builds on: Autoregressive Model, Diffusion Model, Variational Autoencoder, Vision Transformer (ViT) · Leads to: Flow Matching (the paper names it as a replacement for the diffusion loss it did not try)

Why it mattered

Before Transfusion, the open route to a model that generates both text and images was to turn images into discrete tokens and then train an ordinary language model over the mixed token sequence. Chameleon, released in 2024, followed this route. Other systems attached a pretrained image generator to a language model, or grafted a diffusion model onto one. Transfusion showed that a single transformer can be trained with both objectives from scratch, with no discrete image tokens and no separate generator.

The clearest number is the 7B model trained on 2T tokens: 1T text tokens and about 3.5B image-caption pairs. It scored 0.63 on GenEval, above SDXL at 0.55 and close to DeepFloyd at 0.61, and it matched Llama 1 on text benchmarks at 66.1 average accuracy.

The problem

Two things were true at once. Next-token prediction, the objective of language models, dominates discrete data such as text and code. Diffusion, which learns to reverse a noising process, is the state of the art for continuous data such as images. Neither objective is good at the other's data type.

Existing ways to combine them each had a cost. Quantizing images with a VQ-VAE gives discrete tokens that a language model can read, but the quantization step throws away information about the image. Attaching a separate diffusion model to a language model keeps each piece strong but splits the system into parts that are trained apart. The paper asks whether images can stay continuous, with no information loss, while the same model still learns from text with next-token prediction.

How it works

Take one tiny mixed sequence: the text "Hi", then an image of four patches.

  1. Turn the text into integers. "Hi" becomes two integers from a fixed vocabulary, for example 17 and 42. Check: text is discrete, so each token is one integer.
  2. Turn the image into patch vectors. A VAE compresses the image into a small latent grid. Each latent cell is a vector, and the paper cuts the grid into patches, read left to right and top to bottom. Here the image gives four patch vectors, p1p_1 to p4p_4, each a list of numbers. Check: the image is now continuous vectors, with no rounding to a codebook.
  3. Put markers around the image. The sequence becomes [17, 42, BOI, p1p_1, p2p_2, p3p_3, p4p_4, EOI]. BOI and EOI are special tokens that mark where the image starts and ends. Check: one sequence now holds both kinds of element.
  4. Set who may see whom. Each text token and the BOI marker attend causally, looking only at earlier positions. Inside the image, the patches attend bidirectionally to each other. So p1p_1 can see p4p_4, but "Hi" cannot see any patch. Check: information flows forward through the sequence, and freely within each image.
  5. Score the text. At each text position the model predicts the next integer, and the loss is the cross-entropy of that prediction against the true next token. Check: the text loss is computed per token, and no loss is computed at image positions for text.
  6. Score the image. The whole image is noised at one random time step tt and then passed through the transformer, which tries to predict the noise that was added. The loss is the mean squared error between the true and predicted noise, computed once per image. Check: one diffusion loss per image, even though the image spans many patches.
  7. Add the two losses. The total is the text loss plus λ\lambda times the image loss, with λ=5\lambda = 5 in the paper. Check: the same network gets gradients from both, so one set of weights learns both.
  8. Generate. Sampling starts in text mode, one token at a time. When the model samples BOI, the decoder appends pure noise patches and denoises them over several steps, overwriting the noisy patches each time. Then it appends EOI and returns to text mode. Check: any mix of text and images can come out of one loop.
flowchart LR
  T["Text tokens (integers)"] --> S["One transformer"]
  I["Image latent patches (vectors)"] --> S
  S --> L1["Next-token loss on text"]
  S --> L2["Diffusion loss on each image"]
  L1 --> L["Total = LM loss + 5 x diffusion loss"]
  L2 --> L

The math

Definition: Transfusion objective
Given a sequence that mixes text tokens yiy_i and noised image latents xt\mathbf{x}_t, the model is trained on LTransfusion=LLM+λ LDDPM\mathcal{L}_{\text{Transfusion}} = \mathcal{L}_{\text{LM}} + \lambda \, \mathcal{L}_{\text{DDPM}}. Here LLM\mathcal{L}_{\text{LM}} is the average cross-entropy of predicting each text token from the tokens before it, and LDDPM\mathcal{L}_{\text{DDPM}} is the mean squared error between the noise added to an image and the noise the model predicts. The balance λ\lambda is a number that sets how much the image loss counts.

Step 5 in symbols: the text loss

LLM=Eyi[−log⁡Pθ(yi∣y<i)].\mathcal{L}_{\text{LM}} = \mathbb{E}_{y_i}\big[-\log P_\theta(y_i \mid y_{\lt i})\big].

Here Pθ(yi∣y<i)P_\theta(y_i \mid y_{\lt i}) is the probability the model gives the true token yiy_i after the earlier tokens y<iy_{\lt i}. The negative log is small when that probability is high.

Step 6 in symbols: the image loss

Diffusion adds noise in a closed form. With αˉt\bar\alpha_t the cumulative signal level at time tt,

xt=αˉt x0+1−αˉt ϵ,ϵ∼N(0,I).\mathbf{x}_t = \sqrt{\bar\alpha_t}\,\mathbf{x}_0 + \sqrt{1-\bar\alpha_t}\,\boldsymbol{\epsilon}, \qquad \boldsymbol{\epsilon} \sim \mathcal{N}(\mathbf{0}, \mathbf{I}).

The paper uses the cosine schedule, where αˉt≈cos⁡ ⁣(tT⋅π2)\sqrt{\bar\alpha_t} \approx \cos\!\big(\tfrac{t}{T}\cdot\tfrac{\pi}{2}\big) with some adjustments. The network ϵθ\boldsymbol{\epsilon}_\theta sees the noisy latent xt\mathbf{x}_t, the time tt and any context cc, and the loss is

LDDPM=Ex0,t,ϵ[∥ϵ−ϵθ(xt,t,c)∥2].\mathcal{L}_{\text{DDPM}} = \mathbb{E}_{\mathbf{x}_0, t, \boldsymbol{\epsilon}}\big[\lVert \boldsymbol{\epsilon} - \boldsymbol{\epsilon}_\theta(\mathbf{x}_t, t, c) \rVert^2\big].

For the diffusion background, see Diffusion Model; the noise prediction form follows DDPM.

Step 7 in symbols: the combined loss

LTransfusion=LLM+5 LDDPM.\mathcal{L}_{\text{Transfusion}} = \mathcal{L}_{\text{LM}} + 5\,\mathcal{L}_{\text{DDPM}}.

The paper set λ=5\lambda = 5 after preliminary experiments and says it leaves further tuning to future work.

Step 4 in symbols: the attention mask

The mask is a matrix MM with entry Mij=1M_{ij} = 1 when position ii may attend to position jj. For positions in different images or in text, Mij=1M_{ij} = 1 only when j≤ij \le i. Inside one image, Mij=1M_{ij} = 1 for every pair of patches, whatever their order. The paper reports that this intra-image bidirectional attention matters a great deal for image quality.

Worked examples

Example
1. A single loss value (hand-computable)

The model gives the true next text token probability 0.5. Find the text loss at that position, using natural logarithms.

  1. Write the loss: −ln⁡P-\ln P.
  2. Substitute P=0.5P = 0.5: −ln⁡0.5-\ln 0.5.
  3. Use ln⁡0.5=−0.693\ln 0.5 = -0.693, so the loss is 0.6930.693.
  4. Interpretation: a probability of one half gives a loss of about 0.69. If the probability had been one, the loss would have been zero.
Example
2. A noised latent at mid-schedule (standard case)

Take T=1000T = 1000 and t=250t = 250. Use the paper's cosine approximation to find the signal and noise weights, then noise a latent value x0=2.0x_0 = 2.0 with noise ϵ=−1.0\epsilon = -1.0.

  1. Compute the angle: tT⋅π2=0.25×1.5708=0.3927\frac{t}{T} \cdot \frac{\pi}{2} = 0.25 \times 1.5708 = 0.3927, which is π/8\pi/8.
  2. Signal weight: αˉt≈cos⁡(π/8)=0.9239\sqrt{\bar\alpha_t} \approx \cos(\pi/8) = 0.9239.
  3. Noise weight: 1−αˉt=1−0.8536=0.1464=0.3827\sqrt{1 - \bar\alpha_t} = \sqrt{1 - 0.8536} = \sqrt{0.1464} = 0.3827. Check: 0.92392+0.38272=0.8536+0.1464=1.00.9239^2 + 0.3827^2 = 0.8536 + 0.1464 = 1.0.
  4. Scale the signal: 0.9239×2.0=1.84780.9239 \times 2.0 = 1.8478.
  5. Scale the noise: 0.3827×(−1.0)=−0.38270.3827 \times (-1.0) = -0.3827.
  6. Add: xt=1.8478−0.3827=1.465x_t = 1.8478 - 0.3827 = 1.465.
  7. Interpretation: the latent is still mostly signal at this step. The model has to predict ϵ=−1.0\epsilon = -1.0 from xt=1.465x_t = 1.465 and t=250t = 250.
Example
3. Compute saved against Chameleon (realistic)

Table 3 of the paper gives a parity FLOP ratio of 0.029 for image generation FID: Transfusion needs 2.9 percent of Chameleon's compute to match Chameleon's FID.

  1. Read the ratio: 0.0290.029.
  2. Invert it to get how many times less compute: 1/0.029=34.51 / 0.029 = 34.5.
  3. Interpretation: Transfusion matches Chameleon's FID with about 34 times less compute. The paper gives the ratio 0.029 in Table 3, and this 34 is just its inverse.
  4. Compare with the Llama accuracy row of the same table: its ratio is 0.6000.600, so Transfusion needs 60 percent of Chameleon's compute to match that accuracy.
  5. Try it: what is the number of times less compute for the Llama accuracy match, 1/0.6001 / 0.600? Work it out before reading on.

Answer: 1/0.600=1.671 / 0.600 = 1.67, so about 1.7 times less compute, which is the same fact as needing 60 percent of the compute.

Common mistakes and tradeoffs

It is tempting to think Transfusion is just a language model with an image generator bolted on. In fact there is one set of transformer weights, one sequence and one optimiser; only the small patch encoders and decoders are specific to each modality.

It is tempting to think the diffusion loss is computed per patch. In fact the image gets one noise level and one loss, even though it spans many patches. Noise is added to the whole latent before patchification.

It is tempting to think a continuous representation is always better than discrete tokens. In fact the paper reports that Transfusion beats the token approach at matched compute on the benchmarks it tested, but the comparison is on one controlled setup with models up to 7B.

It is tempting to think bidirectional attention can be used everywhere. In fact the mask stays causal across the sequence and is only bidirectional within an image, so the text stays a left-to-right model.

Use Transfusion's recipe when one model must both read and produce text and images at scale and the images can be kept as latents. Do not expect it to beat specialised image generators on image quality alone: the paper's own table shows SD 3 above it on GenEval. For image-only work, a plain latent diffusion model is simpler; for text-only work, a standard Autoregressive Model is enough. The alternatives are the token approach of Chameleon, which keeps one loss but loses information, and grafting a pretrained generator onto a language model, which keeps each part strong but trains them apart.

History

Next-token prediction and diffusion were developed in separate communities. Diffusion models for images came into wide use after denoising diffusion probabilistic models in 2020, and latent diffusion made them efficient by working in a compressed latent space in 2022. Language models trained with next-token prediction scaled up through the late 2010s and early 2020s. Through 2023 and 2024, several groups built mixed-modal systems, most by combining existing parts or by quantizing images. Chameleon, in 2024, was the open baseline that discretised images and trained one language model over both. Transfusion, posted to arXiv in August 2024, asked whether the discretisation step was needed at all. It caught on because it showed a clean controlled comparison at matched compute, and because it gave a single recipe that both loss families could share.

Results

The paper compares a 7B Transfusion model with a 7B Chameleon model trained on the same 0.5T tokens, with the same data, compute and architecture except the image representation.

Metric (direction)Transfusion 7BChameleon 7BParity FLOP ratio
C4 perplexity (lower is better)7.728.410.489
Wikipedia perplexity (lower is better)4.284.690.526
Llama accuracy (higher is better)61.559.10.600
MS-COCO CIDEr (higher is better)27.218.00.218
MS-COCO FID (lower is better)16.829.60.029
MS-COCO CLIP (higher is better)25.524.30.319

Table 3 of the paper, with the parity FLOP ratio defined as the Transfusion FLOPs needed to match the Chameleon 7B result.

The larger 7B model with U-Net patch encoding, trained on 2T tokens, is compared with other image generators on GenEval, a benchmark for how faithfully a model depicts a prompt.

ModelImage trainingGenEval (higher is better)
DALL-E 22.6B images0.52
SDXL1.6B images0.55
DeepFloyd7.5B images0.61
Transfusion 7B3.5B images, 1.0T text tokens0.63
SD 32.0B images0.68

The paper also reports that Transfusion's Llama accuracy is 66.1, the same as Llama 1 on that suite, and that its COCO FID is 6.78. The U-Net encoder and decoder add 0.27B parameters, or 3.8 percent of the 7B model.

Ablations give the other main findings. Intra-image bidirectional attention lowers FID from 61.3 to 20.3 with linear patch encoding. With U-Net layers, an image can be compressed to 16 patches of 8×8 latent pixels, with FID 16.1 and CLIP 25.2, against FID 16.0 and CLIP 25.7 at 64 patches. Limiting diffusion noise to t≤500t \le 500 on the 20 percent of samples where the image comes first raises CIDEr from 33.7 to 35.2 at 7B.

Limits. Most controlled comparisons use 0.5T tokens and models up to 7B. The FLOP ratio uses the approximation 6ND6ND as a proxy for compute. Chameleon's architecture changes for stability confound some of the text comparison, which the paper acknowledges. The image editing result is a fine-tuning test on 8k examples, judged by manual inspection, and the paper calls it a limited experiment. The paper also notes that SD 3 was trained with synthetic captions, which boosts its GenEval score.

Reading guide

Check yourself

QuestionWhich loss is applied to text, which to images, and how are they combined?Show answer
Text uses the next-token (cross-entropy) loss at each text position. Images are noised and the model's noise prediction is scored with a mean squared error loss, once per image. The total is the text loss plus λ=5\lambda = 5 times the image loss.
QuestionA training step gives LLM=2.0\mathcal{L}_{\text{LM}} = 2.0 and LDDPM=0.3\mathcal{L}_{\text{DDPM}} = 0.3. What is the total loss with the paper's λ\lambda?Show answer
2.0+5×0.3=2.0+1.5=3.52.0 + 5 \times 0.3 = 2.0 + 1.5 = 3.5.
QuestionWhy does Transfusion use bidirectional attention between the patches of one image, but causal attention everywhere else?Show answer
Image patches have no natural left-to-right order, so letting every patch see every other patch helps the model build the whole image. Text is sequential, so it keeps the causal mask that next-token prediction needs. The paper finds that causal-only attention on images hurts FID badly, 61.3 against 20.3 with linear patch encoding.
QuestionA team quantizes each image into one of 16,384 codebook tokens and trains a standard language model. What information is lost, and what would Transfusion keep?Show answer
Quantization maps each continuous latent vector to the nearest codebook entry, so the fine detail of each latent is lost. Transfusion keeps the latents continuous, and models them with the diffusion loss inside the same transformer, so no quantization step is needed.

Sources

Read the full paper

Chunting Zhou, Lili Yu, Arun Babu et al., 2024. arXiv:2408.11039