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.

data_test.cc 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /**
  2. * Copyright 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 <iostream>
  17. #include <memory>
  18. #include "common/common_test.h"
  19. #include "common/py_func_graph_fetcher.h"
  20. #include "pipeline/jit/static_analysis/prim.h"
  21. #include "frontend/operator/ops.h"
  22. #include "abstract/utils.h"
  23. namespace mindspore {
  24. namespace abstract {
  25. class TestData : public UT::Common {
  26. public:
  27. void SetUp();
  28. void TearDown();
  29. };
  30. void TestData::SetUp() { UT::InitPythonPath(); }
  31. void TestData::TearDown() {
  32. // destroy resource
  33. }
  34. TEST_F(TestData, test_build_value) {
  35. // assert build_value(S(1)) == 1
  36. AbstractScalar s1 = AbstractScalar(1);
  37. ASSERT_EQ(1, s1.BuildValue()->cast<Int32ImmPtr>()->value());
  38. // assert build_value(S(t=ty.Int[64]), default=ANYTHING) is ANYTHING
  39. s1 = AbstractScalar(kAnyValue, kInt32);
  40. ASSERT_TRUE(s1.BuildValue()->isa<AnyValue>());
  41. ASSERT_TRUE(s1.BuildValue()->isa<AnyValue>());
  42. // assert build_value(T([S(1), S(2)])) == (1, 2)
  43. AbstractBasePtr base1 = std::make_shared<AbstractScalar>(1);
  44. AbstractBasePtr base2 = std::make_shared<AbstractScalar>(2);
  45. AbstractBasePtrList base_list = {base1, base2};
  46. AbstractTuple t1 = AbstractTuple(base_list);
  47. std::vector<ValuePtr> value_list = {MakeValue(1), MakeValue(2)};
  48. auto tup = t1.BuildValue()->cast<ValueTuplePtr>()->value();
  49. ASSERT_TRUE(tup.size() == value_list.size());
  50. for (int i = 0; i < value_list.size(); i++) {
  51. ASSERT_EQ(*tup[i], *value_list[i]);
  52. }
  53. // BuildValue(AbstractFunction) should return kAnyValue.
  54. AbstractBasePtr abs_f1 = FromValue(prim::kPrimReturn, false);
  55. ValuePtr abs_f1_built = abs_f1->BuildValue();
  56. ASSERT_EQ(abs_f1_built, kAnyValue);
  57. FuncGraphPtr fg1 = std::make_shared<FuncGraph>();
  58. AbstractBasePtr abs_fg1 = FromValue(fg1, false);
  59. ValuePtr abs_fg1_built = abs_fg1->BuildValue();
  60. ASSERT_EQ(abs_fg1_built, kAnyValue);
  61. // BuildValue(Tuple(AbstractFunction)) should return kAnyValue;
  62. AbstractBasePtr abs_f2 = FromValue(prim::kPrimScalarAdd, false);
  63. AbstractBasePtr abs_func_tuple = std::make_shared<AbstractTuple>(AbstractBasePtrList({abs_f1, abs_f2}));
  64. ValuePtr func_tuple_built = abs_func_tuple->BuildValue();
  65. ASSERT_EQ(func_tuple_built, kAnyValue);
  66. // BuildValue(List(AbstractFunction)) should return kAnyValue;
  67. AbstractBasePtr abs_func_list = std::make_shared<AbstractList>(AbstractBasePtrList({abs_f1, abs_f2}));
  68. ValuePtr func_list_built = abs_func_list->BuildValue();
  69. ASSERT_EQ(func_list_built, kAnyValue);
  70. // BuildValue(Tuple(AnyAbstractBase, AbstractFunction)) should return kAnyValue
  71. abs_func_tuple = std::make_shared<AbstractTuple>(AbstractBasePtrList({base1, abs_f2}));
  72. func_tuple_built = abs_func_tuple->BuildValue();
  73. ASSERT_EQ(func_tuple_built, kAnyValue);
  74. }
  75. TEST_F(TestData, test_build_type) {
  76. AbstractBasePtr s1 = FromValue(1, false);
  77. AbstractBasePtr s2 = FromValue(2, false);
  78. ASSERT_TRUE(Int(32) == *s1->BuildType());
  79. AbstractFunctionPtr f1 = std::make_shared<PrimitiveAbstractClosure>(nullptr, nullptr);
  80. ASSERT_TRUE(Function() == *f1->BuildType());
  81. AbstractList l1 = AbstractList({s1, s2});
  82. ASSERT_TRUE(List({std::make_shared<Int>(32), std::make_shared<Int>(32)}) == *l1.BuildType());
  83. }
  84. TEST_F(TestData, test_build_shape) {
  85. AbstractBasePtr s1 = FromValue(1, false);
  86. AbstractBasePtr s2 = FromValue(2, false);
  87. ASSERT_TRUE(NoShape() == *s1->BuildShape());
  88. AbstractFunctionPtr f1 = std::make_shared<PrimitiveAbstractClosure>(nullptr, nullptr);
  89. ASSERT_TRUE(NoShape() == *f1->BuildShape());
  90. AbstractList l1 = AbstractList({s1, s2});
  91. auto lshape = l1.BuildShape();
  92. ASSERT_TRUE(lshape);
  93. std::vector<int> weight1_dims = {2, 20, 5, 5};
  94. std::vector<int> weight2_dims = {2, 2, 5, 5};
  95. tensor::TensorPtr weight1 = std::make_shared<tensor::Tensor>(kNumberTypeInt32, weight1_dims);
  96. tensor::TensorPtr weight2 = std::make_shared<tensor::Tensor>(kNumberTypeInt32, weight2_dims);
  97. AbstractBasePtr abstract_weight1 = FromValue(weight1, true);
  98. AbstractBasePtr abstract_weight2 = FromValue(weight2, true);
  99. ShapePtr shape_weight = dyn_cast<Shape>(abstract_weight1->BuildShape());
  100. ASSERT_TRUE(shape_weight);
  101. ASSERT_EQ(weight1_dims, shape_weight->shape());
  102. std::vector<ValuePtr> vec({weight1, weight2});
  103. AbstractBasePtr abstract_tup = FromValue(vec, true);
  104. std::shared_ptr<TupleShape> shape_tuple = dyn_cast<TupleShape>(abstract_tup->BuildShape());
  105. ASSERT_TRUE(shape_tuple);
  106. const std::vector<BaseShapePtr>& ptr_vec = shape_tuple->shape();
  107. ASSERT_EQ(ptr_vec.size(), 2);
  108. ShapePtr shape1 = dyn_cast<Shape>(ptr_vec[0]);
  109. ASSERT_TRUE(shape1);
  110. ASSERT_EQ(weight1_dims, shape1->shape());
  111. ShapePtr shape2 = dyn_cast<Shape>(ptr_vec[1]);
  112. ASSERT_TRUE(shape2);
  113. ASSERT_EQ(weight2_dims, shape2->shape());
  114. }
  115. TEST_F(TestData, test_clone) {
  116. AbstractBasePtr s1 = FromValue(1, false);
  117. AbstractBasePtr s2 = s1->Clone();
  118. ASSERT_TRUE(*s1->GetTypeTrack() == *s2->GetTypeTrack());
  119. ASSERT_TRUE(s1->GetValueTrack() == s2->GetValueTrack());
  120. ASSERT_TRUE(*s1->GetShapeTrack() == *s2->GetShapeTrack());
  121. AbstractFunctionPtr f1 = std::make_shared<FuncGraphAbstractClosure>(std::make_shared<FuncGraph>(),
  122. AnalysisContext::DummyContext());
  123. AbstractBasePtr f2 = f1->Clone();
  124. ASSERT_TRUE(*f2 == *f1);
  125. AbstractList l1 = AbstractList({s1, s2});
  126. AbstractBasePtr l2 = l1.Clone();
  127. AbstractList* l2_cast = dynamic_cast<AbstractList*>(l2.get());
  128. ASSERT_TRUE(l2_cast != nullptr);
  129. ASSERT_TRUE(l2_cast->GetValueTrack() == l1.GetValueTrack());
  130. std::vector<AbstractAttribute> attr = {{"x", std::make_shared<AbstractScalar>(kAnyValue, kInt64)},
  131. {"y", std::make_shared<AbstractScalar>(kAnyValue, kInt64)}};
  132. std::unordered_map<std::string, ValuePtr> methods;
  133. AbstractBasePtr c1 = std::make_shared<AbstractClass>(Named("Point"), attr, methods);
  134. AbstractBasePtr c2 = c1->Clone();
  135. ASSERT_EQ(*c1, *c2);
  136. }
  137. TEST_F(TestData, test_join) {
  138. int int1 = 1;
  139. AbstractBasePtr s1 = FromValue(int1, false);
  140. AbstractBasePtr s2 = s1->Broaden();
  141. std::vector<AbstractBasePtr> xx = {s1, s2};
  142. AbstractListPtr l1 = std::make_shared<AbstractList>(xx);
  143. AbstractListPtr l2 = std::make_shared<AbstractList>(xx);
  144. l1->Join(l2);
  145. }
  146. TEST_F(TestData, test_broaden) {
  147. int int1 = 1;
  148. AbstractBasePtr s1 = FromValue(int1, false);
  149. AbstractBasePtr s2 = s1->Broaden();
  150. ASSERT_TRUE(*s1->GetTypeTrack() == *s2->GetTypeTrack());
  151. ASSERT_TRUE(*s1->GetValueTrack() == *MakeValue(int1));
  152. ASSERT_TRUE(s2->GetValueTrack()->isa<AnyValue>());
  153. AbstractFunctionPtr f1 = std::make_shared<FuncGraphAbstractClosure>(std::make_shared<FuncGraph>(),
  154. AnalysisContext::DummyContext());
  155. AbstractBasePtr f2 = f1->Broaden();
  156. ASSERT_TRUE(f2 == f1);
  157. AbstractList l1 = AbstractList({s1, s2});
  158. AbstractBasePtr l2 = l1.Broaden();
  159. AbstractList* l2_cast = dynamic_cast<AbstractList*>(l2.get());
  160. ASSERT_TRUE(l2_cast != nullptr);
  161. AbstractBasePtr csr = AbstractJoin(l2_cast->elements());
  162. ASSERT_TRUE(csr->GetValueTrack()->isa<AnyValue>());
  163. }
  164. } // namespace abstract
  165. } // namespace mindspore