Skip to content

[PAA+OPT] Layer normalization accuracy alignment and performance optimization#78321

Merged
zhengshengning merged 13 commits intoPaddlePaddle:developfrom
zhengshengning:acc_layer_norm
Mar 26, 2026
Merged

[PAA+OPT] Layer normalization accuracy alignment and performance optimization#78321
zhengshengning merged 13 commits intoPaddlePaddle:developfrom
zhengshengning:acc_layer_norm

Conversation

@zhengshengning
Copy link
Copy Markdown
Contributor

@zhengshengning zhengshengning commented Mar 16, 2026

PR Category

Operator Mechanism

PR Types

Performance

Description

背景与动机

当前 Paddle 的 layer_norm 算子在 GPU 上使用的 kernel 与 PyTorch 的实现存在精度差异,主要体现在:

  • epsilon 参数类型为 float,在某些场景下精度不足
  • GPU kernel 中 mean/variance 的计算采用的数值方法与 PyTorch 不同

本 PR 通过引入精度兼容(accuracy-compatible)kernel,使 Paddle 的 layer_norm 在开启 FLAGS_use_accuracy_compatible_kernel 标志时,能够与 PyTorch 对齐精度。

主要改动

  1. epsilon 参数精度提升:将 layer_norm 所有 kernel(CPU、GPU、XPU、OneDNN)及 InferMeta 中的 epsilon 参数类型从 float 改为 double,避免精度截断。

  2. 新增精度兼容 CUDA kernel:在 rms_norm_cuda_kernel.h 中新增 LayerNormFwdCompatKernel(前向)和 LayerNormBwdCompatKernel(反向),基于 Welford 在线算法实现数值稳定的 mean/variance 计算,与 PyTorch 的 layer_norm kernel 对齐。

  3. 精度兼容开关:通过 FLAGS_use_accuracy_compatible_kernel 全局标志控制是否启用精度兼容 kernel,默认关闭,不影响现有行为。

  4. YAML 同步更新ops.yamlbackward.yamllayer_norm 的 epsilon 类型同步更新为 double

是否引起精度变化

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

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精度对齐的分支(性能更好)。
image

@paddle-bot
Copy link
Copy Markdown

paddle-bot Bot commented Mar 16, 2026

你的PR提交成功,感谢你对开源项目的贡献!
请关注后续CI自动化测试结果,详情请参考Paddle-CI手册
Your PR has been submitted. Thanks for your contribution!
Please wait for the result of CI firstly. See Paddle CI Manual for details.

@zhengshengning zhengshengning changed the title [PAA] Align layer_norm precision with PyTorch by adding accuracy-compatible kernels [PAA+OPT] Layer normalization accuracy alignment and performance optimization Mar 19, 2026
@codecov-commenter
Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.40506% with 6 lines in your changes missing coverage. Please review.
⚠️ Please upload report for BASE (develop@53b56df). Learn more about missing BASE report.

Files with missing lines Patch % Lines
...addle/fluid/ir_adaptor/translator/op_translator.cc 87.09% 4 Missing ⚠️
.../core/distributed/auto_parallel/inferspmd_utils.cc 77.77% 2 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

wanghuancoder
wanghuancoder previously approved these changes Mar 20, 2026
Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@lugimzzz lugimzzz left a comment

Choose a reason for hiding this comment

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

lgtm

Copy link
Copy Markdown
Contributor

@DrRyanHuang DrRyanHuang left a comment

Choose a reason for hiding this comment

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

有速度对比数据嘛,有的话辛苦也贴一下~

? 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.

? 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"))
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.

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 =
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.

下面的 PADDLE_GET_CONST 也改成 double

Copy link
Copy Markdown
Contributor

@wanghuancoder wanghuancoder left a comment

Choose a reason for hiding this comment

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

LGTM

Copy link
Copy Markdown
Contributor

@From00 From00 left a comment

Choose a reason for hiding this comment

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

LGTM

@zhengshengning zhengshengning merged commit 390b532 into PaddlePaddle:develop Mar 26, 2026
185 of 213 checks passed
zhengshengning added a commit to zhengshengning/Paddle that referenced this pull request Mar 26, 2026
sneaxiy pushed a commit that referenced this pull request Mar 31, 2026
…rformance optimization #78321 (#78490)

* [PAA+OPT] Layer normalization accuracy alignment and performance optimization

* fix

* fix2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants