Skip to content

Commit cfcde10

Browse files
committed
[Ops][Feature] Add fused FFN linear op (ffn_linear) for Ascend950
Signed-off-by: chenchris2 <1349418798@qq.com>
1 parent 7d81170 commit cfcde10

131 files changed

Lines changed: 30071 additions & 0 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

csrc/build_aclnn.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ elif [[ "$SOC_VERSION" =~ ^ascend950 ]]; then
225225
"k2q_csr"
226226
"sparse_attention_score"
227227
"mla_prolog_v3"
228+
"ffn"
228229
)
229230

230231
CUSTOM_OPS=$(IFS=';'; echo "${CUSTOM_OPS_ARRAY[*]}")

csrc/ffn/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# -----------------------------------------------------------------------------------------------------------
2+
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
3+
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+
# CANN Open Software License Agreement Version 2.0 (the "License").
5+
# Please refer to the License for details. You may not use this file except in compliance with the License.
6+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
8+
# See LICENSE in the root of the software repository for the full text of the License.
9+
# -----------------------------------------------------------------------------------------------------------
10+
11+
file(GLOB SUBDIRECTORIES LIST_DIRECTORIES true RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*)
12+
# 遍历子目录
13+
foreach(SUBDIR ${SUBDIRECTORIES})
14+
# 检查子目录中是否存在 CMakeLists.txt
15+
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${SUBDIR}/CMakeLists.txt)
16+
add_subdirectory(${SUBDIR})
17+
endif()
18+
endforeach()

csrc/ffn/README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# FFN(arch35 fused variant)
2+
3+
`y = act(x @ W1^T + b1) @ W2^T + b2`,act ∈ {gelu, silu, swiglu},bf16/fp16。
4+
5+
本目录为 **Ascend950PR(A5/arch35)融合实现**。上游通用 FFN 算子见
6+
[ops-transformer 原仓](https://gitcode.com/cann/ops-transformer/tree/master/ffn/ffn)
7+
8+
| 产品 | 是否支持 |
9+
| :--- | :---: |
10+
| Ascend 950PR / 950DT(A5, arch35) ||
11+
| Atlas A2/A3、310P 等 | 需另行适配(本树仅含 arch35 kernel) |
12+
13+
## 构建
14+
15+
算子已加入 `csrc/build_aclnn.sh``ascend950` 列表,随仓库标准链路构建:
16+
17+
```bash
18+
# 路径 A(全链路,需与仓 requirements 匹配的 torch/torch_npu 环境):
19+
export SOC_VERSION=ascend950
20+
pip install -e .
21+
22+
# 路径 B(单算子,调试用):
23+
cd csrc && bash build.sh --pkg --ops=ffn --soc=ascend950 -j8
24+
```
25+
26+
## 调用
27+
28+
```python
29+
import torch
30+
import torch_npu
31+
from vllm_ascend.utils import enable_custom_op
32+
33+
enable_custom_op()
34+
35+
y = torch.ops._C_ascend.ffn_linear(
36+
x, # [M,K] bf16
37+
weight1, # Linear 布局 [H,K](swiglu 为 [2H,K]);canonical [K,H] gelu/silu 亦支持
38+
weight2, # [N,H]
39+
bias1, bias2,# 可选(swiglu 无 bias)
40+
"gelu", # "gelu" | "silu" | "swiglu"
41+
0, # inner_precise
42+
)
43+
```
44+
45+
Meta kernel 已注册(`torch_binding_meta.cpp`),支持 symbolic tracing / ACLGraph capture。
46+
精度测试:`tests/e2e/nightly/single_node/ops/singlecard_ops/test_ffn_linear.py`
47+
(含 meta 契约、确定性、性能 smoke)。950PR 上对标 bf16 小算子链有性能收益。
48+
49+
## 已知限制
50+
51+
- swiglu 仅支持 Linear 布局 weight1 `[2H,K]`;gelu/silu 支持 canonical `[K,H]`
52+
linear `[N,K]` 双布局;**全方阵(H==K==N)歧义默认 linear**
53+
(PyTorch Linear 惯例,与独立 ops-transformer 仓有意分叉)。
54+
- 符号解析:3rd matmul tiling 头引用的 `Ops::Base::ToString` 按 csrc 框架惯例链接
55+
CANN `libops_base.so`(FindOPBASE)解析。

csrc/ffn/ffn/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# -----------------------------------------------------------------------------------------------------------
2+
# Copyright (c) 2025 Huawei Technologies Co., Ltd.
3+
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4+
# CANN Open Software License Agreement Version 2.0 (the "License").
5+
# Please refer to the License for details. You may not use this file except in compliance with the License.
6+
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7+
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8+
# See LICENSE in the root of the software repository for the full text of the License.
9+
# -----------------------------------------------------------------------------------------------------------
10+
11+
file(GLOB CURRENT_DIRS RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/*)
12+
if(NOT ENABLE_TEST AND NOT BENCHMARK)
13+
list(REMOVE_ITEM CURRENT_DIRS tests)
14+
endif()
15+
foreach(SUB_DIR ${CURRENT_DIRS})
16+
if(EXISTS "${CMAKE_CURRENT_SOURCE_DIR}/${SUB_DIR}/CMakeLists.txt")
17+
add_subdirectory(${SUB_DIR})
18+
endif()
19+
endforeach()

csrc/ffn/ffn/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# FFN(arch35 fused variant)
2+
3+
`y = act(x @ W1^T + b1) @ W2^T + b2`,act ∈ {gelu, silu, swiglu},bf16/fp16。
4+
5+
本目录为 **Ascend950PR(A5/arch35)融合实现**的单算子源码树(op_host / op_kernel,
6+
含自持 3rd mat_mul_v3 拷贝)。上游通用 FFN 算子(支持矩阵、MoE/量化等完整功能)见
7+
[ops-transformer 原仓](https://gitcode.com/cann/ops-transformer/tree/master/ffn/ffn)
8+
9+
| 产品 | 是否支持 |
10+
| :--- | :---: |
11+
| Ascend 950PR / 950DT(A5, arch35) ||
12+
| Atlas A2/A3、310P 等 | 需另行适配(本树仅含 arch35 kernel) |
13+
14+
构建/调用/基线/限制见[上级 README](../README.md)`docs/`(aclnn API 文档、设计介绍)。

0 commit comments

Comments
 (0)