Search

Search papers and lessons

Chameleon: Mixed-Modal Early-Fusion Foundation Models

Chameleon Team · 2024

Chameleon turns images and text into one stream of tokens and trains a single transformer on it, so one model can read and write documents that mix both freely.

On this page

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

TL;DR

Chameleon is a family of models, with 7 billion and 34 billion parameters, that treats images and text as one stream of discrete tokens. A single transformer is trained from scratch on interleaved text, images and code, so it can read and write documents in which pictures and words appear in any order. At 34 billion parameters it is competitive with much larger text models and beats several image-and-text models on captioning and visual question answering.

Builds on: Transformer, Vector Quantization, Instruction Tuning · Leads to: Vision-Language Model, Autoregressive Model

Why it mattered

Most multimodal systems before 2024 used a separate encoder for images and a separate decoder for generating them, joined to a language model at some point in the pipeline. Chameleon showed that one token-based transformer could handle both understanding and generation of mixed documents, with no modality-specific parts beyond the tokenizer. This made "a document with pictures in it" a native input and output rather than a special case.

The approach is now a common reference point for unified multimodal models. Its public results are the main evidence that early fusion (mixing modalities from the first layer, rather than attaching an image module to a finished language model) can reach strong benchmark numbers. Its training notes on stability are often cited by people who train large models, because they describe a divergence that the usual recipe does not catch.

The problem

Before Chameleon, the standard recipe for images and text was late fusion: a vision encoder produced features that were inserted into a language model, which then generated text. Images went in, but images did not come out, and interleaved documents with several images were awkward.

Using a single model for both directions raised three problems:

  1. An image has to become a sequence of discrete symbols, the way a word does, so that the same transformer can read it (Vector Quantization and Subword Tokenization are the two ingredients).
  2. The shared weights see text and image tokens with very different statistics. Training then became unstable: the softmax is invariant to adding a constant to all logits, so the two modalities compete by slowly raising the output norms, and the runs diverged late in training. The stability section below covers the fixes.
  3. Generation of a mixed document needs decoding rules that depend on which modality the model is producing, which affects how fast inference can run.

Chameleon addresses each of these in turn.

How it works

Follow one example from input to output. Suppose the input is a 512 by 512 pixel photo followed by the question "What is in the picture?".

flowchart LR
  A[512 x 512 photo] --> B[Image tokenizer: 1024 codes]
  C[Question text] --> D[Text BPE tokens]
  B --> E[One token sequence]
  D --> E
  E --> F[Single transformer predicts next token]
  F --> G[Text answer tokens]
  1. Turn the image into tokens. The image is cut into a grid. The paper's image tokenizer, based on the approach of Gafni et al. (2022), encodes a 512 by 512 image into 1024 discrete tokens. Each token is an index into a learned codebook of 8192 entries. The grid has 32 by 32 positions, so each token covers one 16 by 16 block of pixels, and 32×32=102432 \times 32 = 1024 tokens. Check: 512/16=32512 / 16 = 32, and 32×32=102432 \times 32 = 1024. The image has 512×512=262,144512 \times 512 = 262{,}144 pixels, which is 256256 pixels per token times 10241024 tokens.

  2. Turn the text into tokens. The question is split by a byte-pair style tokenizer (Subword Tokenization) into a short sequence of text tokens. The vocabulary has 65,536 entries. The 8192 image codes are part of that vocabulary, so both kinds of token live in one table. Check: a text token and an image token are both just integers in the same range, so the transformer does not need to know which is which at the input layer.

  3. Put them in one sequence. The image tokens are placed in order with the text, between special markers that say "image starts here" and "image ends here". The model then sees a single list of integers, with no separate image branch. Check: the sequence is ordered exactly as in the document, so a picture that appears between two sentences sits between those sentences in the input.

  4. Predict the next token. The transformer is trained to predict the next token given all tokens before it. This is the same objective used by text-only language models, applied to a sequence that happens to contain image codes (Autoregressive Model). Check: at generation time, the model can produce text tokens or, after an image-start marker, image tokens. Image tokens are decoded back into pixels by the image tokenizer.

  5. Generate the answer. The model writes the answer as text tokens. If the user had asked for a picture, the model would emit an image-start marker and then 1024 image tokens, which the tokenizer decodes into an image. Check: the same weights handle both directions. The only change is which tokens the decoder is allowed to produce, which the inference section handles with masking.

The math

Definition: Early fusion

A multimodal model in which every input modality is converted into tokens from one shared vocabulary and then processed by the same transformer from the first layer onward.

Step 1 in symbols

The image tokenizer assigns each image patch z\mathbf{z} to its nearest codebook vector ek\mathbf{e}_k:

k⋆=arg⁡min⁡k∥z−ek∥2.k^\star = \arg\min_{k} \lVert \mathbf{z} - \mathbf{e}_k \rVert^2.

Here e1,…,e8192\mathbf{e}_1, \dots, \mathbf{e}_{8192} are the codebook vectors, and k⋆k^\star is the index of the chosen one. This index is the image token. The image as a whole becomes 10241024 indices, one per patch.

Step 2 in symbols

The vocabulary has 65,53665{,}536 entries in total, and the 81928192 image codes are among them. So the input is a sequence of indices x1,x2,…,xTx_1, x_2, \dots, x_T drawn from one table, where the image codes occupy a fixed block of 81928192 of those entries. Text and image tokens share one embedding matrix and one output softmax.

Step 3 in symbols

Training minimises the standard next-token loss over the mixed sequence:

L=−∑t=1Tlog⁡pθ(xt∣x1,…,xt−1).\mathcal{L} = -\sum_{t=1}^{T} \log p_\theta(x_t \mid x_1, \dots, x_{t-1}).

Here pθp_\theta is the transformer's predicted probability of the true token, given the tokens before it, and θ\theta denotes all the weights. Summing over every position covers text and image tokens alike.

Step 4 in symbols

Each attention layer computes its softmax over the query and key vectors. Chameleon applies layer normalization to both before the product, which is the query-key normalization (QK-Norm) described in the paper:

Attn(Q,K,V)=softmax ⁣(LN(Q) LN(K)⊤d)V.\mathrm{Attn}(\mathbf{Q}, \mathbf{K}, \mathbf{V}) = \mathrm{softmax}\!\left( \frac{\mathrm{LN}(\mathbf{Q})\, \mathrm{LN}(\mathbf{K})^\top}{\sqrt{d}} \right) \mathbf{V}.

Here dd is the head dimension and LN\mathrm{LN} is layer normalization (Layer Normalization). Normalizing the inputs to the softmax keeps the attention logits from growing without bound.

Step 5 in symbols

The output softmax over the vocabulary uses partition function Z=∑iexiZ = \sum_i e^{x_i}. The paper adds a regularizer, called z-loss in the paper, that penalises a large ZZ:

Ltotal=L+10−5log⁡2Z.\mathcal{L}_{\text{total}} = \mathcal{L} + 10^{-5} \log^2 Z.

The term is zero when Z=1Z = 1 and grows as the output logits drift.

Results

The paper reports two main numbers for each claim. The table values below are the paper's, for the 34 billion parameter model unless noted.

Text-only tasks. On the MATH benchmark, Chameleon-34B reaches 22.5 with a single greedy sample (maj@1), against 13.5 for Llama-2 70B. On GSM8k it reaches 61.4 against 56.8 for Llama-2 70B, both with one sample. On MMLU it reaches 65.8, against 70.6 for Mixtral 8x7B and 71.8 for Gemini Pro. The paper says the 34B model outperforms Llama-2 70B on five of eight commonsense and reading tasks. The 7B model is competitive with Llama-2 7B on those same tasks.

Image captioning (COCO, CIDEr). Chameleon-34B with 2 in-context examples scores 120.2, above Flamingo: a Visual Language Model for Few-Shot Learning at 113.8 and IDEFICS at 116.6, which both use 32 examples. The supervised fine-tuned version reaches 140.8 with no examples.

Visual question answering (VQAv2). The pre-trained 34B model with 2 examples scores 66.0, which is close to the 32-example Flamingo-80B score of 67.6. The multitask fine-tuned model reaches 69.6. LLaVA-1.5 (Improved Baselines with Visual Instruction Tuning) beats Chameleon on VQAv2, which the paper attributes to its extra fine-tuning on conversation and region-level VQA data.

Human evaluation on mixed-modal prompts. The authors built 1,048 prompts, of which 441 are mixed-modal. In pairwise comparisons, Chameleon-34B wins 60.4 percent against Gemini Pro and 51.6 percent against GPT-4V, where both baselines are the versions whose text replies were augmented with generated images (Gemini+ and GPT-4V+).

Limits. The paper says the image tokenizer reconstructs images with a lot of text poorly, which caps the model on heavy OCR tasks. On MMLU and GSM8k the model trails the best closed systems. The human evaluation uses the authors' own prompt set and their own annotators, so it measures a specific setting, not general quality.

Reading guide

Check yourself

QuestionRecall: what is early fusion, and how does Chameleon apply it?Show answer

Early fusion converts every modality into tokens from one shared vocabulary and processes them with the same transformer from the first layer. Chameleon tokenizes images into 1024 discrete codes from an 8192-entry codebook, adds them to the 65,536-entry text vocabulary, and trains a single transformer on the interleaved sequence.

QuestionComputation: an image is 256 by 256 pixels. If the tokenizer uses one token per 16 by 16 block, how many image tokens does it produce?Show answer

The grid has 256/16=16256 / 16 = 16 positions along each side, so 16×16=25616 \times 16 = 256 tokens. Check: the image has 256×256=65,536256 \times 256 = 65{,}536 pixels, and each token covers 16×16=25616 \times 16 = 256 of them, so 65,536/256=25665{,}536 / 256 = 256 tokens.

QuestionWhy: the paper adds QK-Norm and z-loss. What problem do they address, and why is the first one not enough?Show answer

Both address norm growth that eventually causes training to diverge. QK-Norm controls growth inside the attention softmax. But the output softmax over the vocabulary also drifts, because the partition function ZZ can grow. Z-loss penalises a large ZZ directly, so it is needed as well. For the 34B model the paper found z-loss and norm reordering were required, and dropout was dropped.

QuestionApplied: a team wants a model that writes a short report with several charts placed between paragraphs. Which part of Chameleon's design makes this natural, and what would they still need to check?Show answer

The design treats charts and text as one token sequence, so a chart can appear between paragraphs in both input and output, with image-start and image-end markers marking its place. The team would still need to check how well the image tokenizer reconstructs their charts, since the paper says text-heavy images are a known weakness, and they would need to evaluate chart reading on their own data.

Sources

Read the full paper

Chameleon Team, 2024. arXiv:2405.09818