-
Notifications
You must be signed in to change notification settings - Fork 262
feat(npu): route gated norm to aclnn layer norm fwd #1919
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
Fengfengst123
wants to merge
1
commit into
xLLM-AI:main
Choose a base branch
from
Fengfengst123:codex-aclnn-layernorm-fwd-runtime
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Submodule xllm_ops
updated
from bbbdfe to 6aeb3f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| /* Copyright 2026 The xLLM Authors. All Rights Reserved. | ||
|
|
||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| https://github.com/jd-opensource/xllm/blob/main/LICENSE | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| ==============================================================================*/ | ||
|
|
||
| #include "core/kernels/npu/aclnn/pytorch_npu_helper.hpp" | ||
| #include "xllm_ops_api.h" | ||
|
|
||
| namespace xllm::kernel::npu { | ||
| namespace { | ||
|
|
||
| bool is_supported_layer_norm_dtype(at::ScalarType dtype) { | ||
| return dtype == at::kHalf || dtype == at::kBFloat16 || dtype == at::kFloat; | ||
| } | ||
|
|
||
| torch::Tensor make_contiguous_2d(const torch::Tensor& tensor) { | ||
| return tensor.reshape({-1, tensor.size(-1)}).contiguous(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| torch::Tensor layer_norm_fwd_aclnn(const torch::Tensor& x, | ||
| const torch::Tensor& weight, | ||
| const torch::Tensor& bias, | ||
| double eps, | ||
| const std::optional<torch::Tensor>& z, | ||
| int64_t group_size, | ||
| bool norm_before_gate, | ||
| bool is_rms_norm) { | ||
| TORCH_CHECK(x.defined(), "layer_norm_fwd_aclnn: x must be defined"); | ||
| TORCH_CHECK(weight.defined(), | ||
| "layer_norm_fwd_aclnn: weight must be defined"); | ||
| TORCH_CHECK(x.dim() >= 1, | ||
| "layer_norm_fwd_aclnn: x must have at least 1 dim"); | ||
| TORCH_CHECK(is_supported_layer_norm_dtype(x.scalar_type()), | ||
| "layer_norm_fwd_aclnn: x dtype must be fp16, bf16 or fp32, got ", | ||
| x.scalar_type()); | ||
| TORCH_CHECK(weight.dim() == 1, | ||
| "layer_norm_fwd_aclnn: weight must be 1D"); | ||
|
|
||
| const int64_t full_n = x.size(-1); | ||
| if (group_size < 0) { | ||
| group_size = full_n; | ||
| } | ||
| TORCH_CHECK(group_size > 0, | ||
| "layer_norm_fwd_aclnn: group_size must be positive"); | ||
| TORCH_CHECK(full_n % group_size == 0, | ||
| "layer_norm_fwd_aclnn: last dim ", | ||
| full_n, | ||
| " must be divisible by group_size ", | ||
| group_size); | ||
| TORCH_CHECK(weight.numel() == full_n, | ||
| "layer_norm_fwd_aclnn: weight numel must equal x last dim"); | ||
|
|
||
| if (bias.defined()) { | ||
| TORCH_CHECK(bias.dim() == 1, | ||
| "layer_norm_fwd_aclnn: bias must be 1D"); | ||
| TORCH_CHECK(bias.numel() == full_n, | ||
| "layer_norm_fwd_aclnn: bias numel must equal x last dim"); | ||
| } | ||
| if (z.has_value() && z->defined()) { | ||
| TORCH_CHECK(z->sizes() == x.sizes(), | ||
| "layer_norm_fwd_aclnn: z shape must match x"); | ||
| } | ||
|
|
||
| torch::Tensor x_2d = make_contiguous_2d(x); | ||
| torch::Tensor weight_contiguous = weight.contiguous(); | ||
|
|
||
| c10::optional<torch::Tensor> bias_contiguous = c10::nullopt; | ||
| if (bias.defined()) { | ||
| bias_contiguous = bias.contiguous(); | ||
| } | ||
|
|
||
| c10::optional<torch::Tensor> z_2d = c10::nullopt; | ||
| if (z.has_value() && z->defined()) { | ||
| z_2d = make_contiguous_2d(*z); | ||
| } | ||
|
|
||
| torch::Tensor y_2d = torch::empty_like(x_2d); | ||
| const int64_t m = x_2d.size(0); | ||
| const int64_t group_count = full_n / group_size; | ||
| torch::Tensor mean = | ||
| is_rms_norm | ||
| ? torch::empty({0}, x.options().dtype(at::kFloat)) | ||
| : torch::empty({group_count * m}, x.options().dtype(at::kFloat)); | ||
| torch::Tensor rstd = | ||
| torch::empty({group_count * m}, x.options().dtype(at::kFloat)); | ||
|
|
||
| const float eps_f = static_cast<float>(eps); | ||
| EXEC_NPU_CMD(aclnnLayerNormFwd, | ||
| x_2d, | ||
| weight_contiguous, | ||
| bias_contiguous, | ||
| z_2d, | ||
| eps_f, | ||
| group_size, | ||
| norm_before_gate, | ||
| is_rms_norm, | ||
| y_2d, | ||
| mean, | ||
| rstd); | ||
|
|
||
| return y_2d.reshape(x.sizes()); | ||
| } | ||
|
|
||
| } // namespace xllm::kernel::npu |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why add ?