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.

convert_utils.cc 12 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. /**
  2. * Copyright 2019-2022 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 "include/common/utils/convert_utils.h"
  17. #include <vector>
  18. #include <string>
  19. #include <memory>
  20. #include <algorithm>
  21. #include <utility>
  22. #include <cfloat>
  23. #include "ir/value.h"
  24. #include "ir/tensor.h"
  25. #include "ir/param_info.h"
  26. #include "utils/ms_context.h"
  27. #include "utils/anf_utils.h"
  28. namespace mindspore {
  29. bool ValueToBool(const ValuePtr &v, bool *value) {
  30. MS_EXCEPTION_IF_NULL(v);
  31. if (v->isa<BoolImm>()) {
  32. *value = v->cast<BoolImmPtr>()->value();
  33. } else if (v->isa<Int32Imm>()) {
  34. *value = v->cast<Int32ImmPtr>()->value() != 0;
  35. } else if (v->isa<UInt32Imm>()) {
  36. *value = v->cast<UInt32ImmPtr>()->value() != 0;
  37. } else if (v->isa<FP32Imm>()) {
  38. *value = v->cast<FP32ImmPtr>()->value() != 0;
  39. } else if (v->isa<FP64Imm>()) {
  40. *value = v->cast<FP64ImmPtr>()->value() != 0;
  41. } else if (v->isa<tensor::Tensor>()) {
  42. auto tensor = v->cast<tensor::TensorPtr>();
  43. MS_EXCEPTION_IF_NULL(tensor);
  44. tensor->data_sync();
  45. bool *tensor_data = static_cast<bool *>(tensor->data_c());
  46. // maybe need to support if tensor is a bool array
  47. auto vb = tensor_data[0];
  48. *value = vb;
  49. } else {
  50. MS_LOG(WARNING) << "value is not supported to cast to be bool";
  51. return false;
  52. }
  53. return true;
  54. }
  55. bool BaseRefToInt(const ValuePtr &v, int64_t *value) {
  56. MS_EXCEPTION_IF_NULL(v);
  57. if (v->isa<tensor::Tensor>()) {
  58. auto tensor = v->cast<tensor::TensorPtr>();
  59. tensor->data_sync();
  60. if (tensor->Dtype()->ToString() == "Int32") {
  61. auto *tensor_data = static_cast<int32_t *>(tensor->data_c());
  62. auto vb = tensor_data[0];
  63. *value = static_cast<int64_t>(vb);
  64. } else if (tensor->Dtype()->ToString() == "Int64") {
  65. auto *tensor_data = static_cast<int64_t *>(tensor->data_c());
  66. auto vb = tensor_data[0];
  67. *value = vb;
  68. } else {
  69. MS_LOG(ERROR) << "Index must be Int type.";
  70. }
  71. return true;
  72. }
  73. MS_LOG(ERROR) << "Index must be tensor type.";
  74. return false;
  75. }
  76. bool BaseRefToBool(const BaseRef &v, bool *value) {
  77. if (utils::isa<ValuePtr>(v)) {
  78. return ValueToBool(utils::cast<ValuePtr>(v), value);
  79. } else if (utils::isa<bool>(v)) {
  80. auto vb = utils::cast<bool>(v);
  81. *value = vb;
  82. } else if (utils::isa<int>(v)) {
  83. auto vb = utils::cast<int>(v);
  84. *value = vb != 0;
  85. } else if (utils::isa<unsigned int>(v)) {
  86. auto vb = utils::cast<unsigned int>(v);
  87. *value = vb != 0;
  88. } else if (utils::isa<float>(v)) {
  89. auto vb = utils::cast<float>(v);
  90. *value = !(vb >= -FLT_EPSILON && vb <= FLT_EPSILON);
  91. } else if (utils::isa<double>(v)) {
  92. auto vb = utils::cast<double>(v);
  93. *value = !(vb >= -DBL_EPSILON && vb <= DBL_EPSILON);
  94. } else {
  95. MS_LOG(DEBUG) << "value is not supported to cast to be bool";
  96. return false;
  97. }
  98. return true;
  99. }
  100. namespace {
  101. // Isomorphism
  102. bool SameNode(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
  103. NodeMapEquiv *const equiv_node);
  104. bool SameNodeShallow(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
  105. NodeMapEquiv *const equiv_node) {
  106. if (equiv_node == nullptr) {
  107. MS_LOG(ERROR) << "Invalid equiv_node";
  108. return false;
  109. }
  110. if (equiv_node->count(node1) > 0 && (*equiv_node)[node1] == node2) {
  111. return true;
  112. }
  113. if (IsValueNode<FuncGraph>(node1) && IsValueNode<FuncGraph>(node2)) {
  114. return Isomorphic(GetValueNode<FuncGraphPtr>(node1), GetValueNode<FuncGraphPtr>(node2), equiv_func_graph,
  115. equiv_node);
  116. }
  117. if (node1->isa<ValueNode>() && node2->isa<ValueNode>()) {
  118. auto a1 = GetValueNode(node1);
  119. auto a2 = GetValueNode(node2);
  120. if (a1->isa<Primitive>() && a2->isa<Primitive>()) {
  121. return a1->cast<PrimitivePtr>()->name() == a2->cast<PrimitivePtr>()->name();
  122. } else if (a1->isa<tensor::Tensor>() && a2->isa<tensor::Tensor>()) {
  123. return a1->cast<tensor::TensorPtr>()->ValueEqual(*(a2->cast<tensor::TensorPtr>()));
  124. } else {
  125. return *a1 == *a2;
  126. }
  127. }
  128. if (node1->isa<Parameter>() && node2->isa<Parameter>()) {
  129. auto para1 = node1->cast<ParameterPtr>();
  130. auto para2 = node2->cast<ParameterPtr>();
  131. if (para1->name() == para2->name()) {
  132. return true;
  133. }
  134. MS_LOG(DEBUG) << "two parameters are not equal.";
  135. return false;
  136. }
  137. if (AnfUtils::IsCustomActorNode(node1) && AnfUtils::IsCustomActorNode(node2)) {
  138. return AnfUtils::IsCutomActorNodeSame(node1, node2);
  139. }
  140. if (node1->isa<CNode>() && node2->isa<CNode>()) {
  141. return SameNode(node1, node2, equiv_func_graph, equiv_node);
  142. }
  143. MS_LOG(ERROR) << "type error";
  144. return false;
  145. }
  146. bool SameNode(const AnfNodePtr &node1, const AnfNodePtr &node2, FuncGraphPairMapEquiv *equiv_func_graph,
  147. NodeMapEquiv *const equiv_node) {
  148. MS_EXCEPTION_IF_NULL(node1);
  149. MS_EXCEPTION_IF_NULL(node2);
  150. if (node1->isa<CNode>() && node2->isa<CNode>()) {
  151. auto &inputs1 = node1->cast<CNodePtr>()->inputs();
  152. auto &inputs2 = node2->cast<CNodePtr>()->inputs();
  153. for (std::size_t i = 0; i < inputs1.size(); ++i) {
  154. if (!SameNodeShallow(inputs1[i], inputs2[i], equiv_func_graph, equiv_node)) {
  155. return false;
  156. }
  157. }
  158. return true;
  159. }
  160. return SameNodeShallow(node1, node2, equiv_func_graph, equiv_node);
  161. }
  162. bool SameSubgraph(const AnfNodePtr &root1, const AnfNodePtr &root2, FuncGraphPairMapEquiv *equiv_func_graph,
  163. NodeMapEquiv *const equiv_node) {
  164. mindspore::HashSet<AnfNodePtr> done;
  165. std::stack<std::pair<AnfNodePtr, AnfNodePtr>> todo;
  166. todo.push(std::make_pair(root1, root2));
  167. while (!todo.empty()) {
  168. AnfNodePtr node1 = todo.top().first;
  169. if (done.count(node1) > 0) {
  170. todo.pop();
  171. continue;
  172. }
  173. AnfNodePtr node2 = todo.top().second;
  174. bool condition = false;
  175. const auto &s1 = GetInputs(node1);
  176. const auto &s2 = GetInputs(node2);
  177. if (s1.size() != s2.size()) {
  178. return false;
  179. }
  180. for (std::size_t i = 0; i < s1.size(); ++i) {
  181. if (done.count(s1[i]) == 0) {
  182. todo.push(std::make_pair(s1[i], s2[i]));
  183. condition = true;
  184. }
  185. }
  186. if (condition) {
  187. continue;
  188. }
  189. (void)done.insert(node1);
  190. auto res = SameNode(node1, node2, equiv_func_graph, equiv_node);
  191. if (res) {
  192. (*equiv_node)[node1] = node2;
  193. } else {
  194. return false;
  195. }
  196. todo.pop();
  197. }
  198. return true;
  199. }
  200. } // namespace
  201. bool Isomorphic(const FuncGraphPtr &fg1, const FuncGraphPtr &fg2, FuncGraphPairMapEquiv *equiv_func_graph,
  202. NodeMapEquiv *const equiv_node) {
  203. auto fg1_fg2 = std::make_pair(fg1, fg2);
  204. if (equiv_func_graph == nullptr) {
  205. MS_LOG(ERROR) << "equiv_func_graph not init";
  206. return false;
  207. }
  208. if (equiv_func_graph->find(fg1_fg2) != equiv_func_graph->end()) {
  209. return (*equiv_func_graph)[fg1_fg2] != kNotEquiv;
  210. }
  211. if (fg1 == nullptr || fg2 == nullptr) {
  212. MS_LOG(ERROR) << "Invalid function graph";
  213. return false;
  214. }
  215. if (fg1->parameters().size() != fg2->parameters().size()) {
  216. MS_LOG(DEBUG) << "parameters size not match";
  217. return false;
  218. }
  219. if (equiv_node != nullptr) {
  220. for (std::size_t i = 0; i < fg1->parameters().size(); ++i) {
  221. (*equiv_node)[fg1->parameters()[i]] = fg2->parameters()[i];
  222. }
  223. (*equiv_func_graph)[fg1_fg2] = kPending;
  224. auto result = SameSubgraph(fg1->get_return(), fg2->get_return(), equiv_func_graph, equiv_node);
  225. (*equiv_func_graph)[fg1_fg2] = EquivState(result);
  226. return result;
  227. }
  228. MS_LOG(ERROR) << "equiv_node not init";
  229. return false;
  230. }
  231. tensor::TensorPtr ScalarToTensor(const ScalarPtr &scalar) {
  232. if (scalar == nullptr) {
  233. MS_EXCEPTION(ArgumentError) << "Nullptr Error!";
  234. }
  235. TypePtr data_type = scalar->type();
  236. MS_EXCEPTION_IF_NULL(data_type);
  237. TypeId type_id = data_type->type_id();
  238. switch (type_id) {
  239. case kNumberTypeBool:
  240. return std::make_shared<tensor::Tensor>(GetValue<bool>(scalar), data_type);
  241. case kNumberTypeInt8:
  242. return std::make_shared<tensor::Tensor>(static_cast<int64_t>(GetValue<int8_t>(scalar)), data_type);
  243. case kNumberTypeInt16:
  244. return std::make_shared<tensor::Tensor>(static_cast<int64_t>(GetValue<int16_t>(scalar)), data_type);
  245. case kNumberTypeInt32:
  246. return std::make_shared<tensor::Tensor>(static_cast<int64_t>(GetValue<int32_t>(scalar)), data_type);
  247. case kNumberTypeInt64:
  248. return std::make_shared<tensor::Tensor>(GetValue<int64_t>(scalar), data_type);
  249. case kNumberTypeUInt8:
  250. return std::make_shared<tensor::Tensor>(static_cast<uint64_t>(GetValue<uint8_t>(scalar)), data_type);
  251. case kNumberTypeUInt16:
  252. return std::make_shared<tensor::Tensor>(static_cast<uint64_t>(GetValue<uint16_t>(scalar)), data_type);
  253. case kNumberTypeUInt32:
  254. return std::make_shared<tensor::Tensor>(static_cast<uint64_t>(GetValue<uint32_t>(scalar)), data_type);
  255. case kNumberTypeUInt64:
  256. return std::make_shared<tensor::Tensor>(GetValue<uint64_t>(scalar), data_type);
  257. case kNumberTypeFloat32:
  258. return std::make_shared<tensor::Tensor>(GetValue<float>(scalar), data_type);
  259. case kNumberTypeFloat64:
  260. return std::make_shared<tensor::Tensor>(GetValue<double>(scalar), data_type);
  261. default:
  262. MS_LOG(EXCEPTION) << "When convert scalar to tensor, the scalar type: " << data_type << "is valid.";
  263. }
  264. }
  265. void TensorValueToTensor(const ValuePtr &value, std::vector<tensor::TensorPtr> *tensors) {
  266. MS_EXCEPTION_IF_NULL(value);
  267. MS_EXCEPTION_IF_NULL(tensors);
  268. if (value->isa<ValueTuple>()) {
  269. auto value_tuple = value->cast<ValueTuplePtr>();
  270. MS_EXCEPTION_IF_NULL(value_tuple);
  271. for (size_t i = 0; i < value_tuple->size(); ++i) {
  272. ValuePtr element = value_tuple->value()[i];
  273. if (element->isa<tensor::Tensor>()) {
  274. auto tensor = element->cast<tensor::TensorPtr>();
  275. MS_EXCEPTION_IF_NULL(tensor);
  276. tensors->emplace_back(tensor);
  277. } else if (element->isa<ValueTuple>()) {
  278. TensorValueToTensor(element, tensors);
  279. }
  280. }
  281. } else if (value->isa<tensor::Tensor>()) {
  282. auto tensor = value->cast<tensor::TensorPtr>();
  283. MS_EXCEPTION_IF_NULL(tensor);
  284. tensors->emplace_back(tensor);
  285. }
  286. }
  287. size_t CountValueNum(const ValueTuplePtr &value_tuple) {
  288. MS_EXCEPTION_IF_NULL(value_tuple);
  289. size_t cnt = 0;
  290. const auto &value_list = value_tuple->value();
  291. for (const auto &value : value_list) {
  292. if (value->isa<None>()) {
  293. continue;
  294. } else if (value->isa<ValueTuple>()) {
  295. cnt += CountValueNum(value->cast<ValueTuplePtr>());
  296. } else {
  297. cnt++;
  298. }
  299. }
  300. return cnt;
  301. }
  302. bool IsCustomCSROP(const AnfNodePtr &cnode) {
  303. MS_EXCEPTION_IF_NULL(cnode);
  304. const PrimitiveSet prims{prim::kPrimCSRReduceSum, prim::kPrimCSRMul, prim::kPrimCSRMV, prim::kPrimCSRGather,
  305. prim::kPrimCSR2COO, prim::kPrimCOO2CSR, prim::kPrimCSRDiv};
  306. return IsOneOfPrimitiveCNode(cnode, prims);
  307. }
  308. } // namespace mindspore