---
myst:
  html_meta:
    description: "How the vision bucket planner chooses an attention block ladder"
    content_type: "conceptual-deep-dive"
    date-modified: "2026-11-06"
---

# Vision Block Bucket Planner

## Overview

The vision bucket planner chooses the `encoder_graph_shapes` of a
mixed-resolution deployment. It reports the cost of each candidate list, so you
can compare one block size against a ladder of several sizes before you spend a
compile.

Run it with:

```bash
python -m vllm_neuron.model.qwen3_vl.tools.vision_bucket_planner \
    --mix 1920x1080:0.25,1280x720:0.25,640x360:0.5 --frames 600 --sweep
```

Each entry of `--mix` is `WxH` or `FramesxWxH`, with an optional `:share`. Two
numbers take `--frames`; three give the frame count first, so **one mix can
hold image and video entries**. `Frames` 1 is an image, because the server
packs an image as a single item of `grid_h * grid_w` tokens and a video of F
frames as `ceil(F / 2)` such items. So a fleet that serves 10-minute 1080p
videos and 512x512 images is:

```bash
python -m vllm_neuron.model.qwen3_vl.tools.vision_bucket_planner \
    --mix 600x1920x1080:0.25,1x512x512:0.75 --dp-size 8 --max-graphs 4 --best
```

A share is a fraction of the **requests**, because a request draws one entry.
A request that holds several media items at once, such as one video and three
images, is not modelled: the planner prices one item for each request.

The planner ships inside the package, so an installed wheel has it. In that
form it calls the server's own packing code, so it cannot disagree with the
server. On a host with no Neuron runtime, run the file directly
(`python vllm_neuron/model/qwen3_vl/tools/vision_bucket_planner.py ...`) and the tool
falls back to its mirrors of that code, which
`test/unit/test_vision_bucket_planner.py` pins against the production
functions.

[Block packing attention](block_packing_attention.md) describes the mechanism
that the planner configures. Read it first.

## Problem Statement

One block size must hold the largest frame. So a deployment that accepts 1080p
frames (8,160 raw tokens) needs 8192-token blocks, and every request then uses
them — also a 512x512 video whose frames need only 1024. The attention kernel
masks the packed sequences, but it computes every tile of a block.

Several block sizes remove that cost: each encoder run uses the smallest size
that holds its largest frame. The mechanism does not say which sizes to
compile, and the choice is not obvious in either direction:

- Each extra graph costs compile time and device memory. At TP8, an 8192 block
  together with a smaller block does not fit.
- A ladder that omits one resolution's block can cost **more** than one large
  block. A 960x540 video shows this. Its temporal patch group is 2,040 raw
  tokens, which fits a 3584 block **once** and wastes 43%, and fits an 8192
  block **four** times (4 x 2,040 = 8,160) and wastes 0.4%. The packing
  advantage of the larger block is bigger than its larger attention term, so a
  ladder of 1024, 3584 and 8192 costs 1.15 times a single 8192 block for this
  resolution. Adding the 2048 block that the resolution needs turns that into
  a gain of 1.80:

  | Graphs | Runs | Padding | Cost against one 8192 block |
  | --- | --- | --- | --- |
  | `[8192]` | 10 x (8192, 8 blocks) | 1.071x | 1.00x |
  | `[1024, 3584, 8192]` | 17 x (3584, 16) + 1 x (8192, 8) | 1.700x | **0.89x (worse)** |
  | `[1024, 2048, 3584, 8192]` | 17 x (2048, 16) + 1 x (8192, 8) | 1.017x | 1.67x |

  Reproduce it with:

  ```bash
  python -m vllm_neuron.model.qwen3_vl.tools.vision_bucket_planner \
      --mix 960x540 --frames 600 --static-block 8192 --ladder 1024 3584 8192
  ```

  A larger block does not always cost more. Whether omitting a size hurts
  depends on how evenly the group divides each candidate block, so it has to
  be computed. A 640x480 group (1,200 raw tokens) fits a 3584 block twice and
  an 8192 block six times, and there the same incomplete ladder still costs
  less than one 8192 block.

The planner answers the choice with numbers.

## Token Arithmetic

Three conversions decide every count. Each one is a frequent source of error.

1. **The processor resizes each frame.** Each side rounds to the nearest
   multiple of `patch_size * spatial_merge_size` (32 for Qwen3-VL). A side that
   is already a multiple does not move, so a 512-pixel side stays. A 720-pixel
   side becomes 704, and a 1080-pixel side becomes 1088.
2. **Raw tokens come from the resized grid.** `grid_h * grid_w`, where each
   grid side is the resized side divided by `patch_size`. A 1280x720 frame
   gives `80 * 44 = 3520` raw tokens, not `80 * 45 = 3600`.
3. **Two frames share one temporal patch group.** `temporal_patch_size` is 2,
   so a 600-frame video has 300 groups. One group is one packing item, and it
   holds the spatial tokens of one frame. Embed tokens are
   `groups * raw_tokens / merge_factor`, so a 600-frame 512x512 video is
   `300 * 1024 / 4 = 76,800` embeds.

For the target resolutions:

| Resolution | Resized | Raw tokens | Embeds | Smallest block |
| --- | --- | --- | --- | --- |
| 256x256 | 256x256 | 256 | 64 | 256 |
| 384x384 | 384x384 | 576 | 144 | 640 |
| 640x360 | 352x640 | 880 | 220 | 896 |
| 512x512 | 512x512 | 1,024 | 256 | 1024 |
| 640x480 | 480x640 | 1,200 | 300 | 1280 |
| 1280x720 | 704x1280 | 3,520 | 880 | 3584 |
| 1920x1080 | 1088x1920 | 8,160 | 2,040 | 8192 |

The smallest block is the next multiple of 128. The vision encoder pads the
attention sequence to 128 (`_ATTN_SEQ_ALIGN`), so a block size that is not a
multiple of 128 wastes the remainder.

## Cost Model

The planner counts MACs. For each padded token and each layer, the vision tower
has two terms:

| Term | MACs for each padded token and layer | Depends on the block size |
| --- | --- | --- |
| Linear (QKV, O, MLP) | `4*d^2 + 2*d*I` | No |
| Attention (QK^T, PV) | `2*B*d` | Yes |

`d` is the vision hidden size (1152), `I` the intermediate size (4304), and `B`
the block size. A block computes a full `B x B` score matrix twice, so the cost
for each token is linear in `B`.

This explains the size of the gain. Attention is about 13% of the tower at a
block of 1024, and about 55% at 8192. So moving from an 8192 block to a 1024
block cuts the attention term by 8, but the whole tower by about 2:

```text
B = 1024:  15.22M (linear) +  2.36M (attention) = 17.58M
B = 8192:  15.22M (linear) + 18.87M (attention) = 34.10M
                                          ratio = 1.94x
```

A cost ratio is therefore always smaller than the ratio of the block sizes.
Expect about 2x, not about 8x.

### What the model does not cover

**The model counts MACs. It does not model device memory, and device memory is
what decides whether a ladder is deployable at all.** The planner can report a
large gain for a ladder that the server cannot load. Two settings decide
whether a ladder fits, and neither appears in the cost model:

- The engine sizes the **KV cache before** it warms up the encoder, and by
  default it takes about 3.3 times one request: 325,568 tokens at
  `max_model_len` 98,304. That can leave no room for the graphs of a ladder. A
  four-graph ladder for Qwen3-VL-32B at TP16 ran out of memory on its third
  graph until `--num-gpu-blocks-override` capped the KV cache to the one
  sequence that the server runs.
- `encoder_cache_num_blocks` is best left unset on a monolith, so the model
  runner derives it from the scheduler budget. An explicit count for two
  in-flight videos of the largest resolution of a 4-way mix asked for 122,401
  blocks of 4 merged tokens, and the server then failed to load the model.

The long-video recipe records the related limit that an 8192 block together
with a smaller block does not fit at TP8. So read a ladder from the planner as
a **compute** proposal, then check it against device memory: set
`max_model_len` to the smallest value that holds the largest request, cap the
KV cache when the server runs few sequences, and bring the server up once
before you trust the number.

The model also counts MACs and not time. It does not model the achieved MFU,
which falls with a small block, and which the vision head dimension of 72 caps
already: 72 of the 128 PE rows. So use the planner to **rank** candidate
ladders, and measure the chosen ladder on the device. The ranking is robust,
because every candidate pays the same linear term for the same tokens.

## How the Planner Computes a Cost

The planner forms encoder runs the way the server does. A shard is **not**
always one run, and that is the part most easily got wrong. For each
resolution of a workload:

1. **Size a video shard.** The shard takes the smallest compiled block size
   that holds one temporal patch group (`select_vision_block_size`) and fills
   the largest bucket of that size. Its group count is the lower of the groups
   the bucket holds and the groups that the blocks of the compiled graph hold.
   The graph pads its block count to a multiple of the vision DP degree.
2. **Split the request into shards.** Full shards, then one for the remainder.
3. **Group the shards that start inside one prefill step.** The scheduler
   sends all of them to the encoder together, so they share a run.
4. **Split each group into runs and pack each run.** `split_items_for_bucket`
   splits a group into runs that each fit one bucket, and `pack_vision_run`
   packs each run. A run **moves to a larger block size** when no bucket of
   the smaller size holds it.
5. **Price each run.** A run costs the **whole** compiled graph it uses,
   whether or not its frames fill it.
6. **Sum the padded tokens** of every run and multiply by the MACs for each
   token of that run's block size.

Steps 3 and 4 interact, and a model that prices one shard at a time is wrong
because of it. A 76-frame 640x360 video is two shards of 32 and 6 groups. Their
embeds are 7,040 and 1,320, so both start inside the first 8,192-token prefill
step and pack together. Together they need 38 blocks of 1,024 tokens, and the
largest 1024 bucket of this comparison's ladder has 32. So the run moves to the
3584 block, and the request costs about 1.56 times what two separate 1024-block
shards would cost. A request of 512x512 frames does not show this: each of its
shards is exactly one prefill step of embeds, so no two shards share a run.

The planner calls the production FFD packer (`ffd_pack_images`), so its block
counts match the server. Four more helpers repeat production logic, because the
planner also runs on a host without torch: `compiled_block_count`, the
block-size selection rule, `pack_run` (`pack_vision_run`), `split_items`
(`split_items_for_bucket`) and `groups_per_shard`
(`max_temporal_patch_groups_per_run`).
`test/unit/test_vision_bucket_planner.py` asserts that each one agrees with its
production counterpart, so the tool cannot drift.

### Why the whole graph costs

A short run on a large graph pays for its empty blocks. This is why a block
**count** ladder matters as much as a block **size** ladder. A 600-frame
512x512 video is 300 groups. With only a 32-block graph, the 9 full shards use
288 groups and the 12-group tail still costs 32 blocks:

```text
only [[1024, 32]]:            10 runs x 32 blocks = 320 blocks  (1.067x)
with [[1024, 16], [1024, 32]]:  9 x 32 + 1 x 16   = 304 blocks  (1.013x)
```

The long-video recipe pairs `[1024, 8]` with `[1024, 32]` for this reason. The
planner builds each count ladder by doubling from the vision DP degree up to
the shard count.

## Using the Planner

### Compare the candidates

`--sweep` prices every ladder that holds the largest frame, from one static
size to every needed size. Each row is a candidate, and each column a
workload:

```bash
python -m vllm_neuron.model.qwen3_vl.tools.vision_bucket_planner --workload U10 V3 --sweep
```

```text
  ladder                             graphs    U10     V3
  8192                                    1  1.00x  1.00x
  1024,8192                               4  2.04x  1.06x
  3584,8192                               3  1.28x  1.20x
  1024,3584,8192                          6  2.04x  1.22x
  256,896,1024,3584,8192                 15  2.04x  1.23x
```

Read it for the cheapest ladder that reaches the gain. Here
`1024,3584,8192` matches the 15-graph ladder to within 0.01x with 6 graphs, so
the extra 256 and 896 sizes earn nothing.

Check a candidate against **every** workload you serve, not one. A ladder that
helps one mix can hurt another, and a gain below 1.00x in this table means the
ladder costs more than one large block for that workload. The 960x540 case
above is an example: a ladder without its 2048 block reads 0.87x. Add the
resolution to the workload list before you read the sweep, or the sweep cannot
price it.

### Let it choose

`--best` applies the selection rule and prints the list to paste into
`encoder_neuron_config`. **Give it `--max-graphs`**: the cost model counts MACs
and not device memory, so without a budget it can choose a ladder the server
cannot load.

```bash
python -m vllm_neuron.model.qwen3_vl.tools.vision_bucket_planner \
    --mix 1920x1080:0.25,1280x720:0.25,640x360:0.25,256x256:0.25 \
    --frames 76 --dp-size 16 --static-block 8192 --max-graphs 4 --best
```

The rule: every candidate holds the largest frame; for each one take the
**lowest** gain across the workloads, because a ladder that helps one workload
and hurts another is not usable; then take the candidate with the fewest
graphs whose lowest gain is within `--tolerance` of the best.

The budget matters. For the two workloads of the block-bucket comparison the
planner picks `[1024, 3584, 8192]` at 4 graphs, and `[896, 1024, 3584, 8192]`
at 7 if nothing caps it. The 896 block keeps 640x360 off the 3584 block and
buys about 3.5% on the mixed workload for 3 more graphs — a trade the cost
model cannot make, because it does not see the memory those graphs need.

`select_ladder` is the same function, for a caller that configures a server
or a test from the planner rather than by hand.

### Price one choice

Without `--sweep`, the planner prints the before and after cost of each
workload. `--breakdown` adds the per-resolution rows, which show where a gain
comes from:

```bash
python -m vllm_neuron.model.qwen3_vl.tools.vision_bucket_planner --workload V3 --breakdown
```

A mix gains least where its largest resolution already needs the largest
block: in a 4-way mix with 1080p, the 1080p requests gain nothing and hold most
of the work, so the share-weighted gain is near 1.2x even though the 256x256
requests gain 3.4x.

### Flags

| Flag | Purpose |
| --- | --- |
| `--workload` | Workloads to report. Default: every workload. |
| `--dp-size` | Vision DP degree. Every block count is a multiple of it. |
| `--static-block` | Block size of the before leg. Default: the largest needed. |
| `--ladder` | Block sizes of the after leg. |
| `--counts-per-size` | Most graphs for each block size. 1 gives only the shard graph. |
| `--sweep` | Compare candidate ladders. |
| `--breakdown` | Per-resolution rows for each workload. |

Set `--dp-size` to the vision DP degree of the target server. It changes the
answer: a larger degree forces larger block counts, so the minimum shard of an
8192 block grows and the static leg wastes more.

## Context Length Is a Separate Limit

The planner reports the embed tokens of the largest request of each mix, and
marks a workload that needs more context than the largest validated long-video
`max_model_len`.

This limit is independent of block packing, and block packing cannot relieve
it. One request holds one resolution, so the **largest** resolution of a mix
sets `max_model_len`, and the mean does not. A 10-minute 1 fps 1080p request is
300 groups of 2,040 embeds, which is 612,000 embeds. A workload above the limit
needs a lower frame rate, or a `max_pixels` cap that shrinks its largest
frames.

Check this column before you plan a ladder. A workload that no server can
admit does not need a block size.

## Key Files

| File | Purpose |
| --- | --- |
| `vllm_neuron/model/qwen3_vl/tools/vision_bucket_planner.py` | The planner: cost model, shard sizing, ladder sweep |
| `test/unit/test_vision_bucket_planner.py` | Planner tests, and the equivalence with production |
| `test/vllm_neuron/model/qwen3_vl/bf16/e2e/configs.py` | Asks `select_ladder` for the graphs of the perf legs |
| `vllm_neuron/utils/ffd_packing.py` | The FFD packer that the planner calls |
| `vllm_neuron/model/qwen3_vl/utils/vision_block_packing.py` | Block-size and bucket selection on the server |
| `vllm_neuron/model/neuron_config.py` | `encoder_graph_shapes` validation and `compiled_block_count` |
