-
Notifications
You must be signed in to change notification settings - Fork 698
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* rewrite scalar_pow backward * refine * refine Co-authored-by: oneflow-ci-bot <[email protected]>
- Loading branch information
1 parent
1f9db98
commit 77533b9
Showing
4 changed files
with
104 additions
and
1 deletion.
There are no files selected for viewing
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,72 @@ | ||
/* | ||
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_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/framework/op_expr.h" | ||
#include "oneflow/core/framework/op_expr_helper.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct ScalarPowInterpState : public OpExprInterpState { | ||
bool requires_grad; | ||
double exponent; | ||
}; | ||
|
||
class ScalarPow : public OpExprGradFunction<ScalarPowInterpState> { | ||
public: | ||
Maybe<void> Init(const OpExpr& op) override { | ||
const auto* fw_op_expr = dynamic_cast<const UserOpExpr*>(&op); | ||
base_attrs_ = MakeAttrMapFromUserOpConf(fw_op_expr->proto()); | ||
CHECK_NOTNULL_OR_RETURN(fw_op_expr); | ||
const std::string& op_name = fw_op_expr->op_name(); | ||
grad_op_ = JUST(op_expr_helper::ScalarPowGradOp(/*exponent=*/1.0, GradientOpName(op_name))); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Capture(ScalarPowInterpState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override { | ||
CHECK_EQ_OR_RETURN(inputs.size(), 1); | ||
CHECK_EQ_OR_RETURN(outputs.size(), 1); | ||
ctx->requires_grad = inputs.at(0)->requires_grad(); | ||
if (!ctx->requires_grad) { return Maybe<void>::Ok(); } | ||
|
||
ComposedAttrMap composed_attrs(attrs, base_attrs_); | ||
ctx->exponent = JUST(composed_attrs.GetAttr<double>("exponent")); | ||
ctx->SaveTensorForBackward(inputs.at(0)); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Apply(const ScalarPowInterpState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override { | ||
const auto& x = ctx->SavedTensors().at(0); | ||
MutableAttrMap attrs; | ||
JUST(attrs.SetAttr<double>("exponent", ctx->exponent)); | ||
in_grads->resize(1); | ||
in_grads->at(0) = JUST(OpInterpUtil::Dispatch<Tensor>(*grad_op_, {x, out_grads.at(0)}, attrs)); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
private: | ||
std::shared_ptr<OpExpr> grad_op_; | ||
AttrMap base_attrs_; | ||
}; | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("scalar_pow", ScalarPow); | ||
|
||
} // 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