Closed
【Feature】精度比对工具支持自定义比对算法 #956
HowSir_XAdd on Aug 15Closed at Aug 18
【Feature】精度比对工具支持自定义比对算法 #956
Closed
HowSir_XAdd on Aug 15Closed at Aug 18
HowSir_X
HowSir_XCollaborators
Aug 15

【Feature】精度比对工具支持自定义比对算法

image.png

image.png

image.png

likedislike
The current Pull Request has been closed by HowSir_X.
atomgit-bot
atomgit-bot
Aug 15 Comments:

变更摘要

该 PR 围绕「精度比对工具支持自定义比对算法」这一目标,将原先硬编码在 npy_compare.py 中的精度指标计算逻辑(余弦相似度、欧氏距离、最大绝对误差、最大相对误差、千分之一/千分之五误差占比)抽离为独立的算法模块,并新增 AlgorithmManager 统一管理内置与自定义算法。比对结果列与表头由固定常量改为通过 LazyConst/LazyDict 懒加载,使结果列可随自定义算法动态生成;同时将相关张量计算从 numpy 迁移到 torch,并同步调整多进程结果组装、精度校验和可视化等下游逻辑。

主要改动

  • 新增算法管理器 AlgorithmManager:采用线程安全单例模式,扫描 builtin_algorithm 与 custom_algorithm 目录中符合 alg_([a-z0-9_]+)\.py 命名的模块,校验每个算法必须提供 column_name() 与三参数 compare() 函数,并通过统一的 compare 接口按列名批量执行算法。
  • 拆分内置算法模块:新增 alg_cosine_similarity.py、alg_euclidean_distance.py、alg_max_absolute_error.py、alg_max_relative_error.py、alg_one_thousand_error.py、alg_five_thousand_error.py 六个内置算法,分别实现 column_name() 与基于 torch 的 compare()。
  • 引入常量懒加载机制:在 CompareConst 中新增 LazyConst 与 LazyDict,将 ALL_COMPARE_INDEX、COMPARE_RESULT_HEADER、COMPARE_RESULT_HEADER_STACK、HEAD_OF_COMPARE_MODE 改为懒求值,使比对列与表头能够动态包含自定义算法列。
  • 重构 npy_compare.py 的比对流程:删除 TensorComparisonBasic、GetCosineSimilarity、GetEuclideanDistance、GetMaxAbsErr、CompareOps 等类,compare_ops_apply 改为调用 AlgorithmManager;handle_inf_nan、get_relative_err 与 ValidateTensor 由 numpy 实现改为 torch 实现。
  • 适配多进程与可视化等下游逻辑:ComparisonResult 由固定指标字段改为 results: Dict[str, List[str]] 按列名存储,multiprocessing_compute.py、acc_compare.py、msprobe_adapter.py 与 visualization/utils.py 等改为基于动态列名读写结果。
likedislike
Incorrect?
AtlasAccount
AtlasAccountCollaborators
Aug 15 Comments:
atomgit-bot
atomgit-bot
Aug 15 Comments:

代码审查

我已完成对全部 14 个变更文件的审查,并报告了 6 个问题。以下按文件确认审查结果:

  • python/msprobe/core/common/const.py — 有 3 个问题(P1 注解兼容性、P3 staticmethod、P3 LazyDict 协议)
  • python/msprobe/core/compare/acc_compare.py — 无问题(fill_cols 改为 ALL_COMPARE_INDEX.copy() 语义正确)
  • python/msprobe/core/compare/algorithm/__init__.py — 无问题(空文件)
  • python/msprobe/core/compare/algorithm/algorithm_manager.py — 无问题(单例、加载与校验逻辑审查未发现缺陷)
  • python/msprobe/core/compare/algorithm/builtin_algorithm/__init__.py — 无问题(空文件)
  • python/msprobe/core/compare/algorithm/builtin_algorithm/alg_cosine_similarity.py — 无问题
  • python/msprobe/core/compare/algorithm/builtin_algorithm/alg_euclidean_distance.py — 有 1 个问题(P3 张量直传 format_value)
  • python/msprobe/core/compare/algorithm/builtin_algorithm/alg_five_thousand_error.py — 无问题
  • python/msprobe/core/compare/algorithm/builtin_algorithm/alg_max_absolute_error.py — 无问题
  • python/msprobe/core/compare/algorithm/builtin_algorithm/alg_max_relative_error.py — 无问题
  • python/msprobe/core/compare/algorithm/builtin_algorithm/alg_one_thousand_error.py — 无问题
  • python/msprobe/core/compare/algorithm/custom_algorithm/__init__.py — 无问题(空文件)
  • python/msprobe/core/compare/multiprocessing_compute.py — 无问题(批量赋值与错误消息对齐逻辑审查未发现缺陷)
  • python/msprobe/core/compare/npy_compare.py — 有 2 个问题(P1 整型/bool 张量崩溃、P2 相对误差精度回归)

Closing Summary

  • 按优先级统计:P1 × 2、P2 × 1、P3 × 3(共 6 个发现)。
  • 总体风险判断:中等偏高。最需要优先处理的是 handle_inf_nan 对整数/bool 张量调用 torch.isinf/isnan 导致的运行时崩溃(P1),以及 const.py 新增 Any | None 注解缺少 from __future__ import annotations 在 Python<3.10 下的导入崩溃风险(P1)。其余为精度回归与若干低优先级健壮性/一致性问题。该变更整体实现了自定义比对算法能力,架构方向合理,但需修复上述正确性问题后方可稳定上线。

我已完成对所有 7 个变更文件的审查。以下为结论汇总。

审查结论

发现的问题(按优先级):

  • P1 ×1:core/compare/utils.py:24 无条件 import torch,破坏 MindSpore-only / 无 torch 环境的导入(CLI 与 MindSpore 比对路径均顶层导入该模块)。
  • P2 ×1:pytorch/compare/utils.py:44 read_pt_data 返回类型由 numpy 改为 torch tensor,但调用方 pt_compare.py 仍用 np.concatenate,产生混合类型/脆弱隐式转换。
  • P3 ×1:core/compare/utils.py:1256 get_relative_err 对零分母项错误地同时修改分子,且为无引用死代码。

各文件审查确认:

  • python/msprobe/core/compare/utils.py — 发现 P1、P3 问题(另含大量纯格式化重构,无逻辑变化)。
  • python/msprobe/core/config_check/ckpt_compare/metrics.py — 无问题(METRIC_FUNC 仍定义并被 ckpt_comparator.py 引用;移除的 CompareOps 无引用)。
  • python/msprobe/pytorch/compare/utils.py — 发现 P2 问题(返回类型契约变更)。
  • python/msprobe/visualization/builder/msprobe_adapter.py — 无问题(LazyConst/ALL_COMPARE_INDEX 用法与既有 const.py 模式一致,属有意延迟求值)。
  • python/msprobe/visualization/compare/mode_adapter.py — 无问题(str2float 不再使用,tips 置空为前端已不展示,属有意行为)。
  • python/msprobe/visualization/utils.py — 无问题(REAL_DATA_INDEX_LIST 改为 LazyConst 与自定义算法延迟求值意图一致)。
  • test/msprobe_test/core_ut/compare/test_acc_compare_npy_compare.py — 无问题(测试重写,导入的 CompareResult/ValidateTensor 等符号已存在)。

整体风险判断: 本变更引入自定义比对算法能力,核心逻辑重构方向合理,但存在一个中等偏高的回归风险——core 模块无条件依赖 torch,会让无 torch 环境(尤其纯 MindSpore 用户)的整个工具在导入阶段崩溃;建议合并前优先修复该导入问题,并同步 pt_compare.py 的拼接逻辑以消除返回类型不一致。

类型 数量
🔴 阻塞 2
🟡 建议 0

⛔ 需要修改

likedislike
Incorrect?
ascend-robotascend-robotCollaborators
Aug 15 Added label: stat/needs-squash
ascend-robotascend-robotCollaborators
Aug 15 Added label: ascend-cla/yes
423 messages are collapsed here。 See more
lv-kaimengCollaborators
Aug 18 Comments:

/lgtm

likedislike
louyujingCollaborators
Aug 18 Comments:

/lgtm

likedislike
ascend-robotascend-robotCollaborators
Aug 18 Added label: lgtm
wugengjunCollaborators
Aug 18 Comments:

/lgtm

likedislike
HowSir_XHowSir_XCollaborators
Aug 18 Close pull request