@@ -2032,6 +2032,7 @@ def _write_moe_gguf(
20322032 diffusion_fused_qkv : bool = False ,
20332033 fused_qkv_float : bool = False ,
20342034 quantize_tied_embedding : bool = False ,
2035+ output_quantization : str | None = None ,
20352036 expert_scale_suffix : str | None = None ,
20362037 malformed_expert_scale : bool = False ,
20372038) -> None :
@@ -2116,6 +2117,11 @@ def add_mxfp4(name: str, shape: tuple[int, ...]) -> None:
21162117 "mxfp4" : add_mxfp4 ,
21172118 "q4_0" : add_q4 ,
21182119 }[projection_quantization ]
2120+ add_output = {
2121+ "f32" : add_float ,
2122+ "mxfp4" : add_mxfp4 ,
2123+ "q4_0" : add_q4 ,
2124+ }[output_quantization or projection_quantization ]
21192125
21202126 if quantize_tied_embedding :
21212127 assert architecture in {"qwen3moe" , "granitemoe" }
@@ -2192,7 +2198,7 @@ def add_mxfp4(name: str, shape: tuple[int, ...]) -> None:
21922198 if architecture == "phimoe" :
21932199 add_float ("output_norm.bias" , (hidden_size ,))
21942200 if architecture not in {"qwen3moe" , "granitemoe" }:
2195- add_projection ("output.weight" , (vocab_size , hidden_size ))
2201+ add_output ("output.weight" , (vocab_size , hidden_size ))
21962202 if architecture == "phimoe" :
21972203 add_float ("output.bias" , (vocab_size ,))
21982204
@@ -3447,6 +3453,51 @@ def test_phimoe_fused_qkv_is_split_without_loss(
34473453 )
34483454 offset = end
34493455
3456+ @pytest .mark .parametrize ("output_quantization" , ["f32" , "q4_0" ])
3457+ def test_phimoe_output_head_honors_storage_and_values (
3458+ self , output_quantization : str , tmp_path : Path
3459+ ) -> None :
3460+ from mobius .integrations .gguf import build_from_gguf
3461+ from mobius .integrations .gguf ._reader import GGUFModel
3462+ from mobius .integrations .gguf ._repacker import repack_gguf_tensor
3463+
3464+ path = tmp_path / f"phimoe-output-{ output_quantization } .gguf"
3465+ _write_moe_gguf (
3466+ path ,
3467+ "phimoe" ,
3468+ "q4_0" ,
3469+ output_quantization = output_quantization ,
3470+ )
3471+ source = GGUFModel (path )
3472+ package = build_from_gguf (path , keep_quantized = True )
3473+ model = package ["model" ]
3474+ raw , qtype , shape = next (
3475+ (raw , qtype , shape )
3476+ for name , raw , qtype , shape in source .tensor_items_raw ()
3477+ if name == "output.weight"
3478+ )
3479+
3480+ if output_quantization == "q4_0" :
3481+ assert package .config .quantization is not None
3482+ assert package .config .quantization .quantize_lm_head
3483+ repacked = repack_gguf_tensor (raw , qtype .value , shape )
3484+ np .testing .assert_array_equal (
3485+ model .graph .initializers ["lm_head.weight" ].const_value .numpy (),
3486+ repacked .weight ,
3487+ )
3488+ np .testing .assert_array_equal (
3489+ model .graph .initializers ["lm_head.scales" ].const_value .numpy (),
3490+ repacked .scales ,
3491+ )
3492+ else :
3493+ assert package .config .quantization is not None
3494+ assert not package .config .quantization .quantize_lm_head
3495+ assert "lm_head.scales" not in model .graph .initializers
3496+ np .testing .assert_array_equal (
3497+ model .graph .initializers ["lm_head.weight_t" ].const_value .numpy (),
3498+ source .get_tensor ("output.weight" ).T ,
3499+ )
3500+
34503501 @pytest .mark .parametrize ("architecture" , ["qwen3moe" , "granitemoe" ])
34513502 def test_tied_quantized_embedding_is_shared_with_output_head (
34523503 self , architecture : str , tmp_path : Path
0 commit comments