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.

tile_eliminate.h 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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_TILE_ELIMINATE_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_IRPASS_TILE_ELIMINATE_H_
  18. #include <vector>
  19. #include <algorithm>
  20. #include "optimizer/irpass.h"
  21. #include "optimizer/optimizer.h"
  22. #include "ir/visitor.h"
  23. #include "operator/ops.h"
  24. namespace mindspore {
  25. namespace opt {
  26. namespace irpass {
  27. // check if node is value tuple and all one. e.g. (1, 1, 1)
  28. // {PrimTile, X, MultiOne}
  29. class TileMultiplyByOne : public AnfVisitor {
  30. public:
  31. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  32. Reset();
  33. AnfVisitor::Match(prim::kPrimTile, {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. auto cmp = std::all_of(elements.cbegin(), elements.cend(), [](int i) { return i == 1; });
  44. if (cmp) {
  45. return x_;
  46. }
  47. return nullptr;
  48. }
  49. void Visit(const AnfNodePtr &node) override {
  50. if (x_ == nullptr) {
  51. x_ = node;
  52. } else {
  53. tuple_ = node;
  54. }
  55. }
  56. void Reset() {
  57. x_ = nullptr;
  58. tuple_ = nullptr;
  59. }
  60. private:
  61. AnfNodePtr x_{nullptr}, tuple_{nullptr};
  62. };
  63. } // namespace irpass
  64. } // namespace opt
  65. } // namespace mindspore
  66. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_TILE_ELIMINATE_H_