1 /**
2 * Copyright 2020 Huawei Technologies Co., Ltd
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_MINMAX_GRAD_H_
18 #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_MINMAX_GRAD_H_
19
20 #include <vector>
21 #include <memory>
22
23 #include "frontend/optimizer/optimizer.h"
24 #include "frontend/optimizer/irpass.h"
25 #include "frontend/optimizer/anf_visitor.h"
26 #include "frontend/operator/ops.h"
27
28 namespace mindspore {
29 namespace opt {
30 namespace irpass {
31 namespace internal {
32 // check if node is MinimumGrad() or MaximumGrad()
IsOriginMaxMinGrad(const AnfNodePtr & node)33 bool IsOriginMaxMinGrad(const AnfNodePtr &node) {
34 if (!IsPrimitiveCNode(node, prim::kPrimMaximumGrad) && !IsPrimitiveCNode(node, prim::kPrimMinimumGrad)) {
35 return false;
36 }
37
38 auto cnode = node->cast<CNodePtr>();
39 auto prim = GetValueNode<PrimitivePtr>(cnode->input(0));
40 auto x_v = prim->GetAttr("grad_x");
41 auto y_v = prim->GetAttr("grad_y");
42 if (x_v == nullptr || y_v == nullptr || !x_v->isa<BoolImm>() || !y_v->isa<BoolImm>()) {
43 return false;
44 }
45
46 bool x = GetValue<bool>(x_v);
47 bool y = GetValue<bool>(y_v);
48 return x && y;
49 }
50 } // namespace internal
51
52 // {prim::kPrimTupleGetItem, {target_grad, Xs}, C}
53 class MinMaximumGrad : public AnfVisitor {
54 public:
operator()55 AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
56 Reset();
57 AnfVisitor::Match(prim::kPrimTupleGetItem, {internal::IsOriginMaxMinGrad, IsValueNode<Int64Imm>})(node);
58 if (grad_ == nullptr || idx_ < 0 || idx_ > 1 || node->func_graph() == nullptr) {
59 return nullptr;
60 }
61
62 // check single use
63 auto mng = optimizer->manager();
64 auto &users = mng->node_users();
65 if (users.find(grad_) == users.end() || users[grad_].size() != 1) {
66 return nullptr;
67 }
68
69 // {target_grad, Xs}
70 auto &inputs = grad_->inputs();
71 auto prim = GetValueNode<PrimitivePtr>(inputs[0]);
72
73 auto new_prim = std::make_shared<Primitive>(prim->name());
74 new_prim->set_attr("grad_x", MakeValue(true));
75 new_prim->set_attr("grad_y", MakeValue(true));
76
77 if (idx_ == 0) {
78 new_prim->set_attr("grad_y", MakeValue(false));
79 }
80 if (idx_ == 1) {
81 new_prim->set_attr("grad_x", MakeValue(false));
82 }
83
84 std::vector<AnfNodePtr> args;
85 args.push_back(NewValueNode(new_prim));
86 (void)args.insert(args.end(), inputs.begin() + 1, inputs.end());
87
88 auto fg = node->func_graph();
89 auto tuple = fg->NewCNode(args);
90
91 return fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), tuple, NewValueNode(MakeValue(idx_))});
92 }
93
Visit(const CNodePtr & cnode)94 void Visit(const CNodePtr &cnode) override { grad_ = cnode; }
95
Visit(const ValueNodePtr & vnode)96 void Visit(const ValueNodePtr &vnode) override { idx_ = GetValue<int64_t>(vnode->value()); }
97
Reset()98 void Reset() {
99 idx_ = -1;
100 grad_ = nullptr;
101 }
102
103 private:
104 int64_t idx_{-1};
105 CNodePtr grad_{nullptr};
106 };
107 } // namespace irpass
108 } // namespace opt
109 } // namespace mindspore
110 #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_MINMAX_GRAD_H_
111