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.

session_basic.cc 39 kB

5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  1. /**
  2. * Copyright 2019-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. #include "session/session_basic.h"
  17. #include <utility>
  18. #include <algorithm>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include "pipeline/parse/data_converter.h"
  22. #include "ir/manager.h"
  23. #include "ir/param_value_py.h"
  24. #include "kernel/common_utils.h"
  25. #include "operator/ops.h"
  26. #include "common/trans.h"
  27. #include "utils/context/ms_context.h"
  28. #include "utils/config_manager.h"
  29. #include "session/anf_runtime_algorithm.h"
  30. #include "kernel/oplib/oplib.h"
  31. #include "pre_activate/common/common_backend_optimization.h"
  32. #include "pre_activate/pass/const_input_to_attr_registry.h"
  33. #include "pre_activate/common/helper.h"
  34. #include "common/utils.h"
  35. #include "ir/dtype.h"
  36. #include "ir/anf.h"
  37. #include "ir/func_graph_cloner.h"
  38. namespace mindspore {
  39. namespace session {
  40. static std::shared_ptr<std::map<PyObject *, ParameterPtr>> python_paras_;
  41. void ClearPythonParasMap() { python_paras_ = nullptr; }
  42. namespace {
  43. const int kSummaryGetItem = 2;
  44. PyObject *GetParamDefaultInputTensor(const AnfNodePtr &node) {
  45. if (node == nullptr) {
  46. return nullptr;
  47. }
  48. auto parameter = node->cast<ParameterPtr>();
  49. if (parameter == nullptr || !parameter->has_default()) {
  50. return nullptr;
  51. }
  52. auto param_value = std::dynamic_pointer_cast<ParamValuePy>(parameter->default_param());
  53. auto py_param = param_value->value();
  54. return py_param.ptr();
  55. }
  56. BaseRef CreateOneTensor(const AnfNodePtr &node, size_t output_index, const KernelGraph &graph,
  57. const std::vector<tensor::TensorPtr> &input_tensors) {
  58. MS_EXCEPTION_IF_NULL(node);
  59. MS_LOG(INFO) << "create tensor for output[" << node->DebugString() << "] index[" << output_index << "]";
  60. // if node is a value node, no need sync addr from device to host
  61. if (!AnfAlgo::OutputAddrExist(node, output_index)) {
  62. if (node->isa<ValueNode>()) {
  63. auto value_node = node->cast<ValueNodePtr>();
  64. MS_EXCEPTION_IF_NULL(value_node);
  65. return value_node->value();
  66. }
  67. if (node->isa<Parameter>()) {
  68. for (size_t input_idx = 0; input_idx < graph.inputs().size(); input_idx++) {
  69. if (input_idx > input_tensors.size()) {
  70. MS_LOG(EXCEPTION) << "input idx:" << input_idx << "out of range:" << input_tensors.size();
  71. }
  72. if (graph.inputs()[input_idx] == node) {
  73. return input_tensors[input_idx];
  74. }
  75. }
  76. MS_LOG(EXCEPTION) << "parameter : " << node->DebugString() << "has no output addr";
  77. }
  78. }
  79. // if proccess reach here,it remarks item_with_index is a real node(Parameter,or executable CNode)
  80. auto address = AnfAlgo::GetOutputAddr(node, output_index);
  81. MS_EXCEPTION_IF_NULL(address);
  82. auto shape = AnfAlgo::GetOutputInferShape(node, output_index);
  83. TypeId type_id = kNumberTypeFloat32;
  84. type_id = AnfAlgo::GetOutputInferDataType(node, output_index);
  85. std::vector<int> temp_shape;
  86. (void)std::copy(shape.begin(), shape.end(), std::back_inserter(temp_shape));
  87. tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(type_id, temp_shape);
  88. // if in paynative mode,data only copyed to host when user want to print data
  89. auto ms_context = MsContext::GetInstance();
  90. MS_EXCEPTION_IF_NULL(ms_context);
  91. if (ms_context->execution_mode() == kPynativeMode || ms_context->device_target() == kGPUDevice) {
  92. tensor->set_device_address(AnfAlgo::GetMutableOutputAddr(node, output_index));
  93. tensor->set_dirty(false);
  94. } else if (!address->SyncDeviceToHost(trans::GetRuntimePaddingShape(node, output_index),
  95. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  96. tensor->data_c(true))) {
  97. MS_LOG(INFO) << "output sync device to host error!!!";
  98. tensor->set_dirty(false);
  99. }
  100. return tensor;
  101. }
  102. BaseRef CreatTensorForOutput(const AnfNodePtr &anf, const KernelGraph &graph,
  103. const std::vector<tensor::TensorPtr> &input_tensors) {
  104. MS_EXCEPTION_IF_NULL(anf);
  105. MS_LOG(INFO) << "create tensor for output[" << anf->DebugString() << "]";
  106. auto item_with_index = AnfAlgo::VisitKernelWithReturnType(anf, 0);
  107. MS_EXCEPTION_IF_NULL(item_with_index.first);
  108. MS_LOG(INFO) << "create tensor for output after visit:" << item_with_index.first->DebugString();
  109. // special handle for maketuple
  110. if (AnfAlgo::CheckPrimitiveType(item_with_index.first, prim::kPrimMakeTuple)) {
  111. auto cnode = item_with_index.first->cast<CNodePtr>();
  112. MS_EXCEPTION_IF_NULL(cnode);
  113. VectorRef ret;
  114. for (size_t i = 1; i < cnode->inputs().size(); ++i) {
  115. auto out = CreatTensorForOutput(cnode->input(i), graph, input_tensors);
  116. ret.push_back(out);
  117. }
  118. return ret;
  119. }
  120. // if is graph return nothing ,the function should return a null anylist
  121. size_t size = AnfAlgo::GetOutputTensorNum(item_with_index.first);
  122. if (size == 0) {
  123. return VectorRef();
  124. }
  125. return CreateOneTensor(item_with_index.first, item_with_index.second, graph, input_tensors);
  126. }
  127. BaseRef CreatTupleForOutput(const AnfNodePtr &anf, const KernelGraph &graph,
  128. const std::vector<tensor::TensorPtr> &input_tensors) {
  129. MS_EXCEPTION_IF_NULL(anf);
  130. if (!AnfAlgo::IsRealKernel(anf)) {
  131. MS_LOG(EXCEPTION) << "anf[" << anf->DebugString() << "] should be a executable kernel";
  132. }
  133. if (anf->isa<ValueNode>()) {
  134. return CreateOneTensor(anf, 0, graph, input_tensors);
  135. }
  136. VectorRef ret;
  137. if (anf->isa<CNode>() && AnfAlgo::GetCNodeName(anf) != prim::kPrimMakeTuple->name()) {
  138. for (size_t i = 0; i < AnfAlgo::GetOutputTensorNum(anf); ++i) {
  139. auto out = CreateOneTensor(anf, i, graph, input_tensors);
  140. ret.emplace_back(out);
  141. }
  142. }
  143. return ret;
  144. }
  145. ValueNodePtr CreateNewValueNode(const AnfNodePtr &anf, KernelGraph *graph) {
  146. auto value_node = anf->cast<ValueNodePtr>();
  147. MS_EXCEPTION_IF_NULL(value_node);
  148. auto value = value_node->value();
  149. MS_EXCEPTION_IF_NULL(value);
  150. if (value->isa<None>()) {
  151. return nullptr;
  152. }
  153. auto new_value_node = graph->NewValueNode(value_node);
  154. graph->FrontBackendlMapAdd(anf, new_value_node);
  155. graph->AddValueNodeToGraph(new_value_node);
  156. return new_value_node;
  157. }
  158. std::vector<AnfNodePtr> CreateParameterFromTuple(const AnfNodePtr &node, bool valid_input, KernelGraph *graph) {
  159. MS_EXCEPTION_IF_NULL(node);
  160. MS_EXCEPTION_IF_NULL(graph);
  161. std::vector<AnfNodePtr> parameters;
  162. std::vector<AnfNodePtr> pre_graph_out = {node};
  163. // If a cnode is a call, it's input0 is a cnode too, so it doesn't have primitive
  164. if (!AnfAlgo::IsRealKernel(node)) {
  165. pre_graph_out = AnfAlgo::GetAllOutput(node, {prim::kPrimTupleGetItem});
  166. }
  167. auto valid_inputs = graph->MutableValidInputs();
  168. MS_EXCEPTION_IF_NULL(valid_inputs);
  169. auto graph_inputs = graph->MutableInputs();
  170. MS_EXCEPTION_IF_NULL(graph_inputs);
  171. auto create_parameter = [&](const AbstractBasePtr &abstract) -> void {
  172. auto parameter = graph->NewParameter();
  173. MS_EXCEPTION_IF_NULL(parameter);
  174. parameter->set_abstract(abstract);
  175. auto new_parameter = graph->NewParameter(parameter);
  176. parameters.push_back(new_parameter);
  177. valid_inputs->push_back(valid_input);
  178. graph_inputs->push_back(new_parameter);
  179. };
  180. for (const auto &out_node : pre_graph_out) {
  181. MS_EXCEPTION_IF_NULL(out_node);
  182. auto abstract = out_node->abstract();
  183. MS_EXCEPTION_IF_NULL(abstract);
  184. // create multiple parameters if is a tuple output real kernel
  185. if (abstract->isa<abstract::AbstractTuple>() && !AnfAlgo::CheckPrimitiveType(out_node, prim::kPrimTupleGetItem)) {
  186. auto tuple_abstract = abstract->cast<abstract::AbstractTuplePtr>();
  187. MS_EXCEPTION_IF_NULL(tuple_abstract);
  188. MS_LOG(INFO) << "tuple_size [" << tuple_abstract->size() << "]";
  189. for (size_t output_idx = 0; output_idx < tuple_abstract->size(); output_idx++) {
  190. create_parameter((*tuple_abstract)[output_idx]);
  191. }
  192. continue;
  193. }
  194. // create single parameter if is a abstract real kernel
  195. create_parameter(out_node->abstract());
  196. }
  197. return parameters;
  198. }
  199. size_t LoadCtrlInputTensor(const std::shared_ptr<KernelGraph> &graph, std::vector<tensor::TensorPtr> *inputs) {
  200. MS_LOG(INFO) << "Load kInputCtrlTensors";
  201. auto inputs_params = graph->input_ctrl_tensors();
  202. if (inputs_params == nullptr) {
  203. return 0;
  204. }
  205. if (inputs_params->empty()) {
  206. MS_LOG(EXCEPTION) << "Illegal empty inputs_params";
  207. }
  208. auto tensor = (*inputs_params)[0];
  209. MS_EXCEPTION_IF_NULL(tensor);
  210. auto *val = static_cast<int32_t *>(tensor->data_c(true));
  211. MS_EXCEPTION_IF_NULL(val);
  212. *val = 0;
  213. tensor->set_dirty(true);
  214. // set loop_count to zero
  215. MS_EXCEPTION_IF_NULL(inputs);
  216. inputs->push_back(tensor);
  217. return inputs_params->size();
  218. }
  219. ValueNodePtr ConstructRunOpValueNode(const std::shared_ptr<KernelGraph> &graph, const tensor::TensorPtr &input_tensor) {
  220. MS_EXCEPTION_IF_NULL(graph);
  221. MS_EXCEPTION_IF_NULL(input_tensor);
  222. auto value_node = std::make_shared<ValueNode>(input_tensor);
  223. // construct abstract of value node
  224. auto type_of_tensor = input_tensor->Dtype();
  225. auto shape_of_tensor = input_tensor->shape();
  226. auto abstract = std::make_shared<abstract::AbstractTensor>(type_of_tensor, shape_of_tensor);
  227. value_node->set_abstract(abstract);
  228. // add value node to graph
  229. auto input_value_node = graph->NewValueNode(value_node);
  230. graph->AddValueNodeToGraph(input_value_node);
  231. return input_value_node;
  232. }
  233. ParameterPtr ConstructRunOpParameter(const std::shared_ptr<KernelGraph> &graph, const tensor::TensorPtr &input_tensor,
  234. int tensor_mask) {
  235. auto param = graph->NewParameter();
  236. MS_EXCEPTION_IF_NULL(param);
  237. if (tensor_mask == kParameterWeightTensorMask) {
  238. py::object obj;
  239. auto param_value_new = std::make_shared<ParamValuePy>(obj);
  240. param->set_default_param(param_value_new);
  241. }
  242. // set the kernel info of parameter
  243. auto kernel_build_info_builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
  244. MS_EXCEPTION_IF_NULL(input_tensor);
  245. if (input_tensor->device_address().get() == nullptr) {
  246. kernel_build_info_builder->SetOutputsFormat(std::vector<std::string>{kOpFormat_DEFAULT});
  247. TypeId param_init_data_type = AnfAlgo::IsParameterWeight(param) ? kTypeUnknown : input_tensor->data_type();
  248. kernel_build_info_builder->SetOutputsDeviceType(std::vector<TypeId>{param_init_data_type});
  249. } else {
  250. kernel_build_info_builder->SetOutputsFormat(std::vector<std::string>{input_tensor->device_address()->format()});
  251. kernel_build_info_builder->SetOutputsDeviceType(std::vector<TypeId>{input_tensor->device_address()->type_id()});
  252. }
  253. AnfAlgo::SetSelectKernelBuildInfo(kernel_build_info_builder->Build(), param.get());
  254. // construct abstract of parameter
  255. auto type_of_tensor = input_tensor->Dtype();
  256. auto shape_of_tensor = input_tensor->shape();
  257. auto abstract = std::make_shared<abstract::AbstractTensor>(type_of_tensor, shape_of_tensor);
  258. param->set_abstract(abstract);
  259. return param;
  260. }
  261. void DumpGraphOutput(const Any &any, size_t recurse_level = 0) {
  262. MS_LOG(INFO) << "graph outputs:";
  263. const size_t max_deep = 10;
  264. if (recurse_level > max_deep) {
  265. MS_LOG(INFO) << "recurse too deep";
  266. return;
  267. }
  268. std::string tab_str;
  269. for (size_t i = 0; i < recurse_level; i++) {
  270. tab_str = tab_str.append(" ");
  271. }
  272. if (any.is<AnyList>()) {
  273. (void)tab_str.append("{");
  274. MS_LOG(INFO) << tab_str;
  275. auto any_list = any.cast<AnyList>();
  276. for (auto &it : any_list) {
  277. DumpGraphOutput(it, recurse_level + 1);
  278. }
  279. (void)tab_str.append("}");
  280. MS_LOG(INFO) << tab_str;
  281. }
  282. (void)tab_str.append(any.ToString());
  283. MS_LOG(INFO) << tab_str;
  284. }
  285. bool ExistSummaryNode(const KernelGraph *graph) {
  286. auto ret = graph->get_return();
  287. MS_EXCEPTION_IF_NULL(ret);
  288. auto all_nodes = DeepLinkedGraphSearch(ret);
  289. for (auto &n : all_nodes) {
  290. if (IsPrimitiveCNode(n, prim::kPrimScalarSummary) || IsPrimitiveCNode(n, prim::kPrimTensorSummary) ||
  291. IsPrimitiveCNode(n, prim::kPrimImageSummary) || IsPrimitiveCNode(n, prim::kPrimHistogramSummary)) {
  292. return true;
  293. }
  294. }
  295. return false;
  296. }
  297. } // namespace
  298. GraphId SessionBasic::graph_sum_ = 0;
  299. ParameterPtr SessionBasic::CreateNewParameterFromParameter(const AnfNodePtr &anf, bool valid_input,
  300. KernelGraph *graph) {
  301. MS_EXCEPTION_IF_NULL(anf);
  302. if (!anf->isa<Parameter>()) {
  303. MS_LOG(EXCEPTION) << "anf[" << anf->DebugString() << "] is not a parameter";
  304. }
  305. auto m_tensor = GetParamDefaultInputTensor(anf);
  306. auto valid_inputs = graph->MutableValidInputs();
  307. MS_EXCEPTION_IF_NULL(valid_inputs);
  308. auto graph_inputs = graph->MutableInputs();
  309. MS_EXCEPTION_IF_NULL(graph_inputs);
  310. ParameterPtr new_parameter = nullptr;
  311. // if parameter's python parameter has been exist a backend parameter, reuse the exist parameter
  312. if (python_paras_ == nullptr) {
  313. python_paras_ = std::make_shared<std::map<PyObject *, ParameterPtr>>();
  314. }
  315. auto iter = python_paras_->find(m_tensor);
  316. if (iter != python_paras_->end()) {
  317. new_parameter = iter->second;
  318. } else {
  319. TraceManager::DebugTrace(std::make_shared<TraceCopy>(anf->debug_info()));
  320. new_parameter = graph->NewParameter(anf->cast<ParameterPtr>());
  321. if (m_tensor != nullptr) {
  322. (*python_paras_)[m_tensor] = new_parameter;
  323. }
  324. TraceManager::EndTrace();
  325. }
  326. graph_inputs->push_back(new_parameter);
  327. valid_inputs->push_back(valid_input);
  328. return new_parameter;
  329. }
  330. AnfNodePtr SessionBasic::CreateNewParameterFromCNode(const AnfNodePtr &anf, bool valid_input, KernelGraph *graph) {
  331. MS_EXCEPTION_IF_NULL(anf);
  332. MS_LOG(INFO) << "Create a new parameter from cnode[" << anf->DebugString() << "]";
  333. auto parameters = CreateParameterFromTuple(anf, valid_input, graph);
  334. if (parameters.empty()) {
  335. MS_LOG(EXCEPTION) << "No parameter exist!!";
  336. }
  337. if (parameters.size() == 1) {
  338. return parameters[0];
  339. }
  340. std::vector<AnfNodePtr> make_tuple_input = {NewValueNode(prim::kPrimMakeTuple)};
  341. (void)std::copy(parameters.begin(), parameters.end(), std::back_inserter(make_tuple_input));
  342. auto make_tuple = graph->NewCNode(make_tuple_input);
  343. MS_EXCEPTION_IF_NULL(make_tuple);
  344. MS_LOG(INFO) << "New make tuple [" << make_tuple->DebugString() << "] of parameters";
  345. return make_tuple;
  346. }
  347. CNodePtr SessionBasic::CreateNewCNode(const CNodePtr &cnode, bool valid_input, KernelGraph *graph,
  348. bool *from_other_graph,
  349. std::unordered_map<AnfNodePtr, AnfNodePtr> *other_graph_cnode) {
  350. MS_EXCEPTION_IF_NULL(cnode);
  351. MS_EXCEPTION_IF_NULL(graph);
  352. MS_EXCEPTION_IF_NULL(from_other_graph);
  353. MS_EXCEPTION_IF_NULL(other_graph_cnode);
  354. *from_other_graph = false;
  355. // get primitive of old node
  356. std::vector<AnfNodePtr> cnode_inputs;
  357. auto prim = AnfAlgo::GetCNodePrimitive(cnode);
  358. if (prim != nullptr) {
  359. // push attr to inputs[0] of new cnode
  360. cnode_inputs.push_back(std::make_shared<ValueNode>(std::make_shared<Primitive>(*prim)));
  361. } else {
  362. auto fg = AnfAlgo::GetCNodeFuncGraphPtr(cnode);
  363. MS_EXCEPTION_IF_NULL(fg);
  364. auto new_fg = BasicClone(fg);
  365. cnode_inputs.push_back(std::make_shared<ValueNode>(new_fg));
  366. }
  367. // if has multiple depends,only select first depend as parameter
  368. for (size_t input_idx = 1; input_idx < cnode->inputs().size(); input_idx++) {
  369. auto anf = cnode->inputs()[input_idx];
  370. MS_EXCEPTION_IF_NULL(anf);
  371. // anf has been created before
  372. if (graph->GetBackendAnfByFrontAnf(anf) != nullptr) {
  373. cnode_inputs.emplace_back(graph->GetBackendAnfByFrontAnf(anf));
  374. continue;
  375. } else if (other_graph_cnode->find(anf) != other_graph_cnode->end()) {
  376. cnode_inputs.push_back((*other_graph_cnode)[anf]);
  377. continue;
  378. } else if (anf->isa<ValueNode>() && !IsValueNode<FuncGraph>(anf)) {
  379. // if input is a value node,
  380. auto new_value_node = CreateNewValueNode(anf, graph);
  381. if (new_value_node != nullptr) {
  382. cnode_inputs.emplace_back(new_value_node);
  383. }
  384. continue;
  385. } else if (anf->isa<Parameter>() && AnfAlgo::GetOutputTensorNum(anf) == 1) {
  386. auto new_parameter = CreateNewParameterFromParameter(anf, valid_input, graph);
  387. cnode_inputs.push_back(new_parameter);
  388. if (GetGraphIdByNode(anf) == kInvalidGraphId) {
  389. graph->FrontBackendlMapAdd(anf, new_parameter);
  390. } else {
  391. (*other_graph_cnode)[anf] = new_parameter;
  392. }
  393. continue;
  394. } else if (anf->isa<AnfNode>()) {
  395. *from_other_graph = true;
  396. // the input node is a cnode from other graph
  397. auto parameter_from_cnode = CreateNewParameterFromCNode(anf, valid_input, graph);
  398. cnode_inputs.push_back(parameter_from_cnode);
  399. (*other_graph_cnode)[anf] = parameter_from_cnode;
  400. continue;
  401. }
  402. MS_LOG(EXCEPTION) << "Unexpected input[" << anf->DebugString() << "]";
  403. }
  404. TraceManager::DebugTrace(std::make_shared<TraceCopy>(cnode->debug_info()));
  405. auto new_cnode = graph->NewCNode(cnode_inputs);
  406. TraceManager::EndTrace();
  407. return new_cnode;
  408. }
  409. CNodePtr SessionBasic::CreateNewCNode(const CNodePtr &cnode, KernelGraph *graph) {
  410. MS_EXCEPTION_IF_NULL(cnode);
  411. MS_EXCEPTION_IF_NULL(graph);
  412. std::vector<AnfNodePtr> cnode_inputs;
  413. auto attr_input = cnode->input(kAnfPrimitiveIndex);
  414. MS_EXCEPTION_IF_NULL(attr_input);
  415. if (AnfAlgo::IsGraphKernel(cnode)) {
  416. auto fg = AnfAlgo::GetCNodeFuncGraphPtr(cnode);
  417. MS_EXCEPTION_IF_NULL(fg);
  418. auto new_fg = BasicClone(fg);
  419. cnode_inputs.push_back(std::make_shared<ValueNode>(new_fg));
  420. } else if (IsValueNode<FuncGraph>(attr_input)) {
  421. // create primitive of cnode:call
  422. cnode_inputs = {graph->NewValueNode(NewValueNode(std::make_shared<Primitive>(prim::kPrimCall->name())))};
  423. // create a ValueNode<KernelGraph> as input of cnode:call
  424. if (graph->GetBackendAnfByFrontAnf(attr_input) != nullptr) {
  425. cnode_inputs.emplace_back(graph->GetBackendAnfByFrontAnf(attr_input));
  426. } else {
  427. auto new_value_node = CreateValueNodeKernelGraph(attr_input, graph);
  428. if (new_value_node != nullptr) {
  429. cnode_inputs.emplace_back(new_value_node);
  430. }
  431. }
  432. } else if (attr_input->isa<CNode>()) {
  433. // create primitive of cnode:call(switch)
  434. cnode_inputs = {graph->NewValueNode(NewValueNode(std::make_shared<Primitive>(prim::kPrimCall->name())))};
  435. if (graph->GetBackendAnfByFrontAnf(attr_input) != nullptr) {
  436. auto cnode_input = graph->GetBackendAnfByFrontAnf(attr_input);
  437. if (!AnfAlgo::CheckPrimitiveType(cnode_input, prim::kPrimSwitch)) {
  438. MS_LOG(EXCEPTION) << "CNode input[0] must be switch.";
  439. }
  440. cnode_inputs.emplace_back(cnode_input);
  441. } else {
  442. MS_LOG(EXCEPTION) << "CNode input[0] is CNode:" << attr_input->DebugString()
  443. << ", but input[0] has not been created.";
  444. }
  445. } else {
  446. // get primitive of old node
  447. auto prim = AnfAlgo::GetCNodePrimitive(cnode);
  448. MS_EXCEPTION_IF_NULL(prim);
  449. // push attr to inputs[0] of new cnode
  450. cnode_inputs = {graph->NewValueNode(NewValueNode(std::make_shared<Primitive>(*prim)))};
  451. }
  452. for (size_t input_idx = 1; input_idx < cnode->inputs().size(); input_idx++) {
  453. auto anf = cnode->input(input_idx);
  454. MS_EXCEPTION_IF_NULL(anf);
  455. // anf has been created before
  456. if (graph->GetBackendAnfByFrontAnf(anf) != nullptr) {
  457. cnode_inputs.emplace_back(graph->GetBackendAnfByFrontAnf(anf));
  458. continue;
  459. } else if (IsValueNode<None>(anf)) {
  460. continue;
  461. }
  462. MS_LOG(EXCEPTION) << "Unexpected input[" << anf->DebugString() << "]";
  463. }
  464. TraceManager::DebugTrace(std::make_shared<TraceCopy>(cnode->debug_info()));
  465. auto new_cnode = graph->NewCNode(cnode_inputs);
  466. TraceManager::EndTrace();
  467. return new_cnode;
  468. }
  469. ValueNodePtr SessionBasic::CreateValueNodeKernelGraph(const AnfNodePtr &anf, KernelGraph *graph) {
  470. MS_EXCEPTION_IF_NULL(anf);
  471. auto value_node = anf->cast<ValueNodePtr>();
  472. MS_EXCEPTION_IF_NULL(value_node);
  473. auto sub_func_graph = AnfAlgo::GetValueNodeFuncGraph(anf);
  474. MS_EXCEPTION_IF_NULL(sub_func_graph);
  475. if (front_backend_graph_map_.find(sub_func_graph) == front_backend_graph_map_.end()) {
  476. MS_LOG(EXCEPTION) << "FuncGraph: " << sub_func_graph->ToString() << " has not been transformed to KernelGraph.";
  477. }
  478. auto sub_kernel_graph = front_backend_graph_map_[sub_func_graph];
  479. ValueNodePtr new_value_node = std::make_shared<ValueNode>(sub_kernel_graph);
  480. new_value_node->set_abstract(value_node->abstract());
  481. // create new kernel_info of new value_node
  482. auto kernel_info = std::make_shared<device::KernelInfo>();
  483. kernel_info->SetFeatureMapFlag(false);
  484. new_value_node->set_kernel_info(kernel_info);
  485. // create kernel_build_info for new value node
  486. auto kernel_build_info_builder = std::make_shared<kernel::KernelBuildInfo::KernelBuildInfoBuilder>();
  487. AnfAlgo::SetSelectKernelBuildInfo(kernel_build_info_builder->Build(), new_value_node.get());
  488. AnfAlgo::SetGraphId(graph->graph_id(), new_value_node.get());
  489. graph->FrontBackendlMapAdd(anf, new_value_node);
  490. return new_value_node;
  491. }
  492. ParameterPtr SessionBasic::CreateNewParameter(const AnfNodePtr &anf, KernelGraph *graph) {
  493. MS_EXCEPTION_IF_NULL(anf);
  494. if (!anf->isa<Parameter>()) {
  495. MS_LOG(EXCEPTION) << "anf[" << anf->DebugString() << "] is not a parameter";
  496. }
  497. auto m_tensor = GetParamDefaultInputTensor(anf);
  498. ParameterPtr new_parameter = nullptr;
  499. if (python_paras_ == nullptr) {
  500. python_paras_ = std::make_shared<std::map<PyObject *, ParameterPtr>>();
  501. }
  502. auto iter = python_paras_->find(m_tensor);
  503. if (iter != python_paras_->end()) {
  504. new_parameter = iter->second;
  505. } else {
  506. TraceManager::DebugTrace(std::make_shared<TraceCopy>(anf->debug_info()));
  507. new_parameter = graph->NewParameter(anf->cast<ParameterPtr>());
  508. if (m_tensor != nullptr) {
  509. (*python_paras_)[m_tensor] = new_parameter;
  510. }
  511. TraceManager::EndTrace();
  512. }
  513. return new_parameter;
  514. }
  515. KernelGraphPtr SessionBasic::ConstructKernelGraph(const AnfNodePtrList &lst, const AnfNodePtrList &outputs) {
  516. std::unordered_map<AnfNodePtr, AnfNodePtr> other_graph_cnode;
  517. auto graph = NewKernelGraph();
  518. MS_LOG(INFO) << "Create graph: " << graph->graph_id();
  519. size_t from_other_graph_depend_num = 0;
  520. for (const auto &node : lst) {
  521. MS_EXCEPTION_IF_NULL(node);
  522. MS_LOG(DEBUG) << "Start create new cnode, node = " << node->DebugString();
  523. if (!node->isa<CNode>()) {
  524. MS_LOG(EXCEPTION) << "Node " << node->DebugString() << " is not CNode";
  525. }
  526. auto cnode = node->cast<CNodePtr>();
  527. MS_EXCEPTION_IF_NULL(cnode);
  528. // create a new cnode object
  529. bool from_other_graph = false;
  530. // only first depend from other graph can create
  531. bool valid_input = true;
  532. if (from_other_graph_depend_num != 0 && AnfAlgo::CheckPrimitiveType(node, prim::kPrimDepend)) {
  533. valid_input = false;
  534. }
  535. auto new_cnode = CreateNewCNode(cnode, valid_input, graph.get(), &from_other_graph, &other_graph_cnode);
  536. if (AnfAlgo::CheckPrimitiveType(node, prim::kPrimDepend) && from_other_graph) {
  537. from_other_graph_depend_num++;
  538. }
  539. MS_EXCEPTION_IF_NULL(new_cnode);
  540. new_cnode->set_abstract(cnode->abstract());
  541. new_cnode->set_scope(cnode->scope());
  542. // record map relations between anf from ME and new anf node used in backend
  543. graph->FrontBackendlMapAdd(node, new_cnode);
  544. }
  545. // add a make_tuple at the end of graph as output
  546. graph->set_output(ConstructOutput(outputs, graph));
  547. MS_EXCEPTION_IF_NULL(context_);
  548. FuncGraphManagerPtr manager = MakeManager({graph});
  549. if (manager) {
  550. manager->AddFuncGraph(graph);
  551. graph->set_manager(manager);
  552. }
  553. graph->SetExecOrderByDefault();
  554. if (ExistSummaryNode(graph.get())) {
  555. graph->set_summary_node_exist(true);
  556. }
  557. opt::BackendCommonOptimization(graph);
  558. return graph;
  559. }
  560. std::shared_ptr<KernelGraph> SessionBasic::ConstructKernelGraph(const FuncGraphPtr &func_graph,
  561. std::vector<KernelGraphPtr> *all_out_graph) {
  562. MS_EXCEPTION_IF_NULL(func_graph);
  563. MS_EXCEPTION_IF_NULL(all_out_graph);
  564. auto node_list = TopoSort(func_graph->get_return());
  565. auto graph = NewKernelGraph();
  566. front_backend_graph_map_[func_graph] = graph;
  567. MS_LOG(INFO) << "Create graph: " << graph->graph_id();
  568. bool is_trace_back = false;
  569. for (const auto &node : node_list) {
  570. MS_EXCEPTION_IF_NULL(node);
  571. MS_LOG(DEBUG) << "Start create new cnode, node = " << node->DebugString();
  572. if (node->isa<Parameter>()) {
  573. auto graph_inputs = graph->MutableInputs();
  574. MS_EXCEPTION_IF_NULL(graph_inputs);
  575. auto new_parameter = CreateNewParameter(node, graph.get());
  576. graph_inputs->push_back(new_parameter);
  577. graph->FrontBackendlMapAdd(node, new_parameter);
  578. continue;
  579. } else if (node->isa<ValueNode>()) {
  580. if (!IsValueNode<FuncGraph>(node)) {
  581. // if input is a common value node,
  582. (void)CreateNewValueNode(node, graph.get());
  583. } else {
  584. // if input is a ValueNode<FuncGraph>
  585. FuncGraphPtr child_graph = AnfAlgo::GetValueNodeFuncGraph(node);
  586. if (front_backend_graph_map_.find(child_graph) != front_backend_graph_map_.end()) {
  587. is_trace_back = true;
  588. } else {
  589. (void)ConstructKernelGraph(child_graph, all_out_graph);
  590. }
  591. (void)CreateValueNodeKernelGraph(node, graph.get());
  592. }
  593. continue;
  594. } else {
  595. auto cnode = node->cast<CNodePtr>();
  596. MS_EXCEPTION_IF_NULL(cnode);
  597. // create a new cnode object
  598. auto new_cnode = CreateNewCNode(cnode, graph.get());
  599. MS_EXCEPTION_IF_NULL(new_cnode);
  600. new_cnode->set_abstract(cnode->abstract());
  601. new_cnode->set_fullname_with_scope(cnode->fullname_with_scope());
  602. new_cnode->set_scope(cnode->scope());
  603. graph->FrontBackendlMapAdd(node, new_cnode);
  604. if (AnfAlgo::CheckPrimitiveType(new_cnode, prim::kPrimReturn)) {
  605. graph->set_return(new_cnode);
  606. }
  607. }
  608. }
  609. // if a graph jump back unconditionally, return op of this graph will never be executed, so output is null.
  610. graph->set_output_null(is_trace_back);
  611. AddParameterToGraphInputs(func_graph->parameters(), graph.get());
  612. graph->SetExecOrderByDefault();
  613. if (ExistSummaryNode(graph.get())) {
  614. graph->set_summary_node_exist(true);
  615. }
  616. all_out_graph->push_back(graph);
  617. return graph;
  618. }
  619. void SessionBasic::AddParameterToGraphInputs(const std::vector<AnfNodePtr> &parameters, KernelGraph *graph) {
  620. MS_EXCEPTION_IF_NULL(graph);
  621. auto graph_inputs = graph->MutableInputs();
  622. MS_EXCEPTION_IF_NULL(graph_inputs);
  623. graph_inputs->clear();
  624. for (auto &parameter : parameters) {
  625. MS_EXCEPTION_IF_NULL(parameter);
  626. auto backend_parameter = graph->GetBackendAnfByFrontAnf(parameter);
  627. if (backend_parameter == nullptr) {
  628. // for example "def f(x,y,z) {return x + y}", parameter z in unused
  629. auto new_parameter = CreateNewParameter(parameter, graph);
  630. graph_inputs->push_back(new_parameter);
  631. MS_LOG(INFO) << "Can't find parameter:" << parameter->DebugString();
  632. continue;
  633. }
  634. MS_LOG(INFO) << "graph[" << graph->graph_id() << "],parameter:" << parameter->DebugString();
  635. graph_inputs->push_back(backend_parameter);
  636. }
  637. }
  638. // run graph steps
  639. void SessionBasic::LoadInputData(const std::shared_ptr<KernelGraph> &kernel_graph,
  640. const std::vector<tensor::TensorPtr> &inputs_const) const {
  641. std::vector<tensor::TensorPtr> inputs(inputs_const);
  642. size_t input_ctrl_size = 1;
  643. MS_EXCEPTION_IF_NULL(kernel_graph);
  644. if (kernel_graph->input_ctrl_tensors()) {
  645. input_ctrl_size = LoadCtrlInputTensor(kernel_graph, &inputs);
  646. }
  647. auto input_nodes = kernel_graph->inputs();
  648. if ((inputs.size() + input_ctrl_size) - 1 != input_nodes.size()) {
  649. MS_LOG(EXCEPTION) << "tensor input:" << inputs.size() << " is not equal graph inputs:" << input_nodes.size()
  650. << ", input_ctrl_size:" << input_ctrl_size;
  651. }
  652. auto ms_context = MsContext::GetInstance();
  653. MS_EXCEPTION_IF_NULL(ms_context);
  654. for (size_t i = 0; i < inputs.size(); ++i) {
  655. auto tensor = inputs[i];
  656. MS_EXCEPTION_IF_NULL(tensor);
  657. auto input_node = input_nodes[i];
  658. MS_EXCEPTION_IF_NULL(input_node);
  659. if (input_node->isa<Parameter>() && AnfAlgo::OutputAddrExist(input_node, 0)) {
  660. auto pk_node = input_node->cast<ParameterPtr>();
  661. auto device_address = AnfAlgo::GetMutableOutputAddr(pk_node, 0);
  662. bool need_sync = false;
  663. if (ms_context->enable_pynative_infer()) {
  664. if (tensor->device_address().get() == nullptr || tensor->device_address() != device_address) {
  665. need_sync = true;
  666. }
  667. } else {
  668. if (tensor->is_dirty()) {
  669. need_sync = true;
  670. } else if (tensor->device_address() != device_address) {
  671. (void)tensor->data_sync();
  672. need_sync = true;
  673. }
  674. }
  675. if (need_sync) {
  676. if (ms_context->execution_mode() == kPynativeMode || AnfAlgo::IsParameterWeight(pk_node)) {
  677. tensor->set_device_address(device_address);
  678. }
  679. MS_EXCEPTION_IF_NULL(device_address);
  680. if (!device_address->SyncHostToDevice(trans::GetRuntimePaddingShape(pk_node, 0),
  681. LongToSize(tensor->data().nbytes()), tensor->data_type(),
  682. tensor->data_c(false))) {
  683. MS_LOG(EXCEPTION) << "SyncHostToDevice failed.";
  684. }
  685. }
  686. }
  687. tensor->set_dirty(false);
  688. }
  689. }
  690. void SessionBasic::UpdateOutputs(const std::shared_ptr<KernelGraph> &kernel_graph, VectorRef *const outputs,
  691. const std::vector<tensor::TensorPtr> &input_tensors) const {
  692. MS_EXCEPTION_IF_NULL(kernel_graph);
  693. MS_EXCEPTION_IF_NULL(outputs);
  694. if (!kernel_graph->child_graph_order().empty()) {
  695. // use the last child graph output as the root graph output
  696. UpdateOutputs(kernel_graph->child_graph_order().back(), outputs, input_tensors);
  697. return;
  698. }
  699. auto anf_outputs = kernel_graph->outputs();
  700. for (auto &item : anf_outputs) {
  701. MS_LOG(INFO) << "update output[" << item->DebugString() << "]";
  702. MS_EXCEPTION_IF_NULL(item);
  703. if (AnfAlgo::IsTupleOutput(item) && AnfAlgo::IsRealKernel(item)) {
  704. outputs->emplace_back(CreatTupleForOutput(item, *kernel_graph, input_tensors));
  705. continue;
  706. }
  707. outputs->emplace_back(CreatTensorForOutput(item, *kernel_graph, input_tensors));
  708. }
  709. }
  710. void SessionBasic::RegisterSummaryCallBackFunc(const CallBackFunc &callback) {
  711. MS_EXCEPTION_IF_NULL(callback);
  712. summary_callback_ = callback;
  713. }
  714. void SessionBasic::Reorder(std::vector<CNodePtr> *node_list) { AnfAlgo::ReorderExecList(NOT_NULL(node_list)); }
  715. void SessionBasic::GetSummaryNodes(KernelGraph *graph) {
  716. MS_LOG(DEBUG) << "Update summary Start";
  717. MS_EXCEPTION_IF_NULL(graph);
  718. if (!graph->summary_node_exist()) {
  719. return;
  720. }
  721. auto summary = graph->summary_nodes();
  722. auto apply_list = TopoSort(graph->get_return());
  723. for (auto &n : apply_list) {
  724. MS_EXCEPTION_IF_NULL(n);
  725. if (IsPrimitiveCNode(n, prim::kPrimScalarSummary) || IsPrimitiveCNode(n, prim::kPrimTensorSummary) ||
  726. IsPrimitiveCNode(n, prim::kPrimImageSummary) || IsPrimitiveCNode(n, prim::kPrimHistogramSummary)) {
  727. auto cnode = n->cast<CNodePtr>();
  728. MS_EXCEPTION_IF_NULL(cnode);
  729. if (cnode->inputs().size() <= kSummaryGetItem) {
  730. MS_LOG(EXCEPTION) << "the node Summary should have 2 inputs at least!";
  731. }
  732. auto node = cnode->input(kSummaryGetItem);
  733. MS_EXCEPTION_IF_NULL(node);
  734. auto item_with_index = AnfAlgo::VisitKernelWithReturnType(node, 0, true);
  735. if (!AnfAlgo::IsRealKernel(item_with_index.first)) {
  736. MS_LOG(EXCEPTION) << "Unexpected node:" << item_with_index.first->DebugString();
  737. }
  738. summary[n->fullname_with_scope()] = item_with_index;
  739. }
  740. }
  741. graph->set_summary_nodes(summary);
  742. MS_LOG(DEBUG) << "Update summary end size: " << summary.size();
  743. }
  744. void SessionBasic::Summary(KernelGraph *graph) {
  745. if (summary_callback_ == nullptr) {
  746. return;
  747. }
  748. MS_EXCEPTION_IF_NULL(graph);
  749. bool exist_summary = graph->summary_node_exist();
  750. if (!exist_summary) {
  751. return;
  752. }
  753. GetSummaryNodes(graph);
  754. auto summary_outputs = graph->summary_nodes();
  755. std::map<std::string, tensor::TensorPtr> params_list;
  756. // fetch outputs apply kernel in session & run callback functions
  757. for (auto &output_item : summary_outputs) {
  758. auto node = output_item.second.first;
  759. size_t index = IntToSize(output_item.second.second);
  760. auto address = AnfAlgo::GetOutputAddr(node, index);
  761. auto shape = AnfAlgo::GetOutputInferShape(node, index);
  762. TypeId type_id = AnfAlgo::GetOutputInferDataType(node, index);
  763. std::vector<int> temp_shape;
  764. (void)std::copy(shape.begin(), shape.end(), std::back_inserter(temp_shape));
  765. tensor::TensorPtr tensor = std::make_shared<tensor::Tensor>(type_id, temp_shape);
  766. MS_EXCEPTION_IF_NULL(address);
  767. if (!address->GetPtr()) {
  768. continue;
  769. }
  770. if (!address->SyncDeviceToHost(trans::GetRuntimePaddingShape(node, index), LongToSize(tensor->data().nbytes()),
  771. tensor->data_type(), tensor->data_c(true))) {
  772. MS_LOG(ERROR) << "Failed to sync output from device to host.";
  773. }
  774. tensor->set_dirty(false);
  775. params_list[output_item.first] = tensor;
  776. }
  777. // call callback function here
  778. summary_callback_(0, params_list);
  779. }
  780. CNodePtr SessionBasic::ConstructOutput(const AnfNodePtrList &outputs, const std::shared_ptr<KernelGraph> &graph) {
  781. MS_EXCEPTION_IF_NULL(graph);
  782. std::vector<AnfNodePtr> output_args;
  783. for (const auto &output : outputs) {
  784. MS_LOG(INFO) << "output:" << output->DebugString();
  785. }
  786. auto FindEqu = [graph, outputs](const AnfNodePtr &out) -> AnfNodePtr {
  787. auto backend_anf = graph->GetBackendAnfByFrontAnf(out);
  788. if (backend_anf != nullptr) {
  789. return backend_anf;
  790. }
  791. MS_LOG(EXCEPTION) << "Can't find the node in the equiv map!";
  792. };
  793. output_args.push_back(NewValueNode(prim::kPrimMakeTuple));
  794. (void)std::transform(outputs.begin(), outputs.end(), std::back_inserter(output_args),
  795. [&](const AnfNodePtr &out) -> AnfNodePtr { return FindEqu(out); });
  796. return graph->NewCNode(output_args);
  797. }
  798. void SessionBasic::CreateOutputNode(const CNodePtr &cnode, const std::shared_ptr<KernelGraph> &graph) {
  799. MS_LOG(INFO) << "Start!";
  800. std::vector<AnfNodePtr> make_tuple_inputs;
  801. make_tuple_inputs.push_back(NewValueNode(prim::kPrimMakeTuple));
  802. if (AnfRuntimeAlgorithm::GetOutputTensorNum(cnode) > 1) {
  803. for (size_t output_index = 0; output_index < AnfRuntimeAlgorithm::GetOutputTensorNum(cnode); output_index++) {
  804. auto idx = NewValueNode(SizeToInt(output_index));
  805. MS_EXCEPTION_IF_NULL(idx);
  806. auto imm = std::make_shared<Int32Imm>(output_index);
  807. idx->set_abstract(std::make_shared<abstract::AbstractScalar>(imm));
  808. MS_EXCEPTION_IF_NULL(graph);
  809. auto getitem = graph->NewCNode({NewValueNode(prim::kPrimTupleGetItem), cnode, idx});
  810. std::vector<TypeId> types = {AnfAlgo::GetOutputInferDataType(cnode, output_index)};
  811. std::vector<std::vector<size_t>> shapes = {AnfAlgo::GetOutputInferShape(cnode, output_index)};
  812. AnfAlgo::SetOutputInferTypeAndShape(types, shapes, getitem.get());
  813. make_tuple_inputs.push_back(getitem);
  814. }
  815. } else {
  816. make_tuple_inputs.push_back(cnode);
  817. }
  818. // create output
  819. auto g_output = graph->NewCNode(make_tuple_inputs);
  820. graph->set_output(g_output);
  821. // set graph manager,which now is only used to get valuenodes and hardware optimizing
  822. MS_EXCEPTION_IF_NULL(context_);
  823. FuncGraphManagerPtr manager = context_->manager();
  824. if (manager != nullptr) {
  825. manager->AddFuncGraph(graph);
  826. graph->set_manager(manager);
  827. }
  828. MS_LOG(INFO) << "Finish!";
  829. }
  830. std::shared_ptr<KernelGraph> SessionBasic::ConstructSingleOpGraph(const OpRunInfo &op_run_info,
  831. const std::vector<tensor::TensorPtr> &input_tensors,
  832. const std::vector<int> &tensors_mask) {
  833. auto graph = std::make_shared<KernelGraph>();
  834. std::vector<AnfNodePtr> inputs;
  835. // set input[0]
  836. PrimitivePtr op_prim = op_run_info.py_primitive;
  837. MS_EXCEPTION_IF_NULL(op_prim);
  838. inputs.push_back(std::make_shared<ValueNode>(op_prim));
  839. // set input parameter
  840. MS_LOG(INFO) << "Input tensor size: " << input_tensors.size();
  841. if (input_tensors.size() != tensors_mask.size()) {
  842. MS_LOG(EXCEPTION) << "Input tensors size " << input_tensors.size() << " should be equal to tensors mask size "
  843. << tensors_mask.size();
  844. }
  845. for (size_t i = 0; i < input_tensors.size(); ++i) {
  846. if (tensors_mask[i] == kValueNodeTensorMask) {
  847. auto value_node = ConstructRunOpValueNode(graph, input_tensors[i]);
  848. inputs.push_back(value_node);
  849. continue;
  850. }
  851. auto parameter = ConstructRunOpParameter(graph, input_tensors[i], tensors_mask[i]);
  852. inputs.push_back(parameter);
  853. graph->MutableInputs()->push_back(parameter);
  854. }
  855. // set execution order
  856. auto cnode = graph->NewCNode(inputs);
  857. MS_EXCEPTION_IF_NULL(cnode);
  858. // set abstract,which include inferred shapes and types
  859. cnode->set_abstract(op_run_info.abstract);
  860. // set execution order
  861. std::vector<CNodePtr> exe_order = {cnode};
  862. graph->set_execution_order(exe_order);
  863. // set output
  864. CreateOutputNode(cnode, graph);
  865. return graph;
  866. }
  867. BaseRef SessionBasic::TransformBaseRefListToTuple(const BaseRef &base_ref) {
  868. if (utils::isa<VectorRef>(base_ref)) {
  869. auto ref_list = utils::cast<VectorRef>(base_ref);
  870. py::tuple output_tensors(ref_list.size());
  871. for (size_t i = 0; i < ref_list.size(); ++i) {
  872. auto output = TransformBaseRefListToTuple(ref_list[i]); // use pyObjectRef
  873. if (utils::isa<tensor::TensorPtr>(output)) {
  874. auto tensor_ptr = utils::cast<tensor::TensorPtr>(output);
  875. MS_EXCEPTION_IF_NULL(tensor_ptr);
  876. output_tensors[i] = tensor_ptr;
  877. } else if (utils::isa<PyObjectRef>(output)) {
  878. py::object obj = utils::cast<PyObjectRef>(output).object_;
  879. py::tuple tensor_tuple = py::cast<py::tuple>(obj);
  880. output_tensors[i] = tensor_tuple;
  881. } else {
  882. MS_LOG(EXCEPTION) << "The output is not a base ref list or a tensor!";
  883. }
  884. }
  885. return output_tensors; // turn tuple to py::object and store in PyObjectRef
  886. } else if (utils::isa<tensor::TensorPtr>(base_ref)) {
  887. return base_ref;
  888. } else {
  889. MS_LOG(EXCEPTION) << "The output is not a base ref list or a tensor!";
  890. }
  891. }
  892. KernelGraphPtr SessionBasic::NewKernelGraph() {
  893. auto graph = std::make_shared<KernelGraph>();
  894. graph->set_graph_id(graph_sum_);
  895. graphs_[graph_sum_++] = graph;
  896. return graph;
  897. }
  898. } // namespace session
  899. } // namespace mindspore