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.

specialize_transform.h 4.5 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_SPECIALIZE_TRANSFORM_H_
  17. #define MINDSPORE_CCSRC_OPTIMIZER_IRPASS_SPECIALIZE_TRANSFORM_H_
  18. #include <map>
  19. #include <vector>
  20. #include <memory>
  21. #include <utility>
  22. #include <unordered_map>
  23. #include "optimizer/irpass.h"
  24. #include "optimizer/optimizer.h"
  25. #include "ir/visitor.h"
  26. #include "ir/manager.h"
  27. #include "ir/func_graph.h"
  28. #include "ir/func_graph_cloner.h"
  29. #include "operator/ops.h"
  30. namespace mindspore {
  31. namespace opt {
  32. namespace irpass {
  33. namespace internal {
  34. class SpecializeTransform {
  35. public:
  36. SpecializeTransform() : cache_() {}
  37. ~SpecializeTransform() = default;
  38. FuncGraphPtr operator()(const FuncGraphPtr &func_graph, std::vector<FuncGraphPtr> graph_args,
  39. std::vector<PrimitivePtr> prim_args) {
  40. if (cache_.count(func_graph) == 0) {
  41. cache_[func_graph] = {};
  42. }
  43. auto &cache = cache_[func_graph];
  44. auto key = std::make_pair(graph_args, prim_args);
  45. if (cache.count(key) == 0) {
  46. auto mng = func_graph->manager();
  47. MS_EXCEPTION_IF_NULL(mng);
  48. FuncGraphPtr new_fg = TransformableClone(func_graph, std::make_shared<TraceTransform>("sp"));
  49. mng->AddFuncGraph(new_fg);
  50. std::vector<AnfNodePtr> params = new_fg->parameters();
  51. std::vector<AnfNodePtr> new_params;
  52. size_t n = graph_args.size();
  53. for (size_t i = 0; i < n; i++) {
  54. if (graph_args[i] != nullptr) {
  55. auto arg = NewValueNode(graph_args[i]);
  56. (void)mng->Replace(params[i], arg);
  57. continue;
  58. }
  59. if (prim_args[i] != nullptr) {
  60. auto arg = NewValueNode(prim_args[i]);
  61. (void)mng->Replace(params[i], arg);
  62. continue;
  63. }
  64. new_params.push_back(params[i]);
  65. }
  66. mng->SetParameters(new_fg, new_params);
  67. cache[key] = new_fg;
  68. }
  69. return cache[key];
  70. }
  71. private:
  72. std::unordered_map<FuncGraphPtr,
  73. std::map<std::pair<std::vector<FuncGraphPtr>, std::vector<PrimitivePtr>>, FuncGraphPtr>>
  74. cache_;
  75. };
  76. } // namespace internal
  77. // {G, Xs}
  78. class SpecializeOnGraphArguments : public AnfVisitor {
  79. public:
  80. SpecializeOnGraphArguments() : specialize_transform_() {}
  81. ~SpecializeOnGraphArguments() override = default;
  82. AnfNodePtr operator()(const OptimizerPtr &, const AnfNodePtr &node) override {
  83. if (!node->isa<CNode>() || node->func_graph() == nullptr) {
  84. return nullptr;
  85. }
  86. auto &inputs = node->cast<CNodePtr>()->inputs();
  87. if (!IsValueNode<FuncGraph>(inputs[0])) {
  88. return nullptr;
  89. }
  90. auto inp0_fg = GetValueNode<FuncGraphPtr>(inputs[0]);
  91. if (inp0_fg->recursive()) {
  92. return nullptr;
  93. }
  94. std::vector<FuncGraphPtr> graph_args;
  95. std::vector<PrimitivePtr> prim_args;
  96. std::vector<AnfNodePtr> new_xs;
  97. bool hasVNode = false;
  98. for (size_t i = 1; i < inputs.size(); i++) {
  99. if (IsValueNode<FuncGraph>(inputs[i])) {
  100. auto fg_vnode = GetValueNode<FuncGraphPtr>(inputs[i]);
  101. graph_args.push_back(fg_vnode);
  102. prim_args.emplace_back(nullptr);
  103. hasVNode = true;
  104. } else if (IsValueNode<Primitive>(inputs[i])) {
  105. auto p_vnode = GetValueNode<PrimitivePtr>(inputs[i]);
  106. graph_args.emplace_back(nullptr);
  107. prim_args.push_back(p_vnode);
  108. hasVNode = true;
  109. } else {
  110. graph_args.emplace_back(nullptr);
  111. prim_args.emplace_back(nullptr);
  112. new_xs.push_back(inputs[i]);
  113. }
  114. }
  115. if (!hasVNode) {
  116. return nullptr;
  117. }
  118. auto new_fg = specialize_transform_(inp0_fg, graph_args, prim_args);
  119. (void)new_xs.insert(new_xs.begin(), NewValueNode(new_fg));
  120. return node->func_graph()->NewCNode(new_xs);
  121. }
  122. private:
  123. internal::SpecializeTransform specialize_transform_;
  124. };
  125. } // namespace irpass
  126. } // namespace opt
  127. } // namespace mindspore
  128. #endif // MINDSPORE_CCSRC_OPTIMIZER_IRPASS_SPECIALIZE_TRANSFORM_H_