Skip to content

feat(models): port Granite Vision (SigLIP multi-tap + Granite text) - #647

Merged
inureyes merged 1 commit into
mainfrom
feat/issue-539-granite-vision
Jul 3, 2026
Merged

inureyes merged 1 commit into
mainfrom
feat/issue-539-granite-vision

Conversation

@inureyes

@inureyes inureyes commented Jul 3, 2026

Copy link
Copy Markdown
Member

Summary

Adds Granite Vision support: IBM's document VLM pairing a SigLIP tower (four intermediate feature taps) with a dense Granite text backbone, via LLaVA-Next AnyRes multi-tile preprocessing. Closes #539.

Both config.json spellings route to the same model:

  • model_type: "granite_vision" (MLX conversions), and
  • model_type: "llava_next" with a granite text config (the original ibm-granite/granite-vision-3.2-2b).

Architecture

  • AnyRes processor (src/vision/processors/anyres.rs, new and reusable by the planned Granite 4 Vision): select_best_resolution, aspect-preserving resize + symmetric zero-pad into (best_h, best_w), row-major 384-square tile split, and a prepended base tile. It also owns the token-space unpad math (round(v, 7) then truncate), shared by feature packing and prompt-token counting so they cannot drift.
  • Multi-tap SigLIP: SigLipVisionModel::forward_collect_layers returns the requested entries of the 28-entry hidden-state list (taps [-24, -20, -12, -1] → L[4], L[8], L[16], L[27]), concatenated on the channel axis (4×1152 = 4608).
  • Projector: the existing MLPProjector (linear → exact GELU → linear) into the text hidden size.
  • Feature packing (src/vision/granite_vision.rs): grid tiles are re-tiled into a (D, gh, gw) feature grid, unpadded in token space, given a trailing image_newline column, flattened, and prepended with the base tile's 729 rows, then scattered into the <image> (49155) positions via merge_llava. The Granite embedding_multiplier is applied by the backbone to the merged stream (image features are not pre-scaled).
  • Text backbone: GraniteModel gains input_embeddings / forward_embeds entry points; forward now delegates to them, so the token path and embeds path are identical by construction.
  • Wiring: detection (both spellings) -> loader -> LoadedModel::GraniteVisionVLM -> VlmRuntimeRef::GraniteVision -> server/CLI, plus model_metadata and TP dispatch rows.

Validation

Validated on mlx-community/granite-vision-3.2-2b-4bit through the actual CLI runtime:

  • Table VQA: given a 3-column table, "What is the price of Bread?" returns 2.00 (the correct row/column cell, not another row's value).
  • Multi-region reading: a four-corner image returns all four labels (ALPHA 111, BRAVO 222, CHARLIE 333, DELTA 444).
  • Text-only forward produces finite logits.

Automated tests (34 total): AnyRes select_best_resolution and the token-count/unpad invariant, tiling order and base-tile-first, prompt expansion, detection for both spellings, plus tests/granite_vision_parity.rs config-detection and checkpoint-gated forward tests.

cargo test, cargo fmt --all -- --check, and cargo clippy --all-targets --features metal,accelerate are clean for the new code.

Notes for #541 (Granite 4 Vision)

The AnyRes module and the multi-tap SigLIP addition are shared; #541 rebases and reuses them (parameterized by grid side / downsample ratio). Exclusive to #541: the window-QFormer connector, per-layer feature injection, and the hybrid backbone state plumbing.

…nyRes)

Add Granite Vision (`granite_vision`, and the original `llava_next` checkpoint
with a `granite` text config) support. IBM's document VLM: a SigLIP tower with
four intermediate feature taps feeds a 2-layer GELU projector, a learned
`image_newline` embedding, LLaVA-Next AnyRes multi-tile preprocessing, and a
dense Granite text backbone.

Detection routes both `config.json` spellings to `ModelType::GraniteVisionVLM`
(the `llava_next` arm inspects `text_config.model_type == "granite"`).

New shared AnyRes machinery (`src/vision/processors/anyres.rs`, reusable by the
planned Granite 4 Vision): `select_best_resolution`, aspect-preserving resize +
symmetric zero-pad + row-major tile split + base tile, and the token-space
unpad math (round-to-7-then-truncate). The unpad helper is shared between
feature packing and prompt-token counting so they cannot drift.

The tower gains a multi-tap `forward_collect_layers` on `SigLipVisionModel`;
`GraniteModel` gains `input_embeddings` / `forward_embeds` entry points (the
`embedding_multiplier` is applied once to the merged stream). The wrapper
(`src/vision/granite_vision.rs`) runs the four taps, concatenates on the
channel axis, projects, re-tiles the grid into a `(H, W)` feature grid, unpads
in token space, appends the `image_newline` column, prepends the base tile, and
scatters into the `<image>` (49155) positions via `merge_llava`. Prompt
expansion sizes each `<image>` run to the packed feature count.

Wired end to end: detection -> loader -> `LoadedModel::GraniteVisionVLM` ->
`VlmRuntimeRef::GraniteVision` -> server/CLI, plus metadata and TP dispatch rows.

Validated on `mlx-community/granite-vision-3.2-2b-4bit` through the CLI: a table
image is read correctly (answers the price of a specific row) and a four-corner
image is read in full. Co-located unit tests cover `select_best_resolution`,
the token-count/unpad invariant, tiling order, prompt expansion, and detection
for both spellings; `tests/granite_vision_parity.rs` adds config-detection and
checkpoint-gated forward tests.

Closes #539
@inureyes inureyes added type:enhancement New features, capabilities, or significant additions area:models Model architectures, weights, loading, metadata labels Jul 3, 2026
@inureyes
inureyes merged commit f81eec6 into main Jul 3, 2026
4 checks passed
@inureyes
inureyes deleted the feat/issue-539-granite-vision branch July 3, 2026 03:17
@inureyes inureyes self-assigned this Aug 31, 2026
@inureyes inureyes added status:done Completed priority:low Low priority labels Sep 6, 2026
be-free-snowboy pushed a commit to freestylejs/snowbrain-mlxcel that referenced this pull request Oct 27, 2026
…nyRes) (lablup#647)

Add Granite Vision (`granite_vision`, and the original `llava_next` checkpoint
with a `granite` text config) support. IBM's document VLM: a SigLIP tower with
four intermediate feature taps feeds a 2-layer GELU projector, a learned
`image_newline` embedding, LLaVA-Next AnyRes multi-tile preprocessing, and a
dense Granite text backbone.

Detection routes both `config.json` spellings to `ModelType::GraniteVisionVLM`
(the `llava_next` arm inspects `text_config.model_type == "granite"`).

New shared AnyRes machinery (`src/vision/processors/anyres.rs`, reusable by the
planned Granite 4 Vision): `select_best_resolution`, aspect-preserving resize +
symmetric zero-pad + row-major tile split + base tile, and the token-space
unpad math (round-to-7-then-truncate). The unpad helper is shared between
feature packing and prompt-token counting so they cannot drift.

The tower gains a multi-tap `forward_collect_layers` on `SigLipVisionModel`;
`GraniteModel` gains `input_embeddings` / `forward_embeds` entry points (the
`embedding_multiplier` is applied once to the merged stream). The wrapper
(`src/vision/granite_vision.rs`) runs the four taps, concatenates on the
channel axis, projects, re-tiles the grid into a `(H, W)` feature grid, unpads
in token space, appends the `image_newline` column, prepends the base tile, and
scatters into the `<image>` (49155) positions via `merge_llava`. Prompt
expansion sizes each `<image>` run to the packed feature count.

Wired end to end: detection -> loader -> `LoadedModel::GraniteVisionVLM` ->
`VlmRuntimeRef::GraniteVision` -> server/CLI, plus metadata and TP dispatch rows.

Validated on `mlx-community/granite-vision-3.2-2b-4bit` through the CLI: a table
image is read correctly (answers the price of a specific row) and a four-corner
image is read in full. Co-located unit tests cover `select_best_resolution`,
the token-count/unpad invariant, tiling order, prompt expansion, and detection
for both spellings; `tests/granite_vision_parity.rs` adds config-detection and
checkpoint-gated forward tests.

Closes lablup#539
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:models Model architectures, weights, loading, metadata priority:low Low priority status:done Completed type:enhancement New features, capabilities, or significant additions

Projects

None yet

Development

Successfully merging this pull request may close these issues.

feat(models): port Granite Vision (ViT + Granite text)

1 participant