[Bug] fp8 `weight_scale_inv` shape mismatch for `k_proj` in full-attention layers (TP-sharded block grid)

#44
by Kaixuanliu - opened

Summary

In the fp8 checkpoint, the 9 full-attention layers (hybrid_layer_pattern == 0: layers 0, 5, 11, 17, 23, 29, 35, 41, 47) ship a self_attn.k_proj.weight_scale_inv whose block grid does not match the weight it belongs to:

tensor shape expected scale grid (weight_block_size = [128, 128]) actual
model.layers.0.self_attn.k_proj.weight [768, 4096] [6, 32] [8, 32]

768 = num_key_value_heads(4) * head_dim(192), so ceil(768/128) = 6 block rows are expected, but 8 are stored. All other quantized tensors in the checkpoint are consistent(only these 9 are affected).

Root cause

The scales appear to have been computed per tensor-parallel shard with TP=4 rather than on the full tensor: each rank owns 768 / 4 = 192 rows, which needs ceil(192/128) = 2 block rows, giving 4 * 2 = 8 rows in total. The scale rows therefore pair up per kv head ((0,1), (2,3), (4,5), (6,7)), which is also visible in their magnitudes.

This only shows up for k_proj in full-attention layers because every other projection has a per-shard row count that is a multiple of 128:

  • q_proj: 12288 / 4 = 3072 -> OK
  • v_proj: 512 / 4 = 128 -> OK
  • SWA k_proj: 1536 / 4 = 384 -> OK
  • full-attn k_proj: 768 / 4 = 192 -> not a multiple of 128

Reproduction

from transformers import AutoModelForCausalLM, AutoTokenizer

model_id = "XiaomiMiMo/MiMo-V2-Flash"
tok = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(model_id,  device_map="auto")
model.generate(**tok("Hello", return_tensors="pt").to(model.device), max_new_tokens=8)

Close it as I suppose it is designed for tp=4, and in this case we cannot cancel the padding.

Sign up or log in to comment