Fix paddle.incubate.nn.functional.fused_bias_dropout_residual_layer_norm big Tensor #74060
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
Bug fixes
Description
通过一些测试发现和做layernorm在实现上和torch后很大的区别。
paddle实现位置:https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/kernels/funcs/layer_norm_impl.cu.h#L445
torch实现位置:https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/cuda/layer_norm_kernel.cu#L196
paddle通过$$D(x) = E(x^2) - (E(x))^2$$的方式计算方差,而torch采用了Welford算法,在数值上更具稳定性。另外,paddle和torch在计算dx时也有很大的计算差异:
paddle实现位置:
https://github.com/PaddlePaddle/Paddle/blob/develop/paddle/phi/kernels/funcs/layer_norm_impl.cu.h#L1735
torch实现位置:
https://github.com/pytorch/pytorch/blob/main/aten/src/ATen/native/cuda/layer_norm_kernel.cu#L348
这些diff使得在feature_size在较小的情况下导致float32计算会产生上述精度diff。
在float64下正常:
Pcard-73263