↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What Emu3's tokenizer is for

Emu3 trains one transformer on text, images and video with next-token prediction alone, with no diffusion model and no CLIP encoder. That only works if an image can be written as a sequence of ids from a finite vocabulary, as text is, and read back. Its vision tokenizer does both. An encoder maps the image to a small grid of vectors, each vector is replaced by the id of the nearest entry in a learned codebook, and a decoder turns the ids back into pixels.

The page records BAAI's released tokenizer on one photo, the public-domain beagle: 1000 × 750 pixels become 94 × 125 = 11,750 ids, each naming one of 32,768 codes, and decode again at a PSNR of 25.97 dB. The drawer's PyTorch decodes the same ids to exactly the pixels of BAAI's 270.7M-parameter model.

Resize and encode

The Image → latent grid column starts with Emu3's resize: both sides are rounded to multiples of 8 and the area is kept between 512² and 1024² pixels, so the photo becomes 1000 × 752 (bicubic) and its values are scaled to [−1, 1]. The encoder is a convolutional network: ResNet blocks at four widths (256, 512, 512 and 1024 channels), a stride-2 convolution between levels and attention only at the last. Each 8 × 8 patch of pixels becomes one vector of 4 numbers:

The tokenizer was built for video as well. Emu3's paper adds two temporal residual layers with 3D convolutions to the encoder and the decoder, so 4 frames compress into one latent frame; the convolutions are causal, padded only towards the past, so no frame sees a later one. An image goes in as a still clip of 4 identical frames. The four small maps under the photo are the 4 latent channels, blue below 0 and red above.

The nearest code

Quantisation replaces each latent vector by the closest of the codebook vectors and keeps only its index:

The second form is how Emu3 computes it, one matrix product for all positions at once. One id carries bits and stands for pixel values. The Nearest of 32,768 codes → ids column draws three maps over the grid: the id of every position, the squared distance to its chosen code, and how far each token's decoded pixels are from the input. Under them are the picked position's 4 numbers, its 8 nearest codes, and every code drawn in two of its four coordinates.

An id is only a row number in the codebook: neighbouring ids need not be similar codes, so the colours of the id map carry no order. The beagle uses 4,374 different codes, none more than 13 times.

How a codebook is learned

The page records a trained tokenizer; how such a codebook is trained comes from VQ-VAE (van den Oord et al., 2017). The argmin has no gradient, so the decoder's gradient is copied straight through to the encoder's output, and two extra terms pull the chosen code and the encoder's output towards each other ( stops the gradient, is the decoder):

A pixel loss alone gives blurry images at this compression. VQGAN (Esser et al., 2021) added a perceptual loss and a patch-wise adversarial loss, so that a small grid of codes decodes to sharp textures, and showed that a transformer over the ids can then generate megapixel images. According to its paper, Emu3's tokenizer was trained end to end on the LAION-High-Resolution images and the InternVid videos.

Decoding with modulated normalisation

The Ids → image column decodes. The decoder looks each id up in the codebook again and mirrors the encoder, upsampling 8× and making 4 frames, of which the first is the image. Following MoVQ (Zheng et al., 2022), the codes are not only its input: every normalisation layer takes its scale and shift from them, nearest-upsampled to that layer's resolution,

where and are the GroupNorm statistics of the feature map , and and are 1 × 1 convolutions of the looked-up codes . MoVQ's reason: quantisation maps similar neighbouring patches to the same index, and a plain VQGAN decoder then repeats the same patch; feeding the codes into every layer gives it spatially varying information to tell them apart.

Reconstruction is measured by the peak signal-to-noise ratio, : 25.97 dB here, with the decoded pixels off by 9.49 of 255 on average. The crops compare 5 × 5 tokens of the input, the decoded image and their difference × 6, and the middle column's error map shows which tokens the decoder misses most.

The image as a sentence

The strip, What Emu3's language model reads, shows the sequence Emu3-Chat's processor builds around the ids for the prompt “Please describe the image”. Emu3's text vocabulary is Qwen's tiktoken BPE (the o200k tab shows a tokenizer of this kind); the 32,768 visual tokens follow it, so code becomes token . The image is written as text: <|image start|>, its size in ordinary digit tokens (94*125), <|image token|>, then the ids row by row, each row closed by a row-end token, then a frame end and <|image end|>. The whole sequence is 11,873 tokens: 11,750 visual, 94 row ends and 29 of text and markers.

The size and the row ends give the grid's shape, so a flat sequence can hold any aspect ratio. Emu3 trains on such sequences with the ordinary cross-entropy of the next token, and its paper weights the loss of the vision tokens by 0.5 so that they do not dominate. Generating an image runs the page backwards: the model writes visual tokens one at a time, row by row, and the decoder draws them.

Where it came from

VQ-VAE (2017) introduced learned discrete codes for images, and DALL·E (Ramesh et al., 2021) generated images from text with one transformer reading text tokens followed by the image tokens of a discrete VAE. VQGAN (2021) made the codes sharp enough for high resolutions, and MoVQ (2022) added the modulated decoder that Emu3's tokenizer uses. Chameleon (2024) trained one early-fusion model on interleaved text and image tokens. Emu3 (2024) extended the recipe to video and reported that next-token prediction alone surpassed models such as SDXL at generation and LLaVA-1.6 at perception. The World Models Explainer's IRIS and Cosmos AR tabs predict discrete image tokens of the same kind; the FAST tab tokenizes robot actions instead of pixels.

Papers

  1. Emu3: Next-Token Prediction is All You Need Xinlong Wang et al. · arXiv 2024 One transformer trained only on next-token prediction over text, image and video tokens; its vision tokenizer is the one this tab records.
  2. MoVQ: Modulating Quantized Vectors for High-Fidelity Image Generation Chuanxia Zheng, Long Tung Vuong, Jianfei Cai, Dinh Phung · NeurIPS 2022 The decoder whose normalisation layers are modulated by the looked-up codes, the design Emu3's tokenizer is built on.
  3. Taming Transformers for High-Resolution Image Synthesis Patrick Esser, Robin Rombach, Björn Ommer · CVPR 2021 VQGAN: a convolutional VQ autoencoder trained with perceptual and adversarial losses, and a transformer over its codes.
  4. Neural Discrete Representation Learning Aaron van den Oord, Oriol Vinyals, Koray Kavukcuoglu · NeurIPS 2017 VQ-VAE: the nearest-code lookup, the straight-through gradient and the codebook and commitment losses.
  5. Zero-Shot Text-to-Image Generation Aditya Ramesh et al. · ICML 2021 DALL·E: text tokens followed by the image tokens of a discrete VAE, modelled by one autoregressive transformer.
  6. Chameleon: Mixed-Modal Early-Fusion Foundation Models Chameleon Team · arXiv 2024 An early-fusion model trained from scratch on interleaved text and VQ image tokens, the setting Emu3 extends to video.

See also