This document is relevant for: Trn2, Trn3
Block Packing Vision Attention#
Overview#
Block Packing Vision Attention eliminates wasted computation in multi-image attention by packing variable-sized image token sequences into fixed-size blocks using First-Fit-Decreasing (FFD) bin packing. Each block is processed as an independent batch element, removing all cross-block attention and delivering 2x-2.5x efficiency gains over standard approaches.
Problem Statement#
Vision understanding models apply all-to-all attention independently within each image — every visual token in one image attends to every other token in that same image. When a request contains multiple images at varying resolutions, the token sequences differ in length and must be organized for batched computation. The two standard approaches both incur significant waste:
Standard approach 1: Batch-dimension packing#
Each image occupies a separate batch element, padded to the length of the largest image:
Image A: 1024 tokens ████████████████
Image B: 64 tokens ██░░░░░░░░░░░░░░ (93% padding)
Image C: 384 tokens ██████░░░░░░░░░░ (62% padding)
↑ padded to 1024
Short sequences carry massive padding overhead and require two-dimensional bucketing (number of images × max image size).
Standard approach 2: Sequence-dimension concatenation#
All image tokens are concatenated into a single sequence with minimal trailing padding:
[Image A: 1024 tokens][Image B: 64 tokens][Image C: 384 tokens][pad: 64]
← total: 1536 tokens (next bucket) →
This minimizes padding but computes a full seq_len × seq_len attention matrix. A non-causal mask prevents cross-image attention, but the kernel still executes the masked-out regions. For the example of two images (1024 + 2048 tokens):
Useful compute:
1024² + 2048²= 5.2MTotal compute:
3072²= 9.4MWasted: 44%
The waste grows quadratically with more images. For 24 small images of 128 tokens each:
Useful compute:
24 × 128²= 393KTotal compute:
3072²= 9.4MWasted: 95.8%
Solution: Block Packing with FFD#
Block packing restructures the computation by grouping images into fixed-size blocks rather than one flat sequence or one-per-batch-element:
Pack image token sequences into blocks of a compile-time-fixed size using First-Fit-Decreasing (FFD).
Process each block as an independent batch element — the attention kernel computes only the
block_size × block_sizematrix per block with no cross-block computation.
First-Fit-Decreasing (FFD) Algorithm#
FFD is a bin-packing heuristic that operates in two steps:
Sort images by token count in descending order.
Place each image into the first block that has sufficient remaining capacity.
FFD guarantees at most ⌈11/9 · OPT⌉ + 1 blocks, where OPT is the theoretical minimum — worst-case within 22% of optimal. In practice, production image-size distributions (heterogeneous resolutions with natural size gaps) achieve optimal or OPT+1 packing.
Worked Example#
Consider 5 images: one 1024-token, one 512-token, one 256-token, and two 128-token images, with block_size = 1024:
Block 1: [Image 1K] → 1024/1024 used
Block 2: [Image 512][Image 256][2×128] → 1024/1024 used
Compute comparison:
Approach |
Total compute |
Useful compute |
Efficiency |
|---|---|---|---|
Batch packing (pad to 1024) |
5 × 1024² |
1024² + 512² + 256² + 2×128² |
~28% |
Sequence packing (flat) |
2048² |
same |
~35% |
Block packing (1024) |
2 × 1024² |
same |
~70% |
Block packing achieves 2.5x better efficiency than batch packing and 2x better than sequence packing in this scenario.
Relationship to Standard Approaches#
Block packing generalizes both standard approaches:
When
block_size= total sequence length: a single block spans the entire attention matrix, reducing to standard sequence-dimension concatenation.When all images have equal size: setting
block_sizeto that size (rounded to the next multiple of 128) gives each image its own block, reducing to batch-dimension parallelism.When images vary in size (the common case): block packing is strictly more efficient than both alternatives.
How It Works#
Step 1: Preprocessing (Framework Level)#
The framework performs FFD packing before kernel dispatch:
Sort images by token count (descending).
Assign each to the first block with remaining capacity ≥ image token count.
Shuffle vision token position IDs to reflect the image-to-block mapping — the result is a
[num_blocks × block_size]tensor with padded positions at each block’s tail.Compute
bound_minandbound_maxtensors per block, encoding the local start/end indices of each image within that block.
Step 2: Execution (Kernel)#
The packed tensor is dispatched to the attention_cte prefill kernel:
num_blocksmaps to the kernel’s batch dimension.block_sizemaps to the kernel’s sequence length (seqlen_q).The kernel processes blocks sequentially, using
bound_min/bound_maxto restrict attention to valid image-local regions within each block.
Bucketing occurs on two dimensions:
Block size — determined at compile time based on maximum image resolution. With several block sizes, each run selects one (see Several Block Sizes).
Number of blocks — the bucket in whole blocks, padded to a multiple of the vision DP degree:
ceil(ceil(bucket / block_size) / dp) * dp(compiled_block_count).
Choosing the Right Block Size#
Block size is the key tuning parameter. The right value depends on your workload characteristics:
Guidelines#
In general, we want to balance intra-block padding with the wasted compute of fully padded blocks. Efficient packing will produce fewer blocks with less intra-padding within each block, which reduce both.
Workload |
Recommended block size |
Rationale |
|---|---|---|
Single large image (e.g., 4K×4K) |
Match the image token count (rounded to 128) |
Single block, no packing overhead |
Many small images (e.g., thumbnails) |
1024 |
Packs many images per block, minimizes block count — fewer blocks means less wasted compute from fully padded trailing blocks and better NeuronCore utilization along the sequence dimension |
Mixed resolutions |
1024–2048 |
Balances packing density against per-block padding |
Video workload |
Multiple of |
Aligns block boundaries to frame boundaries, avoiding mid-frame splits (e.g., 16 tokens/frame × 64 frames = 1024) |
Max resolution known at deploy time |
|
Guarantees any single image fits in one block |
Key Considerations#
Block size must be ≥ the largest single image’s token count. If an image exceeds the block size, it cannot be packed and the kernel will fail. Set block size to at least
max_num_tokens_per_imagefor your model’s configured resolution. With several block sizes, this applies to the largest size (see Several Block Sizes).Larger blocks = fewer blocks but more intra-block padding. A 4096-token block can absorb any combination of smaller images, but each block carries more unused slots when images don’t fill it completely.
Smaller blocks = more blocks but better packing density. A 1024-token block forces large images into dedicated blocks while letting small images share blocks efficiently. However, very small blocks increase the batch dimension and may underutilize compute for workloads dominated by large images.
Alignment constraint: block size must be a multiple of 128 to satisfy NKI kernel tile alignment requirements.
Several Block Sizes#
One block size must hold the largest image, so a deployment that accepts
1080p images (8,160 tokens) needs blocks of 8192. With one size, every run then
uses 8192-token blocks, also a run of 512x512 images (1,024 tokens each). The
attention kernel masks the packed sequences, but it computes every tile of a
block. So attention costs block_size^2 per block for any number of images.
encoder_graph_shapes lists the graphs to compile, as [block_size, num_blocks] pairs, for example [[1024, 32], [8192, 8]]. Each encoder run
(one embed_multimodal call) uses the smallest size that holds its largest
image or video frame (pack_vision_run). All
images in one run share that size, so a run that mixes a 1080p image with small
images uses 8192 for all of them. FFD keeps each image whole, so an image
larger than half a block needs a block of its own. If no graph of the smallest
size holds the packing, the run uses the next larger size. A run
uses the smallest graph of its size that holds the packing. One larger
block can hold several of those images. At the largest size, the packing is
the same as with only that size. NeuronPlatform.validate_request rejects a
request that no size can hold.
Each pair is one graph, so the list shows the graph count and the device memory
before startup. num_blocks counts the blocks of all vision DP ranks, and it
must be a multiple of dp_size. Token buckets (num_encoder_input_buckets)
are deprecated: set encoder_graph_shapes. The platform converts them to graphs
(EncoderNeuronConfig.get_encoder_graph_shapes): a bucket rounds up to whole
blocks, and the count pads to a multiple of dp_size. So at vision DP 8, bucket
30720 and bucket 32768 both give 32 blocks of 1024, but bucket 30720 fills only
30 of them. Compile time and the number of loaded graphs increase with the
list. Token buckets have one block size; only the graph list gives several.
When max_pixels needs a block larger than the configured block, the platform
converts the token buckets to encoder_graph_shapes: the graphs of each bucket
at the configured block, and at the larger block (the next power of 2). The
smaller images keep the smaller block. The request limits then follow the
graph capacity (block_size x num_blocks), which can be larger than the token
bucket: at vision DP 8, bucket 8192 gives the graph [8192, 8], which holds
65,536 raw tokens. For EPD, set encoder_graph_shapes on the VE and PD pools,
because each pool converts with its own DP degree. With encoder_graph_shapes,
the platform rejects a max_pixels that needs a larger block than the list
has.
To compile only one size, give encoder_graph_shapes one block size that
holds the largest image.
To choose the sizes for a workload that mixes resolutions, use the vision block bucket planner. It prices each candidate list, because the choice is not obvious in either direction: each extra graph costs compile time and device memory, and a list that omits one resolution’s block can cost more than one large block.
Decision Flowchart#
What is your max single-image token count?
│
├─ ≤ 1024 → block_size = 1024
│ (good packing density)
│
├─ ≤ 2048 → block_size = 2048
│ (accommodates large images, still packable)
│
└─ > 2048 → block_size = ceil(max_tokens / 128) × 128
(ensures single-image fit, packing for smaller images)
Example: Estimating Efficiency#
For a request with N images producing token counts [t₁, t₂, ..., tₙ] and a chosen block_size B:
Useful compute = Σ tᵢ²
Block compute = num_blocks × B²
Efficiency = Useful compute / Block compute
Where num_blocks is determined by FFD packing. Higher efficiency means less wasted compute.
Performance Impact#
The efficiency gain depends on workload heterogeneity:
Homogeneous images (all same resolution): Block packing matches batch-dimension parallelism — no gain, no regression.
Heterogeneous images (mixed resolutions): 2x–2.5x efficiency improvement over the best standard approach.
Many small images: Up to 10x+ improvement over flat sequence packing (e.g., 24 thumbnails: ~4% → ~50%+ efficiency).
Key Files#
File |
Purpose |
|---|---|
|
FFD packing, block scatter, bounds computation |
|
CPU preprocessing (RoPE, attention bounds from |
|
Vision encoder (block-level forward with |
|
Model integration (packing invocation, position shuffling) |
|
NKI attention kernel ( |
This document is relevant for: Trn2, Trn3