Open
feat(mxfp8_qat): checkpoint convert with serializable MXFP8 weights #192
feat(mxfp8_qat): checkpoint convert with serializable MXFP8 weights #192
Open
weixuanAdd on 2 days ago
weixuan
weixuan
2 days ago

【合入来源】

关联 issue: #70

本 PR 是已合入 #142(W8A8 MXFP8 Linear QAT)的后续:补齐训练后的 checkpoint convert——把训练产出的 BF16 master weight 打包为可序列化、可恢复的 MXFP8 推理权重。Dense/MoE 的低精推理 forward 在后续 PR 交付。

【修改方案】

训练后调用链:

state = {k: v.detach().cpu() for k, v in qat_model.state_dict().items()}
pack_checkpoint_weights(state, model=qat_model,
                        dense_names=["model.linear"], expert_names=["model.experts"],
                        device=npu_device)
save_safetensors(state, output_dir)

pack_checkpoint_weights 以待保存的 state 为唯一量化数据源(model 仅提供 prepared 模块的配置与参数名),原位替换选中权重,未选中的键原样保留。

1. 打包入口与事务性

  • 名单驱动(dense_names/expert_names 为模块 FQN),未选中的 prepared 权重不打包;
  • fail-closed 校验:key 存在性、BF16 master、形状与模块一致、finite、共享存储、DTensor 分片(P4 前拒绝);
  • W8A8 要求 block_size=32(打包前拒绝其他值);
  • 事务性:全部校验与打包成功后才一次性 state.update,任一失败 state 保持原状。

2. 量化核心

  • quantize_native_grid:RCEIL(scale_alg=1,与训练一致)、块网格对齐、scale 以自然块序 uint8 保存;
  • pack_one_projection:MoE 逐 expert 搬运到 NPU 量化、CPU 拼回(不一次性上卡生产规模权重);W8A8 在打包时补齐到 64 倍数(成对 scale 布局要求),追加零值 FP8 byte 与填充 scale byte;
  • 填充 scale byte 取值经设备实测确认:0(与 kernel 对全零块的原生 byte 一致);255 会产出 NaN(有回归测试钉住),127 数值等价但不采用。

3. 序列化权重对象

MXFP8DenseTensor / MXFP8GMMTensor(TorchAOBaseTensor):

  • 存储契约:qdata/scale 均为 uint8(E4M3FN 值编码 / E8M0 byte),不依赖 safetensors 对 float8 dtype 的支持;NPU 加载时按位 view 构造算子视图;
  • 元数据:orig_shape/orig_dtype/weight_only/block_size 随 checkpoint 往返;两种激活模式共用同一存储格式,加载端按 weight_only 分叉;
  • from_packed 校验:native vs 64 对齐网格(按模式)、scale 块数、dtype、前导形状;
  • 注册到通用 safetensors 子类体系,单文件与分片保存/加载 byte 逐位一致。

4. 配置 prepare-only

MXFP8LinearQATConfig / MXFP8GMMQATConfig 构造期拒绝 step='convert',报错指向 pack_checkpoint_weights;quantize_ handler 只做 prepare。原 Dense 冻结分支(convert_mxfp8_linear_qat_/freeze_weight_inplace/冻结标记)与 MoE convert 占位函数移除;先前试做的公开导出(convert_mxfp8_qat_weights/MXFP8Weight/quantize_mxfp8_weight)均未发布,直接删除、无迁移期。

【资料变更】

不涉及

【接口变更】

接口 变更 说明
pack_checkpoint_weights 新增 唯一训练后转换入口(state 原位替换、事务性、名单选择)
MXFP8DenseTensor / MXFP8GMMTensor 新增 可序列化推理权重对象(uint8 存储 + 模式元数据)
MXFP8LinearQATConfig / MXFP8GMMQATConfig 行为变更 构造期拒绝 step='convert' 并指向新入口
quantize_(..., step='convert') 移除 MXFP8 不再是有效调用(原 Dense 冻结 / MoE 报错路径删除)

【功能验证】

  • 测试环境:Ascend950DT(rack10os7,物理卡 1),CANN 9.3.0,torch 2.11.0+cpu,torch_npu 2.11.0.post3,Python 3.11。
  • CPU:test/unit/ 全量 396 passed, 2 skipped(1 个与本项目无关的既有失败已单独确认)。
  • NPU:92 passed——含打包存储与同设备 RCEIL kernel 的原始 byte 对照、设备端 prepared 模型驱动打包、磁盘保存/重载 byte 往返、填充 byte=255 → NaN 回归断言。
  • 验收:四模式(Dense/MoE × W8A16/W8A8)磁盘重建验收 24/24 通过(state key 集合不变、model 参数不被修改、Tensor 类型/模式元数据/orig_shape 恢复、native(96) vs 64 对齐(128) 网格、byte 逐位一致、未选中权重原样)。
  • 复现命令:
    python -m pytest test/unit/ -q
    ASCEND_RT_VISIBLE_DEVICES=<free card> python -m pytest \
        test/npu/test_mxfp8_qat_weight_conversion_npu.py \
        test/npu/test_mxfp8_qat_pack_entry_npu.py \
        test/npu/test_w8a16_qat_npu.py test/npu/test_w8a8_mxfp8_qat_npu.py \
        test/npu/test_w8a8_qat_npu.py -q
    
  • 新增/变更用例均有 UT 看护。

【CheckList】

likedislike
Merge blocked
weixuanweixuan
2 days ago Created pull request, commit e2cc4819
weixuanweixuan
2 days ago Associated issue: [Feature] 新增 W8A8 MXFP8 Linear QAT + 统一 MXFP8LinearQAT 类(weight_only 选模式)
ascend-robotascend-robotCollaborators
2 days ago Added label: stat/needs-squash
ascend-robotascend-robotCollaborators
2 days ago Added label: ascend-cla/yes
ascend-robot
ascend-robotCollaborators
2 days ago Comments:

Thanks for your pull-request.
The full list of commands accepted by me can be found at here.
You can get sig-info at here.
You can self-configure the PR merge rules for this repository. For more details, please refer to Here.


PR Approval Progress

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-Ascend/torchao-ascend ❌ (0/2)(You can also ask: xuyun15, lizhihai137, wangzhenwu, 楚浩田, chujinjin) ❌ (0/1)(You can also ask: liangsongwei, xuyun15, ffmh, wanglijun55, chujinjin)

💡 Tip:

  • Committer can comment /approve or /lgtm
  • Commenting /approve implies both code review (lgtm) and intent to merge (approve)

CLA Signature Pass

sylvia_wei, thanks for your pull request. All authors of the commits have signed the CLA. 👍

likedislike
55 messages are collapsed here。 See more
weixuanweixuan
9 hours ago Pre merge succeeded (commit_id: b67dd4d72b780b8a0af1c0fe2ae54fe9d0804576)
ascend-robotascend-robotCollaborators
9 hours ago Removed label: ascend-cla/yes
ascend-robotascend-robotCollaborators
9 hours ago Added label: ascend-cla/yes
weixuanweixuan
9 hours ago Change title to "feat(mxfp8_qat): checkpoint convert with serializable MXFP8 weights", originally titled "feat(mxfp8_qat): add read-only weight export (MXFP8 qdata + E8M0 scale)".
weixuanweixuan
9 hours ago Modify description of pull request