Skip to content

refactor: keep gate_up_proj fused, split activations in MLP forward - #155

Merged
justinchuby merged 3 commits into
mainfrom
justinchu/fix-gptq-split
Apr 10, 2026
Merged

justinchuby merged 3 commits into
mainfrom
justinchu/fix-gptq-split

Conversation

@justinchuby

Copy link
Copy Markdown
Member

Problem

split_gate_up_proj in preprocess_weights assumes an uncompressed weight tensor. For GPTQ int32-packed weights, dim 0 = original_dim / pack_factor, causing a build failure:

gate_up weight dim 0 is 2048, expected 16384

Solution

Instead of fixing the split logic, remove all weight splitting entirely. The gate_up_proj weight stays fused; activations are split after the matmul:

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 is cleaner, future-proof, and matches HuggingFace weight names exactly — no preprocess_weights renames needed for gate_up_proj.

Changes

  • components/_mlp.py: Add FusedGateUpMLP — single gate_up_proj param, split activations in forward
  • 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 from preprocess_weights
  • 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 bug in LoRA B split
  • models/glm.py: Remove _split_gate_up_proj private function; GlmCausalLMModel extends FusedGateUpCausalLMModel; Glm4DecoderLayer uses FusedGateUpMLP
  • models/__init__.py: Export FusedGateUpCausalLMModel

Testing

2352 tests pass, 29 skipped. Lintrunner clean.

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]>
@github-actions

github-actions Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

Performance Comparison

Comparing 170f28a → 42259df

Model Metric Baseline Current Delta
bert (feature-extraction) model_size_bytes 359 KB 359 KB +0.0% ⚪
bert (feature-extraction) num_nodes 61 61 +0.0% ⚪
falcon model_size_bytes 364 KB 364 KB +0.0% ⚪
falcon num_nodes 66 66 +0.0% ⚪
gemma2 model_size_bytes 428 KB 428 KB +0.0% ⚪
gemma2 num_nodes 107 107 +0.0% ⚪
gpt2 model_size_bytes 388 KB 388 KB +0.0% ⚪
gpt2 num_nodes 53 53 +0.0% ⚪
llama model_size_bytes 425 KB 425 KB +0.0% ⚪
llama num_nodes 61 61 +0.0% ⚪
llama (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
llama (static-cache) num_nodes 58 58 +0.0% ⚪
mamba (ssm-text-generation) model_size_bytes 360 KB 360 KB +0.0% ⚪
mamba (ssm-text-generation) num_nodes 103 103 +0.0% ⚪
phi3 model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 num_nodes 61 59 -3.3% 🟢
phi3 (static-cache) model_size_bytes 421 KB 421 KB +0.0% ⚪
phi3 (static-cache) num_nodes 58 56 -3.4% 🟢
qwen2 model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 num_nodes 61 61 +0.0% ⚪
qwen2 (static-cache) model_size_bytes 425 KB 425 KB +0.0% ⚪
qwen2 (static-cache) num_nodes 58 58 +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) model_size_bytes 506 KB 506 KB +0.0% ⚪
qwen3_5_moe (hybrid-text-generation) num_nodes 275 275 +0.0% ⚪
qwen3_5_text (hybrid-text-generation) model_size_bytes 458 KB 458 KB +0.0% ⚪
qwen3_5_text (hybrid-text-generation) num_nodes 129 129 +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) model_size_bytes 977 KB 977 KB +0.0% ⚪
qwen3_5_vl (hybrid-qwen-vl) num_nodes 409 409 +0.0% ⚪
t5 (seq2seq) model_size_bytes 836 KB 836 KB +0.0% ⚪
t5 (seq2seq) num_nodes 174 174 +0.0% ⚪
whisper (speech-to-text) model_size_bytes 1008 KB 1008 KB +0.0% ⚪
whisper (speech-to-text) num_nodes 128 128 +0.0% ⚪

No performance regressions.

@codecov

codecov Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@justinchuby justinchuby added the ai Created by an AI agent label Apr 10, 2026
@github-actions

github-actions Bot commented Apr 10, 2026 •

Copy link
Copy Markdown

🏗️ Architecture Diff

Comparing 170f28a → 42259df

Model Sub-model Changes Status
bert (feature-extraction) model 0 ⚪
falcon model 0 ⚪
gemma2 model 0 ⚪
gpt2 model 0 ⚪
llama model 0 ⚪
llama (static-cache) model 0 ⚪
mamba (ssm-text-generation) model 0 ⚪
phi3 model 25 🟡
phi3 (static-cache) model 25 🟡
qwen model 0 ⚪
qwen (static-cache) model 0 ⚪
qwen2 model 0 ⚪
qwen2 (static-cache) model 0 ⚪
qwen2_moe model 0 ⚪
qwen2_moe (static-cache) model 0 ⚪
qwen3 model 0 ⚪
qwen3 (static-cache) model 0 ⚪
qwen3_5_moe (hybrid-text-generation) model 0 ⚪
qwen3_5_text (hybrid-text-generation) model 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) decoder 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) embedding 0 ⚪
qwen3_5_vl (hybrid-qwen-vl) vision 0 ⚪
qwen3_moe model 0 ⚪
qwen3_moe (static-cache) model 0 ⚪
qwen3_next (hybrid-text-generation) model 0 ⚪
t5 (seq2seq) decoder 0 ⚪
t5 (seq2seq) encoder 0 ⚪
whisper (speech-to-text) decoder 0 ⚪
whisper (speech-to-text) encoder 0 ⚪
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
 MatMul

Added nodes:

  • + Split
  • + Split

Removed nodes:

  • - Transpose
  • - MatMul
  • - Transpose
  • - MatMul

Modified attributes:

  • node[41] RotaryEmbedding: num_heads: 4 → 2

Connectivity changes:

  • node[4] Unsqueeze: input_ids [34, 10] → [32, 10]
  • node[8] Concat: input_ids [36, 37, 38] → [34, 35, 36]
  • node[9] Expand: input_ids [35, 39] → [33, 37]
  • node[10] RMSNormalization: input_ids [31, 11] → [29, 11]
  • node[12] MatMul: input_ids [41, 42] → [39, 40]
  • node[14] MatMul: input_ids [41, 44] → [39, 42]
  • node[16] MatMul: input_ids [41, 46] → [39, 44]
  • node[17] RotaryEmbedding: input_ids [43, 32, 33] → [41, 30, 31]
  • node[18] RotaryEmbedding: input_ids [45, 32, 33] → [43, 30, 31]
  • node[19] Attention: input_ids [48, 49, 47, 40, 3, 4] → [46, 47, 45, 38, 3, 4]
  • node[21] MatMul: input_ids [50, 53] → [48, 51]
  • node[22] Add: input_ids [31, 54] → [29, 52]
  • node[23] RMSNormalization: input_ids [55, 16] → [53, 16]
  • node[25] MatMul: input_ids [56, 57] → [54, 55]
  • node[41] RotaryEmbedding: input_ids [69, 32, 33] → [69, 30, 31]
  • node[50] Sigmoid: input_ids [84] → [83]
  • node[51] Mul: input_ids [84, 85] → [83, 85]

Initializer changes:

  • initializer count 24 → 22
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
 MatMul

Added nodes:

  • + Split
  • + Split

Removed nodes:

  • - Transpose
  • - MatMul
  • - Transpose
  • - MatMul

Modified attributes:

  • node[36] RotaryEmbedding: num_heads: 4 → 2

Connectivity changes:

  • node[3] RMSNormalization: input_ids [31, 11] → [29, 11]
  • node[5] MatMul: input_ids [34, 35] → [32, 33]
  • node[7] MatMul: input_ids [34, 37] → [32, 35]
  • node[9] MatMul: input_ids [34, 39] → [32, 37]
  • node[10] RotaryEmbedding: input_ids [36, 32, 33] → [34, 30, 31]
  • node[11] RotaryEmbedding: input_ids [38, 32, 33] → [36, 30, 31]
  • node[12] TensorScatter: input_ids [2, 42, 6] → [2, 40, 6]
  • node[13] TensorScatter: input_ids [3, 40, 6] → [3, 38, 6]
  • node[14] Attention: input_ids [41, 43, 44, None, None, None, 7] → [39, 41, 42, None, None, None, 7]
  • node[16] MatMul: input_ids [45, 48] → [43, 46]
  • node[17] Add: input_ids [31, 49] → [29, 47]
  • node[18] RMSNormalization: input_ids [50, 16] → [48, 16]
  • node[20] MatMul: input_ids [51, 52] → [49, 50]
  • node[36] RotaryEmbedding: input_ids [64, 32, 33] → [64, 30, 31]
  • node[38] TensorScatter: input_ids [4, 70, 6] → [5, 66, 6]
  • node[47] Sigmoid: input_ids [81] → [80]
  • node[48] Mul: input_ids [81, 82] → [80, 82]

Initializer changes:

  • initializer count 23 → 21

Legend: ⚪ No change · 🔵 Minor (attrs/inits) · 🟡 Moderate (nodes added/removed) · 🔴 Major (interface changed)

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 FusedGateUpMLP that keeps a single gate_up_proj parameter and splits activations in forward().
  • Plumb an mlp_class override through DecoderLayer/TextModel, and add FusedGateUpCausalLMModel to use fused-gate MLPs by default.
  • Remove gate_up_proj weight 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.

Comment thread src/mobius/models/base.py Resolved
justinchuby and others added 2 commits April 10, 2026 11:31
- 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]>
@justinchuby
justinchuby merged commit 8693678 into main Apr 10, 2026
17 of 19 checks passed
@justinchuby
justinchuby deleted the justinchu/fix-gptq-split branch April 10, 2026 18:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ai Created by an AI agent

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants