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.

segment_runner.cc 7.8 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019-2021 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "vm/segment_runner.h"
  19. #include <algorithm>
  20. #include <functional>
  21. #include <memory>
  22. #include <set>
  23. #include <unordered_set>
  24. #include <tuple>
  25. #include <unordered_map>
  26. #include <utility>
  27. #include <string>
  28. #include "utils/log_adapter.h"
  29. #include "utils/utils.h"
  30. #include "ir/manager.h"
  31. #include "ir/func_graph_cloner.h"
  32. #include "frontend/operator/ops.h"
  33. namespace mindspore {
  34. namespace compile {
  35. // cached conversion
  36. ConvertCache g_ConvertCache;
  37. void ClearConvertCache() { g_ConvertCache.clear(); }
  38. namespace {
  39. // Return the list of nodes whose values are required beyond this segment.
  40. // Arguments:
  41. // nodes: list of nodes in the segment
  42. // users: dict mapping each node to its users (globally)
  43. // seen: set of nodes that are part of the segment
  44. AnfNodePtrList GetOutput(const AnfNodePtrList &nodes, const NodeUsersMap &users,
  45. const std::unordered_set<AnfNodePtr> &seen) {
  46. AnfNodePtrList output;
  47. if (users.size() == 0) {
  48. return output;
  49. }
  50. for (auto &node : nodes) {
  51. if (!node->isa<CNode>()) {
  52. continue;
  53. }
  54. auto iter = users.find(node);
  55. if (iter == users.end()) {
  56. continue;
  57. }
  58. auto &node_users = iter->second;
  59. const bool has_outer_user = std::any_of(
  60. std::begin(node_users), std::end(node_users), [&seen](const std::pair<AnfNodePtr, int64_t> &u) -> bool {
  61. const bool is_outer_user = (seen.find(u.first) == seen.end());
  62. return is_outer_user && !(IsPrimitiveCNode(u.first, prim::kPrimUpdateState) && u.second > 2);
  63. });
  64. if (has_outer_user) {
  65. output.emplace_back(node);
  66. }
  67. }
  68. return output;
  69. }
  70. AnfNodePtr RefSubGraphNode(const FuncGraphPtr &fg, const AnfNodePtr &node, AnfNodePtrList *const inputs_ptr,
  71. AnfNodePtrToAnfNodePtrMap *eqv_ptr) {
  72. MS_EXCEPTION_IF_NULL(fg);
  73. MS_EXCEPTION_IF_NULL(inputs_ptr);
  74. MS_EXCEPTION_IF_NULL(eqv_ptr);
  75. MS_EXCEPTION_IF_NULL(node);
  76. auto &inputs = *inputs_ptr;
  77. auto &eqv = *eqv_ptr;
  78. if (node->isa<ValueNode>() && !IsValueNode<FuncGraph>(node)) {
  79. eqv[node] = node;
  80. } else if (eqv.find(node) == eqv.end()) {
  81. inputs.push_back(node);
  82. eqv[node] = fg->add_parameter();
  83. eqv[node]->set_abstract(node->abstract());
  84. eqv[node]->set_kernel_info(node->kernel_info_ptr());
  85. }
  86. return eqv[node];
  87. }
  88. } // namespace
  89. std::tuple<FuncGraphPtr, AnfNodePtrList, AnfNodePtrList> TransformSegmentToAnfGraph(const AnfNodePtrList &lst) {
  90. if (lst.empty()) {
  91. MS_LOG(EXCEPTION) << "Input anf node list is empty";
  92. }
  93. FuncGraphPtr fg = nullptr;
  94. {
  95. // limit the lifetime of guard.
  96. TraceGuard guard(std::make_shared<TraceSegmentTransform>(lst[0]->cast<CNodePtr>()->func_graph()->debug_info()));
  97. fg = std::make_shared<FuncGraph>();
  98. }
  99. AnfNodePtrList inputs;
  100. AnfNodePtrToAnfNodePtrMap eqv;
  101. // Merge CNodes into a AnfGraph that represents a linear instruction segment
  102. for (auto n : lst) {
  103. if (!n->isa<CNode>()) {
  104. MS_LOG(EXCEPTION) << "Inst is not CNode";
  105. }
  106. auto &inps = n->cast<CNodePtr>()->inputs();
  107. if (inps.empty()) {
  108. MS_LOG(EXCEPTION) << "Input is empty";
  109. }
  110. if (!IsValueNode<Primitive>(inps[0]) &&
  111. !(IsValueNode<FuncGraph>(inps[0]) &&
  112. inps[0]->cast<ValueNodePtr>()->value()->cast<FuncGraphPtr>()->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL))) {
  113. MS_LOG(EXCEPTION) << "Input[0] Must be a Primitive ValueNode";
  114. }
  115. auto fn = inps[0];
  116. std::vector<AnfNodePtr> args{fn};
  117. if (IsPrimitive(fn, prim::kPrimDepend) && inps.size() >= 3 && eqv.find(inps[kDependAttachNodeIndex]) == eqv.end()) {
  118. args.emplace_back(RefSubGraphNode(fg, inps[kRealInputIndexInDepend], &inputs, &eqv));
  119. for (size_t i = 2; i < inps.size(); ++i) {
  120. args.emplace_back(NewValueNode(MakeValue(0)));
  121. }
  122. } else if (IsPrimitive(fn, prim::kPrimUpdateState)) {
  123. args.emplace_back(RefSubGraphNode(fg, inps[1], &inputs, &eqv));
  124. args.emplace_back(RefSubGraphNode(fg, inps[2], &inputs, &eqv));
  125. for (size_t i = 3; i < inps.size(); ++i) {
  126. auto &input = inps[i];
  127. if (eqv.find(input) != eqv.end()) {
  128. args.emplace_back(RefSubGraphNode(fg, input, &inputs, &eqv));
  129. }
  130. }
  131. } else {
  132. (void)std::transform(std::begin(inps) + 1, std::end(inps), std::back_inserter(args),
  133. [&fg, &inputs, &eqv](const AnfNodePtr &a) { return RefSubGraphNode(fg, a, &inputs, &eqv); });
  134. }
  135. TraceGuard tg(std::make_shared<TraceSegmentTransform>(n->debug_info()));
  136. eqv[n] = fg->NewCNode(args);
  137. eqv[n]->set_abstract(n->abstract());
  138. eqv[n]->set_kernel_info(n->kernel_info_ptr());
  139. }
  140. std::unordered_set<AnfNodePtr> eqv_keys;
  141. (void)std::transform(std::begin(eqv), std::end(eqv), std::inserter(eqv_keys, eqv_keys.end()),
  142. [](const std::pair<AnfNodePtr, AnfNodePtr> &elem) -> AnfNodePtr { return elem.first; });
  143. auto outputs = GetOutput(lst, lst[0]->func_graph()->manager()->node_users(), eqv_keys);
  144. AnfNodePtr fg_output;
  145. if (outputs.size() > 1) {
  146. std::vector<AnfNodePtr> output_args;
  147. output_args.push_back(NewValueNode(prim::kPrimMakeTuple));
  148. (void)std::transform(std::begin(outputs), std::end(outputs), std::back_inserter(output_args),
  149. [&eqv](const AnfNodePtr &o) -> AnfNodePtr { return eqv[o]; });
  150. // Set output for AnfGraph
  151. fg_output = fg->NewCNode(output_args);
  152. } else {
  153. fg_output = eqv[outputs[0]];
  154. }
  155. fg->set_output(fg_output);
  156. return std::make_tuple(fg, inputs, outputs);
  157. }
  158. // Converts the list of nodes to a runnable form.
  159. // All the nodes in the list must represent linear flow (no calls, branches, ...)
  160. // Returns:
  161. // (fn, inputs, outputs):
  162. // - fn: A callable function
  163. // - inputs: the list of inputs nodes whose values should be
  164. // provided to the function
  165. // - outputs: the list of output nodes corresponding to the
  166. // outputs of the function
  167. // Notes:
  168. // This implementation will convert the nodes into a subgraph
  169. // that will run using the MsVM.
  170. template <typename T>
  171. LinConvertResult Convert(const GraphSegmentPtr &segment, const std::string &) {
  172. MS_EXCEPTION_IF_NULL(segment);
  173. auto cached = g_ConvertCache.find(segment);
  174. if (cached != g_ConvertCache.end()) {
  175. return cached->second;
  176. }
  177. LinConvertResult result;
  178. FuncGraphPtr fg = nullptr;
  179. AnfNodePtrList inputs;
  180. AnfNodePtrList outputs;
  181. std::tie(fg, inputs, outputs) = TransformSegmentToAnfGraph(segment->nodes_);
  182. // Clone in case g contains subgraphs that have a different manager
  183. fg = BasicClone(fg);
  184. std::shared_ptr<VMImpl> vm = std::make_shared<T>();
  185. result.run =
  186. std::make_shared<RunFunc>([fg, vm](const VectorRef &args) -> VectorRef { return vm->RunGraph(fg, args); });
  187. result.inputs = inputs;
  188. result.outputs = outputs;
  189. result.graph_id = UINT32_MAX;
  190. (void)g_ConvertCache.emplace(segment, result);
  191. return result;
  192. }
  193. LinkFuncType MsVmConvert = Convert<VM>;
  194. std::set<std::string> backend_list = {
  195. kMsConvert,
  196. kMsVm,
  197. };
  198. } // namespace compile
  199. } // namespace mindspore