You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

minmax_grad.h 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. #ifndef MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_MINMAX_GRAD_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_MINMAX_GRAD_H_
  18. #include <vector>
  19. #include <memory>
  20. #include "frontend/optimizer/optimizer.h"
  21. #include "frontend/optimizer/irpass.h"
  22. #include "frontend/optimizer/anf_visitor.h"
  23. #include "frontend/operator/ops.h"
  24. namespace mindspore {
  25. namespace opt {
  26. namespace irpass {
  27. namespace internal {
  28. // check if node is MinimumGrad() or MaximumGrad()
  29. bool IsOriginMaxMinGrad(const AnfNodePtr &node) {
  30. if (!IsPrimitiveCNode(node, prim::kPrimMaximumGrad) && !IsPrimitiveCNode(node, prim::kPrimMinimumGrad)) {
  31. return false;
  32. }
  33. auto cnode = node->cast<CNodePtr>();
  34. auto prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  35. auto x_v = prim->GetAttr("grad_x");
  36. auto y_v = prim->GetAttr("grad_y");
  37. if (x_v == nullptr || y_v == nullptr || !x_v->isa<BoolImm>() || !y_v->isa<BoolImm>()) {
  38. return false;
  39. }
  40. bool x = GetValue<bool>(x_v);
  41. bool y = GetValue<bool>(y_v);
  42. return x && y;
  43. }
  44. } // namespace internal
  45. // {prim::kPrimTupleGetItem, {target_grad, Xs}, C}
  46. class MinMaximumGrad : public AnfVisitor {
  47. public:
  48. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
  49. Reset();
  50. AnfVisitor::Match(prim::kPrimTupleGetItem, {internal::IsOriginMaxMinGrad, IsValueNode<Int32Imm>})(node);
  51. if (grad_ == nullptr || idx_ < 0 || idx_ > 1 || node->func_graph() == nullptr) {
  52. return nullptr;
  53. }
  54. // check single use
  55. auto mng = optimizer->resource()->manager();
  56. auto &users = mng->node_users();
  57. if (users.find(grad_) == users.end() || users[grad_].size() != 1) {
  58. return nullptr;
  59. }
  60. // {target_grad, Xs}
  61. auto &inputs = grad_->inputs();
  62. auto prim = GetValueNode<PrimitivePtr>(inputs[0]);
  63. auto new_prim = std::make_shared<Primitive>(prim->name());
  64. new_prim->set_attr("grad_x", MakeValue(true));
  65. new_prim->set_attr("grad_y", MakeValue(true));
  66. if (idx_ == 0) {
  67. new_prim->set_attr("grad_y", MakeValue(false));
  68. }
  69. if (idx_ == 1) {
  70. new_prim->set_attr("grad_x", MakeValue(false));
  71. }
  72. std::vector<AnfNodePtr> args;
  73. args.push_back(NewValueNode(new_prim));
  74. (void)args.insert(args.end(), inputs.begin() + 1, inputs.end());
  75. auto fg = node->func_graph();
  76. auto tuple = fg->NewCNode(args);
  77. return fg->NewCNode({NewValueNode(prim::kPrimTupleGetItem), tuple, NewValueNode(MakeValue(idx_))});
  78. }
  79. void Visit(const CNodePtr &cnode) override { grad_ = cnode; }
  80. void Visit(const ValueNodePtr &vnode) override { idx_ = GetValue<int>(vnode->value()); }
  81. void Reset() {
  82. idx_ = -1;
  83. grad_ = nullptr;
  84. }
  85. private:
  86. int idx_{-1};
  87. CNodePtr grad_{nullptr};
  88. };
  89. } // namespace irpass
  90. } // namespace opt
  91. } // namespace mindspore
  92. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_MINMAX_GRAD_H_