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.

reshape_eliminate.h 4.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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_RESHAPE_ELIMINATE_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_RESHAPE_ELIMINATE_H_
  18. #include <vector>
  19. #include "ir/func_graph.h"
  20. #include "frontend/optimizer/optimizer_caller.h"
  21. #include "frontend/optimizer/anf_visitor.h"
  22. #include "frontend/operator/ops.h"
  23. #include "frontend/optimizer/irpass.h"
  24. #include "frontend/optimizer/optimizer.h"
  25. #include "abstract/dshape.h"
  26. namespace mindspore {
  27. namespace opt {
  28. namespace irpass {
  29. using abstract::Shape;
  30. using abstract::ShapePtr;
  31. // {reshape_op, X, Shape}
  32. class ReshapeSameShapeEliminater : public AnfVisitor {
  33. public:
  34. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  35. Reset();
  36. AnfVisitor::Match(prim::kPrimReshape, {IsNode, IsVNode})(node);
  37. // check pattern match
  38. if (shape_ == nullptr) {
  39. return nullptr;
  40. }
  41. auto src_shape_abs = x_->abstract();
  42. if (src_shape_abs == nullptr) {
  43. return nullptr;
  44. }
  45. auto src_shape = src_shape_abs->GetShapeTrack();
  46. auto tgt_shape_abs = node->abstract();
  47. if (tgt_shape_abs == nullptr) {
  48. return nullptr;
  49. }
  50. auto tgt_shape = tgt_shape_abs->GetShapeTrack();
  51. if (src_shape != nullptr && tgt_shape != nullptr && src_shape->isa<Shape>() && tgt_shape->isa<Shape>()) {
  52. auto elements = tgt_shape->cast<ShapePtr>();
  53. auto shape = src_shape->cast<ShapePtr>();
  54. if (shape->shape() == elements->shape()) {
  55. return x_;
  56. }
  57. }
  58. return nullptr;
  59. }
  60. void Visit(const AnfNodePtr &node) override {
  61. if (x_ == nullptr) {
  62. x_ = node;
  63. } else {
  64. shape_ = node;
  65. }
  66. }
  67. void Reset() {
  68. x_ = nullptr;
  69. shape_ = nullptr;
  70. }
  71. private:
  72. AnfNodePtr x_{nullptr}, shape_{nullptr};
  73. };
  74. // {PrimReshape, {PrimReshape, X, Y}, Shape}
  75. class TwoReshapeEliminater : public AnfVisitor {
  76. public:
  77. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  78. Reset();
  79. AnfVisitor::Match(prim::kPrimReshape, {IsCNode, IsNode})(node);
  80. auto fg = node->func_graph();
  81. if (fg != nullptr && x_ != nullptr && shape_ != nullptr) {
  82. auto new_node = fg->NewCNode({NewValueNode(prim_), x_, shape_});
  83. new_node->set_abstract(node->abstract());
  84. return new_node;
  85. }
  86. return nullptr;
  87. }
  88. void Visit(const AnfNodePtr &node) override {
  89. if (IsPrimitiveCNode(node, prim::kPrimReshape)) {
  90. auto &inputs = node->cast<CNodePtr>()->inputs();
  91. // {PrimReshape, X, Y}
  92. if (inputs.size() != 3) {
  93. return;
  94. }
  95. prim_ = GetValueNode<PrimitivePtr>(inputs[0]);
  96. x_ = inputs[1];
  97. } else {
  98. shape_ = node;
  99. }
  100. }
  101. void Reset() {
  102. prim_ = nullptr;
  103. x_ = nullptr;
  104. shape_ = nullptr;
  105. }
  106. private:
  107. PrimitivePtr prim_{nullptr};
  108. AnfNodePtr x_{nullptr}, shape_{nullptr};
  109. };
  110. class ReshapeEliminater : public OptimizerCaller {
  111. public:
  112. ReshapeEliminater() : reshape_same_shape_eliminater_(), two_reshape_eliminater_() {}
  113. ~ReshapeEliminater() = default;
  114. AnfNodePtr operator()(const OptimizerPtr &optimizer, const AnfNodePtr &node) override {
  115. auto new_node = reshape_same_shape_eliminater_(optimizer, node);
  116. if (new_node != nullptr) {
  117. return new_node;
  118. }
  119. new_node = two_reshape_eliminater_(optimizer, node);
  120. if (new_node != nullptr) {
  121. return new_node;
  122. }
  123. return nullptr;
  124. }
  125. private:
  126. ReshapeSameShapeEliminater reshape_same_shape_eliminater_;
  127. TwoReshapeEliminater two_reshape_eliminater_;
  128. };
  129. } // namespace irpass
  130. } // namespace opt
  131. } // namespace mindspore
  132. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_RESHAPE_ELIMINATE_H_