Graph-autofusion 的 Autofuse 组件通过 ASCIR、codegen 和 ATT 性能模型评估融合图代价。CastV2 是 v35 AscendC RegBase 路径中的基础类型转换算子。近期 Cast 性能建模修改围绕 CastExtend(dst, src, output_dims, output_stride, input_stride) 展开,使性能模型能够读取 codegen 阶段确定的原始 Cast 参数,并按真实 MicroAPI 路径估算 Cast、Store、Pack、UnPack、Interleave、DeInterleave、UpdateMask 等成本。
CastExtend(dst, src, output_dims, output_stride, input_stride)
本设计属于 Autofuse v35 ATT 性能评估路径,与 CANN Toolkit、AscendC RegBase API、ASCIR codegen 和 ATT 模型信息生成相关。它不新增 runtime/aclrt 调用,不改变运行包安装目录,不改变 Python 包导入方式。外部接口边界为仓内 C++ 性能建模接口和节点参数结构,未新增对用户可见的 Python/C API。
本需求实现以下能力:
kCast
kStore
-j 8
NodeInfo
dst
src
output_dims
output_stride
input_stride
Expr
TernaryOp
IfCase
PerfParamTableV2
整体数据流为:ASCIR CastV2 节点进入 codegen 后,CastV2ApiCall::Generate 根据 merge/loop 信息填充 Cast 节点参数;specific_params 在 ATT 解析阶段写入 NodeInfo.cast_node_params;CastPerf 从 NodeInfo 读取 dtype、dims、stride,计算 repeat 和 MicroAPI 次数;VfPerfUtils 通过普通单 dtype 表或输入输出 dtype mapping 表获取 latency/throughput;最终得到 VFHeadCost + max_latency + all_vf_instruct_cost 的 AIV_VEC 性能表达式。
CastV2ApiCall::Generate
specific_params
NodeInfo.cast_node_params
VfPerfUtils
VFHeadCost + max_latency + all_vf_instruct_cost
介绍
CastPerf 需要使用 codegen 阶段确定的 output_dims、output_strides 和 input_strides,避免在 ATT 中凭 shape 重新推导 CastExtend 实参。
output_strides
input_strides
输入
输入来自 CastV2 codegen 的 merge/loop 参数,包含输出维度、输出 stride 和输入 stride。输入中的表达式可以是静态常量,也可以是符号表达式。
处理
CastV2ApiCall::Generate 填充 Cast 节点参数,AscirNodeParams::specific_params 承载该参数,ATT 解析阶段写入 NodeInfo.cast_node_params。缺失或无效参数时 CastPerf 回退到 shape 乘积和默认 repeat 计算路径。
AscirNodeParams::specific_params
输出
NodeInfo 中新增可供 CastPerf 使用的 Cast 参数,性能模型可按实际 API 参数计算 repeat 和分支成本。
CastPerf 需要覆盖普通 Cast、同 bit integer Cast、B4/B8/B64 特殊转换、uint8 -> int64 interleave、int64 -> uint8 pack 等分支。
uint8 -> int64
int64 -> uint8
输入为 NodeDetail 中的输入输出 dtype、shape、Cast 参数和 PerfOutputInfo 中的 ternary op 容器。
NodeDetail
PerfOutputInfo
公式拆分为 repeat 计算、data copy 成本、特殊 Cast compute 成本和最终汇总。动态 stride 场景通过 TernaryOp 表达连续与跨步分支。同 bit integer Cast 只统计 DataCopy 和 UpdateMask 相关成本,不额外统计 Cast compute。
perf.pipe_res[PipeType::AIV_VEC] 写入 Cast 节点性能表达式,动态分支写入 perf.ternary_ops。
perf.pipe_res[PipeType::AIV_VEC]
perf.ternary_ops
单 dtype 查表无法表示 kCast、kStore 的输入输出 dtype 组合成本,需要通用结构支持 micro_api + input_dtype + output_dtype 查找 latency/throughput。
micro_api + input_dtype + output_dtype
输入为 MicroAPI 类型、input dtype、output dtype,以及 V2 性能表中配置的 VfInstructDtypeMappingPerf 条目。
VfInstructDtypeMappingPerf
查找顺序为精确 input/output、input/default、default/output、default/default。命中后返回表中 latency/throughput;未命中时保持 0。AddVfInstructDtypeMappingPerf 使用 Max 更新最大 latency,并累加 throughput * repeat_time。
0
AddVfInstructDtypeMappingPerf
Max
throughput * repeat_time
CastPerf 中 kCast、kStore 可按真实输入输出类型组合计算成本,默认回退策略保证表项缺失时不影响公式生成。
CastPerf 拆分为多个小 helper,每个 helper 对应一类 Cast 分支或 MicroAPI 组合。dtype mapping 表是通用结构,后续其他 MicroAPI 可复用,不需要为 Cast 再定义私有表类型。
CastPerf 可通过 ATT UT/ST 直接构造 NodeInfo、TensorShapeInfo 和 Cast 参数验证表达式结果。dtype mapping 默认和特例可通过不同 input/output dtype 组合验证。
TensorShapeInfo
本设计不依赖特定芯片型号字符串,不新增平台判断。依赖 C++17、现有 CMake、CANN Toolkit 和仓内 Autofuse v35 源码结构。
符号除法前增加除数非零断言。dtype mapping 查表未命中时按 0 计算,避免空表导致失败。新增结构使用值语义和 std::vector<std::string>,无手工资源释放。
std::vector<std::string>
super_kernel
libascendsk.so
修改 C++ 源文件和头文件会触发 ATT 相关目标增量编译,不新增 CMake 扫描或全量遍历逻辑。构建仍使用 -j 8 控制并行度。
本设计只影响编译期/建模期的性能表达式计算,不影响实际 runtime 调度或 kernel launch。dtype mapping 查表是小规模 vector 线性扫描,对 ATT 性能模型总耗时影响可忽略。
新增少量 std::map、std::vector 和字符串常量,常驻内存和动态库大小变化很小。未新增大数组、缓存或运行包产物。
std::map
std::vector
新增内部数据结构:
struct VfInstructDtypeMappingPerf { std::vector<std::string> input_dtypes; std::vector<std::string> output_dtypes; int32_t latency{0}; int32_t throughput{0}; };
新增内部虚接口:
virtual const std::vector<VfInstructDtypeMappingPerf> &GetVfInstructDtypeMappingPerfTable( const std::string µ_api_type) const;
新增工具函数:
static af::Status GetVfInstructDtypeMappingPerf(const std::string &vf_instruct_type, const std::string &input_dtype, const std::string &output_dtype, Expr &latency, Expr &throughput); static af::Status AddVfInstructDtypeMappingPerf(const std::string &vf_instruct_type, const std::string &input_dtype, const std::string &output_dtype, Expr &latency, Expr &throughput, Expr repeat_time);
这些接口均为 Autofuse 内部 C++ 接口,不作为外部 ABI/API 承诺。
CastNodeParams
AscirNodeParams
PerfParamTableV2::vf_instruct_type_2_dtype_mapping_api_perf_
Cast repeat 算法根据参数有效性分为 shape 乘积回退、一维输出、动态连续/跨步分支三类。dtype mapping 查表按精确到默认的顺序回退,保证特例优先、默认兜底。
主流程:CastV2 codegen 填参 -> ATT 解析写入 NodeInfo -> CastPerf 计算 repeat -> 按分支累加 MicroAPI 成本 -> 写入 AIV_VEC 性能表达式。异常流程:Cast 参数缺失走 shape 回退;除数为 0 返回失败;mapping 未命中按 0 成本继续生成表达式。
autofuse/v35/codegen/reg_api_call/
autofuse/common/ascir_node_param/
autofuse/att/gen_model_info/parser/
autofuse/v35/att/api_perf_register/
autofuse/att/gen_model_info/api_perf_register/utils/
autofuse/att/base/
本设计不新增文件、内存、runtime 或设备资源申请。编译失败由 CMake/编译器报告;性能模型中的断言沿用 GE_ASSERT_* 返回错误状态。
GE_ASSERT_*
Cast 参数缺失或无效时使用回退路径;除数为 0 时返回失败并打印表达式;mapping 查表未命中时按 0 成本处理,避免表项未覆盖导致性能模型构造失败。
本设计不影响脚本参数、配置文件、安装目录、动态库文件名、Python 包、旧 run 包布局或用户代码。内部虚接口和结构体变更需要相关 Autofuse 目标同步编译,不支持新旧对象文件混用。
测试入口为 ATT UT/ST 中 CastPerf 表达式用例。测试出口为 perf.pipe_res[PipeType::AIV_VEC] 和动态 ternary_ops 表达式。测试打桩使用现有 ATT 测试框架和 runtime/model info stub。
ternary_ops
float32 -> float16
Str(res)
int8 -> uint8
one_rep_size
.o
uint8/int64
cmake --build build --target att -j 8
cmake --build build --target att_ut -j 8
cross_feature_check.md
总体概述
软件概述
项目介绍
Graph-autofusion 的 Autofuse 组件通过 ASCIR、codegen 和 ATT 性能模型评估融合图代价。CastV2 是 v35 AscendC RegBase 路径中的基础类型转换算子。近期 Cast 性能建模修改围绕
CastExtend(dst, src, output_dims, output_stride, input_stride)展开,使性能模型能够读取 codegen 阶段确定的原始 Cast 参数,并按真实 MicroAPI 路径估算 Cast、Store、Pack、UnPack、Interleave、DeInterleave、UpdateMask 等成本。产品环境介绍
本设计属于 Autofuse v35 ATT 性能评估路径,与 CANN Toolkit、AscendC RegBase API、ASCIR codegen 和 ATT 模型信息生成相关。它不新增 runtime/aclrt 调用,不改变运行包安装目录,不改变 Python 包导入方式。外部接口边界为仓内 C++ 性能建模接口和节点参数结构,未新增对用户可见的 Python/C API。
软件功能
本需求实现以下能力:
kCast、kStore能使用输入输出 dtype 映射表计算成本。设计约束
-j 8,避免 Autofuse 编译 OOM。NodeInfo中的参数必须来自 codegen/merge 阶段原始表达式,不保存 tiler 展开字符串。假设和依赖关系
dst、src、output_dims、output_stride、input_stride。NodeInfo获取 Cast 节点输入输出 dtype、shape 和 codegen 透传参数。Expr、TernaryOp、IfCase等符号表达式能力能够表达动态 repeat 分支。PerfParamTableV2是 v35 CastPerf 使用的 V2 性能表来源。需求分析与设计
整体介绍
整体数据流为:ASCIR CastV2 节点进入 codegen 后,
CastV2ApiCall::Generate根据 merge/loop 信息填充 Cast 节点参数;specific_params在 ATT 解析阶段写入NodeInfo.cast_node_params;CastPerf 从NodeInfo读取 dtype、dims、stride,计算 repeat 和 MicroAPI 次数;VfPerfUtils通过普通单 dtype 表或输入输出 dtype mapping 表获取 latency/throughput;最终得到VFHeadCost + max_latency + all_vf_instruct_cost的 AIV_VEC 性能表达式。功能需求
功能需求 1:CastV2 参数透传
介绍
CastPerf 需要使用 codegen 阶段确定的
output_dims、output_strides和input_strides,避免在 ATT 中凭 shape 重新推导 CastExtend 实参。输入
输入来自 CastV2 codegen 的 merge/loop 参数,包含输出维度、输出 stride 和输入 stride。输入中的表达式可以是静态常量,也可以是符号表达式。
处理
CastV2ApiCall::Generate填充 Cast 节点参数,AscirNodeParams::specific_params承载该参数,ATT 解析阶段写入NodeInfo.cast_node_params。缺失或无效参数时 CastPerf 回退到 shape 乘积和默认 repeat 计算路径。输出
NodeInfo中新增可供 CastPerf 使用的 Cast 参数,性能模型可按实际 API 参数计算 repeat 和分支成本。功能需求 2:CastPerf 公式重构与特殊路径建模
介绍
CastPerf 需要覆盖普通 Cast、同 bit integer Cast、B4/B8/B64 特殊转换、
uint8 -> int64interleave、int64 -> uint8pack 等分支。输入
输入为
NodeDetail中的输入输出 dtype、shape、Cast 参数和PerfOutputInfo中的 ternary op 容器。处理
公式拆分为 repeat 计算、data copy 成本、特殊 Cast compute 成本和最终汇总。动态 stride 场景通过
TernaryOp表达连续与跨步分支。同 bit integer Cast 只统计 DataCopy 和 UpdateMask 相关成本,不额外统计 Cast compute。输出
perf.pipe_res[PipeType::AIV_VEC]写入 Cast 节点性能表达式,动态分支写入perf.ternary_ops。功能需求 3:输入输出 dtype mapping 性能表
介绍
单 dtype 查表无法表示
kCast、kStore的输入输出 dtype 组合成本,需要通用结构支持micro_api + input_dtype + output_dtype查找 latency/throughput。输入
输入为 MicroAPI 类型、input dtype、output dtype,以及 V2 性能表中配置的
VfInstructDtypeMappingPerf条目。处理
查找顺序为精确 input/output、input/default、default/output、default/default。命中后返回表中 latency/throughput;未命中时保持
0。AddVfInstructDtypeMappingPerf使用Max更新最大 latency,并累加throughput * repeat_time。输出
CastPerf 中
kCast、kStore可按真实输入输出类型组合计算成本,默认回退策略保证表项缺失时不影响公式生成。非功能需求
可维护性
CastPerf 拆分为多个小 helper,每个 helper 对应一类 Cast 分支或 MicroAPI 组合。dtype mapping 表是通用结构,后续其他 MicroAPI 可复用,不需要为 Cast 再定义私有表类型。
可测试性
CastPerf 可通过 ATT UT/ST 直接构造
NodeInfo、TensorShapeInfo和 Cast 参数验证表达式结果。dtype mapping 默认和特例可通过不同 input/output dtype 组合验证。可移植性
本设计不依赖特定芯片型号字符串,不新增平台判断。依赖 C++17、现有 CMake、CANN Toolkit 和仓内 Autofuse v35 源码结构。
可靠性
符号除法前增加除数非零断言。dtype mapping 查表未命中时按
0计算,避免空表导致失败。新增结构使用值语义和std::vector<std::string>,无手工资源释放。特性交叉影响
super_kernelPython 包、选项解析、pytest 或 wheel 内容。libascendsk.so、AOT、RDV 或 SuperKernel ABI/API。性能
编译时长
修改 C++ 源文件和头文件会触发 ATT 相关目标增量编译,不新增 CMake 扫描或全量遍历逻辑。构建仍使用
-j 8控制并行度。执行性能
本设计只影响编译期/建模期的性能表达式计算,不影响实际 runtime 调度或 kernel launch。dtype mapping 查表是小规模 vector 线性扫描,对 ATT 性能模型总耗时影响可忽略。
内存和产物大小
新增少量
std::map、std::vector和字符串常量,常驻内存和动态库大小变化很小。未新增大数组、缓存或运行包产物。接口设计
新增/修改接口描述
新增内部数据结构:
struct VfInstructDtypeMappingPerf { std::vector<std::string> input_dtypes; std::vector<std::string> output_dtypes; int32_t latency{0}; int32_t throughput{0}; };新增内部虚接口:
virtual const std::vector<VfInstructDtypeMappingPerf> &GetVfInstructDtypeMappingPerfTable( const std::string µ_api_type) const;新增工具函数:
static af::Status GetVfInstructDtypeMappingPerf(const std::string &vf_instruct_type, const std::string &input_dtype, const std::string &output_dtype, Expr &latency, Expr &throughput); static af::Status AddVfInstructDtypeMappingPerf(const std::string &vf_instruct_type, const std::string &input_dtype, const std::string &output_dtype, Expr &latency, Expr &throughput, Expr repeat_time);这些接口均为 Autofuse 内部 C++ 接口,不作为外部 ABI/API 承诺。
接口检查项
软件设计
关键数据结构
CastNodeParams:承载 CastExtend 原始维度和 stride 参数,生命周期随AscirNodeParams/NodeInfo。VfInstructDtypeMappingPerf:承载一个 MicroAPI 的输入 dtype 集、输出 dtype 集、latency 和 throughput。PerfParamTableV2::vf_instruct_type_2_dtype_mapping_api_perf_:缓存 V2 dtype mapping 性能表。关键技术/算法
Cast repeat 算法根据参数有效性分为 shape 乘积回退、一维输出、动态连续/跨步分支三类。dtype mapping 查表按精确到默认的顺序回退,保证特例优先、默认兜底。
流程设计
主流程:CastV2 codegen 填参 -> ATT 解析写入
NodeInfo-> CastPerf 计算 repeat -> 按分支累加 MicroAPI 成本 -> 写入 AIV_VEC 性能表达式。异常流程:Cast 参数缺失走 shape 回退;除数为 0 返回失败;mapping 未命中按 0 成本继续生成表达式。对子模块的修改
autofuse/v35/codegen/reg_api_call/:CastV2 codegen 填充 Cast 参数。autofuse/common/ascir_node_param/:新增或扩展 Cast 节点参数承载能力。autofuse/att/gen_model_info/parser/:将 Cast specific params 写入NodeInfo。autofuse/v35/att/api_perf_register/:重构 CastPerf 公式,扩展 V2 性能表。autofuse/att/gen_model_info/api_perf_register/utils/:新增 dtype mapping 查表工具。autofuse/att/base/:新增通用 dtype mapping 结构和默认字符串常量。错误处理
系统错误
本设计不新增文件、内存、runtime 或设备资源申请。编译失败由 CMake/编译器报告;性能模型中的断言沿用
GE_ASSERT_*返回错误状态。接口错误
Cast 参数缺失或无效时使用回退路径;除数为 0 时返回失败并打印表达式;mapping 查表未命中时按 0 成本处理,避免表项未覆盖导致性能模型构造失败。
安全检查
Expr符号计算。兼容性检查
本设计不影响脚本参数、配置文件、安装目录、动态库文件名、Python 包、旧 run 包布局或用户代码。内部虚接口和结构体变更需要相关 Autofuse 目标同步编译,不支持新旧对象文件混用。
测试设计
测试边界
测试入口为 ATT UT/ST 中 CastPerf 表达式用例。测试出口为
perf.pipe_res[PipeType::AIV_VEC]和动态ternary_ops表达式。测试打桩使用现有 ATT 测试框架和 runtime/model info stub。测试用例设计
float32 -> float16表达式Str(res)int8 -> uint8Cast 参数,比较表达式uint8 -> int64store mapping 特例int64 -> uint8pack 分支one_rep_size用例验证正常路径,非法路径由断言保护.o验收标准
NodeInfo。uint8/int64特殊路径生成可解释性能表达式。kCast、kStore能按输入输出 dtype mapping 查表,默认回退行为符合设计。cmake --build build --target att -j 8和cmake --build build --target att_ut -j 8通过。设计文档检查结果