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.

mark_interface_fusion.h 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_MARK_INTERFACE_FUSION_H
  17. #define MINDSPORE_CCSRC_OPTIMIZER_IRPASS_MARK_INTERFACE_FUSION_H
  18. #include <string>
  19. #include <sstream>
  20. #include <unordered_map>
  21. #include "session/anf_runtime_algorithm.h"
  22. #include "optimizer/optimizer.h"
  23. #include "optimizer/irpass.h"
  24. #include "ir/visitor.h"
  25. #include "operator/ops.h"
  26. #include "utils/graph_utils.h"
  27. #include "operator/composite/composite.h"
  28. namespace mindspore {
  29. namespace opt {
  30. namespace irpass {
  31. static int count = 0;
  32. std::string GetFusionNumber() {
  33. std::stringstream ss;
  34. ss << std::setw(4) << std::setfill('0') << count;
  35. std::string num = ss.str();
  36. ++count;
  37. return "_" + num;
  38. }
  39. // Mark CNodes which can be merged in kernel build
  40. class MarkInterfaceFusion : public AnfVisitor {
  41. public:
  42. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  43. if (node->func_graph()->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL) && IsPrimitiveCNode(node, prim::kPrimSelect)) {
  44. auto cnode = node->cast<CNodePtr>();
  45. auto condition = cnode->input(1);
  46. std::string cmp;
  47. std::unordered_map<std::string, std::string> cmp_list = {{"GreaterEqual", "GE"}, {"Greater", "GT"},
  48. {"LessEqual", "LE"}, {"Less", "LT"},
  49. {"Equal", "EQ"}, {"NotEqual", "NE"}};
  50. if (IsPrimitiveCNode(condition)) {
  51. auto prim_name = GetCNodeFuncName(condition->cast<CNodePtr>());
  52. if (cmp_list.count(prim_name) != 0) {
  53. // Mark Select and compare node
  54. cmp = cmp_list[prim_name];
  55. auto cnt = GetFusionNumber();
  56. AnfAlgo::SetNodeAttr("fusion", MakeValue("Select" + cmp + cnt), condition);
  57. AnfAlgo::SetNodeAttr("fusion", MakeValue("Select" + cmp + cnt + "_end"), node);
  58. for (size_t i = 1; i < cnode->inputs().size(); ++i) {
  59. if (IsPrimitiveCNode(cnode->input(i), prim::kPrimZerosLike)) {
  60. AnfAlgo::SetNodeAttr("fusion", MakeValue("Select" + cmp + cnt), cnode->input(i));
  61. }
  62. }
  63. }
  64. }
  65. }
  66. return nullptr;
  67. }
  68. void Visit(const AnfNodePtr &) override {}
  69. private:
  70. AnfNodePtr y_{nullptr};
  71. };
  72. } // namespace irpass
  73. } // namespace opt
  74. } // namespace mindspore
  75. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_MARK_INTERFACE_FUSION_H