Merged
feat: all_gather_matmul_v2支持mxfp4量化 kernel实现 #7193
feat: all_gather_matmul_v2支持mxfp4量化 kernel实现 #7193
Merged
dbtzyAdd on Jun 18
dbtzy
dbtzyCollaborators
Jun 18

描述

新增AllGatherMatmul算子mxfp4量化单算子通路。解决了AllGatherMatmul算子无法对输入数据类型是float4_e2m1的数据类型做MX量化的缺陷。

关联的Issue

#3257

测试

本地开发者自验证和RDV验证通过

文档更新

类型标签

代码检视报告 — PR #7193

检视概览

  • PR: https://gitcode.com/cann/ops-transformer/pull/7193
  • 算子: all_gather_matmul_v2(新增 FP4/DT_FLOAT4_E2M1 dtype 支持)
  • 代码侧别: Kernel侧 + Tiling侧 + 混合(op_api/tests)
  • 变更文件数: 14
  • 检视文档: cpp-secure.md, ascendc-topk.md, mc2-specific.md, ascendc-api.md, ascendc-perf.md, cpp-general.md
  • 检视时间: 2026-07-01

检视统计

状态 条例数 占比
PASS 52 98.1%
FAIL(发现问题) 1 1.9%
SUSPICIOUS(需关注) 0 0%

发现问题(HIGH 置信度)

[GEN-4.2] 魔鬼数字 — FP4 PR 变更中存在两处裸数字常量

  • 问题描述:FP4 PR 变更中存在两处魔鬼数字(未命名的裸数字常量),违反"禁止使用魔鬼数字\字符串"规则。虽然注释解释了含义,但按编码规范应使用命名常量。

  • 代码片段 1(mc2/all_gather_matmul_v2/op_host/op_tiling/all_gather_matmul_tiling_base.cpp 行 671-678):

    uint64_t AllGatherMatmulTilingBase::CalcGatherLen(uint32_t dimA, uint32_t dimB, uint64_t alignAddrLen)
    {
        // FP4每字节存放2个元素,按元素数/2计字节数;其余dtype按元素数*dtypeSize
        if (CheckSupportDtype(args_.geAType, FP4_DTYPE_SUPPORT_LIST)) {
            return mc2tiling::AlignUp(static_cast<uint64_t>(dimA) * static_cast<uint64_t>(dimB) / 2U, alignAddrLen);
        }
        return mc2tiling::AlignUp(dimA * dimB * args_.inputDtypeSize, alignAddrLen);
    }
    

    / 2U 是裸数字,语义为"FP4每字节含2个数据元素",但未定义为命名常量。Kernel侧已定义 PF4_DATA_NUM_PER_BYTE = 2(common.h:46),Host侧应引用或定义等价常量。

  • 代码片段 2(mc2/all_gather_matmul_v2/op_host/op_tiling/arch35/all_gather_quant_bmm_tiling.cpp 行 690-702):

    CutResult AllGatherQuantBmmTiling::GetTilingResult()
    {
        AllGatherMMFitBalanceTiling tileFormulate(args_, KernelType::ALL_GATHER, TopoType::STANDARD_CARD);
        CutResult result = tileFormulate.GetTiling();
    
        if (isFp4_) {
            AdjustCutResultForHCCL(result, args_.mValue, args_.kValue / EVEN_ALIGN, 1, args_.rankDim, opName_);
        } else {
            AdjustCutResultForHCCL(result, args_.mValue, args_.kValue, args_.inputDtypeSize, args_.rankDim, opName_);
        }
    
        return result;
    }
    

    1 是裸数字,语义为"FP4通信时dtypeSize=1字节"。对比 else 分支使用 args_.inputDtypeSize 命名变量,FP4分支直接硬编码 1 是魔鬼数字。

  • 假设检验证据:

    正向证据:

    证据类型 分值 证据描述
    规范违反 +40% / 2U 裸数字未定义为命名常量
    规范违反 +40% 1 裸数字硬编码 dtypeSize
    PR归属 +20% 两处均在 diff 变更范围内
    上下文防御缺失 +15% Host侧无 PF4_DATA_NUM_PER_BYTE 常量可用

    负向证据:

    证据类型 分值 证据描述
    注释解释 -15% 注释解释了 / 2U 的语义
    上下文推断 -15% 1 的语义可从函数参数名推断

    自信值 = 40+40+20+15-15-15 = 85% ≥ 70% → 判定违规

  • 修复建议:

    1. 在 Host侧定义等价常量 constexpr uint64_t FP4_DATA_NUM_PER_BYTE = 2;(或引用已有定义),替换 CalcGatherLen 中的 / 2U 为 / FP4_DATA_NUM_PER_BYTE
    2. 在 GetTilingResult 中定义 constexpr uint64_t FP4_COMM_DTYPE_SIZE = 1; 或类似命名常量,替换裸数字 1
    3. 更佳方案:Host侧统一定义 FP4_COMM_BYTE_SIZE = 1 和 FP4_ELEMENT_PER_BYTE = 2,集中管理 FP4 编码常量

需关注(建议改进)

SEC-3.5 防御性 null 检查建议

  • 问题描述:CheckMXFP4Input 中 scaleInv1Desc 通过 GetOptionalInputDesc 获取后直接解引用(->GetDataType()),虽上游 CheckInputValid() 已校验非null,但建议在本函数内添加防御性null检查以增强鲁棒性。

  • 代码片段(mc2/all_gather_matmul_v2/op_host/op_tiling/arch35/all_gather_quant_bmm_tiling.cpp 行 151-162):

    auto scaleInv1Desc = context_->GetOptionalInputDesc(SCALE_INV1);
    OP_TILING_CHECK(args_.kValue % EVEN_ALIGN != 0,
        OP_LOGE_FOR_INVALID_VALUE_WITH_REASON(...), return ge::GRAPH_FAILED);
    OP_TILING_CHECK((!args_.isBTrans), ...);
    OP_TILING_CHECK((scaleInv1Desc->GetDataType() != ge::DataType::DT_FLOAT8_E8M0), ...);  // 直接解引用
    
  • 建议修复:

    OP_TILING_CHECK(scaleInv1Desc == nullptr,
        OP_LOGE(opName_, "scale1 is nullptr in mxfp4 scene"), return ge::GRAPH_FAILED);
    OP_TILING_CHECK((scaleInv1Desc->GetDataType() != ge::DataType::DT_FLOAT8_E8M0), ...);
    

GEN-4.2 低风险建议 — CalcGatherLen / 2U 命名常量化

  • 问题描述:CalcGatherLen 中的 / 2U 虽有注释解释,建议改为命名常量提升可读性和维护性。这与上面的 FAIL 发现一致(同一条例,同一代码位置)。

通过条例

高危安全(SEC/TOPK 安全类):
SEC-1.1, SEC-1.2, SEC-1.3, SEC-2.1, SEC-2.2, SEC-2.3, SEC-3.1, SEC-3.3, SEC-3.5, SEC-3.4, SEC-4.1, SEC-4.2, SEC-9.1, SEC-10.3, SEC-10.4, SEC-10.5, SEC-5.1~5.4, SEC-8.1~8.3, SEC-11.1~11.4, TOPK-8

TOPK:
TOPK-1, TOPK-2, TOPK-3, TOPK-4, TOPK-5, TOPK-6, TOPK-7, TOPK-10, TOPK-12

领域规则(MC2):
MC2-01, MC2-02, MC2-03, MC2-04, MC2-09, MC2-13, MC2-14, MC2-15, MC2-17, MC2-18

性能(PERF/PREC/TIL):
PERF-2, PERF-7, PREC-2, TIL-1, TIL-2, TIL-3

通用规范(GEN/API):
GEN-1.1, GEN-4.3, GEN-5.2, GEN-7.1, GEN-14.4, API-12

核心 FP4 逻辑验证结论

验证项 结论
FP4 通信数据量计算 CeilDiv + dataNumPerByte 正确
FP4 HCCL 数据类型 UINT8 选择正确(FP4 数据以字节单位传输)
FP4 地址偏移 tailAGM 计算正确
FP4 K 偶数校验 保证整数除法精确
FP4 workspace/gatherLen 正确反映半字节存储特性
HCCL 通信生命周期 Init→SetCcTiling→AllGather→Finalize 完整
FP4 量化参数(scale) 类型链路一致(DT_FLOAT8_E8M0)
FP4 sizeof 计算 全部使用整数运算,无浮点转换
likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for dbtzy's contribution.)
dbtzydbtzyCollaborators
Jun 18 Created pull request, commit 194141b6
CANN-robotCANN-robotCollaborators
Jun 18 Added label: stat/needs-squash
CANN-robotCANN-robotCollaborators
Jun 18 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jun 18 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


PR Approval Progress

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

Module Approval Details

module lgtm status approve status
*/*/op_api/aclnn*.h ✅ 汤磊, 刘丹 (2/2) ✅ 汤磊 (1/1)
*/*/op_host/*_def.cpp ✅ 汤磊, 刘丹 (2/2) ✅ 汤磊 (1/1)
mc2 ✅ 刘丹, captainmiaow (2/2) ✅ 刘丹 (1/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
dbtzydbtzyCollaborators
Jun 22 Pre merge succeeded (commit_id: 6308f52794e16ce53a90a41e3659e969f0d57ef9)
326 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Jul 9 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Jul 9 Added label: ci-pipeline-passed
captainmiaow
captainmiaowCollaborators
Jul 9 Comments:

/lgtm

likedislike
CANN-robotCANN-robotCollaborators
Jul 9 Added label: lgtm
CANN-robotCANN-robotCollaborators
Jul 9 Merged pull request