Merged
qkv_rms_norm_rope_cache_with_k_scale算子新增支持mrope场景 #8974
qkv_rms_norm_rope_cache_with_k_scale算子新增支持mrope场景 #8974
Merged
shi-ruiAdd on Jul 21
shi-rui
shi-ruiCollaborators
Jul 21

描述

qkv_rms_norm_rope_cache_with_k_scale新增支持mrope场景

  1. 修改query_start_loc、seq_lens等参数为可选参数
  2. 新增mrope_position、mrope_section等描述mrope的参数
  3. aclnn\tiling\infer shape补充相关拦截和校验
  4. kernel支持mrope的计算逻辑
    a. 输入使能l2 uncache
    b. cos_sin根据mrope的输入组织构建计算
    c. q矩阵直接fixp量化输出
    d. k矩阵执行int8量化输出
    e. v矩阵执行perhead perchannel量化

关联的Issue

https://gitcode.com/cann/ops-transformer/issues/3873

测试

本地构造100+case测试。

文档更新

  1. 算子原型文档
  2. aclnn文档
  3. torch_extension文档

类型标签

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for shi-rui's contribution.)
shi-ruishi-ruiCollaborators
Jul 21 Created pull request, commit 36d2db55
atomgit-bot
atomgit-bot
Jul 21 Comments:

变更摘要

该 PR 为 qkv_rms_norm_rope_cache_with_k_scale 算子新增 M-RoPE(Multi-section RoPE)接口支持,使该算子能够在两种场景下工作:传统场景(使用 query_start_loc/seq_lens 配合 FP8 动态量化)和 M-RoPE 场景(使用 mrope_position/mrope_section 配合 BF16 非量化输出)。原有必选参数 query_start_loc、seq_lens、rotation、v_scale 全部改为可选,新增 mrope_position、mrope_section、q_quant_mode、q_out_dtype 参数,输出中 q_scale 在 M-RoPE 场景下返回 None。

主要改动

  • 新增加 M-RoPE 参数支持:在两个公开接口 qkv_rms_norm_rope_cache_with_k_scale_ 和 qkv_rms_norm_rope_cache_with_k_scale 中新增 mrope_position([3, T] int32 位置张量)、mrope_section(3元素列表)、q_quant_mode(PerTokenPerHead/NoQuant)、q_out_dtype(GE_DTYPE_BF16/GE_DTYPE_FLOAT8_E4M3FN)四个可选参数,用于控制 M-RoPE 模式下的位置编码和量化行为。

  • 现有参数改为可选并新增互斥校验:query_start_loc、seq_lens 从必选 at::Tensor 改为 c10::optional<at::Tensor>,rotation、v_scale 在底层实现中同样改为 c10::optional。新增 ValidateInputs 和 _validate_meta_scene 中的互斥校验逻辑,要求传统位置源(query_start_loc + seq_lens)与 M-RoPE 位置源(mrope_position + mrope_section)必须且只能提供一组。

  • 输出签名变更:q_scale 从必选 at::Tensor 改为 c10::optional<at::Tensor>(Python 侧为 Optional[torch.Tensor]),M-RoPE 场景下 q_scale 为 None,传统场景下保持原有的 FP32 scale 张量;q_out 的 dtype 在 M-RoPE 下为 BF16,传统场景下仍为 FP8 E4M3FN。

  • 新增场景特定校验:M-RoPE 模式下强制要求布局为 TND → TND、head_size == 128、k_cache 为 int8 类型、mrope_position 形状为 [3, T];传统场景下要求 k_cache 为 FP8 E4M3FN,禁止 NTD → TND 布局转换。同时在 C++ 和 Python Meta 注册中增加了对称的校验逻辑。

  • 新增辅助函数和常量:C++ 侧新增 ResolveQQuantMode、CheckOptionalTensorDefined 函数及 Q_QUANT_PER_TOKEN_PER_HEAD、Q_QUANT_NO_QUANT、MROPE_HEAD_SIZE 等常量;Python 侧新增 _normalize_q_quant_mode_for_meta、_validate_meta_scene 函数及对应的 dtype 和量化模式常量。

likedislike
Incorrect?
CANN-robotCANN-robotCollaborators
Jul 21 Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
Jul 21 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
*/*/README.md ✅ 陈娇, 俞郑中, 马兵 (3/2) ✅ 陈娇 (1/1)
*/*/docs/acl*.md ✅ 陈娇, 俞郑中, 马兵 (3/2) ✅ 陈娇 (1/1)
*/*/op_host/*_def.cpp ✅ 王永光, 俞郑中, 马兵 (3/2) ✅ 王永光 (1/1)
*/*/op_host/op_api/aclnn*.h ✅ 王永光, 俞郑中, 马兵 (3/2) ✅ 王永光 (1/1)
posembedding ✅ 俞郑中, 马兵 (2/2) ✅ 俞郑中, 马兵 (2/1)
sig-ops-transformer ✅ 马兵, 俞郑中 (2/2) ✅ 马兵, 俞郑中 (2/1)

💡 Tip:

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

CLA Signature Pass

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

likedislike
CANN-robot
CANN-robotCollaborators
Jul 21 Comments:

CLA Signature Pass

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

likedislike
287 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Aug 4 Added label: approved
mabing
mabingCollaborators
Aug 4 Comments:

/lgtm
/approve

likedislike
CANN-robotCANN-robotCollaborators
Aug 4 Added label: lgtm
CANN-robotCANN-robotCollaborators
Aug 4 Closed associated issue
CANN-robotCANN-robotCollaborators
Aug 4 Merged pull request