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

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