Repository navigation
Commit 114a1bc
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1055 | 1055 | | |
1056 | 1056 | | |
1057 | 1057 | | |
1058 | | - | |
| 1058 | + | |
1059 | 1059 | | |
1060 | 1060 | | |
1061 | 1061 | | |
| |||
1108 | 1108 | | |
1109 | 1109 | | |
1110 | 1110 | | |
1111 | | - | |
1112 | | - | |
| 1111 | + | |
| 1112 | + | |
1113 | 1113 | | |
1114 | 1114 | | |
1115 | 1115 | | |
| |||
1128 | 1128 | | |
1129 | 1129 | | |
1130 | 1130 | | |
1131 | | - | |
| 1131 | + | |
| 1132 | + | |
1132 | 1133 | | |
1133 | 1134 | | |
1134 | 1135 | | |
| |||
1342 | 1343 | | |
1343 | 1344 | | |
1344 | 1345 | | |
1345 | | - | |
| 1346 | + | |
1346 | 1347 | | |
1347 | 1348 | | |
1348 | 1349 | | |
| |||
0 commit comments