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.

transpose_eliminate.h 2.2 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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_TRANSPOSE_ELIMINATE_H_
  17. #define MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_TRANSPOSE_ELIMINATE_H_
  18. #include <vector>
  19. #include <algorithm>
  20. #include "frontend/optimizer/irpass.h"
  21. #include "frontend/optimizer/optimizer.h"
  22. #include "frontend/optimizer/anf_visitor.h"
  23. #include "frontend/operator/ops.h"
  24. namespace mindspore {
  25. namespace opt {
  26. namespace irpass {
  27. // check if node is value tuple and ascends one by one from zero. e.g., (0, 1, 2, 3)
  28. // {PrimTranspose, X, AscendingNums}
  29. class TransposeSameIOEliminater : public AnfVisitor {
  30. public:
  31. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  32. Reset();
  33. AnfVisitor::Match(prim::kPrimTranspose, {IsNode, IsVNode})(node);
  34. // check pattern match
  35. if (tuple_ == nullptr) {
  36. return nullptr;
  37. }
  38. auto value = GetValueNode(tuple_);
  39. auto elements = GetValue<std::vector<int>>(value);
  40. if (elements.empty()) {
  41. return nullptr;
  42. }
  43. int j = 0;
  44. bool cmp = std::all_of(elements.cbegin(), elements.cend(), [&j](int i) { return i == j++; });
  45. // same IO settings, eliminate this transpose
  46. if (cmp) {
  47. return x_;
  48. }
  49. return nullptr;
  50. }
  51. void Visit(const AnfNodePtr &node) override {
  52. if (x_ == nullptr) {
  53. x_ = node;
  54. } else {
  55. tuple_ = node;
  56. }
  57. }
  58. void Reset() {
  59. x_ = nullptr;
  60. tuple_ = nullptr;
  61. }
  62. private:
  63. AnfNodePtr x_{nullptr}, tuple_{nullptr};
  64. };
  65. } // namespace irpass
  66. } // namespace opt
  67. } // namespace mindspore
  68. #endif // MINDSPORE_CCSRC_FRONTEND_OPTIMIZER_IRPASS_TRANSPOSE_ELIMINATE_H_