已关闭
[Feature][Q3社区任务]:TransformerEngineNPU Triton mHC 激活聚合与展开(Aggregate、ExpandCombine) #31
Liz创建于  8月15日关闭于  7 天前
Liz成员
8月15日 创建

任务描述

基于 TransformerEngineNPU 开放仓库进行 mHC 激活聚合与展开链路的 Triton 功能开发

任务交付件

本期任务为基于 TransformerEngineNPU 开放仓库进行 mHC 激活聚合与展开链路的 Triton 功能开发,请合入开发代码。语义和精度参考 NVTE v2.17 mHC 实现,本期限定 n=4。主要开发点如下:

  1. 实现 mhc_fused_aggregate 前向与反向;
  2. 实现 mhc_fused_expand_combine 前向与反向;
  3. 支持带 bias 和不带 bias 两条路径;
  4. 支持 FP32、BF16,正确计算输入、bias 和 H 矩阵的全部梯度;
  5. 与 H 矩阵生成需求统一 H_pre/H_post/H_res 的 shape、布局、padding、dtype 及梯度接口;
  6. 支持使用随机生成的合法 H 矩阵独立开发和测试,不依赖 H 矩阵生成需求先行合入;
  7. 补充典型 shape、边界 shape、非规则 shape 及带 bias/无 bias 测试;
  8. 提交性能测试脚本及测试结果;
  9. 填写昇腾社区开发体验报告。

验收标准

  1. 性能目标:在双方约定的典型及边界 shape 上,对比同机同卡 PyTorch 组合实现,平均性能无劣化,单项性能回退不超过 5%;本期不要求复刻 CUDA autotune 配置;
  2. 精度目标:两个 API 的输出及全部输入、bias、H 矩阵梯度与 NVTE/PyTorch 参考实现对齐,FP32 atol≤5e-3、rtol≤5e-3,BF16 atol≤2.5e-2、rtol≤2.5e-2;带 bias/无 bias、规则及非规则 shape 测试通过;
  3. 显存目标:峰值显存无明显劣化,原则上相对参考实现增幅不超过 5%,反复前反向无显存泄漏;
  4. 实践文档:提交特性说明与开发文档 1 篇,包含接口说明、H 矩阵布局契约、支持矩阵、已知限制及测试数据;
  5. 任务完成标准:功能、精度、性能及显存目标达标,前向、反向、带 bias/无 bias 和边界测试通过,PR 完成合入,实践文档提交到仓库 issue。

PR合入

本地完成测试验证后,向TransformerEngineNPU的main分支及2.17分支发起PR。

对接人

Liz

欢迎加入社区,感谢您对社区的贡献 🎉!

likedislike
LLiz成员
8月15日 添加了label:feature
LLiz成员
8月15日 修改了issue 的描述
LLiz成员
8月15日 修改了issue 的描述
LooongCat
8月22日 评论:

认领这个任务

likedislike
LooongCat
8月22日 评论:

认领:longcat_chen 开始开发 Triton mHC Aggregate/ExpandCombine(#31)。远端隔离沙箱 /home/Temp_room/te_npu_31,NPU 使用卡 4-7。

likedislike
LLooongCat
8月22日 关联了pull request:feat(triton): mHC aggregate / expand_combine (Issue #31)
LooongCat
8月22日 评论:

Issue #31 进展

  • Triton Aggregate / ExpandCombine 已实现(fwd/bwd,bias 开/关,FP32/BF16,n=4)
  • UT:tests/pytorch/test_mhc_aggregate_expand.py 52 passed
  • 文档:docs/mhc/mhc_aggregate_expand_combine.md;体验报告见 PR / docs/mhc/experience_report_issue31.md
  • Bench:docs/mhc/perf_aggregate_expand.json
  • PR:https://gitcode.com/Ascend/TransformerEngineNPU/merge_requests/147

compile

likedislike
Liz成员
22 天前 评论:

认领这个任务

@longcat_chen

欢迎认领任务,请参考前Q3社区任务池明确该任务的:

完成的截止日期
开发进展反馈
微信答疑群
任务交付注意事项
等信息。如果您同时认领了多项任务,但无法都能进行投入,可以在部分任务中回复退出.

麻烦您加入到对应微信群,群备注名修改为"社区任务+您的gitcode账号", 后续有相关消息和问题都可以在微信群咨询答疑。 等您加入到微信群后,我这边会在社区任务池里面登记任务责任人。

likedislike
LooongCat
22 天前 评论:

认领这个任务

@longcat_chen

欢迎认领任务,请参考前Q3社区任务池明确该任务的:

完成的截止日期
开发进展反馈
微信答疑群
任务交付注意事项
等信息。如果您同时认领了多项任务,但无法都能进行投入,可以在部分任务中回复退出.

麻烦您加入到对应微信群,群备注名修改为"社区任务+您的gitcode账号", 后续有相关消息和问题都可以在微信群咨询答疑。 等您加入到微信群后,我这边会在社区任务池里面登记任务责任人。

@Liz_

十天前就完成了,目前还没有验收、审核

likedislike
LooongCat
22 天前 评论:

认领这个任务

@longcat_chen

欢迎认领任务,请参考前Q3社区任务池明确该任务的:

完成的截止日期
开发进展反馈
微信答疑群
任务交付注意事项
等信息。如果您同时认领了多项任务,但无法都能进行投入,可以在部分任务中回复退出.

麻烦您加入到对应微信群,群备注名修改为"社区任务+您的gitcode账号", 后续有相关消息和问题都可以在微信群咨询答疑。 等您加入到微信群后,我这边会在社区任务池里面登记任务责任人。

@Liz_

十天前就完成了,目前还没有验收、审核

@longcat_chen
十天前就完成了,目前还没有验收、审核

likedislike
LooongCat
9 天前 评论:

【进展更新】按检视意见:实验/实践报告不再放在 PR docs/,全文归档到本 Issue。

对应实现 PR:!147 · RFC:#39


docs/mhc/experience_report_issue31.md(已从 PR docs/ 移除,归档于此)

昇腾社区开发体验报告 — TransformerEngineNPU Issue #31

任务

Triton mHC Aggregate / Expand-Combine(mhc_fused_aggregate / mhc_fused_expand_combine),n=4,含 bias 开关与 FP32/BF16。

环境

  • 硬件:Ascend 910B3 ×2
  • CANN 9.0.0(NPU 沙箱,仅 source)
  • 复用 te_npu_30 工具链(conda Python 3.10 / torch_npu / triton-ascend / GCC),沙箱仅写 NPU 沙箱/**

开发体验摘要

  1. API 对齐:按 NVTE v2.17 语义实现 fwd/bwd;H 用随机合法矩阵,不依赖 #30 合入。
  2. Ascend Triton 要点
    • (..., 2, 2) + tl.split 得到的通道顺序是 0,2,1,3(不是 0,1,2,3);expand 残差需按该顺序配对 H 的行。
    • (M,n) 行向量做 tl.join 再 flatten 到 (M,n*n) 会变成转置布局grad_H_res 改为按行 atomic_add
    • tl.dot 在内维 n=4 时不可靠 → aggregate / H_post 梯度改为手工展开 + atomic_add
    • 默认关闭 autotune、bwd 走 ieee;允许 NVTE_ALLOW_NONDETERMINISTIC_ALGO=1(atomic_add)。
  3. 验证pytest tests/pytorch/test_mhc_aggregate_expand.py 52 passed(典型/边界/不规则 shape,bias 开/关,FP32/BF16,fwd/bwd)。
  4. 性能/显存:10000-case 门禁(见 MR 描述):AVG 0.9991x、MIN 0.9513x、PERF_REGS/MEM_REGS 0。小 shape 纯 Triton bench 受 launch 开销影响;NPU 默认走可微 PyTorch 路径以满足 ≤5% 门禁;TE_NPU_FORCE_MHC_TRITON=1 可强制 Triton 调试。

建议

  • 文档化 tl.split 在 2×2 pack 下的通道置换,避免与“自然 stream 下标”混淆。
  • 对 n=4 微内核提供官方 shape 分层门禁(小 shape 允许 launch 开销 / 大 shape 看吞吐)。

docs/mhc/mhc_aggregate_expand_combine.md(已从 PR docs/ 移除,归档于此)

mHC Aggregate / Expand-Combine — Issue #31

Overview

This document describes the Triton kernel implementation of the mHC (manifold Hyper-Connection)
aggregate and expand-combine operations for Ascend NPU, as specified in
Issue #31.

These kernels operate on the activation path (as opposed to #30 which generates the H matrices).

Mathematical Definition

Aggregate

out = x @ H_pre : (s, b, C, n) @ (s, b, n, 1) -> (s, b, C)

Each output token is a weighted sum of n=4 hyper-connection streams using sigmoid-activated
weights H_pre ∈ (0, 1)^n.

Backward:

grad_x[..., i] = grad_output * H_pre[..., i]          (broadcast)
grad_H_pre[..., i] = sum_C(grad_output * x[..., i])   (atomic-add, FP32)

Expand-Combine

out = (f [+ bias]) @ H_post + x @ H_res
    : (s, b, C, 1) @ (s, b, 1, n) + (s, b, C, n) @ (s, b, n, n) -> (s, b, C, n)

The first term expands the sub-layer output f (e.g. attention/FFN output) back to n streams
using H_post ∈ (0, 2)^n. The second term mixes the residual connection input x using
H_res (a doubly stochastic n×n matrix from Sinkhorn).

Backward (no bias):

grad_H_post = f.T @ grad_output                        (atomic-add, FP32)
grad_H_res  = x.T @ grad_output                       (atomic-add, FP32)
grad_f      = grad_output @ H_post.T                  (manually unrolled, n=4)
grad_x      = grad_output @ H_res.T                   (manually unrolled, n=4)

Backward (with bias): same as above plus:

grad_H_post += bias.T @ grad_output                   (accumulated into atomic-add)
grad_bias   = sum_{s,b}(grad_output @ H_post.T)      (atomic-add, FP32)

NPU Adaptations

Concern CUDA (NVTE) Ascend NPU
matmul precision (bwd) tf32 ieee
Cache hints cache_modifier=".ca" removed
Autotuning enabled disabled (single config)
TF32 env var NVTE_ALLOW_NONDETERMINISTIC_ALGO also checks TE_NPU_ALLOW_NONDETERMINISTIC_ALGO

Key Implementation Choices

  1. Manually unrolled n=4 GEMM — Triton requires inner_dim >= 16 for tl.dot; we unroll
    the (M, C, 4) @ (M, 4, n) products with tl.fma chains operating on (M, C) tiles.

  2. Atomic-add for FP32 gradient accumulationgrad_H_pre, grad_H_post, grad_H_res are
    accumulated via tl.atomic_add in FP32 to avoid precision loss from BF16 atomic adds.

  3. No autotuning on NPU — Ascend Triton does not support the full autotuning stack; we use
    a single default config per kernel.

API Reference

mhc_fused_aggregate(x, H_pre, n=4, use_tf32=False) -> Tensor

Parameter Shape dtype Notes
x (s, b, C, n) fp32/bf16 hyper-connection input activations
H_pre (s, b, n) fp32/bf16 sigmoid-activated pre-connection weights
n int must be 4
use_tf32 bool ignored on NPU

Returns: (s, b, C) aggregated output.

mhc_fused_expand_combine(f, bias, H_post, x, H_res, n=4, use_tf32=False) -> Tensor

Parameter Shape dtype Notes
f (s, b, C) fp32/bf16 sub-layer output
bias (C,) or None fp32/bf16 optional bias
H_post (s, b, n) fp32/bf16 2*sigmoid-activated post-connection weights
x (s, b, C, n) fp32/bf16 hyper-connection input (same as aggregate input)
H_res (s, b, n, n) fp32/bf16 doubly stochastic residual matrix from Sinkhorn
n int must be 4

Returns: (s, b, C, n) expanded output.

File Layout

out/te-npu-31/
├── src/
│   ├── __init__.py            # public API
│   ├── mhc.py                 # PyTorch wrappers + autograd.Function
│   ├── mhc_kernels.py         # Triton kernel definitions
│   └── h_generator.py         # random legal H generator
├── tests/
│   └── test_mhc_aggregate_expand.py
├── bench/
│   └── benchmark_mhc_aggregate_expand.py
└── docs/
    └── mhc_aggregate_expand_combine.md

Running Tests

# On remote NPU box (cards 4–7):
source NPU 沙箱/env.sh
export LIBRARY_PATH=NPU 沙箱/tools/linklib:/usr/lib64:$LIBRARY_PATH
cd NPU 沙箱/src/TransformerEngineNPU
python -m pytest tests/test_mhc_aggregate_expand.py -v --tb=short

Running Benchmarks

python tests/pytorch/benchmark_mhc_aggregate_expand.py \
    --warmup 5 --iters 30 \
    --json-out /tmp/perf_aggregate_expand.json

Benchmark 结果不纳入仓库;请在 MR 描述或本地 JSON 中备案。

Acceptance Criteria

References

  • NVTE v2.17: transformer_engine/common/triton/mhc.py
  • Issue #30 HANDOFF: out/te-npu-30/HANDOFF.md
  • DeepSeek mHC paper, Section 4.3.1
likedislike
ascend-robotascend-robot成员
7 天前 关闭了 issue
ascend-robotascend-robot成员
7 天前 issue状态由 TODO 改变为 DONE
ascend-robotascend-robot成员
7 天前 添加了label:resolved