Tokenization & Context Treatment — Technologies & Tricks

Updated July 2026 with 2025–2026 SOTA additions — new entries marked ★. Algorithm names link to their papers (arXiv / project page).

April 2026 · Version 1.0


Contents

  1. Why Tokenization Matters
  2. Text Tokenization
  3. Vision Tokenization: Continuous (Patch Embedding)
  4. Vision Tokenization: Discrete (VQ Family)
  5. Video Tokenization
  6. Audio Tokenization
  7. Action and Robot Tokenization
  8. Multi-modal Token Interleaving
  9. Position Encoding for Long Context
  10. Long-Context Training Recipes
  11. Memory and Recurrence Mechanisms
  12. Context Compression and Distillation
  13. Sequence Packing and Batch-Level Tricks
  14. Inference-Time Context Tricks
  15. Retrieval-Augmented Context (RAG)
  16. Long Video and Multi-Image Context for VLMs
  17. Diffusion-Specific Context Treatment

Appendix A: Twenty-Five Things to Memorize

1. Why Tokenization Matters

1.1 The big picture

A model never sees raw bytes / pixels / waveform; it sees tokens: a vocabulary-indexed sequence whose embedding lookup feeds the first layer. Tokenization is the bridge between the world and the model, and it is the most under-appreciated source of capability and bug in modern systems.

Three properties to optimize:

1.2 Modality cheat sheet

Modality Common scheme (2026) Tokens per unit
Text (English) BPE / SentencePiece ~1 token / 4 chars
Text (CJK) BPE / SentencePiece ~1 token / 1.5 chars
Text (code) BPE w/ code-aware vocab ~1 token / 3.5 chars
Image (continuous) Patch embedding \((H/p)(W/p)\), \(p \in \{14, 16\}\)
Image (discrete) VQ / FSQ / LFQ 256–4096 / image
Image (1D) TiTok 32 / image
Video (latent) Causal 3D VAE \((T/4)\cdot(H/8)\cdot(W/8)\)
Audio (24 kHz) EnCodec / SoundStream 75 / sec
Action (robot) Discrete bins or FM 7–14 / step

2. Text Tokenization

2.1 The fundamental algorithms

2.1.1 BPE (Byte-Pair Encoding)

Greedy bottom-up merge of most-frequent symbol pairs:

  1. Initial vocab = all characters / bytes in corpus.
  2. Count adjacent pair frequencies in the corpus.
  3. Merge the most frequent pair into a new symbol; add to vocab.
  4. Repeat until target vocab size reached.

At inference, apply learned merges to input string in the same order.

Used by: GPT-2/3/4 (tiktoken), RoBERTa, Llama 2 (modified), most modern LLMs.

2.1.2 Byte-level BPE (BBPE)

Operate on bytes (256 initial symbols), not Unicode characters. Guarantees no <UNK> ever; covers any text.

Standard since GPT-2.

2.1.3 WordPiece (BERT)

Like BPE but the merge criterion is the likelihood-ratio score:

\[\text{score}(a, b) = \frac{\text{count}(ab)}{\text{count}(a)\cdot\text{count}(b)}.\]

Picks the merge that increases corpus likelihood the most under the unigram model assumption.

Inference: greedy longest-match (## prefix marks subwords).

2.1.4 Unigram (SentencePiece, T5, XLNet)

Define a unigram language model over a candidate vocabulary; train via EM, removing low-probability tokens until target vocab size:

\[P(\mathbf{x}) = \prod_i P(x_i),\]

\[\mathcal{L} = \sum_{\mathbf{w}} \log \sum_{\mathbf{x}\in S(\mathbf{w})} P(\mathbf{x}),\]

with \(S(w)\) = all valid tokenizations of word \(w\). Multiple valid tokenizations per string \(\to\) at training, sample from \(P\); at inference, take the argmax.

2.1.5 SentencePiece (the framework)

Treats input as a raw character / byte stream (no pre-tokenization). Supports BPE and Unigram modes. Handles arbitrary languages without language-specific rules. Used by: T5, Llama 1/2/3, Mistral, Gemma.

2.2 Modern tokenizer specifics

2.2.1 Tiktoken (OpenAI)

Implementation: byte-level BPE with regex pre-splitting (numbers split into individual digits, words split on whitespace).

2.2.2 Llama 3 / Llama 4 tokenizer

~128k vocab (Llama 3) up from ~32k (Llama 2). Big improvement for non-English languages. SentencePiece \(\to\) BPE.

2.2.3 Gemma, Mistral, Qwen, DeepSeek

Mostly SentencePiece BPE with 32k–200k vocabularies. Vocabulary expansion (continued pretraining) when adding new languages or code support.

2.3 Special tokens to know

2.4 Chat / instruction templates

2.4.1 ChatML (OpenAI)

<|im_start|>system
You are a helpful assistant.<|im_end|>
<|im_start|>user
What is 2+2?<|im_end|>
<|im_start|>assistant
4<|im_end|>

2.4.2 Llama 3 template

<|begin_of_text|><|start_header_id|>system<|end_header_id|>

...<|eot_id|><|start_header_id|>user<|end_header_id|>

...<|eot_id|><|start_header_id|>assistant<|end_header_id|>

2.4.3 Why templates matter

A chat-tuned model is trained to expect exactly this format. Off-template prompts produce noticeably worse outputs. Always use the model's official Jinja template (tokenizer.apply_chat_template).

2.5 Tokenization pitfalls

2.5.1 Numbers

Naive BPE merges "100", "1000" into single tokens. Disastrous for arithmetic. Modern tokenizers (Llama 3, GPT-4o) split into individual digits ("1", "0", "0") via regex pre-split.

2.5.2 Code tokenization

Python-aware regex helps (split on operators, keywords, indentation). StarCoder/CodeLlama tokenizers preserve common indents (" " as one token).

2.5.3 Multilingual fairness

A tokenizer trained on English-heavy corpus emits 2–5× more tokens for the same content in low-resource languages. Costs 2–5× more inference dollars per request and burns context budget. Larger / more balanced vocabularies (Llama 3, GPT-4o) partially fix.

2.5.4 Glitch / unspeakable tokens

Rare BPE merges that occur in vocab-build but rarely in pretraining have undefined behavior (SolidGoldMagikarp-style). Identifiable by extremely high logit variance; production filters should reject.

2.5.5 Detokenization issues

Some tokenizers strip leading whitespace; others encode it. "hello" vs " hello" are different tokens. Streaming generation often requires a decode that handles spacing.

Watch out

Tokenizer choice is an architectural decision with multi-year consequences. Vocab is locked once pretrained; expansion (adding tokens later) is fragile because new embeddings start at zero.

★ 2026 SOTA update — Tokenizer-free byte models

★ 2026 SOTA update — Superword and adaptive tokenizers

3. Vision Tokenization: Continuous (Patch Embedding)

3.1 ViT patch embedding

Image \(X \in \mathbb{R}^{H\times W \times 3}\) split into \(N = (H/p)(W/p)\) patches of size \(p \times p\).

Each patch flattened and linearly projected:

\[z_i = W_p \cdot \text{vec}(X_{\text{patch}_i}) + b_p, \qquad W_p \in \mathbb{R}^{d\times 3p^2}.\]

Equivalently: a \(p \times p\) stride-\(p\) convolution with \(d\) output channels.

Standard patch sizes: \(p = 16\) (ViT-B/16 default), \(p = 14\) (DINOv2/CLIP-L), \(p = 32\) (smaller variants).

3.2 Class token

Prepend learnable token \(z_{[\text{CLS}]} \in \mathbb{R}^d\) to the patch sequence. After encoding, \(z^{(L)}_{[\text{CLS}]}\) is used for classification.

Modern alternative: average-pool all patch tokens.

3.3 Position embeddings on patches

3.4 Convolutional stem

ViT-22B and others replace single-layer patchify with a small conv stem (e.g., 3 stride-2 convs). Empirically more stable; trades a small spatial bias for training robustness.

3.5 Pixel shuffle / unshuffle for token compression

Reshape \((H, W, C) \to (H/r, W/r, Cr^2)\); linearly project to target channel: tokens reduced by \(r^2\).

Standard in InternVL family (\(r = 2\), 4× fewer tokens, manageable for high-res).

3.6 AnyRes (LLaVA-NeXT)

For high-resolution inputs:

  1. Resize to a global thumbnail (\(336^2\)).
  2. Tile the original into chunks of \(336^2\).
  3. Encode thumbnail + each tile separately.
  4. Concatenate token sequences with separators.

\(N\) tiles \(\to N + 1\) sub-images; token count grows linearly, OCR / dense reasoning improves dramatically.

3.7 Native dynamic resolution (Qwen2-VL, InternVL3)

Process the image at its native aspect ratio: assign 2D-RoPE coordinates to each patch from absolute (row, col).

Token count \(\approx HW/p^2\). Combined with pixel unshuffle for compression.

3.8 Image cropping for training (RandomResizedCrop)

Augmentation: random crop with random scale (0.08–1.0) and aspect ratio (3/4–4/3), then resize to fixed input size. Standard for ImageNet/CLIP/DINO training; less common for VLM pretraining.

3.9 Register tokens

Append \(K \sim 4\) extra learnable "register" tokens to the patch sequence. They absorb high-norm artifacts that otherwise dump on random patches, cleaning attention maps and improving dense-task quality (DINOv2, V-JEPA).

4. Vision Tokenization: Discrete (VQ Family)

4.1 Why discrete tokens?

4.2 VQ-VAE basics

Encoder \(E : X \mapsto z_e \in \mathbb{R}^{H'\times W' \times d}\). Codebook \(\{e_1, \ldots, e_K\} \subset \mathbb{R}^d\). Quantize each spatial position by nearest neighbor:

\[z_q^{(i,j)} = e_{k^*}, \qquad k^* = \arg\min_k \left\| z_e^{(i,j)} - e_k \right\|_2.\]

Decoder \(D : z_q \mapsto \hat{X}\). Loss with stop-grad straight-through estimator:

\[\mathcal{L}_{\text{VQ}} = \| X - D(z_q) \|^2 + \| \text{sg}(z_e) - e_{k^*} \|^2 + \beta \| z_e - \text{sg}(e_{k^*}) \|^2.\]

Last term (\(\beta \sim 0.25\)) is the commitment loss that pulls encoder output toward codebook entries.

4.3 VQ-VAE-2 (multi-scale)

Hierarchical: top-level codes capture global structure (e.g. pose), bottom-level codes capture fine detail (e.g. texture). Generated autoregressively top-to-bottom.

4.4 VQ-GAN

VQ-VAE + adversarial loss + perceptual (LPIPS) loss:

\[\mathcal{L}_{\text{VQGAN}} = \mathcal{L}_{\text{VQ}} + \lambda\,\mathcal{L}_{\text{GAN}} + \lambda_p\,\mathcal{L}_{\text{LPIPS}}.\]

Sharper reconstructions; standard backbone for Stable Diffusion's KL-VAE alternative.

4.5 Codebook-collapse mitigations

Persistent problem: many codes go unused.

4.6 LFQ (Lookup-Free Quantization, MAGVIT-v2)

Project encoder output to dimension \(L\), sign-quantize to \(\{-1, +1\}^L\):

\[q = \text{sgn}(z), \qquad \text{vocab size} = 2^L.\]

No codebook. No collapse by construction. Decoder receives the sign vector. Loss combines reconstruction + entropy regularizer that promotes uniform usage:

\[\mathcal{L}_{\text{LFQ}} = \mathcal{L}_{\text{recon}} + \lambda_e\,\mathcal{L}_{\text{ent}}, \qquad \mathcal{L}_{\text{ent}} = \mathbb{E}[H(P(q|z))] - H(\mathbb{E}_z[P(q|z)]).\]

Vocabularies up to \(2^{18}\) practical. Used in MAGVIT-v2, Cosmos.

4.7 FSQ (Finite Scalar Quantization)

Each dim of \(z \in \mathbb{R}^d\) rounded to a small set \(\{-K_i, \ldots, K_i\}\):

\[q_i = \text{round}(K_i \cdot \tanh(z_i)), \qquad |\text{vocab}| = \prod_i (2K_i + 1).\]

No codebook, no entropy reg. Simpler than LFQ. Common: 8 dims with levels [8, 8, 8, 5, 5, 5], vocab ~64k.

4.8 BSQ (Binary Spherical Quantization)

Project to unit sphere then sign-quantize per coordinate.

Combines LFQ's binary structure with spherical normalization for stability.

4.9 TiTok (1D tokenization)

Learn a 1D sequence of ~32 latent tokens per \(256^2\) image (vs 256+ for 2D). Cross-attention from learnable queries over the encoder's 2D features. Trade spatial structure for sequence brevity; surprisingly strong downstream generation quality.

4.10 Cosmos Tokenizer (NVIDIA)

Joint image + video tokenizer with continuous and discrete variants. Causal 3D structure (temporal causal, spatial non-causal). Supports up to 8K resolution.

4.11 Comparison summary

Method Vocab Codebook Collapse-prone Use
VQ-VAE 1k–16k yes yes legacy
VQ-GAN 8k–16k yes yes SD-VAE alt
MAGVIT (VQ) 1k yes yes video gen
LFQ (MAGVIT-v2) \(2^{10}\)\(2^{18}\) no no video / image
FSQ up to ~64k no no general
BSQ \(2^L\) no no general
TiTok varies yes some 1D fast gen
Cosmos varies yes/no no video at scale

5. Video Tokenization

5.1 Frame-by-frame 2D tokenization

Apply image tokenizer per frame independently. Simplest; no temporal compression. Used in early video gen (ModelScope T2V).

5.2 Tubelet patches (3D ViT)

Patches are 3D blocks of size \(p_t \times p_h \times p_w\). Token count: \((T/p_t)(H/p_h)(W/p_w)\). Standard in ViViT, VideoMAE.

5.3 Causal 3D VAE

Encoder: 3D convolutions with causal temporal conv (current frame depends only on past). Compression typically \(4\times\) temporal, \(8\times\) spatial. Decoder symmetric.

\[T \times H \times W \; \xrightarrow{\text{causal 3D VAE}} \; (T/4) \times (H/8) \times (W/8) \times C.\]

Standard in Sora, Open-Sora, CogVideoX, Wan, MovieGen.

5.4 First-frame special handling

Causal VAE often handles the first frame asymmetrically (it has no past). Common trick: pad first frame as its own past, or use a separate first-frame encoder.

5.5 MAGVIT-v2 video tokenization

Joint image + video LFQ tokenizer. A still image is the \(T = 1\) case. Same tokenizer trains both modalities; downstream model is unified.

5.6 Cosmos Video Tokenizer

Continuous (CV) and discrete (DV) variants. Continuous gives ~8× spatial × 8× temporal compression for diffusion. Discrete uses FSQ at ~64k vocab for autoregressive video models.

5.7 Token budget per second of video

At 24 fps, \(H = W = 512\), \(p = 8\) spatial ×4 temporal compression:

\[\text{tokens / sec} = 24 \cdot 512^2 / (8^2 \cdot 4) \approx 24{,}576.\]

1-minute clip ~1.5M tokens at this rate. Compression remains a major frontier.

6. Audio Tokenization

6.1 Why audio tokenization?

Generative audio (MusicGen, AudioLM, Suno, Udio), TTS (NaturalSpeech, VALL-E), audio understanding (Whisper variants), joint audio-video generation (MovieGen, Veo 3) all require tokenized audio.

6.2 SoundStream

RVQ (Residual VQ) on raw waveform. Encoder compresses 24 kHz waveform to 75 Hz latents; \(N\) levels of VQ quantize the residual at each step:

\[z_0 = z_e, \qquad q_n = Q_n(z_{n-1}), \qquad z_n = z_{n-1} - q_n.\]

Reconstruct: \(\hat{z} = \sum_n q_n\). Typical \(N = 8\), vocab 1024 per level.

6.3 EnCodec (Meta)

Like SoundStream with adversarial loss. 75 tokens/sec at 24 kHz, 8-level RVQ. Used in MusicGen, AudioLM.

6.4 WaveTokenizer (single-codebook)

One codebook with very large vocab (e.g., 4096). Simpler downstream modeling (no RVQ stacking) at slight quality cost.

6.5 Mel-spectrogram tokens

Some pipelines tokenize the mel-spectrogram instead of raw waveform (more parameter-efficient). Vocoder (HiFi-GAN, BigVGAN) decodes back to waveform.

7. Action and Robot Tokenization

7.1 Discrete action tokens (RT-2, OpenVLA)

Each action dimension binned into \(K\) discrete tokens (typically \(K = 256\)). For a 7-DoF arm: 7 tokens per timestep. Embedded into the LLM vocab so the same model predicts text or actions.

7.2 Action chunks (ACT, π0)

Predict \(H\) future actions per step (action chunk). Reduces compounding error and allows temporal smoothing via overlapping ensemble at inference.

7.3 Continuous action via flow matching (π0)

Small flow expert head produces continuous action vectors \(a_t \in \mathbb{R}^d\) via flow matching:

\[\mathcal{L}_{\text{FM}} = \mathbb{E}\left\| v_\theta(a_t, t, o) - (a_1 - a_0) \right\|^2.\]

Avoids discretization loss for fine manipulation; integrates with VLM backbone.

7.4 Multi-embodiment vocab

For cross-embodiment training (RT-X, OpenVLA), align action dimensions across robots via a canonical action space; each embodiment has its own tokenizer head.

8. Multi-modal Token Interleaving

8.1 Interleaved sequences

A modern VLM input is a single token sequence with image / text / video tokens interleaved. Common patterns:

8.2 Modality-specific embedding tables

Three options:

8.3 Modality boundary tokens

These help the model learn to switch attention patterns at modality boundaries.

8.4 Native multimodal training

9. Position Encoding for Long Context

9.1 The extrapolation problem

A model trained at context length \(L_{\text{train}}\) generally degrades at \(L_{\text{eval}} > L_{\text{train}}\). Extending context cleanly requires position encoding that extrapolates.

9.2 Position Interpolation (PI)

Linearly compress positions: \(p \to p \cdot L_{\text{train}}/L_{\text{eval}}\). Cheap, quick, but sacrifices high-frequency detail. Needs short fine-tuning (~1B tokens) to recover.

9.3 NTK-aware RoPE

Rather than uniform interpolation, scale the base wavelength so high-frequency components stay almost intact and low-frequency ones stretch:

\[\theta'_i = \theta_i \cdot \left(\frac{L_{\text{eval}}}{L_{\text{train}}}\right)^{-2i/(d-2)}.\]

Essentially keeps fine-grained position info while extending coarse-grained.

9.4 YaRN (Yet another RoPE extensioN)

Piecewise rescale by frequency band:

Plus an inverse-temperature term in attention. Achieves 10–32× context extension with light fine-tuning.

9.5 LongRoPE

Search for per-dimension rescaling factors via evolutionary algorithm on a long-context evaluation.

Pushes Llama-2 to 2M context.

9.6 Self-Extend

Inference-time only: bin positions in groups of \(G\) at distant ranges, keep fine positions only locally. No fine-tuning.

Effective for \(2\)\(4\times\) extension.

9.7 ALiBi for extrapolation

ALiBi inherently extrapolates because the bias is a continuous function of distance, not a learned table. Trained at 2K, runs at 16K reasonably. Lower asymptotic quality than RoPE+YaRN at same training.

★ 2026 SOTA update — Near-lossless RoPE scaling

10. Long-Context Training Recipes

10.1 Continued pretraining at longer context

Standard recipe: pretrain at \(L_0\) (e.g., 4K), then continued-pretrain at \(L_1 \in \{16K, 32K, 128K\}\) on a curated long-context corpus. Often two stages: 4K \(\to\) 32K \(\to\) 128K.

10.2 Long-context data curation

Sources: code repositories, books, scientific papers, web pages with long threads. Filter by length and remove templated repetition. Shuffle to balance topics.

10.3 Synthetic long-context data

10.4 Sequence parallelism for training long sequences

Training at 100K+ tokens requires sequence parallelism (and often ring attention). Activation memory dominates without it.

10.5 Long-context evaluation

Watch out

A model can pass NIAH and still fail real long-document tasks. NIAH measures retrieval; real tasks need synthesis, reasoning, and selective ignoring of distractors. Use multiple eval suites.

11. Memory and Recurrence Mechanisms

11.1 Sliding window + sink (recap)

Mistral, StreamingLLM: keep first \(k \sim 4\) "sink" tokens always in cache plus sliding window of \(w \sim 4096\) recent.

Effective unbounded streaming.

11.2 Memorizing Transformer

Attention augmented with a kNN-retrieval over a large external memory of past KVs. At inference, query the memory; concat top-k to local KVs.

11.3 Recurrent Memory Transformer (RMT)

Chunk input into segments; pass a small set of memory tokens between segments. Read-write memory similar to Neural Turing Machine, simpler.

11.4 Compressive Transformer

Maintain two memories: short (recent KVs) and long (compressed older KVs via pooling / conv).

Trades exactness for capacity.

11.5 RWKV / RetNet / Mamba

Linear-recurrence architectures with constant per-token state \(\Rightarrow\) effectively unbounded context at \(O(d^2)\) memory.

RWKV / Mamba have parallel-train, recurrent-inference duality. Trade-off: weaker exact retrieval than attention at the same scale.

11.6 Token-merge / pruning during inference

Periodically merge or drop tokens that have low attention from current queries (DiffPlanner, H2O).

Keeps cache small without full compression.

★ 2026 SOTA update — Trainable sparse attention

12. Context Compression and Distillation

12.1 LLMLingua / LLMLingua-2

Prompt compression by token deletion. A small classifier scores each token; drop low-importance tokens. Achieves 5–20× compression with minor quality loss for long instructions / RAG contexts.

12.2 AutoCompressors

Train a small adapter to compress a long context into \(K\) summary tokens that are concatenated to a short query.

Effectively a learned context summarizer.

12.3 Activation compression

Compress KV cache via:

12.4 Hierarchical / multi-scale context

Process long context at multiple granularities: coarse summaries at top, detailed chunks below.

Used in MovieChat (long video), MA-LMM, Goldfish.

★ 2026 SOTA update — Query-agnostic KV compression

13. Sequence Packing and Batch-Level Tricks

13.1 Naive padding

Pad each sequence to batch max length. Wastes compute on padding tokens.

13.2 Bucket batching

Sort sequences by length; batch similar lengths together. Reduces padding waste.

13.3 Sequence packing

Concatenate multiple short sequences into one row up to max length, separated by EOS. Use block-diagonal attention mask so each example only attends to itself:

\[M_{ij} = \begin{cases} 0 & i, j \text{ same example} \\ -\infty & \text{else} \end{cases}.\]

Eliminates padding; standard in modern training (Megatron, Flash-Attention's varlen API).

13.4 Document packing for pretraining

Pack documents to fixed length, separated by EOS. Common variants:

13.5 Sample packing for SFT

For multi-turn instruction tuning, pack multiple short conversations into one row with proper masking and per-conversation loss masking (don't compute loss on user turns).

13.6 Loss masking in SFT

14. Inference-Time Context Tricks

14.1 Prefix caching

The KV cache for a fixed prefix (e.g. system prompt) is computed once and reused across requests.

Linear speedup proportional to prefix length.

14.2 Prompt caching (Anthropic API)

First ~5 minutes after a prompt is sent, the API caches the KV; subsequent requests with the same prefix benefit from up to 90% cost reduction. Granular checkpoints supported.

14.3 Paged KV cache (vLLM)

KV stored in fixed-size blocks; per-request page table maps logical \(\to\) physical. Enables shared prefix across requests, no fragmentation, and continuous batching.

14.4 Continuous batching

At each token step, accept new requests / drop completed ones. Per-token batching at the kernel level. Bumps throughput 5–10× over static batching for variable-length workloads.

14.5 Speculative decoding

Draft model proposes \(k\) tokens; target verifies all in one forward. Effective speedup 2–3×. Requires the target's KV cache to support the new tokens; tree attention generalizes (Medusa, EAGLE).

14.6 KV reuse across edits / rewrites

Tools like Cursor / Cline cache the file's KV; minor edits invalidate only the suffix. Same idea: incremental computation when prefix is stable.

★ 2026 SOTA update — Cache-augmented generation

15. Retrieval-Augmented Context (RAG)

15.1 The RAG pattern

  1. Encode query with embedding model.
  2. Retrieve top-k relevant chunks from a vector index.
  3. Inject chunks into prompt: <chunk1> <chunk2> ... <query>.
  4. Generate answer.

15.2 Chunking strategies

15.3 Embedding models

15.4 Re-ranking

After top-k retrieval, re-rank with a cross-encoder (BGE-Reranker, Cohere Rerank).

Returns higher-quality top-N for the LLM. Latency cost: \(k\) extra small-model passes.

15.5 Long-context vs RAG

15.6 Query rewriting and decomposition

Multi-hop questions: decompose into sub-questions; retrieve per sub-question; aggregate. Used in Self-RAG, LangGraph patterns, agentic RAG.

15.7 GraphRAG

Build a knowledge graph from corpus (entities + relations), retrieve sub-graphs by query similarity, inject as context. Better for multi-hop reasoning over structured knowledge.

16. Long Video and Multi-Image Context for VLMs

16.1 Token budget management

At 256 tokens/frame (after pixel unshuffle), 1 fps for 1 hour = 920k tokens. Fitting requires aggressive compression.

16.2 Temporal pooling

Average / max pool frame tokens across \(T\) frames before feeding LLM. Crude but cheap; loses temporal detail.

16.3 Token merging across time (ToMe)

Apply Token Merging across the temporal dim: merge tokens with high cosine similarity to neighbors. Adaptive compression per-clip.

16.4 Q-Former summarization (BLIP-2 style)

\(n_q \sim 32\) queries per frame chunk; cross-attend over the chunk's tokens; output fixed-size summary. Then concat summaries across chunks.

16.5 Hierarchical memory (MovieChat, MA-LMM)

16.6 Caption-then-reason (Goldfish, MM-VID)

Generate captions per frame / clip with a small VLM, store in text. On query, retrieve relevant captions; reason in text-only mode. Cheap, scalable to multi-hour video.

16.7 Sparse temporal sampling

For most VLMs, sample 8–64 frames uniformly from a video; encode each; concat. Loses fine-grained motion but works for global understanding.

16.8 Token compression via 3D-RoPE

With 3D-RoPE on \((t, h, w)\), the model can natively handle variable resolution / fps without per-token modality flags. Combined with adaptive sampling, scales to longer videos.

16.9 Long-context video VLMs (2026)

17. Diffusion-Specific Context Treatment

17.1 Text conditioning in U-Net diffusion

17.2 MM-DiT joint context

SD3, FLUX: text + image tokens concatenated and processed by joint self-attention. Text uses learnable position; image uses 2D-RoPE.

17.3 Long-prompt handling

17.4 Image as context

17.5 ControlNet conditioning

Trainable copy of encoder takes the control input (canny, depth, pose, segmentation, scribble); outputs added to base U-Net via zero-conv connections.

17.6 Long video diffusion: context across frames

17.7 Diffusion KV / activation cache (DeepCache, TGATE, Block-Cache)

Cache U-Net (or DiT) intermediate activations across consecutive denoising steps; refresh every \(k\) steps. 2–4× inference speedup with negligible quality loss.

17.8 Reference-only conditioning patterns

Appendix A: Twenty-Five Things to Memorize

  1. BPE merge algorithm; greedy frequency-based merges.
  2. WordPiece score formula: \(\text{count}(ab)/(\text{count}(a)\,\text{count}(b))\).
  3. Unigram LM training via EM for SentencePiece.
  4. Byte-level BPE: no UNK ever.
  5. ChatML and Llama 3 chat template structure.
  6. Number tokenization: digit-split via regex (Llama 3, GPT-4o).
  7. Multilingual tokenizer fairness problem.
  8. ViT patch embedding: \(W_p\) as a stride-\(p\) conv.
  9. Class token vs average pooling for ViT classification.
  10. Pixel unshuffle for \(r^2\) token compression in VLMs.
  11. LLaVA-NeXT AnyRes tiling pattern.
  12. Native dynamic resolution + 2D-RoPE (Qwen2-VL).
  13. Register tokens for absorbing high-norm artifacts.
  14. VQ-VAE training loss with commitment term.
  15. LFQ: \(q = \text{sgn}(z)\), no codebook, vocab \(2^L\).
  16. FSQ: per-dim rounding to a small set, no entropy reg.
  17. Causal 3D VAE for video: \((T/4)\cdot(H/8)\cdot(W/8)\) compression.
  18. Position Interpolation: \(p \to p \cdot L_{\text{train}}/L_{\text{eval}}\).
  19. NTK-aware RoPE base scaling.
  20. YaRN's piecewise-by-frequency rescaling + temp adjust.
  21. Sliding window + sink tokens (StreamingLLM).
  22. Sequence packing with block-diagonal attention mask.
  23. Loss masking in SFT: \(-100\) on user / system positions.
  24. Prefix caching / Anthropic prompt cache cost model.
  25. RAG vs long-context trade-off framework.