Search

Search papers and lessons

Qwen2-VL: Enhancing Vision-Language Model's Perception at Any Resolution

Peng Wang, Shuai Bai, Sinan Tan et al. · 2024

Qwen2-VL lets a vision-language model read images at their native resolution and adds a position scheme that treats time, height and width separately, so one model handles photos, documents and video.

On this page

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

TL;DR

Qwen2-VL is a family of vision-language models, at 2, 7 and 72 billion parameters, that can read an image at whatever resolution it arrives in instead of squeezing it to a fixed size. It also replaces the usual one-dimensional position code with one that tracks time, height and width separately, so the same model handles photos, documents and video. The 72B version scores at or near GPT-4o and Claude 3.5 Sonnet on many of the authors' benchmarks.

Builds on: Vision-Language Model, Vision Transformer (ViT), Rotary Position Embedding, Transformer · Leads to: Multimodal Rotary Position Embedding, Visual Instruction Tuning

Why it mattered

Qwen2-VL is one of the open-weight models that brought strong document and chart reading to a model you can run yourself. Its paper reports 96.5 on DocVQA (test) for the 72B model, 94.5 for the 7B model and 90.1 for the 2B model. The family spans three sizes, and the authors describe the 2B model as "designed to run on-device". Its position scheme, M-RoPE, places video frames and image grids on the same sequence axis as text. The paper also says the dynamic resolution mechanism was first used in the internal Qwen-VL Plus and Qwen-VL MAX models, and Qwen2-VL upgraded it.

The problem

Most vision-language models of the time took an image, resized it to a fixed square such as 224 by 224, and passed that to a vision encoder. The paper names two costs. Downsizing or padding loses detail in high-resolution images, such as dense documents or infographics. And the sequence length is fixed, so a small image and a large one cost the same.

Video had a second problem. A language model places each token on one line, so position is a single number. The paper's point is that an image is a two-dimensional grid and a video is three-dimensional (two space axes plus time), so a one-dimensional position code throws away the structure the model needs. The paper also raises concerns about the frozen CLIP-style vision encoders that most models of the time used, since their features may not suffice for complex reasoning and fine detail.

How it works

Here is the pipeline in the order the data flows through it.

flowchart LR
  A[Image or video frames] --> B[ViT with 2D-RoPE, no fixed size]
  B --> C[MLP merges each 2x2 block of patches]
  C --> D[Visual tokens with vision start and end markers]
  E[Text tokens] --> F[LLM Qwen2 with M-RoPE positions]
  D --> F
  F --> G[Text output]

Trace one small image through it. Use round numbers so the arithmetic is easy to follow. The paper's own case is a 224 by 224 image, which becomes 66 tokens. We use a smaller grid with the same rules.

  1. Input. A 4 by 4 grid of patches, each patch 14 pixels on a side. Each patch is one input unit for the vision encoder. The image is 56 pixels wide and 56 pixels high.
  2. Encode. The vision transformer (a Vision Transformer (ViT) with about 675 million parameters) turns each patch into a feature vector. Instead of a learned absolute position for each patch, it uses 2D rotary position embedding, so each patch carries its row and column in the rotation. Check: 16 patches go in, 16 feature vectors come out.
  3. Merge. A small MLP takes each 2 by 2 block of neighbouring patches and combines them into one token. Check: the 16 vectors become 4 tokens, a four-fold cut.
  4. Mark. Two special tokens, one before and one after the image tokens, tell the language model where the image starts and ends. Check: 4 image tokens plus 2 markers gives 6 tokens entering the language model.
  5. Position. Each visual token gets three position numbers instead of one. Time stays fixed because this is a still image. Height and width count the token's row and column in the merged grid. Check: the four tokens get the (row, column) pairs (0,0), (0,1), (1,0), (1,1), shifted by wherever the image starts in the sequence.
  6. Read. The language model reads the 6 tokens together with the text tokens and generates text. Text tokens use the same number for all three position parts, which makes them behave exactly like ordinary one-dimensional rotary positions.

Step 5 matters most for the rest of the paper. The position of a token is no longer a single number. It is a triple (time, height, width), and each part is rotated separately inside the attention layers.

The same rules scale to the paper's own case. A 224 by 224 image with patch size 14 gives a 16 by 16 grid, which is 256 patches. The 2 by 2 merge leaves 64 image tokens, and the two markers bring the total to the 66 tokens the paper states. The count grows with the image area, so a 448 by 448 image starts with four times as many patches before the merge.

Video follows the same rules with one change. The paper samples each video at two frames per second and treats every frame as a grid. The temporal number then advances by one for each frame, while height and width repeat the image rule.

The math

Definition (M-RoPE, informal). Each token has a position triple (t,h,w)(t, h, w). Text tokens have t=h=wt = h = w, which reduces to ordinary one-dimensional rotary position embedding. Image tokens keep tt constant and set hh and ww from their grid row and column. Video tokens advance tt by one for each frame, while hh and ww follow the image rule.

Step 2 in symbols: the encoder

The vision encoder maps a grid of NN patches to NN feature vectors v1,…,vN\mathbf{v}_1, \dots, \mathbf{v}_N. The paper removes the fixed absolute position embeddings and applies rotary embedding in two dimensions, so the attention between patches depends on their relative row and column offsets. For the 4 by 4 toy grid, N=16N = 16.

Step 3 in symbols: the merge

Let v(r,c)\mathbf{v}_{(r,c)} be the vector at row rr and column cc. The merged token for the 2 by 2 block with top-left corner (r,c)(r, c) is

u(r/2, c/2)=MLP(concat(v(r,c),v(r,c+1),v(r+1,c),v(r+1,c+1)))\mathbf{u}_{(r/2,\,c/2)} = \mathrm{MLP}\big(\mathrm{concat}(\mathbf{v}_{(r,c)}, \mathbf{v}_{(r,c+1)}, \mathbf{v}_{(r+1,c)}, \mathbf{v}_{(r+1,c+1)})\big)

Four vectors become one, so the token count falls by a factor of four. For the toy grid, 16 becomes 4.

Step 5 in symbols: the three position parts

A standard rotary embedding rotates pairs of vector components by an angle proportional to a single position pp. M-RoPE uses three positions and gives each one its own slice of the rotary dimensions. Write the position of token ii as (ti,hi,wi)(t_i, h_i, w_i). The rotary angle for the temporal slice is set by tit_i, for the height slice by hih_i, and for the width slice by wiw_i. When all three are equal, every slice rotates by the same angle, which is ordinary rotary embedding.

The toy image's four tokens therefore get the triples (5, 0, 0), (5, 0, 1), (5, 1, 0) and (5, 1, 1), where 5 is the temporal position where the image starts. The text before the image uses (0, 0, 0) through (4, 4, 4). The numbering of the toy example is ours; the paper states the rule (time constant, then row and column), not this exact offset.

The paper also says that when several modalities appear in one input, the numbering of each new modality starts from the largest position ID of the previous one plus one. This keeps positions from colliding across text, images and video. It also keeps the position IDs for images and video smaller than the raw token count, which the authors credit with letting the model run on longer sequences than it was trained on.

Applying the rule to a video

Take a 16-frame clip where each frame gives a 10 by 10 grid of tokens. The first frame has temporal position 0.

  1. Each frame's grid shares one temporal number, and the next frame's number is one higher. The temporal numbers run from 0 to 15.
  2. Height and width repeat the same layout for every frame.
  3. The total is 16 times 100, which is 1,600 visual tokens.

Try it: if the clip has 25 frames instead, how many visual tokens does it use, and what is the largest temporal number? Answer: 25 times 100 is 2,500 tokens, and the temporal numbers run from 0 to 24, so the largest is 24. (The grid sizes here are illustrative; the paper's Figure 5 cites a maximum training length of 16,384 tokens for its length tests.)

Results

The 72B model's headline numbers from the paper's tables, against the previous best and against GPT-4o and Claude 3.5 Sonnet as the authors report them (Table 2 unless noted):

  • RealWorldQA: 77.8, against 75.4 for GPT-4o and a previous best of 72.2.
  • MMBench-EN test: 86.5, equal to the previous best of 86.5 from another model and above GPT-4o's 83.4.
  • MathVista test-mini: 70.5, against 63.8 for GPT-4o.
  • MMMU validation: 64.5, below GPT-4o's 69.1.
  • Video (Table 4): on Video-MME without subtitles, the 72B model scores 71.2, against 71.9 for GPT-4o and 75.0 for Gemini 1.5-Pro. On EgoSchema it scores 77.9, against 72.2 for GPT-4o and 63.2 for Gemini 1.5-Pro.
  • Multilingual OCR (Table 3, the authors' in-house benchmark): the 72B model beats GPT-4o on every language listed except Arabic, where GPT-4o scores 75.9 and Qwen2-VL-72B scores 70.7.

Ablations. With the 7B model, InfoVQA reached 77.27 at 3,136 average image tokens and 75.89 with dynamic resolution at 1,924 average tokens. The authors' point is that dynamic resolution gets close to the best fixed setting while using fewer tokens on average, and that the right resolution depends on the image. Upscaling small images helped InfoVQA, HallusionBench and OCRBench, but a too-high minimum pixel setting badly hurt OCRBench, which the authors attribute to its extremely small images becoming out-of-distribution once enlarged. For M-RoPE, a small-backbone test (Qwen2-1.5B with a ViT-L) shows gains over 1D-RoPE on video: NextQA 46.0 against 43.9, and STAR 57.9 against 55.5.

What these numbers mean. The strongest gains are on documents, charts and math, where fine detail matters. The ablation results support the two architecture changes, not just the leaderboard. Their limits: most numbers come from the authors' own evaluations, the multilingual OCR benchmark is internal and not public, and several comparisons use scores that other papers reported for the closed models. Treat the rankings as the authors' claim and check them against the benchmark papers before relying on them.

Common misreadings. It is tempting to think that a higher-resolution image always gives a better answer; the ablation shows the right size depends on the image. It is also tempting to think that M-RoPE is a new kind of position embedding. It is rotary embedding applied to three position parts instead of one, and for text it reduces to the old one-dimensional form. The design suits inputs that vary in size, contain fine text, or include video. It is less useful when every input is a small fixed size and batching simplicity matters more; fixed-grid encoders with padding are simpler to batch, and cropping a large image into tiles keeps each tile at a known size at the cost of more bookkeeping.

Reading guide

Read the paper's sections in this order. Each one needs the lessons listed.

  • Introduction. Needs Vision-Language Model. Note the common recipe of visual encoder, connector and language model, and the problem of fixed input size.
  • 2.1 Model architecture. Needs Vision Transformer (ViT) and Rotary Position Embedding. Read "Naive Dynamic Resolution" and "Multimodal Rotary Position Embedding" closely; they are the paper's contributions.
  • 2.2 Training. Needs Transformer and Visual Instruction Tuning. The three-stage recipe is: train the vision encoder first, unfreeze everything next, then fine-tune only the language model on instructions.
  • 2.3 Infrastructure. Skim it unless you train large models. It describes parallelism and storage.
  • 3. Experiments. Read the tables first. The ablations in 3.3 are the evidence for the two architecture changes.
  • Appendix. Qualitative examples and agent demonstrations.

Read next: Multimodal Rotary Position Embedding for the position scheme on its own, and Visual Instruction Tuning for how the model learns to answer questions about images. Learning Transferable Visual Models From Natural Language Supervision (CLIP) is the frozen-encoder baseline the paper argues against.

Check yourself

QuestionWhat does Qwen2-VL do with an image instead of resizing it to a fixed size?Show answer
It feeds the image at its own resolution. The number of visual tokens then depends on the image size, through the patch grid and the 2 by 2 merge, so detail is kept rather than lost to downsizing.
QuestionA 448 by 448 image is split into 14-pixel patches and merged 2 by 2 with the markers added. How many tokens enter the language model?Show answer
448 divided by 14 is 32 patches per side, so 32 times 32 is 1,024 patches. The merge gives 1,024 divided by 4, which is 256 image tokens. Adding the two markers gives 258 tokens.
QuestionWhy does M-RoPE reduce to ordinary rotary embedding for text?Show answer
For text, the temporal, height and width positions are set to the same number. Every slice of the rotary embedding then rotates by the same angle, which is the one-dimensional rotary embedding used in language models.
QuestionIn the paper's ablation, dynamic resolution used fewer tokens than the best fixed setting. Why does that matter?Show answer
Because the best fixed setting needs many tokens for every image. Dynamic resolution gets close to its accuracy with fewer tokens on average, so it saves compute without a large loss. The paper's InfoVQA numbers are 75.89 for dynamic against 77.27 for the largest fixed setting.
QuestionA model is asked to describe a 20-minute video. Why might a one-dimensional position code struggle?Show answer
A video is a sequence of frames, each frame a two-dimensional grid. A one-dimensional code gives every token a single position, so the model cannot separately track when a token occurs and where in the frame it sits. M-RoPE gives time, height and width their own position parts, which is the paper's motivation for the change.

Sources

Read the full paper

Peng Wang, Shuai Bai, Sinan Tan et al., 2024. arXiv:2409.12191