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 3.9 kB

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