-
Notifications
You must be signed in to change notification settings - Fork 760
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
add some fused kernels #6635
Merged
Merged
add some fused kernels #6635
Changes from 6 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
e7d07c3
fix errors, op with dropout successes, but op without dropout has error
dangkai4u 992583d
fix errors, success
dangkai4u f67dfa1
fix typo error
dangkai4u 7a8e983
test dropout
dangkai4u 0968658
add comments
dangkai4u 216a910
fix typos
dangkai4u 0586041
Merge branch 'master' into fused_kernel
oneflow-ci-bot 71b94b1
change format
dangkai4u 2e4aff3
Merge branch 'fused_kernel' of https://github.com/Oneflow-Inc/oneflow…
dangkai4u 8d9fb51
reformat file
dangkai4u 59dd2b0
fix error
dangkai4u 3525d75
change format
dangkai4u 5e929bb
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
dangkai4u 5319a2e
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
dangkai4u 7e2c46f
remove useless head file
dangkai4u 581db45
fix errors
dangkai4u b1bc2c8
fix errors
dangkai4u 5240887
reformat
dangkai4u 3d59716
fix errors
dangkai4u cc5fb8c
reformat
dangkai4u d098c33
fix errors
dangkai4u 5f4dfda
Merge branch 'master' into fused_kernel
MARD1NO 1102afb
Merge branch 'master' into fused_kernel
oneflow-ci-bot 36436b7
Merge branch 'master' into fused_kernel
dangkai4u 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
85 changes: 85 additions & 0 deletions
85
oneflow/core/autograd/gradient_funcs/fused_scale_mask_softmax.cpp
This file contains 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,85 @@ | ||
/* | ||
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/op_expr_grad_function.h" | ||
#include "oneflow/core/framework/op_builder.h" | ||
#include "oneflow/core/framework/op_expr.h" | ||
#include "oneflow/core/framework/op_expr_helper.h" | ||
#include "oneflow/core/framework/op_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/functional/functional.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct FusedScaleMaskSoftmaxInterState : public AutoGradCaptureState { | ||
bool input_requires_grad = false; | ||
float scale = 1.0; | ||
}; | ||
|
||
class FusedScaleMaskSoftmax : public OpExprGradFunction<FusedScaleMaskSoftmaxInterState> { | ||
public: | ||
Maybe<void> Init(const OpExpr& op) override; | ||
Maybe<void> Capture(FusedScaleMaskSoftmaxInterState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override; | ||
Maybe<void> Apply(const FusedScaleMaskSoftmaxInterState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override; | ||
|
||
private: | ||
AttrMap base_attrs_; | ||
}; | ||
|
||
Maybe<void> FusedScaleMaskSoftmax::Init(const OpExpr& op) { | ||
const UserOpExpr* fw_op_expr = dynamic_cast<const UserOpExpr*>(&op); | ||
CHECK_NOTNULL_OR_RETURN(fw_op_expr); | ||
base_attrs_ = MakeAttrMapFromUserOpConf(fw_op_expr->proto()); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> FusedScaleMaskSoftmax::Capture(FusedScaleMaskSoftmaxInterState* ctx, | ||
const TensorTuple& inputs, const TensorTuple& outputs, | ||
const AttrMap& attrs) const { | ||
CHECK_EQ_OR_RETURN(inputs.size(), 2); // input, mask | ||
ctx->input_requires_grad = inputs.at(0)->requires_grad(); | ||
|
||
if (!ctx->input_requires_grad) { return Maybe<void>::Ok(); } | ||
ComposedAttrMap composed_attrs(attrs, base_attrs_); | ||
ctx->scale = JUST(composed_attrs.GetAttr<float>("scale_value")); | ||
|
||
ctx->SaveTensorForBackward(inputs.at(1)); // save mask | ||
ctx->SaveTensorForBackward(outputs.at(0)); // save y, ie. softmax result | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> FusedScaleMaskSoftmax::Apply(const FusedScaleMaskSoftmaxInterState* ctx, | ||
const TensorTuple& out_grads, TensorTuple* in_grads) const { | ||
if (!ctx->input_requires_grad) { return Maybe<void>::Ok(); } | ||
|
||
CHECK_EQ_OR_RETURN(out_grads.size(), 1); // dy | ||
in_grads->resize(2); // input, mask | ||
|
||
const std::shared_ptr<oneflow::one::Tensor>& mask = ctx->SavedTensors().at(0); | ||
const std::shared_ptr<oneflow::one::Tensor>& y = ctx->SavedTensors().at(1); | ||
const std::shared_ptr<oneflow::one::Tensor>& fused_scale_mask_softmax_grad = | ||
JUST(functional::FusedScaleMaskSoftmaxGrad(y, out_grads.at(0), mask, ctx->scale)); | ||
|
||
in_grads->at(0) = fused_scale_mask_softmax_grad; | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("fused_scale_mask_softmax", FusedScaleMaskSoftmax); | ||
|
||
} // namespace one | ||
} // namespace oneflow |
89 changes: 89 additions & 0 deletions
89
oneflow/core/autograd/gradient_funcs/fused_scale_mask_softmax_dropout.cpp
This file contains 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,89 @@ | ||
/* | ||
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/op_expr_grad_function.h" | ||
#include "oneflow/core/framework/op_builder.h" | ||
#include "oneflow/core/framework/op_expr.h" | ||
#include "oneflow/core/framework/op_expr_helper.h" | ||
#include "oneflow/core/framework/op_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/functional/functional.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct FusedScaleMaskSoftmaxDropoutInterState : public AutoGradCaptureState { | ||
bool input_requires_grad = true; | ||
float scale = 1.0; | ||
float dropout_scale = 1.0; | ||
}; | ||
|
||
class FusedScaleMaskSoftmaxDropout : public OpExprGradFunction<FusedScaleMaskSoftmaxDropoutInterState> { | ||
public: | ||
Maybe<void> Init(const OpExpr& op) override; | ||
Maybe<void> Capture(FusedScaleMaskSoftmaxDropoutInterState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override; | ||
Maybe<void> Apply(const FusedScaleMaskSoftmaxDropoutInterState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override; | ||
|
||
private: | ||
AttrMap base_attrs_; | ||
}; | ||
|
||
Maybe<void> FusedScaleMaskSoftmaxDropout::Init(const OpExpr& op) { | ||
const UserOpExpr* fw_op_expr = dynamic_cast<const UserOpExpr*>(&op); | ||
CHECK_NOTNULL_OR_RETURN(fw_op_expr); | ||
base_attrs_ = MakeAttrMapFromUserOpConf(fw_op_expr->proto()); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> FusedScaleMaskSoftmaxDropout::Capture(FusedScaleMaskSoftmaxDropoutInterState* ctx, | ||
const TensorTuple& inputs, const TensorTuple& outputs, | ||
const AttrMap& attrs) const { | ||
CHECK_EQ_OR_RETURN(inputs.size(), 3); // input, mask, dropout_mask | ||
ctx->input_requires_grad = inputs.at(0)->requires_grad(); | ||
|
||
if (!ctx->input_requires_grad) { return Maybe<void>::Ok(); } | ||
ComposedAttrMap composed_attrs(attrs, base_attrs_); | ||
ctx->scale = JUST(composed_attrs.GetAttr<float>("scale_value")); | ||
ctx->dropout_scale = JUST(composed_attrs.GetAttr<float>("dropout_scale_value")); | ||
|
||
ctx->SaveTensorForBackward(inputs.at(1)); // mask | ||
ctx->SaveTensorForBackward(inputs.at(2)); // dropout_mask | ||
ctx->SaveTensorForBackward(outputs.at(1)); // softmax_y | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> FusedScaleMaskSoftmaxDropout::Apply(const FusedScaleMaskSoftmaxDropoutInterState* ctx, | ||
const TensorTuple& out_grads, TensorTuple* in_grads) const { | ||
CHECK_EQ_OR_RETURN(out_grads.size(), 2); // dy, d_softmax_y | ||
if (!ctx->input_requires_grad) { return Maybe<void>::Ok(); } | ||
in_grads->resize(3); // input, mask, dropout_mask | ||
|
||
const std::shared_ptr<oneflow::one::Tensor>& mask = ctx->SavedTensors().at(0); | ||
const std::shared_ptr<oneflow::one::Tensor>& dropout_mask = ctx->SavedTensors().at(1); | ||
const std::shared_ptr<oneflow::one::Tensor>& softmax_y = ctx->SavedTensors().at(2); | ||
const std::shared_ptr<oneflow::one::Tensor>& input_grad = | ||
JUST(functional::FusedScaleMaskSoftmaxDropoutGrad(softmax_y, out_grads.at(0), | ||
dropout_mask, mask, ctx->scale, ctx->dropout_scale)); | ||
|
||
in_grads->at(0) = input_grad; | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("fused_scale_mask_softmax_dropout", FusedScaleMaskSoftmaxDropout); | ||
|
||
} // namespace one | ||
} // namespace oneflow |
This file contains 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 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 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 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
Oops, something went wrong.
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.
同上