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

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