Merged
feat: 优化 Cast 性能计算 #1266
feat: 优化 Cast 性能计算 #1266
Merged
gaoxinAdd on Jul 13
gaoxin
gaoxinCollaborators
Jul 13

Pull Request

描述

主要解决问题

  • CastPerf 需要覆盖更多 CastExtend 实际执行路径,但原性能模型对 Cast 参数、循环次数、stride 分支和特殊类型转换路径的表达不完整,难以准确建模 int4、int64/uint8、同 bit 整数转换等场景。
  • 旧的性能表查询主要按单 dtype 获取 latency/throughput,无法表达 kCast、kStore 这类同时依赖输入 dtype 和输出 dtype 的性能差异,例如 uint8 -> int64 store 成本和 float16/bfloat16/float32 Cast 成本。
  • CastV2 codegen 与 ATT 参数链路需要把 CastExtend(dst, src, output_dims, output_stride, input_stride) 的原始参数传递到性能模型,避免 ATT 侧凭 shape 反推 codegen 已确定的参数。

修改方案

  • 为 CastV2 增加参数透传链路:定义并构建 Cast 节点参数,在 CastV2ApiCall::Generate 中填充 output_dims、output_strides、input_strides,并通过 specific_params 写入 NodeInfo,供 CastPerf 直接使用。
  • 重构 CastPerf 公式实现:拆分 repeat 计算、data copy 成本、B4/B8/B64 特殊转换成本、同 bit integer 转换成本和特殊 Cast compute 成本,降低单函数复杂度并让每个分支更贴近实际 MicroAPI 路径。
  • 新增通用 VfInstructDtypeMappingPerf 结构和 VfPerfUtils::Get/AddVfInstructDtypeMappingPerf,支持按 micro_api + input_dtype + output_dtype 查表,并按 input/default、default/output、default/default 逐级回退,仍未命中时按 0 计算。
  • 在 V2 性能表中为 kCast、kStore 配置输入输出 dtype 映射,并将 CastPerf 中对应 kCast、kStore 成本切换到新的 dtype mapping 查询接口。
  • 扩展 Cast 相关常量、性能注册和性能建模说明,补充 Cast 类型支持和公式建模约束。

变更类型

请选择本次引入的变更类型:

关联的Issue

如何测试

  1. 测试cast不同场景分支的性能计算

核对清单

其他信息

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for gaoxin's contribution.)
gaoxingaoxinCollaborators
Jul 13 Created pull request, commit 9a93e365
atomgit-bot
atomgit-bot
Jul 13 Comments:

变更摘要

此 PR 主要优化了 Autofuse Cast 算子的性能计算逻辑。核心思路是新增 CastNodeParams 参数结构体,将其注入到从图解析、参数注册、API 调用生成到性能计算的完整链路中,使 Cast 算子的性能评估不再依赖简单的输入维度乘积估算,而是能结合合轴信息、stride 步长和调用次数等精确参数,并针对不同数据类型转换场景(如同位宽整数转换、B8/B64 转换等)建模对应的微指令开销,从而显著提升 Cast 性能计算的准确度。

主要改动

  • 新增 CastNodeParams 结构体:在 ascir_node_param.h 中定义 CastNodeParams,包含 valid、is_single_call、outer_repeats、cal_count、input_second_to_last_stride、output_second_to_last_stride 等字段,并加入 AnySpecificParams variant 联合体,为 Cast 算子提供专用的参数存储载体。

  • 在 NodeDetail 和 NodeInfo 中挂载 cast_node_params:在 api_perf.h 的 NodeDetail 和 tuning_space.h 的 NodeInfo 中分别添加 cast_node_params 字段,并在 ascir_api_perf_v2.cpp 的 CastApi 中将 node.cast_node_params 传递给 NodeDetail,打通参数从上层到下层的传递路径。

  • 补充 Cast 参数的构建与注册:在 specific_params_builder.cpp 中新增 FillCastParams 函数从 AscirNodeParams 提取参数;在 ascir_param_builder.cpp 中新增 RegisterCastAscirNodeParams 为 Cast 节点注册初始参数;在 cast_v2_api_call.cpp 的 CastV2ApiCall::Generate 中新增 FillCastNodeParams,利用合轴信息 merge_info 和循环参数 param 回填 CastNodeParams 的详细字段。

  • 重写 CastPerf 性能计算逻辑:在 ascendc_regbase_perf.cpp 中新增 GetCastOneRepeatSize、GetCastRepeatTime、GetSameBitCastRepeatTime、GetCastCallCount 等辅助函数,以及 IsSameBitIntegerCast、IsB8Cast、IsB64Cast、IsB64TransferCast 等类型判断函数;CastPerf 重构为分类型计算 repeat_time 并根据不同转换组合建模对应的 VectorFunc 微指令开销(如 kUpdateMask、kCast、kDuplicate、kAnd/kOr/kAdds 及多种 Pack/Unpack 操作)。

likedislike
Incorrect?
atomgit-bot
atomgit-bot
Jul 13 Comments:

代码审查

审查总结

我已经逐文件审查了所有 10 个变更文件,以下是各文件的审查结果:

文件 结果
.claude/skills/.gitignore 无问题 — 正确添加了 af-perf-modeler/ 白名单条目
.claude/skills/af-perf-modeler/SKILL.md 无问题 — 新增的建模指南文档,内容合理,无安全/配置风险
autofuse/att/gen_model_info/api_perf_register/api_perf.h 无问题 — 正确添加了 CastNodeParams 成员字段和必要的 include
autofuse/att/gen_model_info/parser/specific_params_builder.cpp 无问题 — 正确添加了 FillCastParams 函数和 kCast 分支处理
autofuse/att/gen_model_info/parser/tuning_space.h 无问题 — 正确添加了 cast_node_params 成员字段
autofuse/common/ascir_node_param/ascir_node_param.h 2 个 P3 问题 — is_single_call、input_strides_size/output_strides_size 仅写入未读取(死代码)
autofuse/common/ascir_node_param/ascir_param_builder.cpp 无问题 — 正确添加了 RegisterCastAscirNodeParams 和 kCast 匹配逻辑
autofuse/v35/att/api_perf_register/ascendc_regbase_perf.cpp 1 个 P2 问题 — GetSameBitCastRepeatTime 未区分 contiguous/strided 场景
autofuse/v35/att/api_perf_register/ascir_api_perf_v2.cpp 无问题 — 正确透传 cast_node_params 到 NodeDetail
autofuse/v35/codegen/reg_api_call/cast_v2_api_call.cpp 无问题 — 正确实现了 FillCastNodeParams 参数填充逻辑

汇总

  • P0: 0
  • P1: 0
  • P2: 1(GetSameBitCastRepeatTime 可能低估性能)
  • P3: 2(CastNodeParams 中的死代码字段)

总体风险判断:中等偏低。核心参数传递链路(Enrich → Generate → FillSpecificParams → CastPerf)设计合理,代码结构清晰。主要的 P2 问题涉及性能公式建模的准确性,需要确认 AscendC API 源码的 contiguous/strided 行为后决定是否需要修复。P3 的死代码问题不影响功能但建议清理。

⚠️ 已识别出整体风险,但无法提取行内评论,请参考整体评估。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 13 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 13 Comments:

CLA Signature Pass

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

likedislike
249 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Jul 27 Added label: lgtm
zhanj
zhanjCollaborators
Jul 27 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Jul 27 Added label: approved
CANN-robotCANN-robotCollaborators
Jul 27 Closed associated issue
CANN-robotCANN-robotCollaborators
Jul 27 Merged pull request