-
Notifications
You must be signed in to change notification settings - Fork 6k
[Operator Mechanism] Replace VCOPY/SCAL with direct copy and ForRange scale in CPU/GPU kernels #79468
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
[Operator Mechanism] Replace VCOPY/SCAL with direct copy and ForRange scale in CPU/GPU kernels #79468
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -15,8 +15,8 @@ limitations under the License. */ | |
| #pragma once | ||
|
|
||
| #include "paddle/phi/backends/cpu/cpu_context.h" | ||
| #include "paddle/phi/common/memory_utils.h" | ||
| #include "paddle/phi/core/dense_tensor.h" | ||
| #include "paddle/phi/kernels/funcs/blas/blas.h" | ||
| #include "paddle/phi/kernels/funcs/eigen/common.h" | ||
| #include "paddle/phi/kernels/funcs/elementwise_grad_base.h" | ||
|
|
||
|
|
@@ -90,13 +90,20 @@ ElementwiseAddGrad(const CPUContext& dev_ctx, | |
| DenseTensor* dx, | ||
| DenseTensor* dy, | ||
| int axis = -1) { | ||
| auto blas = funcs::GetBlas<CPUContext, T>(dev_ctx); | ||
| if (dx) { | ||
| blas.VCOPY(dout.numel(), dout.data<T>(), dev_ctx.template Alloc<T>(dx)); | ||
| memory_utils::Copy(dev_ctx.GetPlace(), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这里需要保留 inplace 别名安全:elementwise_add_grad 注册了 Out@GRAD -> X@GRAD 复用,Alloc(dx) 可能与 dout.data() 相同;CPU memory_utils::Copy 最终调用 std::memcpy,会形成 memcpy(p, p, n) 的未定义行为。建议像 GPU 路径一样先比较指针,仅在目标与 dout 不同时复制(dy 同理)。
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 因为过CI需要一定时间,所以此PR暂不修改,后续PR将会修复 |
||
| dev_ctx.template Alloc<T>(dx), | ||
| dev_ctx.GetPlace(), | ||
| dout.data<T>(), | ||
| static_cast<size_t>(dout.numel()) * sizeof(T)); | ||
| } | ||
|
|
||
| if (dy) { | ||
| blas.VCOPY(dout.numel(), dout.data<T>(), dev_ctx.template Alloc<T>(dy)); | ||
| memory_utils::Copy(dev_ctx.GetPlace(), | ||
| dev_ctx.template Alloc<T>(dy), | ||
| dev_ctx.GetPlace(), | ||
| dout.data<T>(), | ||
| static_cast<size_t>(dout.numel()) * sizeof(T)); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -79,17 +79,19 @@ struct GeluGradFunctor { | |
| funcs::CBlas<T>::AXPY(n, static_cast<T>(M_SQRT1_2), x_data, 1, first, 1); | ||
| funcs::CBlas<T>::VMERF(n, first, first, VML_LA); | ||
| for (int i = 0; i < n; i++) { | ||
| first[i] += static_cast<T>(1); | ||
| first[i] = (first[i] + static_cast<T>(1)) * static_cast<T>(0.5); | ||
| } | ||
| funcs::CBlas<T>::SCAL(n, static_cast<T>(0.5), first, 1); | ||
|
|
||
| // second = (0.5 * 2/sqrt(pi) * 1/sqrt(2) * x * exp(-0.5 * x^2)) | ||
| funcs::CBlas<T>::VSQUARE(n, x_data, second); | ||
| funcs::CBlas<T>::SCAL(n, -static_cast<T>(0.5), second, 1); | ||
| for (int i = 0; i < n; i++) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同int 索引
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. int n = std::min(x.size(), dx.size()); |
||
| second[i] = second[i] * -static_cast<T>(0.5); | ||
| } | ||
| funcs::CBlas<T>::VEXP(n, second, second); | ||
| funcs::CBlas<T>::VMUL(n, x_data, second, second); | ||
| funcs::CBlas<T>::SCAL( | ||
| n, static_cast<T>(0.5 * M_2_SQRTPI * M_SQRT1_2), second, 1); | ||
| for (int i = 0; i < n; i++) { | ||
| second[i] = second[i] * static_cast<T>(0.5 * M_2_SQRTPI * M_SQRT1_2); | ||
| } | ||
|
|
||
| // dx = dout * (first + second); | ||
| funcs::CBlas<T>::VADD(n, first, second, first); | ||
|
|
||
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.
这里的int j = 0作为for-loop索引 不会越界吗?
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.
seq_len的定义为 int seq_len = static_cast(x_lod[0][i + 1] - x_lod[0][i]); 所以这里的j并不会越界,对于seq_len的强制转换为int,暂未分析,后续再看一下