Skip to content
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

fix bugs #16

Merged
merged 4 commits into from
Jun 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions oneflow/core/autograd/gradient_funcs/tanh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ struct TanhCaptureState : public AutoGradCaptureState {
class TanhGrad : public OpExprGradFunction<TanhCaptureState> {
Maybe<void> Init(const OpExpr& op) override { return Maybe<void>::Ok(); }

Maybe<void> Capture(TanhCaptureState* ctx, const TensorTuple& inputs,
const TensorTuple& outputs, const AttrMap& attrs) const override {
Maybe<void> Capture(TanhCaptureState* ctx, const TensorTuple& inputs, const TensorTuple& outputs,
const AttrMap& attrs) const override {
ctx->x_requires_grad = inputs.at(0)->requires_grad();
ctx->SaveTensorForBackward(outputs.at(0));
return Maybe<void>::Ok();
Expand All @@ -40,9 +40,8 @@ class TanhGrad : public OpExprGradFunction<TanhCaptureState> {
TensorTuple* in_grads) const override {
if (!ctx->x_requires_grad) { return Maybe<void>::Ok(); }
const auto& y = ctx->SavedTensors().at(0);
const auto& a = functional::Mul(y, y);
const auto& aa = functional::ScalarSub(1, JUST(a));
in_grads->at(0) = JUST(functional::Mul(out_grads.at(0), JUST(aa)));
in_grads->at(0) = JUST(functional::Mul(
out_grads.at(0), JUST(functional::ScalarSub(1, JUST(functional::Mul(y, y)), 1))));
return Maybe<void>::Ok();
}
};
Expand Down
2 changes: 1 addition & 1 deletion oneflow/core/functional/impl/higher_derivative_functor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class ExpGradGradFunctor {
class LogGradGradFunctor {
public:
Maybe<Tensor> operator()(const std::shared_ptr<Tensor>& x,
const std::shared_ptr<Tensor>& dydx) const {
const std::shared_ptr<Tensor>& dydx) const {
auto res = sequence_function(functional::Square)
.then(functional::Negative)
.then(functional::Reciprocal)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,24 @@ def test_consistent_cos_grad_grad(test_case):
for sbp in all_sbp(placement, max_dim=2):
_consistent_math_op_grad_grad_impl(test_case, "cos", placement, sbp)

@globaltest
def test_consistent_exp_grad_grad(test_case):
for placement in all_placement():
for sbp in all_sbp(placement, max_dim=2):
_consistent_math_op_grad_grad_impl(test_case, "exp", placement, sbp)

@globaltest
def test_consistent_log_grad_grad(test_case):
for placement in all_placement():
for sbp in all_sbp(placement, max_dim=2):
_consistent_math_op_grad_grad_impl(test_case, "log", placement, sbp)

@globaltest
def test_consistent_tanh_grad_grad(test_case):
for placement in all_placement():
for sbp in all_sbp(placement, max_dim=2):
_consistent_math_op_grad_grad_impl(test_case, "tanh", placement, sbp)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,13 @@ def test_cos_grad_grad(test_case):

def test_exp_grad_grad(test_case):
_test_math_op_grad_grad_impl(test_case, "exp")

def test_log_grad_grad(test_case):
_test_math_op_grad_grad_impl(test_case, "log")

def test_tanh_grad_grad(test_case):
_test_math_op_grad_grad_impl(test_case, "tanh")


if __name__ == "__main__":
unittest.main()