Merged
feat(quant): add TurboQuant operator #10996
chen-weipeng12Add on Sep 21
feat(quant): add TurboQuant operator #10996
Merged
chen-weipeng12Add on Sep 21
chen-weipeng12
chen-weipeng12Collaborators
Sep 21

描述

背景

TurboQuant 将 MLA 的 KV latent 按 token 归一化后量化为 4bit codebook 索引,并单独输出每个 token 的 FLOAT16 L2 norm,用于 KV cache 的 4bit 压缩。生产路径为 quant/turbo_quant。

本 PR 内容

新增turboquant算子

关联的Issue

https://gitcode.com/cann/ops-nn/issues/5714

测试

  • op_host UT:23/23 通过(Tiling 14 + InferShape 9)
  • op_kernel 仿真 UT:15/15 通过
  • ACLNN eager example:编译运行通过并产出输出
  • 环境:CANN 9.2.0,Ascend 910B
  • 格式门禁:clang-format 18.1.8、ruff-format 0.14.14(.pre-commit-config.yaml 中 pin 的版本)通过

文档更新

新增:

  • quant/turbo_quant/README.md
  • quant/turbo_quant/docs/aclnnTurboQuant.md
  • quant/turbo_quant/docs/torchapi_turbo_quant.md

更新(各新增 1 行接口登记):

  • docs/zh/op_api_list.md
  • docs/zh/torch_api_list.md
  • docs/zh/menu_aclnn_api.md
  • docs/zh/menu_torch_api.md

类型标签

AI/Agent生成声明

likedislike
Pull Request has been successfully merged by CANN-robot.
(Thank you for chen-weipeng12's contribution.)
chen-weipeng12chen-weipeng12Collaborators
Sep 21 Created pull request, commit 839a7bc2
atomgit-bot
atomgit-bot
Sep 21 Comments:

变更摘要

本 PR 新增 quant/turbo_quant 算子,用于 MLA KV latent 的 4bit 量化:将每个 token 的 latent 做 L2 归一化后映射到 16 个升序码本中心,两个 nibble 打包为一个字节输出 y,并单独输出每 token 的 FLOAT16 L2 范数 scale。算子覆盖 op_graph 原型、op_host 定义/推导/tiling、op_kernel、binary 配置、Torch 扩展与测试文档,支持 ascend910b(Atlas A2)与 ascend910_93(Atlas A3)。

主要改动

  • 算子接口定义:在 turbo_quant_def.cpp 与 turbo_quant_proto.h 中注册 TurboQuant,输入 latent(DT_FLOAT)、centroids(DT_FLOAT),输出 y(DT_UINT8)、scale(DT_FLOAT16),并通过同一份 OpAICoreConfig 挂载到 ascend910b 与 ascend910_93。
  • Shape 与数据类型推导:turbo_quant_infershape.cpp 中 InferShapeForTurboQuant 依据输入 [numTokens, headDim] 推导 y 为 [numTokens, headDim/2]、scale 为 [numTokens],并对 rank、numTokens 及 headDim(仅支持 TQ_SUPPORTED_HEAD_DIM=512)做校验,同时 InferDataTypeForTurboQuant 固定两个输出类型。
  • Tiling 策略与数据结构:turbo_quant_tiling.h 定义 TurboQuantTilingData(numTokens、tokensPerCore、headDim、packedBytes、tokensPerBatch)及 TQ_MAX_TOKENS_PER_BATCH 等常量;turbo_quant_tiling.cpp 运行时读取 GetCoreNumAiv() 与 UB 大小,按 token 划分核心并计算批处理 token 数,校验 dtype、centroids 形状 [16] 与 uint32 上限。
  • Kernel 实现:turbo_quant.h 中 KernelTurboQuant 完成 norm 归约、按中点边界计数的最近码本查找、nibble 打包与 scale 写出,并采用 tokensPerBatch 批处理;turbo_quant.cpp 为算子入口,从 tiling 读取参数并调用 Init/Process。
  • 上层接入与配置:新增 ascend910b/ascend910_93 的 turbo_quant_binary.json,以及 Torch 扩展 turbo_quant.py、csrc/turbo_quant.cpp、graph_convert_turbo_quant.py,对外暴露 turbo_quant 接口与 GE 图转换器,并新增 ACLNN example、op_host/op_kernel UT 与 golden 参考实现。
likedislike
Incorrect?
atomgit-bot
atomgit-bot
Sep 21 Comments:

代码审查

✅ 未发现问题

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

CLA Signature Pass

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

likedislike
145 messages are collapsed here。 See more
CANN-robotCANN-robotCollaborators
Sep 28 Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
Sep 28 Added label: ci-pipeline-passed
CANN-robotCANN-robotCollaborators
Sep 28 Closed associated issue
CANN-robotCANN-robotCollaborators
Sep 28 Merged pull request
CANN-robot
CANN-robotCollaborators
Sep 28 Comments:

Pull Request 已合并或已关闭。

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

likedislike