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

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