已合并
补充tla.mmad中hf32模式支持 #1104
init__zhb__创建于 25 天前
补充tla.mmad中hf32模式支持 #1104
已合并
init__zhb__创建于 25 天前
init__zhb__成员
25 天前

描述

  • 补充tla.mmad中use_hf32参数,使用示例:
# 注意hf32模式需在A, B均为f32时启用
# 可支持的 hf32 模式:
# ``tla.params.Hf32Mode.HF32_NEAREST_ZERO``
# ``tla.params.Hf32Mode.HF32_NEAREST_EVEN``
if tla.const_expr(hf32_mode != tla.params.Hf32Mode.HF32_DISABLE and dtype_a == tla.Float32 and dtype_b == tla.Float32):
   tla.mmad(l0_c, l0_a, l0_b, init_c=init_c, unit_flag=unit_flag, hf32_mode=hf32_mode)
else:
   tla.mmad(l0_c, l0_a, l0_b, init_c=init_c, unit_flag=unit_flag)
  • 补充hf32精度测试:
  1. to_hf32模拟尾数截断;
  2. 精度比较使用混合容差,阈值参考来自:生态算子开源精度标准

关联的Issue

原因

测试

  • lit, IR测试
$ lit -sv csrc/mlir/build/tests/lit --filter=mmad-hf32-mode-verify
$ llvm-lit -sv csrc/mlir/build/tests/lit/tla-compile/mmad-hf32.mlir
$ python -m pytest -q tests/test_frontend_lowering.py  -k hf32
  • end_to_end 测试
  1. RTZ(Round to Zero)
(catlass-pacr) xx@xxxx:~/catlass/python/tla_dsl$ python examples/end_to_end/basic_mmad/basic_matmul.py --device 1 --dtype-a f32 --dtype-b f32 --dtype-c f32
--- mnk=(256,512,1024) layout=row/row dtype=f32/f32/f32 ---
tensor([[ 421.2704,   70.1035,   81.0321,  ..., -161.6765, -442.6078,
         -425.0583],
        ...,
        [-113.9693, -272.3091,  294.2283,  ...,  180.0958, -498.2363,
          184.6163]])
tensor([[ 421.2694,   70.1012,   81.0294,  ..., -161.6766, -442.6080,
         -425.0576],
        ...,
        [-113.9658, -272.3072,  294.2266,  ...,  180.0937, -498.2347,
          184.6140]])
passed=True cache_key=xxx
kernel.o=/xxx/kernel.o
  1. RTE(Round to Even)
(catlass-pacr) xx@xxxx:~/catlass/python/tla_dsl$ python examples/end_to_end/basic_mmad/basic_matmul.py  --device 1 --dtype-a f32 --dtype-b f32 --dtype-c f32
--- mnk=(256,512,1024) layout=row/row dtype=f32/f32/f32 ---
tensor([[ 421.2699,   70.1012,   81.0291,  ..., -161.6766, -442.6080,
         -425.0576],
        ...,
        [-113.9693, -272.3091,  294.2283,  ...,  180.0958, -498.2363,
          184.6163]])
tensor([[ 421.2698,   70.1012,   81.0291,  ..., -161.6766, -442.6080,
         -425.0576],
        ...,
        [-113.9693, -272.3092,  294.2284,  ...,  180.0957, -498.2363,
          184.6163]])
passed=True cache_key=xxx
kernel.o=/xxx/kernel.o
  • 性能测试:56.577999us 符合预期(M/N/K=3200/4096/256)

passed=True cache_key=ee468b3cae6d14a4
kernel.o=/home/npu_user0/.cache/catlass/ee468b3cae6d14a4/kernel.o
...

2026-08-14 22:21:30 [INFO]  Operator Basic Information:

        Op Name: basic_mmad_kernel
        Op Type: cube
        Task Duration(us): 56.577999
        Block Dim: 28
        Mix Block Dim: 
        Device Id: 6
        Pid: 3521978
        Current Freq: 1650
        Rated Freq: 1650

文档更新

类型标签

likedislike
Pull Request已成功合入, 合并人@CANN-robot
(感谢 init__zhb__ 的贡献)
Iinit__zhb__成员
25 天前 创建了 pull request,commit 6e3e414c
atomgit-bot
atomgit-bot
25 天前 评论:

变更摘要

该 PR 为 tla.mmad 补充了 use_hf32 参数,用于控制 FP32 操作数在 L0A/L0B 上进行 HF32 舍入(等价于 AscendC::SetHF32Mode)。改动贯穿前端 DSL API、MLIR 算子定义与 Python 绑定、以及底层 lowering 流程:新增 BoolAttr 属性并通过 hivm::SetCtrlOp 操作 CTRL[46] 控制位,同时补充了函数级统一设置与逐 mmad 设置的回退策略、函数退出时的默认值恢复逻辑,以及相应的测试用例。

主要改动

  • Tla_MmadOp 算子定义扩展: 在 Tla.td 中为 tla.mmad 新增必选属性 BoolAttr:$use_hf32,并补充了该属性对应 CTRL[46] 控制位的语义说明。
  • 前端 DSL API 扩展: 在 core_api.pymmad() 中新增 use_hf32: bool = False 参数,校验其必须为 bool,并生成 mlir_ir.BoolAttr 传递给底层算子;同时更新了生成的 tla_ops_gen.py 绑定以接收并设置 use_hf32
  • Lowering 新增 HF32 控制位逻辑: 在 TlaCubeRegionPass.cpp 中新增 Hf32ModeBit = 46,通过 hivm::SetCtrlOp 设置 CTRL[46];当函数内所有 mmaduse_hf32 一致时在函数入口统一设置一次,混用 true/false 时改为在每个 mmad 前逐条设置。
  • 函数退出时恢复默认值: 在 TlaPrologueEpiloguePass.cpp 中新增对 CTRL[46] 的复位逻辑,在函数末尾的 PIPE_ALL barrier 前或函数结束处写入 hivm::SetCtrlOp 将 HF32 模式恢复为 false
  • 测试补充与更新: 新增 mmad-hf32.mlir 覆盖 true、默认 false、双 true 与混合场景的 lowering 行为;更新现有 tla.mmad 相关 lit 测试以携带 use_hf32,并在 test_frontend_lowering.py 中新增参数发射与非法参数校验测试。
likedislike
不准确?
atomgit-bot
atomgit-bot
25 天前 评论:

代码审查

✅ 未发现问题

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

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


PR Approval Progress

Congratulations! All modules have met the lgtm and approve requirements.

Module Approval Details

module lgtm status approve status
repo-cann/catlass sunhao_hw, 周建伟 (2/2) sunhao_hw (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
此处折叠了187条消息 查看更多
zjw666888成员
20 天前 评论:

/lgtm

likedislike
sunhao_hw成员
20 天前 评论:

/lgtm
/approve

likedislike
CANN-robotCANN-robot成员
20 天前 添加了label:lgtmapproved
CANN-robotCANN-robot成员
20 天前 合入了pull request
CANN-robot
CANN-robot成员
20 天前 评论:

Pull Request 已合并或已关闭。

If you want to solve this problem, you can click here to do it in the FAQs.

likedislike