已开启
feat: 支持量化结果持久化与恢复第一里程碑 #267
feat: 支持量化结果持久化与恢复第一里程碑 #267
已开启
sunshine750创建于 17 天前
sunshine750
17 天前

描述

本 PR 完成 Classic 量化结果持久化与恢复的第一里程碑,范围仅包含 Classic API,不包含 Qwen3.8、LLM CLI、benchmark、模型文件或样例改动。

原有 quantize(model, config) 只把校准结果保留在当前进程的量化模块中,进程结束后无法复用。本次为 quantize()convert() 增加可选的 quant_result_dir

  • quant_result_dir=None 时沿用原有内存量化和转换流程,convert(model) 的返回值仍为 None
  • 指定调用方已经创建的目录时,各量化模块在参数真正定稿的位置导出结果;MinMax、AWQ、GPTQ 分别保存其恢复和部署所需的参数。
  • 每层张量使用 safetensors 保存,完整配置集中写入 quant_manifest.json。文件大小、SHA-256、header、tensor key/dtype/shape 和 entry digest 在发布前重新校验。
  • 每个单元先通过同目录临时文件、fsyncos.replace 原子落盘;manifest 最后发布。这样中断运行只会留下没有 manifest 的未发布目录,不会被误当成完整产物。
  • convert(model, quant_result_dir) 先校验 manifest、目录、源模型指纹和全部单元,再从注册表反查量化类并恢复校准态,最后复用既有 deploy 转换流程。
  • reload 采用“全部校验并在深拷贝模块上构建,再统一替换”的两阶段方式;任意单元失败都不会留下部分改写的输入模型,提交阶段异常也会回滚。

这里没有按文件名扫描目录或从 artifact 动态导入类。文件名不能表达完整量化配置,也无法判断目录是否完整;动态导入则会扩大不可信输入的执行面。manifest 是唯一配置源,量化类只能从现有 AlgorithmRegistry 反查。

本次同时落实了评审意见:生产代码使用最终格式命名;目录必须由调用方创建且允许无关文件共存;writer 只管理自己的文件并保持内部实现属性最小化;指纹参数统一命名为 model_fingerprint;不在 reload 中重复 convert() 已负责的模型入口检查;仅暴露真正稳定的用户接口。

额外审查补齐了算法状态完整性校验:MinMax 根据渐进量化、激活量化和对称配置检查必需 scale/offset;AWQ、GPTQ 在非对称配置下检查 offset_w;GPTQ 必须携带不可重算的优化后 weight。校验发生在事务提交前,避免结构合法但算法参数不完整的 artifact 被加载。

如何测试

  1. 聚焦测试:

    pytest -q \
      tests/unit_test/classic/test_quant_result.py \
      tests/unit_test/classic/ptq/test_quantize.py \
      tests/unit_test/classic/test_quantize_modules.py \
      tests/unit_test/classic/test_quant_reload.py \
      tests/unit_test/algorithms/test_register_algo.py \
      tests/amct_pytorch/test_config.py \
      tests/unit_test/classic/test_deploy_kv_quant.py
    

    结果:466 passed。包含 MinMax、AWQ、GPTQ 的 artifact 往返、事务回滚、格式/完整性负例和真实 NPU deploy-forward 对照。

  2. 全量回归对照:

    pytest -q tests/unit_test tests/amct_pytorch \
      --continue-on-collection-errors --tb=short
    

    最新 master:48 failed / 1510 passed / 7 skipped / 130 errors。

    本分支:48 failed / 1868 passed / 17 skipped / 130 errors。

    两侧 JUnit 中 178 个 failed/error 测试 ID 集合完全一致,本分支无新增失败;现有失败来自环境缺少可选依赖及上游既有用例。

  3. 工程检查:pre-commit 全部通过,包含 ruff、format、codespell、私钥检测和 OAT 开源合规检查;git diff --check 通过。

  4. 构建:bash build.sh --torch 成功生成 amct_pytorch-1.1.0-py3-none-linux_aarch64.tar.gz

文档更新

无。协议字段、行为边界和兼容性说明已写入新增模块及公开 API 的 docstring;本 PR 不引入独立用户文档,避免带入第一里程碑以外的内容。

类型标签

likedislike
合并受阻
Ssunshine750
17 天前 创建了 pull request,commit 236b2933
atomgit-bot
atomgit-bot
17 天前 评论:

🤖 正在生成合并请求摘要,请稍候…

likedislike
不准确?
atomgit-bot
atomgit-bot
17 天前 评论:

🤖 AI 代码检视正在进行中,请稍候…

likedislike
不准确?
CANN-robotCANN-robot成员
17 天前 添加了label:cann-cla/yes
CANN-robot
CANN-robot成员
17 天前 评论:

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.
For more, you also can visit HICANN.


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-cann/amct ❌ (0/2)(You can also ask: wulinyu, fujun19, 金瑛琪, sophia1213, 姚广修) ❌ (0/1)(You can also ask: wulinyu, 梁横溢, sophia1213, 姚广修, li_ting)

💡 Tip:

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

CLA Signature Pass

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

likedislike
此处折叠了124条消息 查看更多
Ssunshine750
5 天前 update merge request[project id: 8782573, iid: 267, commit_id: 8c4d5a20276af6837f035caeb047f270b8b46311] virtual merging success
Ssunshine750
5 天前 推送  1 个提交:166445ec-Revert "docs: publish week1-4 requirement documents"
Ssunshine750
5 天前 update merge request[project id: 8782573, iid: 267, commit_id: 700837c8bad11a677519b2da3416195733383c61] virtual merging success
CANN-robotCANN-robot成员
5 天前 删除了label:cann-cla/yes
CANN-robotCANN-robot成员
5 天前 添加了label:cann-cla/yes