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 8.4 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019 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 <tuple>
  24. #include <unordered_map>
  25. #include <utility>
  26. #include <string>
  27. #include "utils/log_adapter.h"
  28. #include "utils/utils.h"
  29. #include "ir/manager.h"
  30. #include "ir/func_graph_cloner.h"
  31. #include "frontend/operator/ops.h"
  32. namespace mindspore {
  33. namespace compile {
  34. // cached conversion
  35. ConvertCache g_ConvertCache;
  36. void ClearConvertCache() { g_ConvertCache.clear(); }
  37. // Return the list of nodes whose values are required beyond this segment.
  38. // Arguments:
  39. // lst: list of nodes (the segment)
  40. // users: dict mapping each node to its users (globally)
  41. // seen: set of nodes that are part of the segment
  42. AnfNodePtrList GetOutput(const AnfNodePtrList &lst, const NodeUsersMap &users, const std::vector<AnfNodePtr> &seen) {
  43. AnfNodePtrList output;
  44. if (users.size() == 0) {
  45. return output;
  46. }
  47. (void)std::transform(
  48. std::begin(lst), std::end(lst), std::back_inserter(output), [&users, &seen](AnfNodePtr n) -> AnfNodePtr {
  49. auto usersn = users.find(n);
  50. bool is_referred_out_of_segment = std::any_of(
  51. std::begin(usersn->second), std::end(usersn->second), [&seen](const std::pair<AnfNodePtr, int64_t> &u) -> bool {
  52. return std::find(std::begin(seen), std::end(seen), u.first) == std::end(seen);
  53. });
  54. if (n->isa<CNode>() && is_referred_out_of_segment) {
  55. return n;
  56. }
  57. return nullptr;
  58. });
  59. // remove nullptr
  60. for (auto it = output.begin(); it != output.end();) {
  61. if (*it == nullptr) {
  62. it = output.erase(it);
  63. } else {
  64. ++it;
  65. }
  66. }
  67. return output;
  68. }
  69. namespace {
  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. if (IsPrimitiveCNode(node, prim::kPrimControlDepend)) {
  82. eqv[node] = NewValueNode(MakeValue(0));
  83. return eqv[node];
  84. }
  85. bool ignore_make_tuple = false;
  86. if (IsPrimitiveCNode(node, prim::kPrimMakeTuple)) {
  87. ignore_make_tuple = true;
  88. auto cnode = node->cast<CNodePtr>();
  89. MS_EXCEPTION_IF_NULL(cnode);
  90. const auto &node_inputs = cnode->inputs();
  91. for (size_t i = 1; i < node_inputs.size(); ++i) {
  92. if (!IsPrimitiveCNode(node_inputs[i], prim::kPrimControlDepend)) {
  93. ignore_make_tuple = false;
  94. break;
  95. }
  96. }
  97. }
  98. if (!ignore_make_tuple) {
  99. inputs.push_back(node);
  100. }
  101. eqv[node] = fg->add_parameter();
  102. eqv[node]->set_abstract(node->abstract());
  103. eqv[node]->set_kernel_info(node->kernel_info_ptr());
  104. }
  105. return eqv[node];
  106. }
  107. } // namespace
  108. std::tuple<FuncGraphPtr, AnfNodePtrList, AnfNodePtrList> TransformSegmentToAnfGraph(const AnfNodePtrList &lst) {
  109. if (lst.empty()) {
  110. MS_LOG(EXCEPTION) << "Input anf node list is empty";
  111. }
  112. FuncGraphPtr fg = nullptr;
  113. {
  114. // limit the lifetime of guard.
  115. TraceGuard guard(std::make_shared<TraceSegmentTransform>(lst[0]->cast<CNodePtr>()->func_graph()->debug_info()));
  116. fg = std::make_shared<FuncGraph>();
  117. }
  118. AnfNodePtrList inputs;
  119. AnfNodePtrToAnfNodePtrMap eqv;
  120. // Merge CNodes into a AnfGraph that represents a linear instruction segment
  121. for (auto n : lst) {
  122. if (!n->isa<CNode>()) {
  123. MS_LOG(EXCEPTION) << "Inst is not CNode";
  124. }
  125. auto &inps = n->cast<CNodePtr>()->inputs();
  126. if (inps.empty()) {
  127. MS_LOG(EXCEPTION) << "Input is empty";
  128. }
  129. if (!IsValueNode<Primitive>(inps[0]) &&
  130. !(IsValueNode<FuncGraph>(inps[0]) &&
  131. inps[0]->cast<ValueNodePtr>()->value()->cast<FuncGraphPtr>()->has_attr(FUNC_GRAPH_ATTR_GRAPH_KERNEL))) {
  132. MS_LOG(EXCEPTION) << "Input[0] Must be a Primitive ValueNode";
  133. }
  134. auto fn = inps[0];
  135. std::vector<AnfNodePtr> args{fn};
  136. if (IsPrimitive(fn, prim::kPrimDepend) && inps.size() >= 3 && eqv.find(inps[kDependAttachNodeIndex]) == eqv.end()) {
  137. args.emplace_back(RefSubGraphNode(fg, inps[kRealInputIndexInDepend], &inputs, &eqv));
  138. for (size_t i = 2; i < inps.size(); ++i) {
  139. args.emplace_back(NewValueNode(MakeValue(0)));
  140. }
  141. } else if (IsPrimitive(fn, prim::kPrimControlDepend) && inps.size() == 3) {
  142. for (size_t i = 1; i < inps.size(); ++i) {
  143. if (inps[i]->isa<CNode>() && std::find(lst.begin(), lst.end(), inps[i]) == lst.end()) {
  144. args.emplace_back(NewValueNode(MakeValue(static_cast<int>(i))));
  145. } else {
  146. args.emplace_back(RefSubGraphNode(fg, inps[i], &inputs, &eqv));
  147. }
  148. }
  149. } else {
  150. (void)std::transform(std::begin(inps) + 1, std::end(inps), std::back_inserter(args),
  151. [&fg, &inputs, &eqv](const AnfNodePtr &a) { return RefSubGraphNode(fg, a, &inputs, &eqv); });
  152. }
  153. TraceGuard tg(std::make_shared<TraceSegmentTransform>(n->debug_info()));
  154. eqv[n] = fg->NewCNode(args);
  155. eqv[n]->set_abstract(n->abstract());
  156. eqv[n]->set_kernel_info(n->kernel_info_ptr());
  157. }
  158. std::vector<AnfNodePtr> eqv_keys;
  159. (void)std::transform(std::begin(eqv), std::end(eqv), std::back_inserter(eqv_keys),
  160. [](const std::pair<AnfNodePtr, AnfNodePtr> &elem) -> AnfNodePtr { return elem.first; });
  161. auto outputs = GetOutput(lst, lst[0]->func_graph()->manager()->node_users(), eqv_keys);
  162. AnfNodePtr fg_output;
  163. if (outputs.size() > 1) {
  164. std::vector<AnfNodePtr> output_args;
  165. output_args.push_back(NewValueNode(prim::kPrimMakeTuple));
  166. (void)std::transform(std::begin(outputs), std::end(outputs), std::back_inserter(output_args),
  167. [&eqv](const AnfNodePtr &o) -> AnfNodePtr { return eqv[o]; });
  168. // Set output for AnfGraph
  169. fg_output = fg->NewCNode(output_args);
  170. } else {
  171. fg_output = eqv[outputs[0]];
  172. }
  173. fg->set_output(fg_output);
  174. return std::make_tuple(fg, inputs, outputs);
  175. }
  176. // Converts the list of nodes to a runnable form.
  177. // All the nodes in the list must represent linear flow (no calls, branches, ...)
  178. // Returns:
  179. // (fn, inputs, outputs):
  180. // - fn: A callable function
  181. // - inputs: the list of inputs nodes whose values should be
  182. // provided to the function
  183. // - outputs: the list of output nodes corresponding to the
  184. // outputs of the function
  185. // Notes:
  186. // This implementation will convert the nodes into a subgraph
  187. // that will run using the MsVM.
  188. template <typename T>
  189. LinConvertResult Convert(const GraphSegmentPtr &segment, const std::string &) {
  190. MS_EXCEPTION_IF_NULL(segment);
  191. auto cached = g_ConvertCache.find(segment);
  192. if (cached != g_ConvertCache.end()) {
  193. return cached->second;
  194. }
  195. LinConvertResult result;
  196. FuncGraphPtr fg = nullptr;
  197. AnfNodePtrList inputs;
  198. AnfNodePtrList outputs;
  199. std::tie(fg, inputs, outputs) = TransformSegmentToAnfGraph(segment->nodes_);
  200. // Clone in case g contains subgraphs that have a different manager
  201. fg = BasicClone(fg);
  202. std::shared_ptr<VMImpl> vm = std::make_shared<T>();
  203. result.run =
  204. std::make_shared<RunFunc>([fg, vm](const VectorRef &args) -> VectorRef { return vm->RunGraph(fg, args); });
  205. result.inputs = inputs;
  206. result.outputs = outputs;
  207. result.graph_id = UINT32_MAX;
  208. (void)g_ConvertCache.emplace(segment, result);
  209. return result;
  210. }
  211. LinkFuncType MsVmConvert = Convert<VM>;
  212. std::set<std::string> backend_list = {
  213. kMsConvert,
  214. kMsVm,
  215. };
  216. } // namespace compile
  217. } // namespace mindspore