@@ -1113,6 +1113,7 @@ def _write_granitehybrid_moe_gguf(
11131113 expert_used_count : int = 1 ,
11141114 shared_width : int = 16 ,
11151115 attention_biases : bool = True ,
1116+ mlp_biases : bool = False ,
11161117 malformed_shape : str | None = None ,
11171118) -> None :
11181119 """Write a tiny mixed GraniteHybrid GGUF with routed and shared experts."""
@@ -1205,38 +1206,47 @@ def add_q4(name: str, shape: tuple[int, ...]) -> None:
12051206 prefix = f"blk.{ layer } ."
12061207 add_float (prefix + "attn_norm.weight" , (hidden ,))
12071208 add_float (prefix + "ffn_norm.weight" , (hidden ,))
1208- add_float (prefix + "ffn_gate_inp.weight" , (expert_count , hidden ))
12091209 expert_projection = add_q4 if quantized else add_float
1210- if quantized :
1211- expert_projection (
1212- prefix + "ffn_gate_exps.weight" ,
1213- (expert_count , expert_width , hidden ),
1214- )
1215- expert_projection (
1216- prefix + "ffn_up_exps.weight" ,
1217- (expert_count , expert_width , hidden ),
1218- )
1219- expert_projection (
1220- prefix + "ffn_down_exps.weight" ,
1221- (expert_count , hidden , expert_width ),
1222- )
1210+ if not expert_count :
1211+ expert_projection (prefix + "ffn_gate.weight" , (expert_width , hidden ))
1212+ expert_projection (prefix + "ffn_up.weight" , (expert_width , hidden ))
1213+ expert_projection (prefix + "ffn_down.weight" , (hidden , expert_width ))
1214+ if mlp_biases :
1215+ add_float (prefix + "ffn_gate.bias" , (expert_width ,), fill = 41.0 )
1216+ add_float (prefix + "ffn_up.bias" , (expert_width ,), fill = 42.0 )
1217+ add_float (prefix + "ffn_down.bias" , (hidden ,), fill = 43.0 )
12231218 else :
1224- add_float (
1225- prefix + "ffn_gate_exps.weight" ,
1226- (expert_count , expert_width , hidden ),
1227- expert_base = 11.0 ,
1228- )
1229- add_float (
1230- prefix + "ffn_up_exps.weight" ,
1231- (expert_count , expert_width , hidden ),
1232- expert_base = 21.0 ,
1233- )
1234- add_float (
1235- prefix + "ffn_down_exps.weight" ,
1236- (expert_count , hidden , expert_width ),
1237- expert_base = 31.0 ,
1238- )
1239- if shared_width :
1219+ add_float (prefix + "ffn_gate_inp.weight" , (expert_count , hidden ))
1220+ if quantized :
1221+ expert_projection (
1222+ prefix + "ffn_gate_exps.weight" ,
1223+ (expert_count , expert_width , hidden ),
1224+ )
1225+ expert_projection (
1226+ prefix + "ffn_up_exps.weight" ,
1227+ (expert_count , expert_width , hidden ),
1228+ )
1229+ expert_projection (
1230+ prefix + "ffn_down_exps.weight" ,
1231+ (expert_count , hidden , expert_width ),
1232+ )
1233+ else :
1234+ add_float (
1235+ prefix + "ffn_gate_exps.weight" ,
1236+ (expert_count , expert_width , hidden ),
1237+ expert_base = 11.0 ,
1238+ )
1239+ add_float (
1240+ prefix + "ffn_up_exps.weight" ,
1241+ (expert_count , expert_width , hidden ),
1242+ expert_base = 21.0 ,
1243+ )
1244+ add_float (
1245+ prefix + "ffn_down_exps.weight" ,
1246+ (expert_count , hidden , expert_width ),
1247+ expert_base = 31.0 ,
1248+ )
1249+ if expert_count and shared_width :
12401250 add_float (prefix + "ffn_gate_shexp.weight" , (shared_width , hidden ))
12411251 add_float (prefix + "ffn_up_shexp.weight" , (shared_width , hidden ))
12421252 add_float (prefix + "ffn_down_shexp.weight" , (hidden , shared_width ))
@@ -1622,11 +1632,24 @@ def add_float(
16221632 * ,
16231633 expert_order : bool = False ,
16241634 negative : bool = False ,
1635+ permute_heads : int | None = None ,
16251636 ) -> None :
16261637 if name == omit :
16271638 return
16281639 shape = adjusted_shape (name , shape )
16291640 values = rng .normal (0.0 , 0.03 , size = shape ).astype (np .float32 )
1641+ if permute_heads is not None :
1642+ values = np .arange (np .prod (shape ), dtype = np .float32 ).reshape (shape )
1643+ values = (
1644+ values .reshape (
1645+ permute_heads ,
1646+ 2 ,
1647+ shape [0 ] // permute_heads // 2 ,
1648+ * shape [1 :],
1649+ )
1650+ .swapaxes (1 , 2 )
1651+ .reshape (shape )
1652+ )
16301653 if negative :
16311654 values = - np .exp (values )
16321655 if expert_order :
@@ -1700,8 +1723,20 @@ def projection(name: str, shape: tuple[int, ...]) -> None:
17001723 projection ("blk.1.ffn_latent_down.weight" , (latent_width , hidden ))
17011724 projection ("blk.1.ffn_latent_up.weight" , (hidden , latent_width ))
17021725
1703- projection ("blk.2.attn_q.weight" , (heads * head_dim , hidden ))
1704- projection ("blk.2.attn_k.weight" , (kv_heads * head_dim , hidden ))
1726+ if quantized :
1727+ projection ("blk.2.attn_q.weight" , (heads * head_dim , hidden ))
1728+ projection ("blk.2.attn_k.weight" , (kv_heads * head_dim , hidden ))
1729+ else :
1730+ add_float (
1731+ "blk.2.attn_q.weight" ,
1732+ (heads * head_dim , hidden ),
1733+ permute_heads = heads ,
1734+ )
1735+ add_float (
1736+ "blk.2.attn_k.weight" ,
1737+ (kv_heads * head_dim , hidden ),
1738+ permute_heads = kv_heads ,
1739+ )
17051740 projection ("blk.2.attn_v.weight" , (kv_heads * head_dim , hidden ))
17061741 projection ("blk.2.attn_output.weight" , (hidden , heads * head_dim ))
17071742
@@ -5774,7 +5809,6 @@ def test_float_import_preserves_expert_order_and_round_trips(self, tmp_path: Pat
57745809 f"model.layers.1.feed_forward.experts.{ expert } .{ projection } .weight_t"
57755810 ].const_value .numpy ()
57765811 np .testing .assert_array_equal (value , expert + 1 )
5777-
57785812 output_dir = tmp_path / "saved-jamba"
57795813 package .save (output_dir , progress_bar = False )
57805814 session = OnnxModelSession (ModelPackage .load (output_dir )["model" ])
@@ -6472,6 +6506,32 @@ def test_attention_biases_can_be_absent(self, tmp_path: Path) -> None:
64726506 for name in model .graph .initializers
64736507 )
64746508
6509+ def test_dense_ffn_biases_are_fused_in_gate_then_up_order (self , tmp_path : Path ) -> None :
6510+ from mobius .integrations .gguf import build_from_gguf
6511+
6512+ path = tmp_path / "granitehybrid-dense-biased.gguf"
6513+ _write_granitehybrid_moe_gguf (
6514+ path ,
6515+ quantized = False ,
6516+ expert_count = 0 ,
6517+ expert_used_count = 0 ,
6518+ shared_width = 0 ,
6519+ mlp_biases = True ,
6520+ )
6521+
6522+ model = build_from_gguf (path )["model" ]
6523+ for layer in range (2 ):
6524+ prefix = f"model.layers.{ layer } .shared_mlp."
6525+ fused_bias = model .graph .initializers [
6526+ prefix + "input_linear.bias"
6527+ ].const_value .numpy ()
6528+ np .testing .assert_array_equal (fused_bias [:32 ], 41.0 )
6529+ np .testing .assert_array_equal (fused_bias [32 :], 42.0 )
6530+ np .testing .assert_array_equal (
6531+ model .graph .initializers [prefix + "output_linear.bias" ].const_value .numpy (),
6532+ 43.0 ,
6533+ )
6534+
64756535 def test_quantized_source_requires_explicit_dequantization (self , tmp_path : Path ) -> None :
64766536 from mobius .integrations .gguf import build_from_gguf
64776537
@@ -6580,6 +6640,18 @@ def test_float_import_preserves_expert_order_state_and_roundtrip(
65806640 f"model.layers.1.moe.experts.{ expert } .{ projection } .weight_t"
65816641 ].const_value .numpy ()
65826642 np .testing .assert_array_equal (value , expert + 1 )
6643+ np .testing .assert_array_equal (
6644+ model .graph .initializers [
6645+ "model.layers.2.self_attn.q_proj.weight_t"
6646+ ].const_value .numpy (),
6647+ np .arange (32 * 32 , dtype = np .float32 ).reshape (32 , 32 ).T ,
6648+ )
6649+ np .testing .assert_array_equal (
6650+ model .graph .initializers [
6651+ "model.layers.2.self_attn.k_proj.weight_t"
6652+ ].const_value .numpy (),
6653+ np .arange (16 * 32 , dtype = np .float32 ).reshape (16 , 32 ).T ,
6654+ )
65836655
65846656 output_dir = tmp_path / "saved-nemotron-h-moe"
65856657 package .save (output_dir , progress_bar = False )
@@ -7943,6 +8015,24 @@ def test_second_cohort_exact_closure_and_mutations(self, architecture: str) -> N
79438015 )
79448016 )
79458017
8018+ if architecture == "granitehybrid" :
8019+ dense_biases = [
8020+ f"blk.{ layer } .ffn_{ projection } .bias"
8021+ for layer in range (2 )
8022+ for projection in ("gate" , "up" , "down" )
8023+ ]
8024+ _raise_for_invalid_hybrid_tensor_contract (
8025+ self ._FakeGGUF (architecture , metadata , [* names , * dense_biases ])
8026+ )
8027+ with pytest .raises (ValueError , match = r"partial dense shared-MLP bias family" ):
8028+ _raise_for_invalid_hybrid_tensor_contract (
8029+ self ._FakeGGUF (
8030+ architecture ,
8031+ metadata ,
8032+ [* names , * dense_biases [:- 1 ]],
8033+ )
8034+ )
8035+
79468036 def test_partial_fused_and_separate_experts_are_rejected (self ) -> None :
79478037 from mobius .integrations .gguf ._builder import (
79488038 _raise_for_invalid_hybrid_tensor_contract ,
0 commit comments