Skip to content

[Feature] Add FFN op for Ascend950 - #15572

Open
chenchris2 wants to merge 1 commit into
vllm-project:mainfrom
chenchris2:feat/ffn-linear-op
Open

[Feature] Add FFN op for Ascend950#15572
chenchris2 wants to merge 1 commit into
vllm-project:mainfrom
chenchris2:feat/ffn-linear-op

Conversation

@chenchris2

@chenchris2 chenchris2 commented Sep 2, 2026

Copy link
Copy Markdown

What this PR does / why we need it?

Refs #10928
On 950PR/DT there is no official arch35 FFN yet, so the block runs as a small-op chain (linear → act → linear), paying 3 kernel launches plus the activation round-trip through GM. This PR adds a fused FFN operator:

  • Single kernel: up/down GEMMs with gelu/silu/swiglu fused into the epilogue; bf16/fp16 with fp16/bf16/fp32 bias; non-quantized, single-expert only.
  • Supports both canonical [K, N] and Linear [N, K] weight layouts.
  • Custom torch API: torch.ops._C_ascend.ffn_linear.
  • e2e test under tests/e2e/nightly/single_node/ops/singlecard_ops/.

Does this PR introduce any user-facing change?

No — it only adds a new operator; no model path or public API changes.

How was this patch tested?

Accuracy (ATK dual-benchmark: fused op vs small-op chain vs CPU-fp32 golden, level L0): 20/20 model shapes + 200+ generalization cases (incl. non-aligned) PASS.
Performance (kernel-level Task Duration vs small-op chain kernel-time sum): on-par or better on all test cases, up to ~20%; 3 launches → 1.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces a fused FFN linear operator designed to improve performance on Ascend950 hardware. By fusing the linear operations within the FFN structure, the implementation aims to reduce overhead and improve throughput for transformer-based models.

Highlights

  • Fused FFN Linear Op: Added a fused FFN linear operator (ffn_linear) specifically optimized for the Ascend950 architecture.
  • Build System Integration: Integrated the new operator into the build system via csrc/build_aclnn.sh and added necessary CMake configuration files.
  • Documentation: Added comprehensive documentation including READMEs and aclnn API specifications for the new fused FFN operator.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@github-actions github-actions Bot added documentation Improvements or additions to documentation module:tests labels Sep 2, 2026
@github-actions

github-actions Bot commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

👋 Hi! Thank you for contributing to the vLLM Ascend project. The following points will speed up your PR merge:‌‌

  • A PR should do only one thing, smaller PRs enable faster reviews.
  • Every PR should include unit tests and end-to-end tests ‌to ensure it works and is not broken by other future PRs.
  • Write the commit message by fulfilling the PR description to help reviewer and future developers understand.

If CI fails, you can run linting and testing checks locally according Contributing and Testing.


Tip

💡 Consider Linking a Related Issue or RFC

Your PR title contains the [Feature] tag, indicating a bug fix or new feature.

Linking a related issue or RFC in the PR description is strongly encouraged — it gives reviewers helpful context and speeds up the review. You can use any of these keywords:

  • Fixes #<issue_number>
  • Closes #<issue_number>
  • Resolves #<issue_number>
  • Refs #<rfc_or_issue_number> (for RFCs)

🙏 Thanks for helping us keep the project well-organized!

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for the FFN (Feed-Forward Network) operator with an optimized fused implementation for the Ascend950 (arch35) architecture. It includes new kernel tiling strategies, operator definitions, and ACLNN interfaces to support fused GELU, SILU, and SWIGLU activations. Feedback was provided regarding a potential use-after-free vulnerability in the ffn_linear function due to pointer lifetime issues and a request to improve the robustness of attribute access by using names instead of fragile numerical indices.

Comment on lines +94 to +110
std::string actStr(activation.data(), activation.size());
auto output_size = ffn_linear_npu_output_size(x_contiguous, w1_contiguous, w2_contiguous, actStr);
at::Tensor y = at::empty(output_size, x_contiguous.options());

const char *activationCStr = actStr.c_str();
const bool tokensIndexFlag = false;

EXEC_NPU_CMD(aclnnFFNV2,
x_contiguous, w1_contiguous, w2_contiguous,
expertTokens,
bias1, bias2,
scale, offset,
deqScale1, deqScale2,
antiquantScale1, antiquantScale2,
antiquantOffset1, antiquantOffset2,
activationCStr, inner_precise, tokensIndexFlag,
y);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

critical

The activationCStr pointer is derived from a local std::string variable actStr. This pointer becomes invalid once ffn_linear function returns. If EXEC_NPU_CMD involves any asynchronous operations or if the underlying aclnnFFNV2 implementation stores this pointer for later use (e.g., within the returned executor), this will lead to a use-after-free bug. This is a very dangerous pattern.

To ensure safety, the lifetime of the string data must be guaranteed for the entire duration of the asynchronous operation. Please ensure that EXEC_NPU_CMD is fully synchronous and does not hold on to the pointer, or refactor the code to manage the string's lifetime appropriately.

Comment on lines +83 to +94
if (attrsNum <= 2) {
OP_LOGE(context_->GetNodeName(), "enable_uncache attr not registered, use default.");
return ge::GRAPH_FAILED;
}
// 倒数第2个属性
auto enableUncacheAttr = attrs->GetAttrPointer<int64_t>(attrsNum - 2);
enableUncache_ = (enableUncacheAttr != nullptr && *enableUncacheAttr != 0);

auto shiftValuePtr = attrs->GetAttrPointer<int64_t>(attrsNum - 1);
shiftValue_ = shiftValuePtr ? *shiftValuePtr : 0;
shiftValue_ = shiftValue_ == 0 ? ORI_SHIFT_VALUE : shiftValue_;
supportMmadS8S4_ = true;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Accessing operator attributes by their numerical index from the end of the list (attrsNum - 2, attrsNum - 1) is highly fragile. This approach will break if attributes are added, removed, or reordered in the operator definition, leading to incorrect behavior or crashes.

It is strongly recommended to access attributes by their names to make the code more robust and maintainable. For example, use attrs->GetAttrPointer<int64_t>("enable_uncache") instead of relying on indices.

@chenchris2
chenchris2 force-pushed the feat/ffn-linear-op branch 6 times, most recently from 5380e75 to c9b48c0 Compare September 3, 2026 11:00
@chenchris2 chenchris2 changed the title [Feature][Ops] Add fused FFN linear op (ffn_linear) for Ascend950 [Feature] Add FFN op for Ascend950 Sep 3, 2026
Signed-off-by: chenchris2 <1349418798@qq.com>
@ZT-AIA ZT-AIA added the ready-precise run selected e2e test for pr label Sep 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation module:tests ready-precise run selected e2e test for pr

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants