-
Notifications
You must be signed in to change notification settings - Fork 1k
add fuse_layer_norm_grad functor and op #10612
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
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1558,6 +1558,14 @@ | |
signature: "Tensor (Tensor dy, Tensor x, Tensor mean, Tensor inv_variance, Tensor gamma, Int64 begin_norm_axis, Double epsilon) => LayerNormAffineGrad" | ||
bind_python: False | ||
|
||
- name: "fuse_layer_norm_grad" | ||
signature: "TensorTuple (Tensor dy, Tensor x, Tensor mean, Tensor inv_variance, Int64 begin_norm_axis, Int64 begin_params_axis, Double epsilon) => FuseLayerNormGrad" | ||
bind_python: False | ||
|
||
- name: "fuse_layer_norm_affine_grad" | ||
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. fuse_layer_norm_grad fuse_layer_norm_affine_grad 建议只保留一个 |
||
signature: "TensorTuple (Tensor dy, Tensor x, Tensor mean, Tensor inv_variance, Tensor gamma, Int64 begin_norm_axis, Int64 begin_params_axis, Double epsilon) => FuseLayerNormAffineGrad" | ||
bind_python: False | ||
|
||
- name: "layer_norm_param_grad" | ||
signature: "TensorTuple (Tensor dy, Tensor x, Tensor mean, Tensor inv_variance, Int64 begin_params_axis) => LayerNormParamGrad" | ||
bind_python: False | ||
|
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 |
---|---|---|
|
@@ -983,6 +983,64 @@ class LayerNormAffineGradFunctor { | |
std::shared_ptr<OpExpr> op_; | ||
}; | ||
|
||
class FuseLayerNormGradFunctor { | ||
crazy-JiangDongHua marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
FuseLayerNormGradFunctor() { | ||
op_ = CHECK_JUST(one::OpBuilder("fuse_layer_norm_grad") | ||
.Input("dy") | ||
.Input("x") | ||
.Input("mean") | ||
.Input("inv_variance") | ||
.Output("dx") | ||
.Output("gamma_diff") | ||
.Output("beta_diff") | ||
.Build()); | ||
} | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& dy, | ||
const std::shared_ptr<one::Tensor>& x, | ||
const std::shared_ptr<one::Tensor>& mean, | ||
const std::shared_ptr<one::Tensor>& inv_variance, | ||
const int64_t& begin_norm_axis, const int64_t& begin_params_axis, | ||
const double& epsilon) const { | ||
auto& attrs = THREAD_CACHED_MUTABLE_ATTR_MAP("begin_norm_axis", "begin_params_axis", "epsilon"); | ||
attrs.SetAllAttrs(begin_norm_axis, begin_params_axis, epsilon); | ||
return OpInterpUtil::Dispatch<TensorTuple>(*op_, {dy, x, mean, inv_variance}, attrs); | ||
} | ||
|
||
private: | ||
std::shared_ptr<OpExpr> op_; | ||
}; | ||
|
||
class FuseLayerNormAffineGradFunctor { | ||
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. 建议只保留带affine功能的这个 |
||
public: | ||
FuseLayerNormAffineGradFunctor() { | ||
op_ = CHECK_JUST(one::OpBuilder("fuse_layer_norm_grad") | ||
.Input("dy") | ||
.Input("x") | ||
.Input("mean") | ||
.Input("inv_variance") | ||
.Input("gamma") | ||
.Output("dx") | ||
.Output("gamma_diff") | ||
.Output("beta_diff") | ||
.Build()); | ||
} | ||
Maybe<TensorTuple> operator()(const std::shared_ptr<one::Tensor>& dy, | ||
const std::shared_ptr<one::Tensor>& x, | ||
const std::shared_ptr<one::Tensor>& mean, | ||
const std::shared_ptr<one::Tensor>& inv_variance, | ||
const std::shared_ptr<one::Tensor>& gamma, | ||
const int64_t& begin_norm_axis, const int64_t& begin_params_axis, | ||
const double& epsilon) const { | ||
auto& attrs = THREAD_CACHED_MUTABLE_ATTR_MAP("begin_norm_axis", "begin_params_axis", "epsilon"); | ||
attrs.SetAllAttrs(begin_norm_axis, begin_params_axis, epsilon); | ||
return OpInterpUtil::Dispatch<TensorTuple>(*op_, {dy, x, mean, inv_variance, gamma}, attrs); | ||
} | ||
|
||
private: | ||
std::shared_ptr<OpExpr> op_; | ||
}; | ||
|
||
class LayerNormParamGradFunctor { | ||
public: | ||
LayerNormParamGradFunctor() { | ||
|
@@ -1707,6 +1765,8 @@ ONEFLOW_FUNCTION_LIBRARY(m) { | |
m.add_functor<impl::LayerNormGradFunctor>("LayerNormGrad"); | ||
m.add_functor<impl::LayerNormAffineGradFunctor>("LayerNormAffineGrad"); | ||
m.add_functor<impl::LayerNormParamGradFunctor>("LayerNormParamGrad"); | ||
m.add_functor<impl::FuseLayerNormGradFunctor>("FuseLayerNormGrad"); | ||
m.add_functor<impl::FuseLayerNormAffineGradFunctor>("FuseLayerNormAffineGrad"); | ||
m.add_functor<impl::GroupNormGradFunctor>("GroupNormGrad"); | ||
m.add_functor<impl::GroupNormParamGradFunctor>("GroupNormParamGrad"); | ||
m.add_functor<impl::BroadcastMatmulGradBFunctor>("BroadcastMatmulGradB"); | ||
|
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Copyright 2020 The OneFlow Authors. All rights reserved. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
#include "oneflow/core/framework/framework.h" | ||
|
||
namespace oneflow { | ||
|
||
template<typename T> | ||
class FuseLayerNormGradCpuKernel final : public user_op::OpKernel { | ||
public: | ||
FuseLayerNormGradCpuKernel() = default; | ||
~FuseLayerNormGradCpuKernel() = default; | ||
|
||
private: | ||
bool AlwaysComputeWhenAllOutputsEmpty() const override { return false; } | ||
void Compute(user_op::KernelComputeContext* ctx) const override { TODO(); }; | ||
}; | ||
|
||
#define REGISTER_FUSE_LAYER_NORM_GRAD_CPU_KERNEL(dtype) \ | ||
REGISTER_USER_KERNEL("fuse_layer_norm_grad") \ | ||
.SetCreateFn<FuseLayerNormGradCpuKernel<dtype>>() \ | ||
.SetIsMatchedHob((user_op::HobDeviceType() == DeviceType::kCPU) \ | ||
&& (user_op::HobDataType("dy", 0) == GetDataType<dtype>::value)); | ||
|
||
REGISTER_FUSE_LAYER_NORM_GRAD_CPU_KERNEL(float) | ||
REGISTER_FUSE_LAYER_NORM_GRAD_CPU_KERNEL(double) | ||
|
||
} // namespace oneflow |
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.
Uh oh!
There was an error while loading. Please reload this page.