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.
Give each patch context
Each 16 × 16 RGB patch starts with 768 color values. A learned projection turns those into a 768-value vector and adds position information. Over 16 layers, patches attend to one another. The values change, but the grid keeps the same number of positions.
Nine patches become one token
For a 512 × 512 image at v70, select a group of nine encoded patches to see its pooled token.
Region 28 → token 28
At v70, a 512 × 512 image is resized to 384 × 384 pixels. Each group of nine encoded patches becomes one token. The token keeps 768 values until projection.
How the Core ML export handles padding
The pooler groups patches by their coordinates and averages real patches in FP32 before scaling by √768. The export fixes the number of output slots per tier so F.one_hot can be traced. The projection zeroes unused rows, and the pipeline takes only the valid tokens.
Match the backbone’s width
Each vision token is projected to 512 values for the shared backbone.
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
| Tier | Input | Latency | Physical footprint | Cosine similarity |
|---|---|---|---|---|
| v70 | 512 × 512 | 12.0 ms | 404 MB | 0.999987 |
| v140 | 512 × 512 | 24.4 ms | 448 MB | 0.999989 |
| v140 | 200 × 600 | 23.9 ms | 448 MB | 0.999993 |
| v280 | 512 × 512 | 53.5 ms | 536 MB | 0.999991 |
| v280 | 800 × 300 | 53.5 ms | 536 MB | 0.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