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.

mindapi_test.cc 18 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. /**
  2. * Copyright 2021-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 <cmath>
  17. #include <memory>
  18. #include <sstream>
  19. #include <unordered_map>
  20. #include "common/common_test.h"
  21. #include "mindapi/base/logging.h"
  22. #include "mindapi/ir/func_graph.h"
  23. #include "mindapi/ir/primitive.h"
  24. #include "mindapi/ir/tensor.h"
  25. #include "mindapi/ir/utils.h"
  26. namespace mindspore::api {
  27. class TestMindApi : public UT::Common {
  28. public:
  29. TestMindApi() = default;
  30. };
  31. /// Feature: MindAPI
  32. /// Description: test basic 'is()' 'cast()'
  33. /// Expectation: is/cast works correctly.
  34. TEST_F(TestMindApi, test_base_isa_cast) {
  35. auto value_node = MakeShared<ValueNode>(MakeValue(0));
  36. auto base = MakeShared<Base>(value_node->impl());
  37. ASSERT_TRUE(base->isa<Base>());
  38. ASSERT_TRUE(base->isa<AnfNode>());
  39. ASSERT_TRUE(base->isa<ValueNode>());
  40. ASSERT_FALSE(base->isa<AbstractBase>());
  41. auto anf_node = base->cast<AnfNodePtr>();
  42. ASSERT_TRUE(anf_node != nullptr);
  43. ASSERT_TRUE(anf_node->impl() == value_node->impl());
  44. ASSERT_TRUE(base->cast<AbstractBasePtr>() == nullptr);
  45. }
  46. /// Feature: MindAPI
  47. /// Description: test graph construction.
  48. /// Expectation: graph is constructed as expected.
  49. TEST_F(TestMindApi, test_graph_construction) {
  50. // fg(x) { return myprim(x, 1); }
  51. auto fg = FuncGraph::Create();
  52. auto x = fg->add_parameter();
  53. x->set_name("x");
  54. auto prim = MakeShared<Primitive>("myprim");
  55. auto prim_node = MakeShared<ValueNode>(prim);
  56. auto value_node = MakeShared<ValueNode>(MakeValue(1));
  57. auto cnode = fg->NewCNode({prim_node, x, value_node});
  58. fg->set_output(cnode);
  59. // Now we check the graph.
  60. ASSERT_EQ(fg->parameters().size(), 1);
  61. ASSERT_TRUE(fg->parameters()[0]->isa<Parameter>());
  62. ASSERT_EQ(fg->parameters()[0]->cast<ParameterPtr>()->name(), "x");
  63. auto ret_node = fg->get_return();
  64. ASSERT_TRUE(ret_node != nullptr);
  65. auto output_node = fg->output();
  66. ASSERT_TRUE(output_node != nullptr);
  67. ASSERT_TRUE(output_node->isa<CNode>());
  68. auto output_cnode = output_node->cast<CNodePtr>();
  69. ASSERT_EQ(output_cnode->inputs().size(), 3);
  70. ASSERT_TRUE(output_cnode->input(0)->isa<ValueNode>());
  71. ASSERT_TRUE(output_cnode->input(0)->cast<ValueNodePtr>()->value()->isa<Primitive>());
  72. ASSERT_EQ(output_cnode->input(0)->cast<ValueNodePtr>()->value()->cast<PrimitivePtr>()->name(), "myprim");
  73. ASSERT_TRUE(output_cnode->input(1)->isa<Parameter>());
  74. ASSERT_EQ(output_cnode->input(1)->cast<ParameterPtr>()->name(), "x");
  75. ASSERT_TRUE(output_cnode->input(2)->isa<ValueNode>());
  76. ASSERT_EQ(output_cnode->impl(), cnode->impl());
  77. }
  78. /// Feature: MindAPI
  79. /// Description: test value related functions.
  80. /// Expectation: value related functions work as expected.
  81. TEST_F(TestMindApi, test_values) {
  82. int64_t one = 1;
  83. auto s = MakeValue("hello");
  84. auto i = MakeValue(one);
  85. auto i2 = MakeValue(2);
  86. auto b = MakeValue(true);
  87. auto f = MakeValue(3.14f);
  88. auto seq = MakeValue(std::vector<int64_t>{3, 4, 5});
  89. auto seq_str = MakeValue(std::vector<std::string>({"this", "is", "mindspore", "api"}));
  90. ASSERT_TRUE(s->isa<StringImm>());
  91. ASSERT_TRUE(i->isa<Int64Imm>());
  92. ASSERT_TRUE(i2->isa<Int64Imm>());
  93. ASSERT_TRUE(b->isa<BoolImm>());
  94. ASSERT_TRUE(f->isa<FP32Imm>());
  95. ASSERT_TRUE(seq->isa<ValueSequence>());
  96. ASSERT_TRUE(seq_str->isa<ValueSequence>());
  97. ASSERT_EQ(GetValue<std::string>(s), "hello");
  98. ASSERT_EQ(GetValue<int64_t>(i), one);
  99. ASSERT_EQ(GetValue<int64_t>(i2), 2);
  100. ASSERT_TRUE(GetValue<bool>(b));
  101. ASSERT_TRUE(std::abs(GetValue<float>(f) - 3.14f) < 0.00001f);
  102. ASSERT_EQ(GetValue<std::string>(i), "");
  103. ASSERT_EQ(GetValue<int64_t>(s), 0);
  104. ASSERT_FALSE(GetValue<bool>(s));
  105. ASSERT_EQ(GetValue<float>(s), 0.0f);
  106. auto seq_ptr = seq->cast<ValueSequencePtr>();
  107. ASSERT_TRUE(seq_ptr != nullptr);
  108. ASSERT_EQ(seq_ptr->size(), 3);
  109. ASSERT_EQ(seq_ptr->value().size(), 3);
  110. ASSERT_TRUE(seq_ptr->value()[0]->isa<Int64Imm>());
  111. ASSERT_EQ(GetValue<int64_t>(seq_ptr->value()[0]), 3);
  112. ASSERT_EQ(GetValue<int64_t>(seq_ptr->value()[1]), 4);
  113. ASSERT_EQ(GetValue<int64_t>(seq_ptr->value()[2]), 5);
  114. auto seq_values = GetValue<std::vector<int64_t>>(seq);
  115. ASSERT_EQ(seq_values.size(), 3);
  116. ASSERT_EQ(seq_values[0], 3);
  117. ASSERT_EQ(seq_values[1], 4);
  118. ASSERT_EQ(seq_values[2], 5);
  119. auto str_values = GetValue<std::vector<std::string>>(seq_str);
  120. ASSERT_EQ(str_values.size(), 4);
  121. ASSERT_EQ(str_values[0], "this");
  122. ASSERT_EQ(str_values[1], "is");
  123. ASSERT_EQ(str_values[2], "mindspore");
  124. ASSERT_EQ(str_values[3], "api");
  125. auto value_list = GetValue<ValuePtrList>(seq);
  126. ASSERT_EQ(value_list.size(), 3);
  127. ASSERT_EQ(utils::cast<int64_t>(value_list[0]), 3);
  128. ASSERT_EQ(utils::cast<int64_t>(value_list[1]), 4);
  129. ASSERT_EQ(utils::cast<int64_t>(value_list[2]), 5);
  130. std::vector<uint8_t> vec_uint8{5, 6, 7};
  131. auto uint8_seq = MakeValue<std::vector<uint8_t>>(vec_uint8);
  132. ASSERT_TRUE(uint8_seq->isa<ValueSequence>());
  133. auto uint8_values = GetValue<std::vector<uint8_t>>(uint8_seq);
  134. ASSERT_EQ(uint8_values.size(), 3);
  135. ASSERT_EQ(uint8_values[0], 5);
  136. ASSERT_EQ(uint8_values[1], 6);
  137. ASSERT_EQ(uint8_values[2], 7);
  138. auto seq_bool = MakeValue(std::vector<bool>{true, false, true});
  139. auto seq_bool_ptr = seq_bool->cast<ValueSequencePtr>();
  140. ASSERT_TRUE(seq_bool_ptr != nullptr);
  141. ASSERT_EQ(seq_bool_ptr->size(), 3);
  142. ASSERT_EQ(seq_bool_ptr->value().size(), 3);
  143. ASSERT_TRUE(seq_bool_ptr->value()[0]->isa<BoolImm>());
  144. ASSERT_TRUE(GetValue<bool>(seq_bool_ptr->value()[0]));
  145. ASSERT_FALSE(GetValue<bool>(seq_bool_ptr->value()[1]));
  146. ASSERT_TRUE(GetValue<bool>(seq_bool_ptr->value()[2]));
  147. auto prim = MakeShared<Primitive>("myprim");
  148. auto node = NewValueNode(prim);
  149. ASSERT_TRUE(node != nullptr);
  150. ASSERT_EQ(node->value(), prim);
  151. }
  152. /// Feature: MindAPI
  153. /// Description: test graph manager functions.
  154. /// Expectation: graph manager functions work as expected.
  155. TEST_F(TestMindApi, test_func_graph_manager) {
  156. // fg(x, y) { return myprim(add(x, y), 1); }
  157. auto fg = FuncGraph::Create();
  158. auto x = fg->add_parameter();
  159. x->set_name("x");
  160. auto y = fg->add_parameter();
  161. y->set_name("y");
  162. auto add = MakeShared<Primitive>("add");
  163. auto add_node = MakeShared<ValueNode>(add);
  164. auto add_cnode = fg->NewCNode({add_node, x, y});
  165. auto prim = MakeShared<Primitive>("myprim");
  166. auto prim_node = MakeShared<ValueNode>(prim);
  167. auto value_node = MakeShared<ValueNode>(MakeValue(1));
  168. auto cnode = fg->NewCNode({prim_node, add_cnode, value_node});
  169. fg->set_output(cnode);
  170. auto mgr = FuncGraphManager::Manage(fg);
  171. ASSERT_TRUE(mgr != nullptr);
  172. ASSERT_TRUE(fg->manager() != nullptr);
  173. ASSERT_EQ(fg->manager()->impl(), mgr->impl());
  174. ASSERT_EQ(fg->manager(), mgr);
  175. ASSERT_EQ(cnode->input(1)->impl(), add_cnode->impl());
  176. mgr->Replace(add_cnode, x);
  177. ASSERT_EQ(cnode->input(1)->impl(), x->impl());
  178. mgr->SetEdge(cnode, 1, y);
  179. ASSERT_EQ(cnode->input(1)->impl(), y->impl());
  180. mgr->AddEdge(cnode, x);
  181. ASSERT_EQ(cnode->size(), 4);
  182. ASSERT_EQ(cnode->input(3)->impl(), x->impl());
  183. auto users = mgr->GetUsers(value_node);
  184. ASSERT_EQ(users.size(), 1);
  185. ASSERT_EQ(users[0].first, cnode);
  186. ASSERT_EQ(users[0].second, 2);
  187. }
  188. /// Feature: MindAPI
  189. /// Description: test value node utils.
  190. /// Expectation: value node utils work as expected.
  191. TEST_F(TestMindApi, test_value_node_utils) {
  192. auto fg = FuncGraph::Create();
  193. auto fg_node = MakeShared<ValueNode>(fg);
  194. auto prim = MakeShared<Primitive>("myprim");
  195. auto prim_node = MakeShared<ValueNode>(prim);
  196. auto one = MakeShared<ValueNode>(MakeValue(1));
  197. auto cnode = fg->NewCNode({fg_node, prim_node, one});
  198. ASSERT_TRUE(GetValueNode<FuncGraphPtr>(cnode) == nullptr);
  199. auto fg1 = GetValueNode<FuncGraphPtr>(cnode->input(0));
  200. ASSERT_TRUE(fg1 != nullptr);
  201. ASSERT_TRUE(fg1->isa<FuncGraph>());
  202. auto prim1 = GetValueNode<PrimitivePtr>(cnode->input(1));
  203. ASSERT_TRUE(prim1 != nullptr);
  204. ASSERT_TRUE(prim1->isa<Primitive>());
  205. auto imm = GetValueNode<Int64ImmPtr>(cnode->input(2));
  206. ASSERT_TRUE(imm != nullptr);
  207. ASSERT_TRUE(imm->isa<Int64Imm>());
  208. ASSERT_EQ(imm->cast<Int64ImmPtr>()->value(), 1);
  209. auto value = GetValueNode(cnode->input(2));
  210. ASSERT_TRUE(value != nullptr);
  211. ASSERT_EQ(GetValue<int64_t>(value), 1);
  212. ASSERT_TRUE(GetValueNode<PrimitivePtr>(cnode->input(0)) == nullptr);
  213. ASSERT_TRUE(GetValueNode<FuncGraphPtr>(cnode->input(1)) == nullptr);
  214. ASSERT_TRUE(GetValueNode<StringImmPtr>(cnode->input(2)) == nullptr);
  215. // Test NewValueNode.
  216. auto int_node = NewValueNode(1);
  217. auto bool_node = NewValueNode(true);
  218. auto float_node = NewValueNode(1.23f);
  219. auto str_node = NewValueNode("hello");
  220. ASSERT_TRUE(int_node->value()->isa<Int64Imm>());
  221. ASSERT_EQ(int_node->value()->cast<Int64ImmPtr>()->value(), 1);
  222. ASSERT_TRUE(bool_node->value()->isa<BoolImm>());
  223. ASSERT_TRUE(bool_node->value()->cast<BoolImmPtr>()->value());
  224. ASSERT_TRUE(float_node->value()->isa<FP32Imm>());
  225. ASSERT_TRUE(std::abs(float_node->value()->cast<FP32ImmPtr>()->value() - 1.23f) < 0.0000001f);
  226. ASSERT_TRUE(str_node->value()->isa<StringImm>());
  227. ASSERT_EQ(str_node->value()->cast<StringImmPtr>()->value(), "hello");
  228. }
  229. /// Feature: MindAPI
  230. /// Description: test SharedPtr.
  231. /// Expectation: SharedPtr work as expected.
  232. TEST_F(TestMindApi, test_object_ptr) {
  233. auto fg = FuncGraph::Create();
  234. auto fg_node = MakeShared<ValueNode>(fg);
  235. auto prim = MakeShared<Primitive>("myprim");
  236. auto prim_node = MakeShared<ValueNode>(prim);
  237. auto one = MakeShared<ValueNode>(MakeValue(1));
  238. auto cnode = fg->NewCNode({fg_node, prim_node, one});
  239. ASSERT_TRUE(fg != nullptr);
  240. ASSERT_FALSE(!fg);
  241. ASSERT_TRUE(fg ? true : false);
  242. ASSERT_TRUE((*cnode).input(0) == fg_node);
  243. ASSERT_TRUE(cnode->input(0) == fg_node);
  244. ASSERT_TRUE(cnode.get()->input(0) == fg_node);
  245. ASSERT_EQ(cnode->input(0), fg_node);
  246. ASSERT_EQ(cnode->input(1), prim_node);
  247. ASSERT_EQ(cnode->input(2), one);
  248. ASSERT_TRUE(cnode->input(0) != fg);
  249. AnfNodePtr p = fg_node;
  250. ASSERT_TRUE(p == fg_node);
  251. ASSERT_TRUE(p->isa<ValueNode>());
  252. ASSERT_TRUE(p->cast<ValueNodePtr>() != nullptr);
  253. ASSERT_TRUE(p->cast<ValueNodePtr>() == fg_node);
  254. p = cnode;
  255. ASSERT_TRUE(p == cnode);
  256. ASSERT_TRUE(p->isa<CNode>());
  257. ASSERT_TRUE(p->cast<CNodePtr>() != nullptr);
  258. ASSERT_TRUE(p->cast<CNodePtr>() == cnode);
  259. ASSERT_TRUE(p.get() == cnode.get());
  260. ASSERT_TRUE(p != nullptr);
  261. ASSERT_FALSE(p == nullptr);
  262. ASSERT_TRUE(p > nullptr);
  263. ASSERT_FALSE(p < nullptr);
  264. ASSERT_TRUE(p >= nullptr);
  265. ASSERT_FALSE(p <= nullptr);
  266. ASSERT_TRUE(nullptr != p);
  267. ASSERT_FALSE(nullptr == p);
  268. ASSERT_TRUE(nullptr < p);
  269. ASSERT_FALSE(nullptr > p);
  270. ASSERT_TRUE(nullptr <= p);
  271. ASSERT_FALSE(nullptr >= p);
  272. AnfNodePtr q = fg_node;
  273. ASSERT_TRUE(p != q);
  274. if (p.get()->impl() > q.get()->impl()) {
  275. ASSERT_TRUE(p > q);
  276. ASSERT_TRUE(p >= q);
  277. ASSERT_TRUE(q < p);
  278. ASSERT_TRUE(q <= p);
  279. } else {
  280. ASSERT_TRUE(p < q);
  281. ASSERT_TRUE(p <= q);
  282. ASSERT_TRUE(q > p);
  283. ASSERT_TRUE(q >= p);
  284. }
  285. std::stringstream ss1;
  286. std::stringstream ss2;
  287. ss1 << p;
  288. ss2 << cnode.get()->impl().get();
  289. ASSERT_EQ(ss1.str(), ss2.str());
  290. std::unordered_map<AnfNodePtr, AnfNodePtr> mymap;
  291. mymap.emplace(p, q);
  292. mymap.emplace(q, p);
  293. ASSERT_TRUE(mymap.find(p) != mymap.end());
  294. ASSERT_TRUE(mymap.find(q) != mymap.end());
  295. ASSERT_TRUE(mymap[p] == q);
  296. ASSERT_TRUE(mymap[q] == p);
  297. }
  298. /// Feature: MindAPI
  299. /// Description: test Tensor API.
  300. /// Expectation: Tensor API work as expected.
  301. TEST_F(TestMindApi, test_tensor_api) {
  302. ShapeVector shape{1, 2, 3};
  303. auto tensor = MakeShared<Tensor>(kNumberTypeFloat32, shape);
  304. ASSERT_EQ(tensor->data_type(), kNumberTypeFloat32);
  305. ASSERT_EQ(tensor->shape(), shape);
  306. ASSERT_EQ(tensor->DataSize(), 6);
  307. ASSERT_EQ(tensor->Size(), 24);
  308. ShapeVector shape2{2, 3};
  309. tensor->set_data_type(kNumberTypeInt32);
  310. tensor->set_shape(shape2);
  311. ASSERT_EQ(tensor->data_type(), kNumberTypeInt32);
  312. ASSERT_EQ(tensor->shape(), shape2);
  313. // TensorType.
  314. TypePtr tensor_type = MakeShared<TensorType>(Type::GetType(TypeId::kNumberTypeFloat32));
  315. ASSERT_TRUE(tensor_type->isa<TensorType>());
  316. ASSERT_EQ(tensor_type->cast<TensorTypePtr>()->element()->type_id(), kNumberTypeFloat32);
  317. }
  318. /// Feature: MindAPI
  319. /// Description: test Tensor with dynamic shape.
  320. /// Expectation: Tensor API work as expected.
  321. TEST_F(TestMindApi, test_tensor_with_dyn_shape) {
  322. ShapeVector shape{1, 2, -1, -2};
  323. auto tensor = MakeShared<Tensor>(kNumberTypeFloat32, shape);
  324. ASSERT_EQ(tensor->data_type(), kNumberTypeFloat32);
  325. ASSERT_EQ(tensor->shape(), shape);
  326. ASSERT_EQ(tensor->DataSize(), 0);
  327. ASSERT_EQ(tensor->Size(), 0);
  328. ShapeVector shape2{2, 3};
  329. tensor->set_data_type(kNumberTypeInt32);
  330. tensor->set_shape(shape2);
  331. ASSERT_EQ(tensor->data_type(), kNumberTypeInt32);
  332. ASSERT_EQ(tensor->shape(), shape2);
  333. ShapeVector shape3{1, -1, 3};
  334. auto tensor2 = MakeShared<Tensor>(kNumberTypeFloat32, shape);
  335. ASSERT_EQ(tensor2->data_type(), kNumberTypeFloat32);
  336. ASSERT_EQ(tensor2->shape(), shape);
  337. ASSERT_EQ(tensor2->DataSize(), 0);
  338. ASSERT_EQ(tensor2->Size(), 0);
  339. ShapeVector shape4{3, 4};
  340. tensor2->set_data_type(kNumberTypeInt32);
  341. tensor2->set_shape(shape4);
  342. ASSERT_EQ(tensor2->data_type(), kNumberTypeInt32);
  343. ASSERT_EQ(tensor2->shape(), shape4);
  344. }
  345. /// Feature: MindAPI
  346. /// Description: test utils API.
  347. /// Expectation: Tensor API work as expected.
  348. TEST_F(TestMindApi, test_api_utils) {
  349. // Test utils::isa, utils::cast.
  350. auto anf_node = NewValueNode("hello");
  351. ASSERT_TRUE(utils::isa<AnfNode>(anf_node));
  352. ASSERT_TRUE(utils::isa<AnfNodePtr>(anf_node));
  353. ASSERT_FALSE(utils::isa<AbstractBase>(anf_node));
  354. ASSERT_TRUE(utils::cast<AnfNodePtr>(anf_node) != nullptr);
  355. ASSERT_TRUE(utils::cast<AbstractBasePtr>(anf_node) == nullptr);
  356. ASSERT_TRUE(utils::isa<std::string>(anf_node->value()));
  357. ASSERT_EQ(utils::cast<std::string>(anf_node->value()), "hello");
  358. auto int_value = MakeValue(123);
  359. ASSERT_TRUE(utils::isa<int64_t>(int_value));
  360. ASSERT_EQ(utils::cast<int64_t>(int_value), 123);
  361. anf_node = nullptr;
  362. ASSERT_FALSE(utils::isa<AnfNode>(anf_node));
  363. ASSERT_FALSE(utils::isa<AnfNodePtr>(anf_node));
  364. ASSERT_TRUE(utils::cast<AnfNodePtr>(anf_node) == nullptr);
  365. // Test clone graph.
  366. auto fg = FuncGraph::Create();
  367. auto x = fg->add_parameter();
  368. x->set_name("x");
  369. auto y = fg->add_parameter();
  370. y->set_name("y");
  371. auto add = MakeShared<Primitive>("add");
  372. auto add_node = MakeShared<ValueNode>(add);
  373. auto add_cnode = fg->NewCNode({add_node, x, y});
  374. auto prim = MakeShared<Primitive>("myprim");
  375. auto prim_node = MakeShared<ValueNode>(prim);
  376. auto value_node = MakeShared<ValueNode>(MakeValue(1));
  377. auto cnode = fg->NewCNode({prim_node, add_cnode, value_node});
  378. fg->set_output(cnode);
  379. auto cloned_fg = utils::CloneGraph(fg);
  380. ASSERT_TRUE(cloned_fg != nullptr);
  381. ASSERT_EQ(cloned_fg->parameters().size(), 2);
  382. auto new_output = cloned_fg->output();
  383. ASSERT_TRUE(new_output != nullptr);
  384. ASSERT_TRUE(new_output->isa<CNode>());
  385. ASSERT_EQ(new_output->cast<CNodePtr>()->size(), cnode->size());
  386. ASSERT_TRUE(new_output != cnode);
  387. ASSERT_TRUE(new_output->cast<CNodePtr>() != cnode);
  388. // Test get pad mode.
  389. auto pm_lower = MakeValue("pad");
  390. auto pm_upper = MakeValue("PAD");
  391. ASSERT_EQ(utils::GetPadMode(pm_lower), 0);
  392. ASSERT_EQ(utils::GetPadMode(pm_lower, false), 0);
  393. ASSERT_EQ(utils::GetPadMode(pm_upper, true), 0);
  394. }
  395. /// Feature: MindAPI
  396. /// Description: test logging API.
  397. /// Expectation: logging work as expected.
  398. TEST_F(TestMindApi, test_api_logging) {
  399. std::string name = "mindspore";
  400. MS_LOG(DEBUG) << "hello debug";
  401. MS_LOG(INFO) << "hello info";
  402. MS_LOG(WARNING) << "hello warning";
  403. MS_LOG(ERROR) << "hello error";
  404. MS_LOG(ERROR) << name;
  405. MS_LOG(ERROR) << "hello " << name;
  406. MS_LOG(ERROR) << name << " hello";
  407. try {
  408. MS_LOG(EXCEPTION) << "hello exception";
  409. ASSERT_TRUE(false);
  410. } catch (...) {
  411. }
  412. ASSERT_TRUE(true);
  413. }
  414. /// Feature: MindAPI
  415. /// Description: test AbstractSequence API.
  416. /// Expectation: AbstractSequence work as expected.
  417. TEST_F(TestMindApi, test_abstract_sequence) {
  418. AbstractBasePtrList abs_list;
  419. abs_list.emplace_back(MakeShared<AbstractScalar>(int64_t(1)));
  420. abs_list.emplace_back(MakeShared<AbstractScalar>(float(1.2f)));
  421. abs_list.emplace_back(MakeShared<AbstractScalar>(true));
  422. abs_list.emplace_back(MakeShared<AbstractScalar>(std::string("hello")));
  423. ShapeVector shape{1, 2, 3};
  424. abs_list.emplace_back(MakeShared<AbstractTensor>(TypeId::kNumberTypeFloat32, shape));
  425. auto abs_tuple = MakeShared<AbstractTuple>(abs_list);
  426. ASSERT_EQ(abs_tuple->elements().size(), abs_list.size());
  427. ASSERT_EQ(GetValue<int64_t>(abs_tuple->elements()[0]->value()), 1);
  428. ASSERT_TRUE(abs_tuple->elements()[1]->value()->isa<FP32Imm>());
  429. ASSERT_TRUE(GetValue<bool>(abs_tuple->elements()[2]->value()));
  430. ASSERT_EQ(GetValue<std::string>(abs_tuple->elements()[3]->value()), "hello");
  431. ASSERT_TRUE(abs_tuple->elements()[4]->isa<AbstractTensor>());
  432. ASSERT_EQ(abs_tuple->elements()[4]->type()->type_id(), TypeId::kObjectTypeTensorType);
  433. ASSERT_EQ(abs_tuple->elements()[4]->shape()->shape(), shape);
  434. ASSERT_EQ(abs_tuple->elements()[4]->cast<AbstractTensorPtr>()->element()->type()->type_id(),
  435. TypeId::kNumberTypeFloat32);
  436. ShapeVector shape2{2, 3, 4};
  437. abs_tuple->elements()[4]->set_shape(MakeShared<Shape>(shape2));
  438. ASSERT_EQ(abs_tuple->elements()[4]->shape()->shape(), shape2);
  439. }
  440. } // namespace mindspore::api