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

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