forked from Oneflow-Inc/oneflow
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* edit tanh to a closure op * add grad-looped sin_cos_negative * add test case Co-authored-by: yoonlee888 <qiuyunlei@zhejianglab.com> Co-authored-by: Zhenhua <1209435+hengzi@users.noreply.github.com>
- Loading branch information
1 parent
c484cb9
commit c256a5a
Showing
6 changed files
with
214 additions
and
2 deletions.
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,53 @@ | ||
/* | ||
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_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/functional/functional.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct CosCaptureState : public AutoGradCaptureState { | ||
bool x_requires_grad; | ||
}; | ||
|
||
class Cos : public OpExprGradFunction<CosCaptureState> { | ||
Maybe<void> Init(const OpExpr& op) override { return Maybe<void>::Ok(); } | ||
|
||
Maybe<void> Capture(CosCaptureState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override { | ||
ctx->x_requires_grad = inputs.at(0)->requires_grad(); | ||
ctx->SaveTensorForBackward(inputs.at(0)); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Apply(const CosCaptureState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override { | ||
if (!ctx->x_requires_grad) { return Maybe<void>::Ok(); } | ||
|
||
const auto& x = ctx->SavedTensors().at(0); | ||
const auto& xx = JUST(functional::Sin(x)); | ||
const auto& xxx = JUST(functional::Negative(xx)); | ||
in_grads->at(0) = JUST(functional::Mul(out_grads.at(0), xxx)); | ||
return Maybe<void>::Ok(); | ||
} | ||
}; | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("cos", Cos); | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
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_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/functional/functional.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct NegativeCaptureState : public AutoGradCaptureState { | ||
bool x_requires_grad; | ||
}; | ||
|
||
class Negative : public OpExprGradFunction<NegativeCaptureState> { | ||
Maybe<void> Init(const OpExpr& op) override { return Maybe<void>::Ok(); } | ||
|
||
Maybe<void> Capture(NegativeCaptureState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override { | ||
ctx->x_requires_grad = inputs.at(0)->requires_grad(); | ||
ctx->SaveTensorForBackward(inputs.at(0)); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Apply(const NegativeCaptureState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override { | ||
if (!ctx->x_requires_grad) { return Maybe<void>::Ok(); } | ||
in_grads->at(0) = JUST(functional::Negative(out_grads.at(0))); | ||
return Maybe<void>::Ok(); | ||
} | ||
}; | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("negative", Negative); | ||
|
||
} // 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/* | ||
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_interpreter/op_interpreter_util.h" | ||
#include "oneflow/core/functional/functional.h" | ||
|
||
namespace oneflow { | ||
namespace one { | ||
|
||
struct SinCaptureState : public AutoGradCaptureState { | ||
bool x_requires_grad; | ||
}; | ||
|
||
class Sin : public OpExprGradFunction<SinCaptureState> { | ||
Maybe<void> Init(const OpExpr& op) override { return Maybe<void>::Ok(); } | ||
|
||
Maybe<void> Capture(SinCaptureState* ctx, const TensorTuple& inputs, | ||
const TensorTuple& outputs, const AttrMap& attrs) const override { | ||
ctx->x_requires_grad = inputs.at(0)->requires_grad(); | ||
ctx->SaveTensorForBackward(inputs.at(0)); | ||
return Maybe<void>::Ok(); | ||
} | ||
|
||
Maybe<void> Apply(const SinCaptureState* ctx, const TensorTuple& out_grads, | ||
TensorTuple* in_grads) const override { | ||
if (!ctx->x_requires_grad) { return Maybe<void>::Ok(); } | ||
|
||
const auto& x = ctx->SavedTensors().at(0); | ||
const auto& xx = JUST(functional::Cos(x)); | ||
in_grads->at(0) = JUST(functional::Mul(out_grads.at(0), xx)); | ||
return Maybe<void>::Ok(); | ||
} | ||
}; | ||
|
||
REGISTER_OP_EXPR_GRAD_FUNCTION("sin", Sin); | ||
|
||
} // 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