Skip to content

Commit 114a1bc

Browse files
justinchubyCopilot
andauthored
Move Gemma4 per-layer embeddings to embedding model (Alternative A) (#296)
## Summary Replace L separate `[V, D]` per-layer embedding tables with a single fused `[V, L*D]` table, matching HuggingFace's original weight layout. ## Background The per-layer split (`nn.ModuleList` of L separate embeddings) was a workaround for the [ORT CUDA Gather int32 overflow bug](https://github.com/microsoft/onnxruntime/issues/28107). Each `[V, D]` table had only 67M elements (V=262144, D=256), staying under the 2.1B int32 limit. That ORT bug is now fixed (merged in ORT 1.27), so the fused `[V, L*D]` table (2.35B elements for E2B, 2.82B for E4B) works correctly with int64 indexing. ## Changes **Model (`__init__`)**: - Replace `nn.ModuleList` of L `Gemma3TextScaledWordEmbedding(V, D)` with single `Gemma3TextScaledWordEmbedding(V, L*D)` - embed_scale remains `sqrt(D)` (not `sqrt(L*D)`) **Forward pass (`_compute_per_layer_inputs`)**: - Single Gather on fused table → `[B, S, L*D]` - Reshape to `[B, S, L, D]` - Slice per layer (same as projection slicing) **Weight loading (`preprocess_weights`)**: - Remove split logic in `Gemma4CausalLMModel` (was splitting `[V, L*D]` → L × `[V, D]`) - Remove split logic in `Gemma4MultimodalCausalLMModel` (same) - HF weight `model.embed_tokens_per_layer.weight` maps directly to ONNX parameter ## Testing - L1: All 15 Gemma4 tests pass - Full L1: 2736 passed, 0 failed - Net code change: -39 lines, +25 lines --------- Signed-off-by: Justin Chu <[email protected]> Co-authored-by: Copilot <[email protected]>
1 parent 0d2aed2 commit 114a1bc

3 files changed

Lines changed: 192 additions & 119 deletions

File tree

‎src/mobius/integrations/ort_genai/auto_export_test.py‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,7 @@ class FakeConfig:
10551055
decoder = _mock_model_with_inputs(
10561056
[
10571057
"inputs_embeds",
1058-
"input_ids",
1058+
"per_layer_inputs",
10591059
"attention_mask",
10601060
"position_ids",
10611061
"past_key_values.0.key",
@@ -1108,8 +1108,8 @@ def test_gemma4_vision_inputs(self, tmp_path):
11081108
assert "image_grid_thw" not in vision_inputs
11091109
assert data["model"]["vision"]["spatial_merge_size"] == 2
11101110

1111-
def test_gemma4_decoder_has_input_ids_and_inputs_embeds(self, tmp_path):
1112-
"""Gemma4 decoder has both inputs_embeds and input_ids."""
1111+
def test_gemma4_decoder_has_per_layer_inputs_and_inputs_embeds(self, tmp_path):
1112+
"""Gemma4 decoder has inputs_embeds and per_layer_inputs."""
11131113
pkg = self._make_gemma4_pkg()
11141114
path = _write_genai_config(
11151115
pkg.config,
@@ -1128,7 +1128,8 @@ def test_gemma4_decoder_has_input_ids_and_inputs_embeds(self, tmp_path):
11281128
data = json.load(f)
11291129
decoder_inputs = data["model"]["decoder"]["inputs"]
11301130
assert "inputs_embeds" in decoder_inputs
1131-
assert "input_ids" in decoder_inputs
1131+
assert "per_layer_inputs" in decoder_inputs
1132+
assert "input_ids" not in decoder_inputs
11321133
# KV cache templates are present
11331134
assert decoder_inputs["past_key_names"] == "past_key_values.%d.key"
11341135

@@ -1342,7 +1343,7 @@ def test_gemma4_genai_config_from_real_model(self, tmp_path):
13421343
# Decoder inputs introspected from graph
13431344
decoder_inputs = data["model"]["decoder"]["inputs"]
13441345
assert "inputs_embeds" in decoder_inputs
1345-
assert "input_ids" in decoder_inputs
1346+
assert "input_ids" not in decoder_inputs
13461347
assert "attention_mask" in decoder_inputs
13471348
assert "position_ids" in decoder_inputs
13481349
assert decoder_inputs["past_key_names"] == ("past_key_values.%d.key")

0 commit comments

Comments
 (0)