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.

single_kernel_graph.cc 2.7 kB

4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. /**
  2. * Copyright 2021 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 "backend/session/single_kernel_graph.h"
  17. #include "backend/session/anf_runtime_algorithm.h"
  18. #include "utils/trace_base.h"
  19. namespace mindspore {
  20. namespace session {
  21. std::shared_ptr<session::KernelGraph> SingleKernelGraph::ConstructKernelGraphBasedOnSingleOp(
  22. const std::string &op_name, const std::vector<TypeId> &input_dtypes, const std::vector<ShapeVector> &input_shapes,
  23. const std::vector<TypeId> &output_dtypes, const std::vector<std::vector<size_t>> &output_shapes) {
  24. auto graph = std::make_shared<session::KernelGraph>();
  25. MS_EXCEPTION_IF_NULL(graph);
  26. std::vector<AnfNodePtr> inputs;
  27. // set input[0]
  28. PrimitivePtr op_prim = std::make_shared<Primitive>(op_name);
  29. MS_EXCEPTION_IF_NULL(op_prim);
  30. inputs.push_back(std::make_shared<ValueNode>(op_prim));
  31. // construct real input
  32. if (input_dtypes.size() != input_shapes.size()) {
  33. MS_LOG(EXCEPTION) << " input_dtypes size should equal to input_shapes size, the op name is: " << op_name;
  34. }
  35. auto input_num = input_dtypes.size();
  36. for (size_t i = 0; i < input_num; ++i) {
  37. auto tensor = std::make_shared<tensor::Tensor>(input_dtypes[i], input_shapes[i]);
  38. auto value_node = graph->NewValueNode(tensor);
  39. inputs.push_back(value_node);
  40. }
  41. // obtain cnode
  42. auto cnode = graph->NewCNode(inputs);
  43. MS_EXCEPTION_IF_NULL(cnode);
  44. // get output dynamic shape info
  45. AnfAlgo::SetNodeAttr(kAttrOutputIsDynamicShape, MakeValue(false), cnode);
  46. if (output_dtypes.size() != output_shapes.size()) {
  47. MS_LOG(EXCEPTION)
  48. << "The size of output_dtypes should be equal to size of output_shapes, but got output_dtypes size: "
  49. << output_dtypes.size() << ", output_shapes size: " << output_shapes.size() << ". The op name is: " << op_name
  50. << trace::DumpSourceLines(cnode);
  51. }
  52. AnfAlgo::SetOutputInferTypeAndShape(output_dtypes, output_shapes, cnode.get());
  53. // set execution order
  54. std::vector<CNodePtr> exe_order = {cnode};
  55. graph->set_execution_order(exe_order);
  56. // set graph output
  57. graph->set_output(cnode);
  58. graph->SetInputNodes();
  59. return graph;
  60. }
  61. } // namespace session
  62. } // namespace mindspore