Repository navigation
refactor: keep gate_up_proj fused, split activations in MLP forward - #155
Conversation
Add FusedGateUpMLP component that keeps the gate_up_proj weight as a single fused tensor and splits activations after the matmul instead: gate_up = gate_up_proj(x) # [*, 2 * intermediate_size] gate, up = Split(gate_up, axis=-1) # each [*, intermediate_size] return down_proj(act(gate) * up) This eliminates all gate_up_proj weight splitting in preprocess_weights, which was broken for GPTQ int32-packed weights where dim 0 is original_dim / pack_factor. Changes: - components/_mlp.py: Add FusedGateUpMLP class - components/_decoder.py: Add mlp_class param to DecoderLayer/create_decoder_layer - models/base.py: Add FusedGateUpCausalLMModel; TextModel accepts mlp_class - models/phi3.py: Extend FusedGateUpCausalLMModel; remove gate_up split - models/phi.py: Remove gate_up_proj split from _preprocess_phi4mm_weights; all LoRA decoder layers use FusedGateUpMLP; fix num_kv_heads → num_key_value_heads - models/glm.py: Remove _split_gate_up_proj; GlmCausalLMModel extends FusedGateUpCausalLMModel; Glm4DecoderLayer uses FusedGateUpMLP - models/__init__.py: Export FusedGateUpCausalLMModel Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Performance Comparison
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
🏗️ Architecture Diff
phi3 / model — 25 change(s)Op summary: 61 → 59 nodes --- base
+++ head
@@ -24,10 +24,9 @@
RMSNormalization
Transpose
MatMul
+Split
Sigmoid
Mul
-Transpose
-MatMul
Mul
Transpose
MatMul
@@ -48,10 +47,9 @@
RMSNormalization
Transpose
MatMul
+Split
Sigmoid
Mul
-Transpose
-MatMul
Mul
Transpose
MatMulAdded nodes:
Removed nodes:
Modified attributes:
Connectivity changes:
Initializer changes:
phi3 (static-cache) / model — 25 change(s)Op summary: 58 → 56 nodes --- base
+++ head
@@ -19,10 +19,9 @@
RMSNormalization
Transpose
MatMul
+Split
Sigmoid
Mul
-Transpose
-MatMul
Mul
Transpose
MatMul
@@ -45,10 +44,9 @@
RMSNormalization
Transpose
MatMul
+Split
Sigmoid
Mul
-Transpose
-MatMul
Mul
Transpose
MatMulAdded nodes:
Removed nodes:
Modified attributes:
Connectivity changes:
Initializer changes:
Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed) |
There was a problem hiding this comment.
Pull request overview
Refactors MLP handling for architectures with fused gate_up_proj (Phi-3/4, GLM) to avoid splitting fused weights during preprocess_weights (which fails for GPTQ int32-packed checkpoints), by instead splitting activations after the fused matmul during the MLP forward pass.
Changes:
- Introduce
FusedGateUpMLPthat keeps a singlegate_up_projparameter and splits activations inforward(). - Plumb an
mlp_classoverride throughDecoderLayer/TextModel, and addFusedGateUpCausalLMModelto use fused-gate MLPs by default. - Remove
gate_up_projweight splitting from Phi-3/Phi-4MM/GLM preprocessing paths and update model wiring accordingly.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/mobius/models/phi3.py | Switch Phi-3 to FusedGateUpCausalLMModel; stop splitting gate_up_proj weights. |
| src/mobius/models/phi.py | Use FusedGateUpMLP in LoRA decoder layers; remove gate_up_proj split logic from Phi4MM preprocessing. |
| src/mobius/models/glm.py | Move GLM/GLM4 to fused-gate MLP path; remove local gate/up weight splitting helper and preprocess hook. |
| src/mobius/models/base.py | Add mlp_class plumbing to TextModel; introduce FusedGateUpCausalLMModel. |
| src/mobius/models/init.py | Export FusedGateUpCausalLMModel. |
| src/mobius/components/_mlp.py | Add FusedGateUpMLP implementation (fused weight, activation split). |
| src/mobius/components/_decoder.py | Add mlp_class override to DecoderLayer and create_decoder_layer. |
| src/mobius/components/init.py | Export FusedGateUpMLP from the components public API. |
- Fix misleading '# SwiGLU' comment in FusedGateUpMLP.forward() — activation is configurable, not always SwiGLU; now reads '# gated activation (e.g. SiLU(gate) * up) → down_proj' - Add TestFusedGateUpMLP unit test class in _mlp_test.py covering projection shapes, parameter count, graph construction (Split op present), SiLU activation, and linear_class injection - Fix '# Replace TextModel' comment in FusedGateUpCausalLMModel.__init__ to '# Parameterize TextModel to use FusedGateUpMLP for each decoder layer' Co-authored-by: Copilot <[email protected]> Signed-off-by: Justin Chu <[email protected]>
Problem
split_gate_up_projinpreprocess_weightsassumes an uncompressed weight tensor. For GPTQ int32-packed weights,dim 0 = original_dim / pack_factor, causing a build failure:Solution
Instead of fixing the split logic, remove all weight splitting entirely. The
gate_up_projweight stays fused; activations are split after the matmul:This is cleaner, future-proof, and matches HuggingFace weight names exactly — no
preprocess_weightsrenames needed forgate_up_proj.Changes
components/_mlp.py: AddFusedGateUpMLP— singlegate_up_projparam, split activations in forwardcomponents/_decoder.py: Addmlp_classparam toDecoderLayer/create_decoder_layermodels/base.py: AddFusedGateUpCausalLMModel;TextModelacceptsmlp_classmodels/phi3.py: ExtendFusedGateUpCausalLMModel; remove gate_up split frompreprocess_weightsmodels/phi.py: Remove gate_up_proj split from_preprocess_phi4mm_weights; all LoRA decoder layers useFusedGateUpMLP; fixnum_kv_heads→num_key_value_headsbug in LoRA B splitmodels/glm.py: Remove_split_gate_up_projprivate function;GlmCausalLMModelextendsFusedGateUpCausalLMModel;Glm4DecoderLayerusesFusedGateUpMLPmodels/__init__.py: ExportFusedGateUpCausalLMModelTesting
2352 tests pass, 29 skipped. Lintrunner clean.