-
Notifications
You must be signed in to change notification settings - Fork 5.8k
[Speed]Refine elementwise_mul_op gradient functor #8810
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
Merged
chengduoZH
merged 1 commit into
PaddlePaddle:develop
from
chengduoZH:feature/refine_elementwise_mul
Mar 7, 2018
Merged
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
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 |
---|---|---|
|
@@ -40,80 +40,14 @@ class ElementwiseMulKernel : public framework::OpKernel<T> { | |
}; | ||
|
||
template <typename T> | ||
struct ElementwiseMulGradFunctor { | ||
template <typename Device, typename X, typename Y, typename Z, typename dX, | ||
typename dY, typename dZ> | ||
void operator()(Device d, X x, Y y, Z z, dX dx, dY dy, dZ dz) { | ||
auto x_e = framework::EigenVector<T>::Flatten(*x); | ||
auto y_e = framework::EigenVector<T>::Flatten(*y); | ||
auto dz_e = framework::EigenVector<T>::Flatten(*dz); | ||
|
||
if (dx) { | ||
auto dx_e = framework::EigenVector<T>::Flatten(*dx); | ||
dx_e.device(d) = dz_e * y_e; | ||
} | ||
|
||
if (dy) { | ||
auto dy_e = framework::EigenVector<T>::Flatten(*dy); | ||
dy_e.device(d) = x_e * dz_e; | ||
} | ||
} | ||
struct IdentityGrad_DX { | ||
HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout * y; } | ||
}; | ||
|
||
template <typename T> | ||
struct ElementwiseMulBroadCastGradFunctor { | ||
template <typename Device, typename X, typename Y, typename Z, typename dX, | ||
typename dY, typename dZ, typename Pre, typename N> | ||
void operator()(Device d, X x, Y y, Z z, dX dx, dY dy, dZ dz, Pre pre, N n) { | ||
auto x_e = framework::EigenVector<T>::Flatten(*x); | ||
auto y_e = framework::EigenVector<T>::Flatten(*y); | ||
auto dz_e = framework::EigenVector<T>::Flatten(*dz); | ||
|
||
auto y_e_bcast = y_e.reshape(Eigen::DSizes<int, 2>(1, n)) | ||
.broadcast(Eigen::DSizes<int, 2>(pre, 1)) | ||
.reshape(Eigen::DSizes<int, 1>(x_e.size())); | ||
|
||
if (dx) { | ||
auto dx_e = framework::EigenVector<T>::Flatten(*dx); | ||
dx_e.device(d) = dz_e * y_e_bcast; | ||
} | ||
|
||
if (dy) { | ||
auto dy_e = framework::EigenVector<T>::Flatten(*dy); | ||
dy_e.device(d) = (x_e * dz_e) | ||
.reshape(Eigen::DSizes<int, 2>(pre, n)) | ||
.sum(Eigen::array<int, 1>{{0}}); | ||
} | ||
} | ||
struct IdentityGrad_DY { | ||
HOSTDEVICE T operator()(T x, T y, T out, T dout) const { return dout * x; } | ||
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. This is not a |
||
}; | ||
|
||
template <typename T> | ||
struct ElementwiseMulBroadCast2GradFunctor { | ||
template <typename Device, typename X, typename Y, typename Z, typename dX, | ||
typename dY, typename dZ, typename Pre, typename N, typename Post> | ||
void operator()(Device d, X x, Y y, Z z, dX dx, dY dy, dZ dz, Pre pre, N n, | ||
Post post) { | ||
auto x_e = framework::EigenVector<T>::Flatten(*x); | ||
auto y_e = framework::EigenVector<T>::Flatten(*y); | ||
auto dz_e = framework::EigenVector<T>::Flatten(*dz); | ||
|
||
auto y_e_bcast = y_e.reshape(Eigen::DSizes<int, 3>(1, n, 1)) | ||
.broadcast(Eigen::DSizes<int, 3>(pre, 1, post)) | ||
.reshape(Eigen::DSizes<int, 1>(x_e.size())); | ||
if (dx) { | ||
auto dx_e = framework::EigenVector<T>::Flatten(*dx); | ||
dx_e.device(d) = dz_e * y_e_bcast; | ||
} | ||
|
||
if (dy) { | ||
auto dy_e = framework::EigenVector<T>::Flatten(*dy); | ||
dy_e.device(d) = (x_e * dz_e) | ||
.reshape(Eigen::DSizes<int, 3>(pre, n, post)) | ||
.sum(Eigen::array<int, 2>{{0, 2}}); | ||
} | ||
} | ||
}; | ||
|
||
template <typename DeviceContext, typename T> | ||
class ElementwiseMulGradKernel : public framework::OpKernel<T> { | ||
public: | ||
|
@@ -127,12 +61,11 @@ class ElementwiseMulGradKernel : public framework::OpKernel<T> { | |
auto* dx = ctx.Output<Tensor>(framework::GradVarName("X")); | ||
auto* dy = ctx.Output<Tensor>(framework::GradVarName("Y")); | ||
int axis = ctx.Attr<int>("axis"); | ||
ElementwiseGradCompute<DeviceContext, T, ElementwiseMulGradFunctor<T>, | ||
ElementwiseMulBroadCastGradFunctor<T>, | ||
ElementwiseMulBroadCast2GradFunctor<T>>( | ||
ctx, x, y, out, dout, axis, dx, dy); | ||
ElemwiseGradCompute<DeviceContext, T, IdentityGrad_DX<T>, | ||
IdentityGrad_DY<T>>(ctx, *x, *y, *out, *dout, axis, dx, | ||
dy, IdentityGrad_DX<T>(), | ||
IdentityGrad_DY<T>()); | ||
} | ||
}; | ||
|
||
} // namespace operators | ||
} // namespace paddle |
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
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.
This is not a
IdentityGrad
functor.Please use a better name.
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.
It has been renamed here.