Merged
fix(operation): Change scaleA/B to per-batch when using scaleMM+BatchMatmul. #5571
yang_rAdd on Aug 20
fix(operation): Change scaleA/B to per-batch when using scaleMM+BatchMatmul. #5571
Merged
yang_rAdd on Aug 20
yang_r
yang_rCollaborators
Aug 20

此前 scaled_mm 与 BatchMatmul 组合时,scaleA/scaleB 采用共享 batch的方案:
无论矩阵的 batch 维度如何,scale 始终固定为 3-D,并在所有 batch 间广播同一组 scale。
这不符合按 batch 缩放的语义,当各 batch 携带不同 scale 时会导致结果错误。
本 PR 将 scaleA/scaleB 改为按 batch 语义:每个 scale 的维度需比其配对矩阵多一维,
且 scale 的 batch 维度必须与配对矩阵的 batch 维度一致。

Changes:

  • Python(matmul.py):要求 scale_dim == input_dim + 1;校验 scale 的 batch 维
    等于配对矩阵的 batch 维;scale 元素索引由固定 [0/1/2] 改为 [-3/-2/-1] 以支持
    2-D/3-D/4-D 矩阵;收紧 __validate_scale_k1_dimensions(AND 改为 OR,要求所有
    k1 维均等于 2);更新 docstring,补充 2-D/3-D/4-D scale 形状示例
  • C++(cube_operation_impl.cpp):CheckMXMatmulShape 增加 inputDim∈[2,4]、
    scaleDim==inputDim+1 及逐轴 batch 配对校验;通过 scaleDimOffset 泛化 scale 偏移
    索引;新增 GetBatchScaleTensor3D/GetBatchScaleTensor4D(按 batch 做 View+Reshape
    切片,含 ASSERT 空指针保护,与 GetBatchTensor3D/4D 模式一致),并接入
    ConstructBatchMatmulMXTensorGraph3D/4D
  • 测试:新增 UT test_scaled_mm_batch_scale.py(3-D 拒绝 / 4-D 接受 / 4-D 拒绝);
    更新 C++ UT test_operation_impl.cpp、ST test_scaled_bmm.py 与
    scaled_bmm_test_case.py
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for yang_r's contribution.)
yang_ryang_rCollaborators
Aug 20 Created pull request, commit 0188ba6e
atomgit-bot
atomgit-bot
Aug 20 Comments:

变更摘要

本 PR 修复 scaleMM + BatchMatmul 组合下 scale 张量的语义:将 scaleA/scaleB 从所有 batch 共享的 3 维张量改为 per-batch(每个 batch 拥有独立的 scale),scale 张量维度必须比输入矩阵多 1 维(2/3/4 维输入分别对应 3/4/5 维 scale),且 scale 的 batch 维度必须等于输出 batch 维度、不支持广播。改动覆盖 C++ 框架侧的形状校验与构图逻辑(cube_operation_impl.cpp)、Python 侧 pypto.op.matmul 的 scaled_mm 接口校验与文档,以及配套的 ST/UT 测试更新。

主要改动

  • scale 形状校验规则变更: CheckMXMatmulShape 由"scale 必须为 3 维"改为"scale 维度 = 输入维度 + 1"(aTensor/bTensor 维度必须相等且为 2/3/4 维),并逐 batch 校验 scale 的 batch 轴等于输出 batch 轴,scale 的 M/K/N 维度改用 scaleDimOffset 偏移访问;Python 侧 __validate_scaled_inputs(expected_scale_dim = input_dim + 1)、__validate_scaled_shape(新增 batch 维度校验、改用负索引 -3/-2/-1 取 scale 维度)及 __validate_scale_k1_dimensions(两个 scale 末维均须等于 2)同步调整。
  • 按 batch 切分 scale 参与构图: 新增 GetBatchScaleTensor3D 与 GetBatchScaleTensor4D,在 ConstructBatchMatmulMXTensorGraph3D/ConstructBatchMatmulMXTensorGraph4D 循环内通过 View+Reshape 取出当前 batch 的 scale 子张量,替换原先直接复用整体 aScale/bScale 传入 MatmulGraphNodes 的逻辑。
  • scaled_mm 接口文档更新: matmul.py 中 scaled_mm 的 docstring 明确 2/3/4 维矩阵对应的 3/4/5 维 scale 形状约定(如 3 维矩阵对应 (batch, m, kScale, 2)),并更新异常说明与示例。
  • 测试用例适配 per-batch scale: ST 测试 test_scaled_bmm.py 的 3D/4D tensor view 与 kernel 签名(scale 张量改为 4/5 维、batch 轴设为 DYNAMIC)、_process_scale_tensors/prepare_inputs 的 scale 形状构造均改为携带 batch 维度;scaled_bmm_test_case.py 将 a_shape/b_shape 改为不同 batch 大小以覆盖广播场景;UT 新增 Test_BatchMatmulMX_InvalidScaleDim、Test_BatchMatmulMX_InvalidScaleBatch(C++)及 test_scaled_mm_batch_scale.py(Python,校验共享 scale 与 scale batch 广播被拒绝)。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Aug 20 Comments:

代码审查

审查总结

我已逐一审查全部 6 个变更文件,重点核对了 C++ 侧 CheckMXMatmulShape/GetBatchScaleTensor3D/4D/ConstructBatchMatmulMXTensorGraph3D/4D 与 Python 侧 __validate_scaled_inputs/__validate_scaled_shape/ST 测试 golden 计算之间的契约一致性:

  • framework/src/interface/operation/cube_operation_impl.cpp:CheckMXMatmulShape 新的维度/批处理/trans 偏移计算(scaleDimOffset = inputDim-2、batch 校验 max(a,b)、m/k/n scale 尺寸索引)逐项验证均与 Python 侧一致;新引入的 per-batch scale 切片因校验强制 scale batch == 输出 batch 而必然越界安全;valid shape 传播与既有 GetBatchTensor3D/4D 模式一致。仅发现 1 个 P3 可选改进(两处新增 ASSERT 报错信息中的维度标注错误)。
  • framework/tests/ut/operator/src/test_operation_impl.cpp:两个 3D/4D 正例的 scale 改为 per-batch 后与校验公式吻合;两个新增反例均能命中预期 CHECK 抛错路径。无问题。
  • python/pypto/op/matmul.py:expected_scale_dim = input_dim + 1、批处理校验、trans 时 valid[-2]/[-3] 取值与 C++ 一致;__validate_scale_k1_dimensions 由旧的 and 逻辑修正为 or 逻辑是正确修复。无问题。
  • python/tests/st/operation/matmul/test_scaled_bmm.py:_process_scale_tensors 的 reshape/repeat_interleave 与 golden 逐元素对应关系(64-block→2×32 分解)验证正确;3D/4D 广播下 scale 全 batch 语义与矩阵广播语义自洽。无问题。
  • python/tests/st/operation/matmul/testcase/scaled_bmm_test_case.py:配置改为 batch 广播(a=1/b=5、a=[2,1]/b=[1,3])后 scale batch 取输出 batch,与校验一致。无问题。
  • python/tests/ut/operation/test_scaled_mm_batch_scale.py:两个 UT 的错误信息正则与 __validate_* 的实际抛出文本匹配,且该 pytest.raises(PyptoError, match=...) 模式与仓库既有 UT 一致。无问题。

结论

  • 共报告 1 个 P3 发现(可选改进,非必须修复),0 个 P0/P1/P2 发现。
  • 总体风险评估:该 PR 的核心改动(scaleA/B 改为 per-batch 语义)在 C++ 校验、Python 校验、ST 测试 golden、UT 测试四处保持了一致的契约,数学推导与索引计算均验证无误;属有意的接口契约变更且调用方(测试)已同步更新。整体风险低,可合入。
类型 数量
🔴 阻塞 0
🟡 建议 1

💬 仅评论

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

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.
You can self-configure the PR merge rules for this repository. For more details, please refer to 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
framework/src/interface/operation ✅ dai-zhentao, shi-dingkun (2/2) ✅ dai-zhentao (1/1)
python/pypto/op ✅ dai-zhentao, shi-dingkun (2/2) ✅ dai-zhentao (1/1)
repo-cann/pypto ✅ dai-zhentao, shi-dingkun (2/2) ✅ dai-zhentao (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
195 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 29 Added label: ci-pipeline-passed
shi-dingkun
shi-dingkunCollaborators
Aug 29 Comments:

/lgtm

likedislike
dai-zhentao
dai-zhentaoCollaborators
Aug 29 Comments:

/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 29 Added label: lgtmapproved
CANN-robotCANN-robotCollaborators
Aug 29 Merged pull request