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.

generate_graph.cc 6.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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 "parallel/graph_util/generate_graph.h"
  17. #include <algorithm>
  18. #include <memory>
  19. #include <string>
  20. #include <utility>
  21. using mindspore::tensor::Tensor;
  22. namespace mindspore {
  23. namespace parallel {
  24. std::string GetOpPythonPath(const OperatorName &op_name) {
  25. // almost all ops are defined in two main paths
  26. const std::string ops_module = OP_PATH;
  27. py::module mod = py::module::import(common::SafeCStr(ops_module));
  28. if (!py::hasattr(mod, common::SafeCStr(op_name))) {
  29. MS_LOG(EXCEPTION) << ops_module << " don't have op:" << op_name;
  30. }
  31. return ops_module;
  32. }
  33. ValuePtr CreatOpInstance(const OperatorAttrs &attrs, const OperatorName &op_name, const std::string &instance_name) {
  34. std::string op_path = GetOpPythonPath(op_name);
  35. py::module mod = py::module::import(common::SafeCStr(op_path));
  36. if (!py::hasattr(mod, common::SafeCStr(op_name))) {
  37. MS_LOG(ERROR) << "Failure: op_path:" << op_path << " don't have attr " << op_name;
  38. return nullptr;
  39. }
  40. std::vector<py::object> arg_list;
  41. (void)std::transform(attrs.begin(), attrs.end(), std::back_inserter(arg_list),
  42. [](const Attr &attr) { return ValuePtrToPyData(attr.second); });
  43. py::object obj =
  44. parse::python_adapter::CallPyFn(GET_OP_FUNCTION_PATH, GET_OP_FUNCTION, op_name, op_path, instance_name, arg_list);
  45. ValuePtr op_instance = nullptr;
  46. bool succ = parse::ConvertData(obj, &op_instance);
  47. if (!succ) {
  48. MS_LOG(ERROR) << "Failure:get Python op " << op_path << " from " << op_name << " fail";
  49. return nullptr;
  50. }
  51. return op_instance;
  52. }
  53. AnfNodePtr ValuePtrToAnfNodePtr(const ValuePtr &value_ptr) {
  54. auto value_node = NewValueNode(value_ptr);
  55. MS_EXCEPTION_IF_NULL(value_node);
  56. return value_node->cast<AnfNodePtr>();
  57. }
  58. static std::unordered_map<int32_t, AnfNodePtr> int_tensor_map = {};
  59. AnfNodePtr CreateInt32Tensor(int32_t value) {
  60. auto it = int_tensor_map.find(value);
  61. if (it != int_tensor_map.end()) {
  62. return it->second;
  63. }
  64. mindspore::tensor::TensorPtr tensor_ptr = std::make_shared<tensor::Tensor>(py::int_(value), kInt32);
  65. ValuePtr value_ptr = MakeValue(tensor_ptr);
  66. auto anf_node_ptr = ValuePtrToAnfNodePtr(value_ptr);
  67. int_tensor_map[value] = anf_node_ptr;
  68. return anf_node_ptr;
  69. }
  70. AnfNodePtr CreatTypeInt(int32_t value) {
  71. ValuePtr value_ptr = MakeValue(std::make_shared<Int>(value));
  72. return ValuePtrToAnfNodePtr(value_ptr);
  73. }
  74. AnfNodePtr CreatInt32Imm(int32_t value) {
  75. ValuePtr value_ptr = MakeValue(std::make_shared<Int32Imm>(value));
  76. return ValuePtrToAnfNodePtr(value_ptr);
  77. }
  78. std::string GetInstanceNameByCNode(const CNodePtr &cnode) {
  79. PrimitivePtr prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  80. if (!prim) {
  81. MS_LOG(EXCEPTION) << "The first input of the cnode is not a PrimitivePtr.";
  82. }
  83. std::string instance_name = prim->instance_name();
  84. return HashInstanceName(instance_name);
  85. }
  86. std::string HashInstanceName(const std::string &name) {
  87. auto using_hash_name = common::GetEnv(USING_HASH_NAME);
  88. std::string instance_name;
  89. if ((using_hash_name.empty()) || (using_hash_name == "on")) {
  90. instance_name = HashName(name);
  91. } else {
  92. instance_name = name;
  93. }
  94. return instance_name;
  95. }
  96. Status GenerateGraph::Init(const CNodePtr &cnode) {
  97. if (!cnode) {
  98. MS_LOG(ERROR) << "Init:cnode is nullptr";
  99. return FAILED;
  100. }
  101. cnode_ = cnode;
  102. func_graph_ = cnode->func_graph();
  103. if (!func_graph_) {
  104. MS_LOG(ERROR) << "Init:func_graph_ is nullptr";
  105. return FAILED;
  106. }
  107. manager_ = func_graph_->manager();
  108. if (!manager_) {
  109. MS_LOG(ERROR) << "Init:manager_ is nullptr";
  110. return FAILED;
  111. }
  112. scope_ = cnode_->scope();
  113. if (!scope_) {
  114. MS_LOG(ERROR) << "Init:scope_ is nullptr";
  115. return FAILED;
  116. }
  117. virtual_input_node_ = std::make_shared<AnfNode>(nullptr);
  118. virtual_input_node_->set_scope(scope_);
  119. instance_name_base_ = GetInstanceNameByCNode(cnode_);
  120. name_idx_ = 0;
  121. return SUCCESS;
  122. }
  123. AnfNodePtr GenerateGraph::PushBack(const std::vector<AnfNodePtr> &inputs) {
  124. CNodePtr cnode = func_graph_->NewCNode(inputs); // using NewCNode to creat anfnode
  125. MS_EXCEPTION_IF_NULL(cnode);
  126. cnode->set_scope(scope_);
  127. if (inputs.size() < 2) {
  128. MS_LOG(EXCEPTION) << "inputs.size() must be more than 1";
  129. }
  130. (void)manager_->Replace(inputs.at(1), cnode); // using Replace function to insert cnode after inputs[0]
  131. auto new_anf_node_ptr = cnode->cast<AnfNodePtr>();
  132. MS_EXCEPTION_IF_NULL(new_anf_node_ptr);
  133. return new_anf_node_ptr;
  134. }
  135. AnfNodePtr GenerateGraph::NewOpInst(const OperatorName &op_name, const OperatorAttrs &attrs) {
  136. name_idx_++;
  137. ValuePtr pyop_instance = CreatOpInstance(attrs, op_name, instance_name_base_ + op_name + std::to_string(name_idx_));
  138. if (pyop_instance == nullptr) {
  139. MS_LOG(EXCEPTION) << "Failure:" << op_name << " CreatOpInstance failed";
  140. }
  141. auto value_node = NewValueNode(pyop_instance);
  142. return value_node->cast<AnfNodePtr>();
  143. }
  144. AnfNodePtr GenerateGraph::NewOpInst(const OperatorName &op_name) {
  145. name_idx_++;
  146. OperatorAttrs attrs;
  147. ValuePtr pyop_instance = CreatOpInstance(attrs, op_name, instance_name_base_ + std::to_string(name_idx_));
  148. if (pyop_instance == nullptr) {
  149. MS_LOG(EXCEPTION) << "Failure:" << op_name << " CreatOpInstance failed";
  150. }
  151. auto value_node = NewValueNode(pyop_instance);
  152. return value_node->cast<AnfNodePtr>();
  153. }
  154. } // namespace parallel
  155. } // namespace mindspore