ArmNN
 20.02
TensorTest.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2017 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 #include <boost/test/unit_test.hpp>
6 #include <armnn/Tensor.hpp>
7 
8 namespace armnn
9 {
10 
11 // Adds unit test framework for interpreting TensorInfo type.
12 std::ostream& boost_test_print_type(std::ostream& ostr, const TensorInfo& right)
13 {
14  ostr << "TensorInfo[ "
15  << right.GetNumDimensions() << ","
16  << right.GetShape()[0] << ","
17  << right.GetShape()[1] << ","
18  << right.GetShape()[2] << ","
19  << right.GetShape()[3]
20  << " ]" << std::endl;
21  return ostr;
22 }
23 
24 std::ostream& boost_test_print_type(std::ostream& ostr, const TensorShape& shape)
25 {
26  ostr << "TensorShape[ "
27  << shape.GetNumDimensions() << ","
28  << shape[0] << ","
29  << shape[1] << ","
30  << shape[2] << ","
31  << shape[3]
32  << " ]" << std::endl;
33  return ostr;
34 }
35 
36 } //namespace armnn
37 using namespace armnn;
38 
40 
41 struct TensorInfoFixture
42 {
43  TensorInfoFixture()
44  {
45  unsigned int sizes[] = {6,7,8,9};
46  m_TensorInfo = TensorInfo(4, sizes, DataType::Float32);
47  }
48  ~TensorInfoFixture() {};
49 
50  TensorInfo m_TensorInfo;
51 };
52 
53 BOOST_FIXTURE_TEST_CASE(ConstructShapeUsingListInitialization, TensorInfoFixture)
54 {
55  TensorShape listInitializedShape{ 6, 7, 8, 9 };
56  BOOST_TEST(listInitializedShape == m_TensorInfo.GetShape());
57 }
58 
59 BOOST_FIXTURE_TEST_CASE(ConstructTensorInfo, TensorInfoFixture)
60 {
61  BOOST_TEST(m_TensorInfo.GetNumDimensions() == 4);
62  BOOST_TEST(m_TensorInfo.GetShape()[0] == 6); // <= Outer most
63  BOOST_TEST(m_TensorInfo.GetShape()[1] == 7);
64  BOOST_TEST(m_TensorInfo.GetShape()[2] == 8);
65  BOOST_TEST(m_TensorInfo.GetShape()[3] == 9); // <= Inner most
66 }
67 
68 BOOST_FIXTURE_TEST_CASE(CopyConstructTensorInfo, TensorInfoFixture)
69 {
70  TensorInfo copyConstructed(m_TensorInfo);
71  BOOST_TEST(copyConstructed.GetNumDimensions() == 4);
72  BOOST_TEST(copyConstructed.GetShape()[0] == 6);
73  BOOST_TEST(copyConstructed.GetShape()[1] == 7);
74  BOOST_TEST(copyConstructed.GetShape()[2] == 8);
75  BOOST_TEST(copyConstructed.GetShape()[3] == 9);
76 }
77 
78 BOOST_FIXTURE_TEST_CASE(TensorInfoEquality, TensorInfoFixture)
79 {
80  TensorInfo copyConstructed(m_TensorInfo);
81  BOOST_TEST(copyConstructed == m_TensorInfo);
82 }
83 
84 BOOST_FIXTURE_TEST_CASE(TensorInfoInequality, TensorInfoFixture)
85 {
86  TensorInfo other;
87  unsigned int sizes[] = {2,3,4,5};
88  other = TensorInfo(4, sizes, DataType::Float32);
89 
90  BOOST_TEST(other != m_TensorInfo);
91 }
92 
93 BOOST_FIXTURE_TEST_CASE(TensorInfoAssignmentOperator, TensorInfoFixture)
94 {
95  TensorInfo copy;
96  copy = m_TensorInfo;
97  BOOST_TEST(copy == m_TensorInfo);
98 }
99 
100 void CheckTensor(const ConstTensor& t)
101 {
102  t.GetInfo();
103 }
104 
105 BOOST_AUTO_TEST_CASE(TensorVsConstTensor)
106 {
107  int mutableDatum = 2;
108  const int immutableDatum = 3;
109 
110  armnn::Tensor uninitializedTensor;
111  armnn::ConstTensor uninitializedTensor2;
112 
113  uninitializedTensor2 = uninitializedTensor;
114 
115  armnn::Tensor t(TensorInfo(), &mutableDatum);
116  armnn::ConstTensor ct(TensorInfo(), &immutableDatum);
117 
118  // Checks that both Tensor and ConstTensor can be passed as a ConstTensor.
119  CheckTensor(t);
120  CheckTensor(ct);
121 }
122 
123 BOOST_AUTO_TEST_CASE(ModifyTensorInfo)
124 {
126  info.SetShape({ 5, 6, 7, 8 });
127  BOOST_TEST((info.GetShape() == TensorShape({ 5, 6, 7, 8 })));
129  BOOST_TEST((info.GetDataType() == DataType::QAsymmU8));
130  info.SetQuantizationScale(10.0f);
131  BOOST_TEST(info.GetQuantizationScale() == 10.0f);
132  info.SetQuantizationOffset(5);
133  BOOST_TEST(info.GetQuantizationOffset() == 5);
134 }
135 
136 BOOST_AUTO_TEST_CASE(TensorShapeOperatorBrackets)
137 {
138  TensorShape shape({0,1,2,3});
139  // Checks version of operator[] which returns an unsigned int.
140  BOOST_TEST(shape[2] == 2);
141  // Checks the version of operator[] which returns a reference.
142  shape[2] = 20;
143  BOOST_TEST(shape[2] == 20);
144 }
145 
146 BOOST_AUTO_TEST_CASE(TensorInfoPerAxisQuantization)
147 {
148  // Old constructor
149  TensorInfo tensorInfo0({ 1, 1 }, DataType::Float32, 2.0f, 1);
150  BOOST_CHECK(!tensorInfo0.HasMultipleQuantizationScales());
151  BOOST_CHECK(tensorInfo0.GetQuantizationScale() == 2.0f);
152  BOOST_CHECK(tensorInfo0.GetQuantizationOffset() == 1);
153  BOOST_CHECK(tensorInfo0.GetQuantizationScales()[0] == 2.0f);
154  BOOST_CHECK(!tensorInfo0.GetQuantizationDim().has_value());
155 
156  // Set per-axis quantization scales
157  std::vector<float> perAxisScales{ 3.0f, 4.0f };
158  tensorInfo0.SetQuantizationScales(perAxisScales);
159  BOOST_CHECK(tensorInfo0.HasMultipleQuantizationScales());
160  BOOST_CHECK(tensorInfo0.GetQuantizationScales() == perAxisScales);
161 
162  // Set per-tensor quantization scale
163  tensorInfo0.SetQuantizationScale(5.0f);
164  BOOST_CHECK(!tensorInfo0.HasMultipleQuantizationScales());
165  BOOST_CHECK(tensorInfo0.GetQuantizationScales()[0] == 5.0f);
166 
167  // Set quantization offset
168  tensorInfo0.SetQuantizationDim(Optional<unsigned int>(1));
169  BOOST_CHECK(tensorInfo0.GetQuantizationDim().value() == 1);
170 
171  // New constructor
172  perAxisScales = { 6.0f, 7.0f };
173  TensorInfo tensorInfo1({ 1, 1 }, DataType::Float32, perAxisScales, 1);
174  BOOST_CHECK(tensorInfo1.HasMultipleQuantizationScales());
175  BOOST_CHECK(tensorInfo1.GetQuantizationOffset() == 0);
176  BOOST_CHECK(tensorInfo1.GetQuantizationScales() == perAxisScales);
177  BOOST_CHECK(tensorInfo1.GetQuantizationDim().value() == 1);
178 }
179 
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
BOOST_FIXTURE_TEST_CASE(ConstructShapeUsingListInitialization, TensorInfoFixture)
Definition: TensorTest.cpp:53
Copyright (c) 2020 ARM Limited.
BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::WaitingForAck)
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:90
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:191
void CheckTensor(const ConstTensor &t)
Definition: TensorTest.cpp:100
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:264
float GetQuantizationScale() const
Definition: Tensor.cpp:247
DataType GetDataType() const
Definition: Tensor.hpp:95
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:259
BOOST_AUTO_TEST_CASE(CheckConvolution2dLayer)
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:167
void SetDataType(DataType type)
Definition: Tensor.hpp:96
std::ostream & boost_test_print_type(std::ostream &ostr, const TensorInfo &right)
Definition: TensorTest.cpp:12
BOOST_AUTO_TEST_SUITE_END()
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:43
void SetQuantizationOffset(int32_t offset)
Definition: Tensor.cpp:275
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:92