From image to vision tokens

Follow an image from pixels to visual tokens. The same process works for a single video frame.

Start with the image

The pipeline first resizes the image to fit the chosen token budget, then cuts it into 16 × 16 pixel patches. For a 512 × 512 image at v280, the resized image is 768 × 768 pixels, making a grid of 48 patches across and 48 down.

Look closer

How image size affects token count

The token budget sets the maximum number of output tokens. The image processor keeps the image’s proportions while resizing it to fit that budget. Each side is a multiple of 48 pixels, so 16 × 16 patches fit into 3 × 3 pooling groups.

Each square represents one pooled token. Try a tier or image shape to see how the grid changes.

The encoder accepts one image or video frame per call. The pipeline collects valid tokens before calling the backbone.

Inside the encoder

Each 16 × 16 RGB patch contains 768 color values. A learned projection produces a 768-value vector, and the encoder adds its row and column position. A mask keeps padding out of attention.

Sixteen layers let each valid patch attend to the others. The pooler then groups neighboring patches in non-overlapping 3 × 3 squares, averages their vectors in FP32, and scales the result by √768.

RMS normalization and a linear projection change each pooled token from 768 to 512 values for the backbone.

What the Core ML export revealed

The earlier export with an internal image loop measured about 550 ms per image and peaked above 13 GiB RSS. The standalone v280 function measured 53.5 ms per image.

MLComputePlan placed all 1,877 inspected v280 operations on the GPU. Keeping Vision (v280) and Backbone (s512) resident together stayed at a 647 MB physical footprint in the single-image end-to-end worker.

Benchmarks per tier
TierInputLatencyPhysical footprintCosine similarity
v70512 × 51212.0 ms404 MB0.999987
v140512 × 51224.4 ms448 MB0.999989
v140200 × 60023.9 ms448 MB0.999993
v280512 × 51253.5 ms536 MB0.999991
v280800 × 30053.5 ms536 MB0.999993

Compiled Core ML, warm measurements, all compute units. Physical footprint is the isolated worker’s peak dirty and compressed memory (excluding clean mapped weight files; includes ~290 MB of Python runtime). Cosine similarity compares Core ML and PyTorch outputs for the same input; closer to 1 means their directions match more closely. The Vision → Backbone comparisons reached 0.999984 cosine for one image and 0.999999 for both multiple images and four video frames.

Terms used above
Token budget
The maximum number of visual tokens the selected tier can return for one image.
Patch
A 16 × 16-pixel square cut from the resized image.
Vector and dimension
A vector is a list of numbers. Its dimension is how many values it contains.
Attention
A way for a patch to use information from other patches as it passes through the encoder.
Pooling
Combining each 3 × 3 group of encoded patches into one visual token.
Visual token
A vector representing part of the image in the sequence sent to the backbone.
Projection
A learned operation that changes a vector into another representation, such as from 768 values to 512.
Padding
Unused slots added to fill a fixed-size tier. The pipeline passes only valid tokens to the backbone.
Build and verify
# 1. Verify PyTorch wrapper parity
uv run python -m vision.verify_pytorch

# 2. Export multifunction FP16 Core ML package (329 MB)
uv run python -m vision.export

# 3. Compile to .mlmodelc
xcrun coremlcompiler compile models/vision_fp16.mlpackage models/

# 4. Verify Core ML parity, latency, peak memory, and Vision → Backbone handoff
uv run python -m vision.verify_coreml --model models/vision_fp16.mlmodelc --backbone models/backbone_fp16.mlmodelc --compare-cu