Skip to content

Commit ed370d8

Browse files
justinchubyCopilot
andcommitted
Fix Gemma 3 processor token contract
Target processor-native image soft tokens without indexing beyond the text embedding table. Add Qwen vision, prefill, and cached decode execution evidence, correct parity references, and close the multimodal artifact documentation census. Co-authored-by: Copilot App <[email protected]> Signed-off-by: Justin Chu <[email protected]>
1 parent b0d26e0 commit ed370d8

7 files changed

Lines changed: 111 additions & 12 deletions

File tree

‎docs/api/build_from_gguf.md‎

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1155,10 +1155,11 @@ Projector support is pinned to llama.cpp
11551155
and `UNKNOWN`. `clip` has vision, audio, and generated-audio presence flags;
11561156
there is no text-encoder presence key at this pin.
11571157

1158-
Only `gemma4v` and `muse-glimmer` currently pass metadata, suffix-exact tensor
1159-
closure, target-pairing, graph, and component-parity gates. This admits graph
1160-
import, not a runtime package: both runtime verdicts remain deferred pending the
1161-
paired text target, processor, and deterministic end-to-end generation.
1158+
`gemma3`, `gemma4v`, `muse-glimmer`, `qwen2vl_merger`, and
1159+
`qwen2.5vl_merger` currently pass metadata, suffix-exact tensor closure,
1160+
target-pairing, graph, and component-parity gates. This admits graph import,
1161+
not runtime support: all five runtime verdicts remain deferred pending proven
1162+
processor asset publication and deterministic downstream orchestration.
11621163
`gemma4a` remains
11631164
deferred: the real sidecar contains `a.pre_encode.*` tensors that the partial
11641165
mapping does not consume. A sidecar may contain that audio tower alongside a
@@ -1239,8 +1240,28 @@ Real-artifact audit pins:
12391240

12401241
| Family | Revision and file | Size | LFS SHA-256 | Metadata/tensor qtypes | Paired text target |
12411242
|---|---|---:|---|---|---|
1243+
| Gemma 3 | `ggml-org/gemma-3-4b-it-GGUF@ab31416aceb30cd095cb34cc27eea120940964e4`<br>`mmproj-model-f16.gguf` | 851,251,104 | `8c0fb064b019a6972856aaae2c7e4792858af3ca4561be2dbf649123ba6c40cb` | `gemma3`; 276 F32 + 163 F16 tensors; vision 1152→2560 | `gemma-3-4b-it-Q4_K_M.gguf` (`gemma3`) |
12421244
| Gemma4 | `unsloth/gemma-4-E2B-it-GGUF@0314792d7f1f7e229411f620751375812bb9faf2`<br>`mmproj-F16.gguf` | 985,654,080 | `337ee849e80b6169ce9d1d573d424fc1653bcafa5f0cb0cbb901beba54f4b41c` | `gemma4v` + deferred `gemma4a`; 1,163 F32 + 248 F16 tensors; vision 768→1536, audio 1024→1536 | `gemma-4-E2B-it-Q4_K_M.gguf` (`gemma4`) |
12431245
| Muse Glimmer | `unsloth/Muse-Glimmer-30B-GGUF@faa5b025c584459c13febfa5c59883516710ae39`<br>`mmproj-Muse-Glimmer-30B-BF16.gguf` | 3,849,173,728 | `7aa788cfe25ae5e4bf4837511f64df22cabe595e58223708274a67b3136f53ab` | `muse-glimmer`; 506 F32 + 303 BF16 tensors; vision 1536, merge 2, projection 6656 | `Muse-Glimmer-30B-UD-Q4_K_XL.gguf` (`muse-glimmer`) |
1246+
| Qwen2-VL | `ggml-org/Qwen2-VL-2B-Instruct-GGUF@bb307c036e8a1ed7b663bbd0c35b41c4c9294cfd`<br>`mmproj-Qwen2-VL-2B-Instruct-f16.gguf` | 1,331,656,160 | `ecb20cabcdd8dbc277de06bd6eb980aeb2adfaaba9f199a434e328d205675d03` | `qwen2vl_merger`; 324 F32 + 196 F16 tensors; vision 1280→1536 | `Qwen2-VL-2B-Instruct-Q4_K_M.gguf` (`qwen2vl`) |
1247+
| Qwen2.5-VL | `ggml-org/Qwen2.5-VL-3B-Instruct-GGUF@5037fcf163dd95d1e41d1974465f0898ed108ca2`<br>`mmproj-Qwen2.5-VL-3B-Instruct-f16.gguf` | 1,338,428,128 | `b9160fe9d814d1fadf68395677468534778b39ac33c2e7561b7b218626e60d5e` | `qwen2.5vl_merger`; 291 F32 + 228 F16 tensors; vision 1280→2048 | `Qwen2.5-VL-3B-Instruct-Q4_K_M.gguf` (`qwen2vl`) |
1248+
1249+
The Gemma 3 processor assets are pinned to
1250+
`google/gemma-3-4b-it@093f9f388b31de276ce2de164bdc2081324b9767`.
1251+
`Gemma3Processor` emits `pixel_values` as
1252+
`float32[num_images,3,896,896]` and 256 `<image_soft_token>` placeholders
1253+
per image. The vision graph processes one image row per invocation; callers
1254+
concatenate its 256-row outputs in processor order. The gated parent assets
1255+
and ORT GenAI orchestration remain runtime waivers.
1256+
1257+
The Qwen processor assets are pinned to
1258+
`Qwen/Qwen2-VL-2B-Instruct@895c3a49bc3fa70a340399125c650a463535e71c`
1259+
and
1260+
`Qwen/Qwen2.5-VL-3B-Instruct@66285546d2b821cf421d4f5eb2576359d3770cd3`.
1261+
Their independent float32 image/video patch streams bind to separate vision
1262+
invocations, then to `image_features` and `video_features` on the embedding
1263+
graph. Qwen2.5 timing metadata and downstream M-RoPE construction are not
1264+
consumed by these graphs, so runtime support remains deferred.
12441265

12451266
The Gemma4 processor contract uses tokenizer token `<|image|>` (ID 258880 in
12461267
the audited paired GGUF) and 3×3 spatial pooling. The sidecar's image mean/std

‎src/mobius/integrations/gguf/_docs_test.py‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,15 @@ def test_document_is_exact_generator_output() -> None:
2525
assert check_document()
2626

2727

28+
def test_manual_real_artifact_table_covers_every_registry_pin() -> None:
29+
document = Path("docs/api/build_from_gguf.md").read_text(encoding="utf-8")
30+
for pin in MMPROJ_ARTIFACT_PINS:
31+
assert f"`{pin.repository}@{pin.revision}`" in document
32+
assert f"`{pin.filename}`" in document
33+
assert f"`{pin.lfs_sha256}`" in document
34+
assert f"{pin.size:,}" in document
35+
36+
2837
def test_stale_at_style_pin_is_rejected(tmp_path: Path) -> None:
2938
document = tmp_path / "build_from_gguf.md"
3039
document.write_text(

‎src/mobius/integrations/gguf/_mmproj.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ def build_gemma3_vlm_from_gguf(
16161616
if image_token_id is not None
16171617
else config.image_token_id
16181618
if config.image_token_id is not None
1619-
else _token_id(text_gguf, "<start_of_image>")
1619+
else _token_id(text_gguf, "<image_soft_token>")
16201620
)
16211621
config = dataclasses.replace(
16221622
config,

‎src/mobius/integrations/gguf/_mmproj_registry.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,7 +1032,7 @@ def _rejected(
10321032
),
10331033
tensor_qtypes=(("F32", 324), ("F16", 196)),
10341034
tensor_count=520,
1035-
parity_test="TestQwenVLMMProj.test_qwen2_tensor_transform_values",
1035+
parity_test=("TestQwenVLMMProj.test_qwen_tensor_transform_values[qwen2vl_merger]"),
10361036
processor_repository="Qwen/Qwen2-VL-2B-Instruct",
10371037
processor_revision="895c3a49bc3fa70a340399125c650a463535e71c",
10381038
processor_files=(
@@ -1071,7 +1071,7 @@ def _rejected(
10711071
),
10721072
tensor_qtypes=(("F32", 291), ("F16", 228)),
10731073
tensor_count=519,
1074-
parity_test="TestQwenVLMMProj.test_qwen25_tensor_transform_values",
1074+
parity_test=("TestQwenVLMMProj.test_qwen_tensor_transform_values[qwen2.5vl_merger]"),
10751075
processor_repository="Qwen/Qwen2.5-VL-3B-Instruct",
10761076
processor_revision="66285546d2b821cf421d4f5eb2576359d3770cd3",
10771077
processor_files=(

‎src/mobius/integrations/gguf/_mmproj_test.py‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,6 +1120,20 @@ def test_builds_canonical_package_and_runs_mixed_media(
11201120
assert package["decoder"].graph.inputs[0].name == "inputs_embeds"
11211121
assert package["decoder"].graph.inputs[2].shape[0] == 3
11221122

1123+
vision_session = ort.InferenceSession(
1124+
ir.serde.serialize_model(package["vision_encoder"]).SerializeToString(),
1125+
providers=["CPUExecutionProvider"],
1126+
)
1127+
vision_result = vision_session.run(
1128+
None,
1129+
{
1130+
"pixel_values": np.arange(96, dtype=np.float32).reshape(4, 24) / 100,
1131+
"image_grid_thw": np.array([[1, 2, 2]], dtype=np.int64),
1132+
},
1133+
)[0]
1134+
assert vision_result.shape == (1, 16)
1135+
assert np.isfinite(vision_result).all()
1136+
11231137
embedding_path = tmp_path / "embedding.onnx"
11241138
ir.save(package["embedding"], str(embedding_path))
11251139
session = ort.InferenceSession(
@@ -1153,6 +1167,36 @@ def test_builds_canonical_package_and_runs_mixed_media(
11531167
assert text_only.shape == (2, 1, 16)
11541168
assert np.isfinite(text_only).all()
11551169

1170+
decoder_session = ort.InferenceSession(
1171+
ir.serde.serialize_model(package["decoder"]).SerializeToString(),
1172+
providers=["CPUExecutionProvider"],
1173+
)
1174+
prefill_feeds = {
1175+
"inputs_embeds": result[:1, :2],
1176+
"attention_mask": np.ones((1, 2), dtype=np.int64),
1177+
"position_ids": np.zeros((3, 1, 2), dtype=np.int64),
1178+
}
1179+
for layer in range(2):
1180+
for cache_type in ("key", "value"):
1181+
prefill_feeds[f"past_key_values.{layer}.{cache_type}"] = np.empty(
1182+
(1, 2, 0, 8),
1183+
dtype=np.float32,
1184+
)
1185+
prefill = decoder_session.run(None, prefill_feeds)
1186+
assert prefill[0].shape == (1, 2, 32)
1187+
1188+
decode_feeds = {
1189+
"inputs_embeds": text_only[:1],
1190+
"attention_mask": np.ones((1, 3), dtype=np.int64),
1191+
"position_ids": np.full((3, 1, 1), 2, dtype=np.int64),
1192+
}
1193+
for layer in range(2):
1194+
decode_feeds[f"past_key_values.{layer}.key"] = prefill[1 + 2 * layer]
1195+
decode_feeds[f"past_key_values.{layer}.value"] = prefill[2 + 2 * layer]
1196+
decode = decoder_session.run(None, decode_feeds)
1197+
assert decode[0].shape == (1, 1, 32)
1198+
assert all(cache.shape[2] == 3 for cache in decode[1:])
1199+
11561200

11571201
class TestReadVisionConfig:
11581202
def test_extracts_expected_fields(self, clip_mmproj_gguf: Path):

‎src/mobius/models/gemma3.py‎

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,14 +134,21 @@ def __init__(self, config: ArchitectureConfig):
134134
self.image_token_id = config.image_token_id or 0
135135

136136
def forward(self, op: OpBuilder, input_ids: ir.Value, image_features: ir.Value):
137-
text_embeds = self.embed_tokens(op, input_ids)
138-
139137
image_mask = op.Equal(
140138
input_ids,
141139
op.Constant(value_int=self.image_token_id),
142140
)
143141
image_mask_3d = op.Unsqueeze(image_mask, [-1])
144142

143+
# Gemma 3's <image_soft_token> is one past the text embedding table.
144+
# Substitute a valid row before Gather; image positions are replaced below.
145+
safe_input_ids = op.Where(
146+
image_mask,
147+
op.Constant(value_int=self.config.pad_token_id or 0),
148+
input_ids,
149+
)
150+
text_embeds = self.embed_tokens(op, safe_input_ids)
151+
145152
# Number image placeholders over the flattened batch so row 2 starts
146153
# after row 1's features rather than reusing feature row zero.
147154
mask_int = op.Cast(image_mask, to=7)
@@ -176,7 +183,7 @@ class Gemma3MultiModalModel(nn.Module):
176183
- embedding: token embedding + image feature fusion
177184
"""
178185

179-
default_task: str = "vision-language"
186+
default_task: str = "gemma3-vision-language"
180187
category: str = "Multimodal"
181188

182189
# Runtime HF ``named_modules()`` sub-trees per ONNX component.

‎src/mobius/models/gemma3_test.py‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def _session(model: ir.Model) -> ort.InferenceSession:
2020
return ort.InferenceSession(proto.SerializeToString(), providers=["CPUExecutionProvider"])
2121

2222

23-
def _embedding_session() -> ort.InferenceSession:
23+
def _embedding_session(*, image_token_id: int = 9) -> ort.InferenceSession:
2424
config = ArchitectureConfig(
2525
vocab_size=16,
2626
hidden_size=4,
2727
pad_token_id=0,
28-
image_token_id=9,
28+
image_token_id=image_token_id,
2929
)
3030
model = build_embedding_from_features(
3131
_Gemma3EmbeddingModel(config),
@@ -64,6 +64,24 @@ def test_two_rows_use_flattened_feature_offsets(self) -> None:
6464
np.testing.assert_array_equal(actual[0, 0], features[0])
6565
np.testing.assert_array_equal(actual[1, 1], features[1])
6666

67+
def test_processor_soft_tokens_beyond_vocab_are_safely_replaced(self) -> None:
68+
session = _embedding_session(image_token_id=16)
69+
features = np.arange(256 * 4, dtype=np.float32).reshape(256, 4)
70+
input_ids = np.array([[14, *([16] * 256), 15]], dtype=np.int64)
71+
72+
actual = session.run(
73+
None,
74+
{
75+
"input_ids": input_ids,
76+
"image_features": features,
77+
},
78+
)[0]
79+
80+
weights = 2.0 * np.arange(64, dtype=np.float32).reshape(16, 4)
81+
np.testing.assert_array_equal(actual[0, 0], weights[14])
82+
np.testing.assert_array_equal(actual[0, 1:257], features)
83+
np.testing.assert_array_equal(actual[0, 257], weights[15])
84+
6785

6886
class TestGemma3VisionEncoder:
6987
def test_projector_matches_numpy_reference(self) -> None:

0 commit comments

Comments
 (0)