[PAA+OPT] Layer normalization accuracy alignment and performance optimization#78321
Merged
zhengshengning merged 13 commits intoPaddlePaddle:developfrom Mar 26, 2026
Merged
Conversation
|
你的PR提交成功,感谢你对开源项目的贡献! |
6d43903 to
e0d27dd
Compare
8b4d0ce to
7ef2871
Compare
8895c5e to
87854d2
Compare
fe36951 to
f3e8cd8
Compare
7ace83c to
17cd33b
Compare
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #78321 +/- ##
==========================================
Coverage ? 92.40%
==========================================
Files ? 13
Lines ? 79
Branches ? 0
==========================================
Hits ? 73
Misses ? 6
Partials ? 0 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
DrRyanHuang
reviewed
Mar 23, 2026
| ? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon")) | ||
| : 1e-5f; | ||
| const double eps = op_desc.HasAttr("epsilon") | ||
| ? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon")) |
This comment was marked as outdated.
This comment was marked as outdated.
Sorry, something went wrong.
| ? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon")) | ||
| : 1e-5f; | ||
| const double eps = op_desc.HasAttr("epsilon") | ||
| ? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon")) |
Contributor
There was a problem hiding this comment.
Suggested change
| ? PADDLE_GET_CONST(float, op_desc.GetAttr("epsilon")) | |
| ? PADDLE_GET_CONST(double, op_desc.GetAttr("epsilon")) |
| int begin_norm_axis = | ||
| PADDLE_GET_CONST(int, layer_norm_op->Op()->GetAttr("begin_norm_axis")); | ||
| float eps = | ||
| double eps = |
Contributor
There was a problem hiding this comment.
下面的 PADDLE_GET_CONST 也改成 double
xiaoguoguo626807
approved these changes
Mar 25, 2026
Jiang-Jia-Jun
approved these changes
Mar 25, 2026
390b532
into
PaddlePaddle:develop
185 of 213 checks passed
zhengshengning
added a commit
to zhengshengning/Paddle
that referenced
this pull request
Mar 26, 2026
5 tasks
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
PR Category
Operator Mechanism
PR Types
Performance
Description
背景与动机
当前 Paddle 的
layer_norm算子在 GPU 上使用的 kernel 与 PyTorch 的实现存在精度差异,主要体现在:float,在某些场景下精度不足本 PR 通过引入精度兼容(accuracy-compatible)kernel,使 Paddle 的
layer_norm在开启FLAGS_use_accuracy_compatible_kernel标志时,能够与 PyTorch 对齐精度。主要改动
epsilon 参数精度提升:将
layer_norm所有 kernel(CPU、GPU、XPU、OneDNN)及 InferMeta 中的epsilon参数类型从float改为double,避免精度截断。新增精度兼容 CUDA kernel:在
rms_norm_cuda_kernel.h中新增LayerNormFwdCompatKernel(前向)和LayerNormBwdCompatKernel(反向),基于 Welford 在线算法实现数值稳定的 mean/variance 计算,与 PyTorch 的layer_normkernel 对齐。精度兼容开关:通过
FLAGS_use_accuracy_compatible_kernel全局标志控制是否启用精度兼容 kernel,默认关闭,不影响现有行为。YAML 同步更新:
ops.yaml和backward.yaml中layer_norm的 epsilon 类型同步更新为double。是否引起精度变化
是。

FLAGS_use_accuracy_compatible_kernel打开时:
当
FLAGS_use_accuracy_compatible_kernel开启时,layer_norm的前向和反向计算将使用与 PyTorch 对齐的高精度 kernel,计算结果会有精度变化(更接近 PyTorch)。FLAGS_use_accuracy_compatible_kernel默认关闭时:

默认FLAGS_use_accuracy_compatible_kernel关闭状态下,当layer_nrom不走高性能版本(FAST_LN_V1和FAST_LN_V2)时,且(!isPowerOfTwo(feature_size) && feature_size > 1024)时将会走与Torch精度对齐的分支(性能更好)。