Merged
feat: 新增 FP8 PTQ 精度评测(对齐上游 lm_eval 流程) #159
feat: 新增 FP8 PTQ 精度评测(对齐上游 lm_eval 流程) #159
Merged
Chun LiAdd on 4 days ago
Chun Li
Chun Li
4 days ago

【合入来源】

如有社区issue,请关联issue链接
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

新增 benchmarks/quantization/ FP8 PTQ 精度评测 benchmark,对齐上游 torchao benchmarks/quantization/measure_accuracy_and_performance.sh 的精度评测流程(lm_eval),用于验证 torchao FP8 PTQ recipe 在 NPU 上的精度:

  1. 文件组成(6 个文件 + .gitignore 两行):
    • measure_accuracy_and_performance.sh:orchestrator,按 recipe(None 基线 / float8_rowwise)循环 create → eval → parse,日志开头记录 torch/torch_npu/torchao/torchao_ascend/transformers 版本;
    • create_quantized_model.py:加载 bf16 模型 → quantize_ 量化 → 落盘;
    • eval_accuracy.py:lm_eval 精度评测(支持 --ckpt_dir 直载或 --model_path+--recipe 进程内量化);
    • utils.py:recipe 映射 / 模型加载 / 量化,import torch_npu、torchao_ascend 激活 NPU 补丁;
    • parse_log.py:解析日志输出精度汇总表(含 ckpt 体积列、库版本头);
    • README.md :使用说明与依赖。
  2. 落盘格式对齐上游:pytorch_model.bin(pickle 的 Float8Tensor 子类权重)+ config.json(quantization_config)+ tokenizer;from_pretrained 直载时 transformers quantizer 触发的 quantize_ 对已量子类权重为 no-op(torchao 显式行为),量化值零重算。
  3. 与上游代码的 3 处差异均为新版 transformers(实测 5.21.0)+ wrapper 子类(无 storage)所致,非 NPU 特有(CUDA 同版本同样触发;上游 dev-requirements 不钉 transformers 版本且 benchmark 脚本不进 CI,该路径在上游未被 transformers 5.x 覆盖),已在 create_quantized_model.py 内 Gap 1/2/3 注释逐条说明:①量化在加载后手动执行(该版本 quantize-at-load 流程被 loading report raise);②torch.save 直写 state_dict(save_pretrained 的 tied-weights 清理对无 storage 的 wrapper 子类崩溃);③保存的 config 中 tie_word_embeddings=False(tie_weights 用 aten.equal 比较,Float8Tensor 未实现)。
  4. NPU 适配点:量化必须在 NPU 设备上执行(torchao_ascend from_hp 补丁路由到 npu_dynamic_quant,无 CPU 后端);lm_eval 设备须写带索引的 npu:x(valid-device 列表无裸 npu);ASCEND_RT_VISIBLE_DEVICES 选卡。
  5. .gitignore 新增 benchmarks/data(运行产物,镜像上游);运行依赖 lm-eval==0.4.13(见 README.md)。

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写"不涉及"

不涉及(新增仓内 benchmarks/quantization/README.md 作为使用说明,无对外资料变更)。

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写"不涉及"

不涉及。

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

在 950DT 单卡上按 README 用法实测(lm_eval 任务 wikitext + winogrande,对照物为同会话 bf16 基线):

  1. Qwen2.5-7B-Instruct,limit 500:

    recipe winogrande acc wikitext word_ppl bits_per_byte ckpt 大小
    None(bf16) 0.740(±0.020) 9.404 0.605 15.24 GB
    float8_rowwise 0.744(±0.020) 9.643 0.611 8.18 GB

    精度持平(acc 在 stderr 内;ppl Δ +2.5% word / +1.1% bits);权重体积 0.54x。与上游 inference.md 的 Llama-3.1-8B 结论同量级(上游 Δppl +1.2%、winogrande 持平)。

  2. 量化 ckpt 落盘往返验证:from_pretrained 直载与进程内量化模型同输入 logits 逐位一致(diff 0.0),确认零重量化。

  3. Qwen2.5-0.5B 全链冒烟(create 落盘 → ckpt 直载 lm_eval → 汇总表输出),exit 0;跨卡复现一致(卡 2 与卡 4 word_ppl 均为 18.6339);另按 README 手动复测通过。

  4. 任务数据离线验证:数据集缓存后 HF_HUB_OFFLINE=1 可完全离线运行。

测试命令(详见 README):

MODEL_ID=/path/to/Qwen2.5-7B LIMIT=500 \
  bash benchmarks/quantization/measure_accuracy_and_performance.sh

本次为 benchmark 脚本新增(不改库代码),未新增 UT 用例,以真实模型实测验证(如上);

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request has been successfully merged by ascend-robot.
(Thank you for Chun Li's contribution.)
Chun LiChun Li
4 days ago Created pull request, commit d69954f3
ascend-robotascend-robotCollaborators
4 days ago Added label: ascend-cla/yes
ascend-robot
ascend-robotCollaborators
4 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

✅ Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-Ascend/torchao-ascend ✅ culechan, wanglijun55 (2/2) ✅ wanglijun55 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
Chun LiChun Li
4 days ago Associated issue: 新增 FP8 PTQ 精度评测基准(950DT,对齐上游 lm_eval 流程)
Chun Li
Chun Li
4 days ago Comments:

compile

likedislike
42 messages are collapsed here。 See more
wanglijun55wanglijun55Collaborators
4 days ago Resolved final issue
wanglijun55
wanglijun55Collaborators
4 days ago Comments:

/approve

likedislike
ascend-robotascend-robotCollaborators
4 days ago Added label: approvedlgtm
ascend-robotascend-robotCollaborators
4 days ago Merged pull request
ascend-robot
ascend-robotCollaborators
4 days ago Comments:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike