-
Notifications
You must be signed in to change notification settings - Fork 5.9k
implementation of broadcast div backward by reduce #38044
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
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
d3173f8
add elementwise div
Zjq9409 c6cef2e
move mul and div grad functor
Zjq9409 9265a8d
Combine multiple CUDA kernels
Zjq9409 080bf95
Merge branch 'develop' into broadcast_div
Zjq9409 f0f1cf3
Update the reduce interface call
Zjq9409 8c43581
Merge branch 'develop' into broadcast_div
Zjq9409 b1f58dc
Merge branch 'develop' into broadcast_div
Zjq9409 e07e54e
add multi-output
Zjq9409 3594f6b
Merge branch 'develop' into broadcast_div
Zjq9409 7adf371
add multi-output div
Zjq9409 560ed45
add branch judge
Zjq9409 2920824
Package branch
Zjq9409 476c797
Combine the x and y functions into one
Zjq9409 8259c34
Merge branch 'develop' into broadcast_div
Zjq9409 d2f3776
Merge branch 'develop' into broadcast_div
Zjq9409 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
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
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
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 |
|---|---|---|
|
|
@@ -43,6 +43,7 @@ limitations under the License. */ | |
| #include <thrust/iterator/iterator_adaptor.h> | ||
|
|
||
| #include "paddle/fluid/operators/elementwise/elementwise_op_broadcast.cu.h" | ||
| #include "paddle/fluid/operators/reduce_ops/reduce_op.cu.h" | ||
| #include "paddle/fluid/platform/device/gpu/gpu_device_function.h" | ||
| #include "paddle/fluid/platform/device/gpu/gpu_primitives.h" | ||
|
|
||
|
|
@@ -2619,5 +2620,77 @@ static inline std::vector<int> GetReduceDim(const framework::DDim &in, | |
| } | ||
| return dims; | ||
| } | ||
|
|
||
| #if defined(__NVCC__) || defined(__HIPCC__) | ||
| template <typename T> | ||
| void ReduceWrapper(const platform::CUDADeviceContext &dev_ctx, int axis, | ||
| framework::Tensor *src, framework::Tensor *dst) { | ||
| std::vector<int> reduce_dims = GetReduceDim(dst->dims(), src->dims(), axis); | ||
| TensorReduceFunctorImpl<T, T, kps::AddFunctor, kps::IdentityFunctor<T>>( | ||
| *src, dst, kps::IdentityFunctor<T>(), reduce_dims, dev_ctx.stream()); | ||
| } | ||
|
|
||
| template <ElementwiseType ET, typename T, typename Functor> | ||
| void GetGradXAndYOut(const platform::CUDADeviceContext &dev_ctx, | ||
| const platform::Place &place, int axis, | ||
| std::vector<const framework::Tensor *> ins, | ||
| const framework::Tensor *dout, framework::Tensor *dx, | ||
| framework::Tensor *dy, Functor func) { | ||
| framework::Tensor tmp_dx; | ||
| framework::Tensor tmp_dy; | ||
| dy->mutable_data<T>(place); | ||
| std::vector<framework::Tensor *> outs; | ||
| if (dx->dims() == dout->dims() && dy->dims() == dout->dims()) { | ||
| outs = {dx, dy}; | ||
| } else if (dx->dims() != dout->dims() && dy->dims() == dout->dims()) { | ||
| tmp_dx.mutable_data<T>(dout->dims(), place); | ||
| outs = {&tmp_dx, dy}; | ||
| } else if (dx->dims() == dout->dims() && dy->dims() != dout->dims()) { | ||
| tmp_dy.mutable_data<T>(dout->dims(), place); | ||
| outs = {dx, &tmp_dy}; | ||
| } else if (dx->dims() != dout->dims() && dy->dims() != dout->dims()) { | ||
| tmp_dy.mutable_data<T>(dout->dims(), place); | ||
| tmp_dx.mutable_data<T>(dout->dims(), place); | ||
| outs = {&tmp_dx, &tmp_dy}; | ||
| } | ||
|
|
||
| LaunchElementwiseCudaKernel<ET, T, T, decltype(func), 2>(dev_ctx, ins, &outs, | ||
| axis, func); | ||
|
|
||
| if (dx->dims() != dout->dims() && dy->dims() == dout->dims()) { | ||
| ReduceWrapper<T>(dev_ctx, axis, &tmp_dx, dx); | ||
| } else if (dx->dims() == dout->dims() && dy->dims() != dout->dims()) { | ||
| ReduceWrapper<T>(dev_ctx, axis, &tmp_dy, dy); | ||
| } else if (dx->dims() != dout->dims() && dy->dims() != dout->dims()) { | ||
| ReduceWrapper<T>(dev_ctx, axis, &tmp_dx, dx); | ||
| ReduceWrapper<T>(dev_ctx, axis, &tmp_dy, dy); | ||
| } | ||
| } | ||
|
|
||
| template <ElementwiseType ET, typename T, typename Functor> | ||
| void GetGradXOrYOut(const platform::CUDADeviceContext &dev_ctx, | ||
| const platform::Place &place, int axis, | ||
| std::vector<const framework::Tensor *> ins, | ||
| const framework::Tensor *dout, framework::Tensor *dxy, | ||
| Functor func) { | ||
| framework::Tensor tmp_dxy; | ||
| dxy->mutable_data<T>(place); | ||
|
|
||
| std::vector<framework::Tensor *> outs; | ||
| if (dxy->dims() != dout->dims()) { | ||
| tmp_dxy.mutable_data<T>(dout->dims(), place); | ||
| outs = {&tmp_dxy}; | ||
| } else { | ||
| outs = {dxy}; | ||
| } | ||
|
|
||
| LaunchElementwiseCudaKernel<ET, T, T>(dev_ctx, ins, &outs, axis, func); | ||
| if (dxy->dims() != dout->dims()) { | ||
| ReduceWrapper<T>(dev_ctx, axis, &tmp_dxy, dxy); | ||
| } | ||
| } | ||
|
|
||
| #endif | ||
|
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.
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. Done. |
||
|
|
||
| } // namespace operators | ||
| } // namespace paddle | ||
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.
Uh oh!
There was an error while loading. Please reload this page.