Closed
【Feature】精度比对工具支持自定义比对算法 #956
HowSir_XAdd on Aug 15Closed at Aug 18
【Feature】精度比对工具支持自定义比对算法 #956
Closed
Merged fromHowSir_X/msprobemscompare_refactorintoAscend/msprobemaster
The current Pull Request has been closed by HowSir_X.
atomgit-bot
Aug 15 Comments:
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等改为基于动态列名读写结果。


Incorrect?
AtlasAccount
Aug 15 Comments:
Aug 15 Comments:
atomgit-bot
Aug 15 Comments:
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:44read_pt_data返回类型由 numpy 改为 torch tensor,但调用方pt_compare.py仍用np.concatenate,产生混合类型/脆弱隐式转换。 - P3 ×1:
core/compare/utils.py:1256get_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 |
⛔ 需要修改


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


Aug 18 Added label: lgtm
Aug 18 Close pull request
【Feature】精度比对工具支持自定义比对算法