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.

anf.cc 9.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /**
  2. * This is the C++ adaptation and derivative work of Myia (https://github.com/mila-iqia/myia/).
  3. *
  4. * Copyright 2019-2020 Huawei Technologies Co., Ltd
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. #include "ir/anf.h"
  19. #include <algorithm>
  20. #include <sstream>
  21. #include <vector>
  22. #include <unordered_map>
  23. #include "base/core_ops.h"
  24. #include "ir/func_graph.h"
  25. #include "ir/primitive.h"
  26. #include "utils/ms_context.h"
  27. namespace mindspore {
  28. // namespace to support intermediate representation definition
  29. CNode::CNode(const std::vector<AnfNodePtr> &inputs, const FuncGraphPtr &func_graph)
  30. : AnfNode(func_graph), inputs_(inputs), stop_gradient_(false), output_value_(std::make_pair(nullptr, "")) {}
  31. // Check if CNode is an apply with the specific Primitive.
  32. bool CNode::IsApply(const PrimitivePtr &value) const {
  33. if (value == nullptr) {
  34. return false;
  35. }
  36. if (inputs_.size() != 0 && IsValueNode<Primitive>(inputs_[0])) {
  37. PrimitivePtr fn_value = GetValueNode<PrimitivePtr>(inputs_[0]);
  38. if (fn_value->Hash() == value->Hash() && fn_value->name() == value->name()) {
  39. return true;
  40. }
  41. }
  42. return false;
  43. }
  44. void CNode::set_input(size_t i, const AnfNodePtr &new_input) { inputs_[i] = new_input; }
  45. std::string CNode::DebugString(int recursive_level) const {
  46. std::ostringstream buffer;
  47. if (recursive_level > 0) {
  48. if (func_graph() != nullptr) {
  49. buffer << func_graph()->ToString() << ":";
  50. }
  51. buffer << ToString() << "{";
  52. bool is_first_node = true;
  53. int idx = 0;
  54. for (auto &node : inputs_) {
  55. MS_EXCEPTION_IF_NULL(node);
  56. if (is_first_node) {
  57. is_first_node = false;
  58. } else {
  59. buffer << ", ";
  60. }
  61. buffer << "[" << idx << "]: " << node->DebugString(recursive_level - 1);
  62. idx++;
  63. }
  64. buffer << "}";
  65. } else {
  66. buffer << ToString();
  67. }
  68. return buffer.str();
  69. }
  70. std::string Parameter::DebugString(int recursive_level) const {
  71. std::ostringstream buffer;
  72. if (recursive_level > 0) {
  73. if (func_graph() != nullptr) {
  74. buffer << func_graph()->ToString() << ":";
  75. }
  76. }
  77. buffer << ToString();
  78. return buffer.str();
  79. }
  80. ParamInfoPtr Parameter::param_info() const {
  81. if (!has_default()) {
  82. return nullptr;
  83. }
  84. auto tensor = default_param()->cast<tensor::MetaTensorPtr>();
  85. if (tensor == nullptr || !tensor->is_parameter()) {
  86. return nullptr;
  87. }
  88. return tensor->param_info();
  89. }
  90. std::string ValueNode::ToString() const {
  91. MS_EXCEPTION_IF_NULL(value_);
  92. if (value_->isa<FuncGraph>()) {
  93. return value_->cast<FuncGraphPtr>()->ToString();
  94. }
  95. std::ostringstream buffer;
  96. buffer << AnfNode::ToString();
  97. buffer << "(" << value_->ToString() << ")";
  98. return buffer.str();
  99. }
  100. std::string ValueNode::DebugString(int) const {
  101. MS_EXCEPTION_IF_NULL(value_);
  102. std::ostringstream buffer;
  103. buffer << "ValueNode<" << value_->type_name() << "> " << value_->ToString();
  104. return buffer.str();
  105. }
  106. std::string ValueNode::fullname_with_scope() {
  107. if (!fullname_with_scope_.empty()) {
  108. return fullname_with_scope_;
  109. }
  110. MS_EXCEPTION_IF_NULL(scope());
  111. fullname_with_scope_ = scope()->name() + "/" + "data-" + id_generator::get_id(shared_from_base<ValueNode>());
  112. return fullname_with_scope_;
  113. }
  114. bool IsPrimitiveCNode(const AnfNodePtr &node, const PrimitivePtr &value) {
  115. MS_EXCEPTION_IF_NULL(node);
  116. auto cnode = node->cast<CNodePtr>();
  117. if (cnode == nullptr) {
  118. return false;
  119. }
  120. if (value != nullptr) {
  121. return cnode->IsApply(value);
  122. }
  123. const auto &prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  124. return prim != nullptr;
  125. }
  126. PrimitivePtr GetCNodePrimitive(const AnfNodePtr &node) {
  127. if (node == nullptr) {
  128. return nullptr;
  129. }
  130. auto cnode = node->cast<CNodePtr>();
  131. if (cnode != nullptr) {
  132. if (cnode->size() > 0) {
  133. auto prim = GetValueNode<PrimitivePtr>(cnode->input(0));
  134. return prim;
  135. }
  136. }
  137. return nullptr;
  138. }
  139. std::string GetCNodeFuncName(const CNodePtr cnode) {
  140. if (cnode->inputs().empty()) {
  141. return "";
  142. }
  143. AnfNodePtr valuenode = cnode->input(0);
  144. if (valuenode->isa<ValueNode>()) {
  145. auto value = GetValueNode(valuenode);
  146. // check whether the valuenode is primitive
  147. if (value->isa<Primitive>()) {
  148. return value->cast<PrimitivePtr>()->name();
  149. }
  150. return value->ToString();
  151. }
  152. return "";
  153. }
  154. bool IsPrimitive(const AnfNodePtr &node, const PrimitivePtr &value) {
  155. if (IsValueNode<Primitive>(node)) {
  156. PrimitivePtr fn_value = GetValueNode<PrimitivePtr>(node);
  157. MS_EXCEPTION_IF_NULL(value);
  158. if (fn_value->Hash() == value->Hash() && fn_value->name() == value->name()) {
  159. return true;
  160. }
  161. }
  162. return false;
  163. }
  164. size_t NewSeenGeneration() {
  165. static size_t seen_generation = 0;
  166. return ++seen_generation;
  167. }
  168. namespace id_generator {
  169. static std::unordered_map<std::string, int> node_ids;
  170. std::string get_id(const AnfNodePtr &node) {
  171. auto type_name = node->type_name();
  172. if (node_ids.find(type_name) == node_ids.end()) {
  173. node_ids[type_name] = 0;
  174. } else {
  175. node_ids[type_name]++;
  176. }
  177. return std::to_string(node_ids[type_name]);
  178. }
  179. void reset_id() { node_ids.clear(); }
  180. } // namespace id_generator
  181. namespace {
  182. std::string GetMaketupleNodeTarget(const CNodePtr &cnode) {
  183. MS_EXCEPTION_IF_NULL(cnode);
  184. auto func_graph = cnode->func_graph();
  185. MS_EXCEPTION_IF_NULL(func_graph);
  186. auto manager = func_graph->manager();
  187. MS_EXCEPTION_IF_NULL(manager);
  188. auto users = manager->node_users()[cnode];
  189. std::string first_user_target = GetCNodeTarget(users.back().first);
  190. bool is_used_by_different_target =
  191. std::any_of(std::begin(users), std::end(users), [&first_user_target](const std::pair<AnfNodePtr, int> &u) -> bool {
  192. return GetCNodeTarget(u.first) != first_user_target;
  193. });
  194. if (!is_used_by_different_target) {
  195. return first_user_target;
  196. }
  197. auto inputs = cnode->inputs();
  198. std::vector<AnfNodePtr> real_inputs;
  199. std::copy(inputs.begin() + 1, inputs.end(), std::back_inserter(real_inputs));
  200. std::string first_input_target = GetCNodeTarget(real_inputs[0]);
  201. bool is_from_different_target =
  202. std::any_of(std::begin(real_inputs), std::end(real_inputs),
  203. [&first_input_target](const AnfNodePtr &n) -> bool { return GetCNodeTarget(n) != first_input_target; });
  204. if (!is_from_different_target) {
  205. return first_input_target;
  206. }
  207. auto context_ptr = MsContext::GetInstance();
  208. MS_EXCEPTION_IF_NULL(context_ptr);
  209. std::string default_target = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  210. return default_target;
  211. }
  212. std::string GetTupleGetItemTarget(const CNodePtr &cnode, const PrimitivePtr &primitive) {
  213. MS_EXCEPTION_IF_NULL(cnode);
  214. MS_EXCEPTION_IF_NULL(primitive);
  215. auto input_target = GetCNodeTarget(cnode->input(1));
  216. primitive->set_attr("primitive_target", MakeValue(input_target));
  217. return input_target;
  218. }
  219. } // namespace
  220. std::string GetCNodeTarget(const AnfNodePtr &node) {
  221. auto context_ptr = MsContext::GetInstance();
  222. MS_EXCEPTION_IF_NULL(context_ptr);
  223. std::string default_target = context_ptr->get_param<std::string>(MS_CTX_DEVICE_TARGET);
  224. if (!node->isa<CNode>()) {
  225. return default_target;
  226. }
  227. auto cnode = node->cast<CNodePtr>();
  228. MS_EXCEPTION_IF_NULL(cnode);
  229. auto attr_input = cnode->input(0);
  230. if (attr_input == nullptr) {
  231. return default_target;
  232. }
  233. auto value_node = attr_input->cast<ValueNodePtr>();
  234. if (value_node == nullptr) {
  235. return default_target;
  236. }
  237. auto value = value_node->value();
  238. if (value == nullptr) {
  239. return default_target;
  240. }
  241. if (!value->isa<Primitive>()) {
  242. return default_target;
  243. }
  244. auto primitive = value->cast<PrimitivePtr>();
  245. auto att_target = primitive->GetAttr("primitive_target");
  246. if (att_target != nullptr) {
  247. if (IsPrimitive(attr_input, prim::kPrimImageSummary) || IsPrimitive(attr_input, prim::kPrimScalarSummary) ||
  248. IsPrimitive(attr_input, prim::kPrimTensorSummary) || IsPrimitive(attr_input, prim::kPrimHistogramSummary) ||
  249. IsPrimitive(attr_input, prim::kPrimStateSetItem) || IsPrimitive(attr_input, prim::kPrimDepend) ||
  250. IsPrimitive(attr_input, prim::kPrimControlDepend) || IsPrimitive(attr_input, prim::kPrimReturn) ||
  251. IsPrimitive(attr_input, prim::kPrimPartial)) {
  252. primitive->EraseAttr("primitive_target");
  253. return default_target;
  254. }
  255. if (!att_target->isa<StringImm>()) {
  256. MS_LOG(EXCEPTION) << "Only support string CPU|GPU|Ascend for primitive_target";
  257. }
  258. auto target = GetValue<std::string>(att_target);
  259. if (kTargetSet.find(target) == kTargetSet.end()) {
  260. MS_LOG(EXCEPTION) << "Only support string CPU|GPU|Ascend for primitive_target, but get " << target;
  261. }
  262. return target;
  263. }
  264. if (IsPrimitiveCNode(node, prim::kPrimMakeTuple)) {
  265. return GetMaketupleNodeTarget(cnode);
  266. }
  267. if (IsPrimitiveCNode(node, prim::kPrimTupleGetItem)) {
  268. return GetTupleGetItemTarget(cnode, primitive);
  269. }
  270. return default_target;
  271. }
  272. } // namespace mindspore