2424
2525from typing import TYPE_CHECKING
2626
27- import torch
2827from onnxscript import nn
2928from onnxscript ._internal import builder
3029
3130from mobius ._configs import ArchitectureConfig
3231from mobius .components ._attention import StaticCacheState
3332from mobius .components ._decoder import DecoderLayer
33+ from mobius .components ._mlp import FusedGateUpMLP
3434from mobius .components ._rms_norm import RMSNorm
35- from mobius .models .base import CausalLMModel , TextModel
35+ from mobius .models .base import CausalLMModel , FusedGateUpCausalLMModel , TextModel
3636
3737if TYPE_CHECKING :
3838 import onnx_ir as ir
3939
4040
41- def _split_gate_up_proj (
42- state_dict : dict [str , torch .Tensor ],
43- ) -> dict [str , torch .Tensor ]:
44- """Split fused ``gate_up_proj`` into separate ``gate_proj`` + ``up_proj``.
45-
46- HF GLM/GLM4 store: ``mlp.gate_up_proj.weight`` [2*intermediate, hidden]
47- ONNX MLP expects: ``mlp.gate_proj.weight`` [intermediate, hidden]
48- ``mlp.up_proj.weight`` [intermediate, hidden]
49- """
50- new_state_dict : dict [str , torch .Tensor ] = {}
51- for key , value in state_dict .items ():
52- if ".mlp.gate_up_proj." in key :
53- # Split along dim 0: first half = gate, second half = up
54- mid = value .shape [0 ] // 2
55- gate_key = key .replace (".gate_up_proj." , ".gate_proj." )
56- up_key = key .replace (".gate_up_proj." , ".up_proj." )
57- new_state_dict [gate_key ] = value [:mid ]
58- new_state_dict [up_key ] = value [mid :]
59- else :
60- new_state_dict [key ] = value
61- return new_state_dict
62-
63-
64- class GlmCausalLMModel (CausalLMModel ):
41+ class GlmCausalLMModel (FusedGateUpCausalLMModel ):
6542 """GLM text model (standard pre-norm + fused gate_up_proj).
6643
6744 GLM is architecturally identical to LLaMA except for the fused
68- ``gate_up_proj`` in the MLP. ``preprocess_weights`` splits it into
69- separate ``gate_proj`` + ``up_proj`` tensors.
45+ ``gate_up_proj`` in the MLP. The weight stays fused; activations
46+ are split in the MLP forward pass (see
47+ :class:`~mobius.models.base.FusedGateUpCausalLMModel`).
7048 """
7149
72- def preprocess_weights (
73- self , state_dict : dict [str , torch .Tensor ]
74- ) -> dict [str , torch .Tensor ]:
75- state_dict = _split_gate_up_proj (state_dict )
76- return super ().preprocess_weights (state_dict )
77-
7850
7951# ---------------------------------------------------------------------------
8052# GLM4: 4-norm decoder layer (pre+post norm on both attention and MLP)
@@ -102,9 +74,8 @@ class Glm4DecoderLayer(DecoderLayer):
10274 """
10375
10476 def __init__ (self , config : ArchitectureConfig ):
105- # Initialize the base DecoderLayer (pre-norm mode) to get
106- # input_layernorm, post_attention_layernorm, self_attn, mlp.
107- super ().__init__ (config )
77+ # Initialize the base DecoderLayer with FusedGateUpMLP
78+ super ().__init__ (config , mlp_class = FusedGateUpMLP )
10879
10980 # Add the two extra post-sub-layer norms
11081 self .post_self_attn_layernorm = RMSNorm (config .hidden_size , eps = config .rms_norm_eps )
@@ -156,8 +127,9 @@ def forward(
156127class Glm4CausalLMModel (CausalLMModel ):
157128 """GLM4 text model (4-norm decoder + fused gate_up_proj).
158129
159- Uses ``Glm4DecoderLayer`` with pre+post norms on both sub-layers,
160- and splits the fused ``gate_up_proj`` in ``preprocess_weights``.
130+ Uses ``Glm4DecoderLayer`` with pre+post norms on both sub-layers.
131+ ``gate_up_proj`` is kept fused — activations are split in the MLP
132+ forward pass (via :class:`FusedGateUpMLP` in ``Glm4DecoderLayer``).
161133 """
162134
163135 def __init__ (self , config : ArchitectureConfig ):
@@ -169,12 +141,6 @@ def __init__(self, config: ArchitectureConfig):
169141
170142 self .lm_head = Linear (config .hidden_size , config .vocab_size , bias = False )
171143
172- def preprocess_weights (
173- self , state_dict : dict [str , torch .Tensor ]
174- ) -> dict [str , torch .Tensor ]:
175- state_dict = _split_gate_up_proj (state_dict )
176- return super ().preprocess_weights (state_dict )
177-
178144
179145class Glm4TextModel (TextModel ):
180146 """GLM4 text model backbone using 4-norm decoder layers."""
0 commit comments