Repository navigation
feat(models): port Granite Vision (SigLIP multi-tap + Granite text) - #647
Merged
Merged
Conversation
…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
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.jsonspellings route to the same model:model_type: "granite_vision"(MLX conversions), andmodel_type: "llava_next"with agranitetext config (the originalibm-granite/granite-vision-3.2-2b).Architecture
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.SigLipVisionModel::forward_collect_layersreturns 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).MLPProjector(linear → exact GELU → linear) into the text hidden size.src/vision/granite_vision.rs): grid tiles are re-tiled into a(D, gh, gw)feature grid, unpadded in token space, given a trailingimage_newlinecolumn, flattened, and prepended with the base tile's 729 rows, then scattered into the<image>(49155) positions viamerge_llava. The Graniteembedding_multiplieris applied by the backbone to the merged stream (image features are not pre-scaled).GraniteModelgainsinput_embeddings/forward_embedsentry points;forwardnow delegates to them, so the token path and embeds path are identical by construction.LoadedModel::GraniteVisionVLM->VlmRuntimeRef::GraniteVision-> server/CLI, plusmodel_metadataand TP dispatch rows.Validation
Validated on
mlx-community/granite-vision-3.2-2b-4bitthrough the actual CLI runtime:2.00(the correct row/column cell, not another row's value).ALPHA 111,BRAVO 222,CHARLIE 333,DELTA 444).Automated tests (34 total): AnyRes
select_best_resolutionand the token-count/unpad invariant, tiling order and base-tile-first, prompt expansion, detection for both spellings, plustests/granite_vision_parity.rsconfig-detection and checkpoint-gated forward tests.cargo test,cargo fmt --all -- --check, andcargo clippy --all-targets --features metal,accelerateare 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.