Draft
[WIP]feat(tensor): add vocab-parallel embedding_lookup operator #64
[WIP]feat(tensor): add vocab-parallel embedding_lookup operator #64
Draft
yizigeAdd on 2 days ago
yizige
yizige
2 days ago

PR功能描述 / 为什么需要这个合入:

新增 vocab-parallel embedding_lookup 算子,按 README 与 docs/adding_an_operator.md 的组织方式落入 tensor 业务域:

  • src/cann_ops_tilelang/tensor/embedding_lookup.py:公共接口与契约校验。
  • src/cann_ops_tilelang/tensor/embedding_lookup_kernel.py:get_embedding_lookup_kernel 与调度配置。
  • 从 cann_ops_tilelang 包入口导出 embedding_lookup。

语义与接口:

  • 对本地权重分片 [vocab_start_index, vocab_end_index) 做 gather;区间外 id 输出零行,因此各 TP rank 的分片结果相加可重建全表。
  • 支持 int32/int64 ids(任意 rank,含 rank-0)、bfloat16/float16/float32 权重、连续输入;支持可选 out=;空输入不启动
    kernel。
  • 非连续输入、非法 dtype、区间与权重行数不一致、out 形状/dtype/别名不合法等均给出明确异常,不做静默拷贝或回退。

Kernel 设计:

  • AIV persistent 网格;满网格时每 tile 一次块 MTE3 存储,非整除的尾部行由前 tail_rows 个 core 各存一行,输出严格为
    input_ids.shape + (H,),无 host 侧 reshape/pad/slice。
  • miss 行不读权重(clamp 到 L2 行的读是纯浪费),直接在 UB 中清零。
  • 行宽为 256B 向量寄存器整数倍时用 SimdVF 矢量清零,否则回退 SimtVF;UB 预算仍按 216 KiB(248 KiB 物理 − 32 KiB SimtVF
    context)规划。

为什么需要:生产推理使用 TP 切分的词表 embedding lookup;现有 tensor/gather 不提供分片区间零行语义和精确输出形状/尾行处理,本 PR
补齐该算子。

该PR关联的issue

fixes #(如无对应 issue,可填写 N/A)

希望检视人员了解:

  • 正确性用 torch.equal 位精确比较;reference 使用 torch.where 产生 +0.0,避免 miss 行 ±0.0 的字节差异。
  • per-row 路径对最后一个不满 tile 的 ids copy 用 valid_rows 截断,避免越界读;块存储只在网格填满且尾行可分配时启用。
  • 性能证据见下方「性能证据」章节:测试环境、计时/带宽口径、benchmark 数据、流水利用率与复现命令。
  • benchmark 基线与 NPU 显存画像不随 PR 提交,可用 --refresh-baseline / --refresh-mem 在本地生成。

性能证据

测试环境

项 值
设备 Ascend950DT(A5),AIV 56,1650 MHz,L2 112 MiB
软件 CANN 9.3.0;PyTorch / torch_npu 2.10.0;Python 3.12
采集日期 2026-11-10

计时与带宽口径

  • 时延:仓库统一口径 tilelang.profiler.bench.do_bench(backend='msprof')(warmup=0、rep=30 ms),只计设备端 kernel 时间;msprof 对多次 kernel 时长取平均,轮间 flush L2。
  • 带宽:(ids + 输出 + 命中行读) / 时延;命中行读 = shard_rows / vocab_size × num_tokens × hidden × element_size(本 rank 外的 id 在 UB 置零,不读表)。
  • 口径核对:与 msprof op 的 Memory.csv 实测 HBM 流量对比,h7168 / h4096 / tp8_h4096 / fp32 / small 五个场景偏差 0.2–0.4%。
  • 带宽基准:同结构(persistent + 多版本流水、无 gather)纯 copy kernel 实测 ≈3015 GB/s(标称 HBM 4 TB/s 的 75%),作为 100% 利用率。

实测性能(仓库 benchmark)

用例(bf16,除注明) 时延 带宽
nt=32768, H=7168, TP=1(全表 1.85 GB) 327.0 µs 2874 GB/s
nt=8192, H=4096, TP=8(命中 12.5%) 28.6 µs 2642 GB/s
nt=2048, H=7168, TP=8(命中 12.5%) 12.2 µs 2708 GB/s
nt=8192, H=7168, fp16, TP=8(命中 12.5%) 51.0 µs 2591 GB/s
参考:同进程 torch F.embedding(nt=32768, H=7168) 348.2 µs 2699 GB/s
  • 全表场景达到同构 copy 上限的 95–99%(同配置重复测量 315–327 µs,物理地址导致 ±4%);
  • 同 shape 下比 torch 原生 embedding 快约 6–10%;
  • TP=8 场景带宽低于 copy 上限,是因为 87.5% 的 token 只写零、读流量本身很小,属写主导负载。

流水利用率(msprof op,56 block)

场景 时延 MTE2 MTE3 VEC SCALAR
TP=1, nt=32768, H=7168 311.7 µs 98.9% 83.1% 0% 8.4%
TP=1, nt=32768, H=4096 178.7 µs 97.4% 47.5% 0% 10.1%
TP=8, H=7168(命中 12.5%) 196.9 µs 62.2% 89.8% 14.7% 13.5%
TP=8, H=4096(命中 12.5%) 112.2 µs 68.6% 84.2% 19.0% 19.0%
TP=1, H=7168, fp32 628.1 µs 98.4% 55.5% 0% 4.1%
TP=1, nt=64, H=7168(decode) 2.9 µs 69.8% 36.6% 0% 26.2%
  • 全命中 / fp32 场景读路径饱和(MTE2 98–99%),trace 中 MTE2 事件最大间隔 21.8 ns,流水无空泡;写路径被读完全掩盖;
  • TP=8 场景转为写瓶颈(MTE3 84–90%),VEC 14–19% 为 miss 行置零(87.5% 的行),标量约 13–19%;
  • 剩余差距来自随机 14 KiB 行的读效率(MTE2 active BW ≈26.5 GB/s/core,顺序读 ≈53.6 GB/s/core),不是调度或空泡问题。

采集口径说明

  • PipeTimeline 不与 PipeUtilization 写在同一条 msprof op 命令里:56 block 超过 108 个 sub-block 的插桩上限,计数会失真(实测 MTE2 记为 0%、时长翻倍)。
  • trace.json 只采样部分 lane(h7168 为 12/56),核利用率以 PipeUtilization.csv 的 Block Dim(56)为准。

复现命令

# 时延/带宽(仓库 benchmark)
OPS_TILELANG_TEST_LEVEL=2 python -m pytest tests/tensor/test_embedding_lookup.py -m benchmark --run-benchmark -q

# 流水占比(driver 侧)
msprof op --kernel-name=embedding_lookup_kernel_kernel --aic-metrics=PipeUtilization,Memory,L2Cache --output=/tmp/oprof <launch_script>

建议把性能基线固定到同一台机器:同配置下全表场景仍有 ±4% 的物理地址抖动,跨机器对比会引入额外噪声。

改动类型 / Change Type

测试信息 / Testing

复现命令:

OPS_TILELANG_TEST_LEVEL=2 pytest tests/tensor/test_embedding_lookup.py -m 'not benchmark'
pytest tests/tensor/test_embedding_lookup.py -m benchmark --run-benchmark
./scripts/check.sh

检查清单 / Checklist

- [x] 代码符合规范 / Code follows style guide
- [x] 测试添加并通过 / Tests added and passed
- [x] 文档已更新 / Docs updated if needed(接口 docstring 写明支持范围、限制与空输入语义)
- [x] 无硬编码敏感信息 / No secrets hardcoded
- [x] 提交信息符合规范 / Commit message follows convention
likedislike
Merge blocked
yizigeyizige
2 days ago Created pull request, commit 5778447b
CANN-robot
CANN-robotCollaborators
2 days ago Comments:

Hi @Will_09, welcome to submitting your first PR to CANN!

PR Merge Steps

1. CLA Signing

If the current PR label includes cann-cla/yes, it means you have signed the CLA and can proceed to the next step. If the label includes cann-cla/no, please sign the CLA first. If you have any questions, please refer to the FAQ.

2. CI Check

Please comment /compile to trigger the CI pipeline check. If the CI run is successful, the PR will be tagged with ci-pipeline-passed and you can proceed to the next step. If the CI run fails, the PR will be tagged with ci-pipeline-failed, please check the CI logs to fix the issues in the PR. If you have any questions, please refer to the FAQ.

3. Code Review

After CI passes, please refer to the PR Approval Progress and proactively @ the committers in the table to review the code. After approval, committers will comment /lgtm and /approve. Once the lgtm and approved labels are successfully added, the PR will be merged automatically.
%!(EXTRA string=https://atomgit.com/cann/community/blob/master/en/sig-infrastructure/command.md)

likedislike
CANN-robotCANN-robotCollaborators
2 days ago Added label: stat/needs-squashai-co-authored
CANN-robotCANN-robotCollaborators
2 days ago Added label: cann-cla/yes
CANN-robot
CANN-robotCollaborators
2 days ago 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

⚠️ This PR does not yet meet the following requirements:lgtm (requires ≥ 2 person(s) per module)、approve (requires ≥ 1 person(s) per module)

Module Approval Details

module lgtm status approve status
repo-cann/ops-tilelang ❌ (0/2)(You can also ask: 俞郑中, jiawei_zhuang, 马兵, kevin365, 黄俊健) ❌ (0/1)(You can also ask: ChaoyangJi, 蒋礼锐, 俞郑中, 黄俊健, 马兵)

💡 Tip:

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

CLA Signature Pass

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

likedislike
194 messages are collapsed here。 See more
CANN-robot
CANN-robotCollaborators
12 hours ago Comments:
🚀 CI 流水线已启动
📋 执行详情: 点击查看流水线
likedislike
CANN-robotCANN-robotCollaborators
12 hours ago Removed label: ci-pipeline-failed
CANN-robotCANN-robotCollaborators
12 hours ago Added label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
7 hours ago Removed label: ci-pipeline-running
CANN-robotCANN-robotCollaborators
7 hours ago Added label: ci-pipeline-passed