ArmNN
 20.08
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>
8 
9 
10 namespace armnn
11 {
12 
13 // Adds unit test framework for interpreting TensorInfo type.
14 std::ostream& boost_test_print_type(std::ostream& ostr, const TensorInfo& right)
15 {
16  ostr << "TensorInfo[ "
17  << right.GetNumDimensions() << ","
18  << right.GetShape()[0] << ","
19  << right.GetShape()[1] << ","
20  << right.GetShape()[2] << ","
21  << right.GetShape()[3]
22  << " ]" << std::endl;
23  return ostr;
24 }
25 
26 std::ostream& boost_test_print_type(std::ostream& ostr, const TensorShape& shape)
27 {
28  ostr << "TensorShape[ "
29  << shape.GetNumDimensions() << ","
30  << shape[0] << ","
31  << shape[1] << ","
32  << shape[2] << ","
33  << shape[3]
34  << " ]" << std::endl;
35  return ostr;
36 }
37 
38 } //namespace armnn
39 using namespace armnn;
40 
42 
43 struct TensorInfoFixture
44 {
45  TensorInfoFixture()
46  {
47  unsigned int sizes[] = {6,7,8,9};
48  m_TensorInfo = TensorInfo(4, sizes, DataType::Float32);
49  }
50  ~TensorInfoFixture() {};
51 
52  TensorInfo m_TensorInfo;
53 };
54 
55 BOOST_FIXTURE_TEST_CASE(ConstructShapeUsingListInitialization, TensorInfoFixture)
56 {
57  TensorShape listInitializedShape{ 6, 7, 8, 9 };
58  BOOST_TEST(listInitializedShape == m_TensorInfo.GetShape());
59 }
60 
61 BOOST_FIXTURE_TEST_CASE(ConstructTensorInfo, TensorInfoFixture)
62 {
63  BOOST_TEST(m_TensorInfo.GetNumDimensions() == 4);
64  BOOST_TEST(m_TensorInfo.GetShape()[0] == 6); // <= Outer most
65  BOOST_TEST(m_TensorInfo.GetShape()[1] == 7);
66  BOOST_TEST(m_TensorInfo.GetShape()[2] == 8);
67  BOOST_TEST(m_TensorInfo.GetShape()[3] == 9); // <= Inner most
68 }
69 
70 BOOST_FIXTURE_TEST_CASE(CopyConstructTensorInfo, TensorInfoFixture)
71 {
72  TensorInfo copyConstructed(m_TensorInfo);
73  BOOST_TEST(copyConstructed.GetNumDimensions() == 4);
74  BOOST_TEST(copyConstructed.GetShape()[0] == 6);
75  BOOST_TEST(copyConstructed.GetShape()[1] == 7);
76  BOOST_TEST(copyConstructed.GetShape()[2] == 8);
77  BOOST_TEST(copyConstructed.GetShape()[3] == 9);
78 }
79 
80 BOOST_FIXTURE_TEST_CASE(TensorInfoEquality, TensorInfoFixture)
81 {
82  TensorInfo copyConstructed(m_TensorInfo);
83  BOOST_TEST(copyConstructed == m_TensorInfo);
84 }
85 
86 BOOST_FIXTURE_TEST_CASE(TensorInfoInequality, TensorInfoFixture)
87 {
88  TensorInfo other;
89  unsigned int sizes[] = {2,3,4,5};
90  other = TensorInfo(4, sizes, DataType::Float32);
91 
92  BOOST_TEST(other != m_TensorInfo);
93 }
94 
95 BOOST_FIXTURE_TEST_CASE(TensorInfoAssignmentOperator, TensorInfoFixture)
96 {
97  TensorInfo copy;
98  copy = m_TensorInfo;
99  BOOST_TEST(copy == m_TensorInfo);
100 }
101 
102 void CheckTensor(const ConstTensor& t)
103 {
104  t.GetInfo();
105 }
106 
107 BOOST_AUTO_TEST_CASE(TensorVsConstTensor)
108 {
109  int mutableDatum = 2;
110  const int immutableDatum = 3;
111 
112  armnn::Tensor uninitializedTensor;
113  armnn::ConstTensor uninitializedTensor2;
114 
115  uninitializedTensor2 = uninitializedTensor;
116 
117  armnn::Tensor t(TensorInfo(), &mutableDatum);
118  armnn::ConstTensor ct(TensorInfo(), &immutableDatum);
119 
120  // Checks that both Tensor and ConstTensor can be passed as a ConstTensor.
121  CheckTensor(t);
122  CheckTensor(ct);
123 }
124 
125 BOOST_AUTO_TEST_CASE(ModifyTensorInfo)
126 {
128  info.SetShape({ 5, 6, 7, 8 });
129  BOOST_TEST((info.GetShape() == TensorShape({ 5, 6, 7, 8 })));
131  BOOST_TEST((info.GetDataType() == DataType::QAsymmU8));
132  info.SetQuantizationScale(10.0f);
133  BOOST_TEST(info.GetQuantizationScale() == 10.0f);
134  info.SetQuantizationOffset(5);
135  BOOST_TEST(info.GetQuantizationOffset() == 5);
136 }
137 
138 BOOST_AUTO_TEST_CASE(TensorShapeOperatorBrackets)
139 {
140  const TensorShape constShape({0,1,2,3});
141  TensorShape shape({0,1,2,3});
142 
143  // Checks version of operator[] which returns an unsigned int.
144  BOOST_TEST(shape[2] == 2);
145  shape[2] = 20;
146  BOOST_TEST(shape[2] == 20);
147 
148  // Checks the version of operator[] which returns a reference.
149  BOOST_TEST(constShape[2] == 2);
150 }
151 
152 BOOST_AUTO_TEST_CASE(TensorInfoPerAxisQuantization)
153 {
154  // Old constructor
155  TensorInfo tensorInfo0({ 1, 1 }, DataType::Float32, 2.0f, 1);
156  BOOST_CHECK(!tensorInfo0.HasMultipleQuantizationScales());
157  BOOST_CHECK(tensorInfo0.GetQuantizationScale() == 2.0f);
158  BOOST_CHECK(tensorInfo0.GetQuantizationOffset() == 1);
159  BOOST_CHECK(tensorInfo0.GetQuantizationScales()[0] == 2.0f);
160  BOOST_CHECK(!tensorInfo0.GetQuantizationDim().has_value());
161 
162  // Set per-axis quantization scales
163  std::vector<float> perAxisScales{ 3.0f, 4.0f };
164  tensorInfo0.SetQuantizationScales(perAxisScales);
165  BOOST_CHECK(tensorInfo0.HasMultipleQuantizationScales());
166  BOOST_CHECK(tensorInfo0.GetQuantizationScales() == perAxisScales);
167 
168  // Set per-tensor quantization scale
169  tensorInfo0.SetQuantizationScale(5.0f);
170  BOOST_CHECK(!tensorInfo0.HasMultipleQuantizationScales());
171  BOOST_CHECK(tensorInfo0.GetQuantizationScales()[0] == 5.0f);
172 
173  // Set quantization offset
174  tensorInfo0.SetQuantizationDim(Optional<unsigned int>(1));
175  BOOST_CHECK(tensorInfo0.GetQuantizationDim().value() == 1);
176 
177  // New constructor
178  perAxisScales = { 6.0f, 7.0f };
179  TensorInfo tensorInfo1({ 1, 1 }, DataType::Float32, perAxisScales, 1);
180  BOOST_CHECK(tensorInfo1.HasMultipleQuantizationScales());
181  BOOST_CHECK(tensorInfo1.GetQuantizationOffset() == 0);
182  BOOST_CHECK(tensorInfo1.GetQuantizationScales() == perAxisScales);
183  BOOST_CHECK(tensorInfo1.GetQuantizationDim().value() == 1);
184 }
185 
186 BOOST_AUTO_TEST_CASE(TensorShape_scalar)
187 {
188  float mutableDatum = 3.1416f;
189 
192  const armnn::Tensor tensor ( info, &mutableDatum );
193 
194  BOOST_CHECK(armnn::Dimensionality::Scalar == shape.GetDimensionality());
195  float scalarValue = *reinterpret_cast<float*>(tensor.GetMemoryArea());
196  BOOST_CHECK_MESSAGE(mutableDatum == scalarValue, "Scalar value is " << scalarValue);
197 
198  armnn::TensorShape shape_equal;
199  armnn::TensorShape shape_different;
200  shape_equal = shape;
201  BOOST_TEST(shape_equal == shape);
202  BOOST_TEST(shape_different != shape);
203  BOOST_CHECK_MESSAGE(1 == shape.GetNumElements(), "Number of elements is " << shape.GetNumElements());
204  BOOST_CHECK_MESSAGE(1 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
205  BOOST_CHECK(true == shape.GetDimensionSpecificity(0));
206  BOOST_CHECK(shape.AreAllDimensionsSpecified());
207  BOOST_CHECK(shape.IsAtLeastOneDimensionSpecified());
208 
209  BOOST_TEST(1 == shape[0]);
210  BOOST_TEST(1 == tensor.GetShape()[0]);
211  BOOST_TEST(1 == tensor.GetInfo().GetShape()[0]);
212  BOOST_CHECK_THROW( shape[1], InvalidArgumentException );
213 
214  float newMutableDatum = 42.f;
215  std::memcpy(tensor.GetMemoryArea(), &newMutableDatum, sizeof(float));
216  scalarValue = *reinterpret_cast<float*>(tensor.GetMemoryArea());
217  BOOST_CHECK_MESSAGE(newMutableDatum == scalarValue, "Scalar value is " << scalarValue);
218 }
219 
220 BOOST_AUTO_TEST_CASE(TensorShape_DynamicTensorType1_unknownNumberDimensions)
221 {
222  float mutableDatum = 3.1416f;
223 
226  armnn::Tensor tensor ( info, &mutableDatum );
227 
229  BOOST_CHECK_THROW( shape[0], InvalidArgumentException );
230  BOOST_CHECK_THROW( shape.GetNumElements(), InvalidArgumentException );
231  BOOST_CHECK_THROW( shape.GetNumDimensions(), InvalidArgumentException );
232 
233  armnn::TensorShape shape_equal;
234  armnn::TensorShape shape_different;
235  shape_equal = shape;
236  BOOST_TEST(shape_equal == shape);
237  BOOST_TEST(shape_different != shape);
238 }
239 
240 BOOST_AUTO_TEST_CASE(TensorShape_DynamicTensorType1_unknownAllDimensionsSizes)
241 {
242  float mutableDatum = 3.1416f;
243 
244  armnn::TensorShape shape ( 3, false );
246  armnn::Tensor tensor ( info, &mutableDatum );
247 
248  BOOST_CHECK(armnn::Dimensionality::Specified == shape.GetDimensionality());
249  BOOST_CHECK_MESSAGE(0 == shape.GetNumElements(), "Number of elements is " << shape.GetNumElements());
250  BOOST_CHECK_MESSAGE(3 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
251  BOOST_CHECK(false == shape.GetDimensionSpecificity(0));
252  BOOST_CHECK(false == shape.GetDimensionSpecificity(1));
253  BOOST_CHECK(false == shape.GetDimensionSpecificity(2));
254  BOOST_CHECK(!shape.AreAllDimensionsSpecified());
255  BOOST_CHECK(!shape.IsAtLeastOneDimensionSpecified());
256 
257  armnn::TensorShape shape_equal;
258  armnn::TensorShape shape_different;
259  shape_equal = shape;
260  BOOST_TEST(shape_equal == shape);
261  BOOST_TEST(shape_different != shape);
262 }
263 
264 BOOST_AUTO_TEST_CASE(TensorShape_DynamicTensorType1_unknownSomeDimensionsSizes)
265 {
266  std::vector<float> mutableDatum { 42.f, 42.f, 42.f,
267  0.0f, 0.1f, 0.2f };
268 
269  armnn::TensorShape shape ( {2, 0, 3}, {true, false, true} );
271  armnn::Tensor tensor ( info, &mutableDatum );
272 
273  BOOST_CHECK(armnn::Dimensionality::Specified == shape.GetDimensionality());
274  BOOST_CHECK_MESSAGE(6 == shape.GetNumElements(), "Number of elements is " << shape.GetNumElements());
275  BOOST_CHECK_MESSAGE(3 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
276  BOOST_CHECK(true == shape.GetDimensionSpecificity(0));
277  BOOST_CHECK(false == shape.GetDimensionSpecificity(1));
278  BOOST_CHECK(true == shape.GetDimensionSpecificity(2));
279  BOOST_CHECK(!shape.AreAllDimensionsSpecified());
280  BOOST_CHECK(shape.IsAtLeastOneDimensionSpecified());
281 
282  BOOST_CHECK_THROW(shape[1], InvalidArgumentException);
283  BOOST_CHECK_THROW(tensor.GetShape()[1], InvalidArgumentException);
284  BOOST_CHECK_THROW(tensor.GetInfo().GetShape()[1], InvalidArgumentException);
285 
286  BOOST_TEST(2 == shape[0]);
287  BOOST_TEST(2 == tensor.GetShape()[0]);
288  BOOST_TEST(2 == tensor.GetInfo().GetShape()[0]);
289  BOOST_CHECK_THROW( shape[1], InvalidArgumentException );
290 
291  BOOST_TEST(3 == shape[2]);
292  BOOST_TEST(3 == tensor.GetShape()[2]);
293  BOOST_TEST(3 == tensor.GetInfo().GetShape()[2]);
294 
295  armnn::TensorShape shape_equal;
296  armnn::TensorShape shape_different;
297  shape_equal = shape;
298  BOOST_TEST(shape_equal == shape);
299  BOOST_TEST(shape_different != shape);
300 }
301 
302 BOOST_AUTO_TEST_CASE(TensorShape_DynamicTensorType1_transitionFromUnknownToKnownDimensionsSizes)
303 {
304  std::vector<float> mutableDatum { 42.f, 42.f, 42.f,
305  0.0f, 0.1f, 0.2f };
306 
309  armnn::Tensor tensor ( info, &mutableDatum );
310 
311  // Specify the number of dimensions
312  shape.SetNumDimensions(3);
313  BOOST_CHECK(armnn::Dimensionality::Specified == shape.GetDimensionality());
314  BOOST_CHECK_MESSAGE(3 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
315  BOOST_CHECK(false == shape.GetDimensionSpecificity(0));
316  BOOST_CHECK(false == shape.GetDimensionSpecificity(1));
317  BOOST_CHECK(false == shape.GetDimensionSpecificity(2));
318  BOOST_CHECK(!shape.AreAllDimensionsSpecified());
319  BOOST_CHECK(!shape.IsAtLeastOneDimensionSpecified());
320 
321  // Specify dimension 0 and 2.
322  shape.SetDimensionSize(0, 2);
323  shape.SetDimensionSize(2, 3);
324  BOOST_CHECK_MESSAGE(3 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
325  BOOST_CHECK_MESSAGE(6 == shape.GetNumElements(), "Number of elements is " << shape.GetNumElements());
326  BOOST_CHECK(true == shape.GetDimensionSpecificity(0));
327  BOOST_CHECK(false == shape.GetDimensionSpecificity(1));
328  BOOST_CHECK(true == shape.GetDimensionSpecificity(2));
329  BOOST_CHECK(!shape.AreAllDimensionsSpecified());
330  BOOST_CHECK(shape.IsAtLeastOneDimensionSpecified());
331 
332  info.SetShape(shape);
333  armnn::Tensor tensor2( info, &mutableDatum );
334  BOOST_TEST(2 == shape[0]);
335  BOOST_TEST(2 == tensor2.GetShape()[0]);
336  BOOST_TEST(2 == tensor2.GetInfo().GetShape()[0]);
337 
338  BOOST_CHECK_THROW(shape[1], InvalidArgumentException);
339  BOOST_CHECK_THROW(tensor.GetShape()[1], InvalidArgumentException);
340  BOOST_CHECK_THROW(tensor.GetInfo().GetShape()[1], InvalidArgumentException);
341 
342  BOOST_TEST(3 == shape[2]);
343  BOOST_TEST(3 == tensor2.GetShape()[2]);
344  BOOST_TEST(3 == tensor2.GetInfo().GetShape()[2]);
345 
346  armnn::TensorShape shape_equal;
347  armnn::TensorShape shape_different;
348  shape_equal = shape;
349  BOOST_TEST(shape_equal == shape);
350  BOOST_TEST(shape_different != shape);
351 
352  // Specify dimension 1.
353  shape.SetDimensionSize(1, 5);
354  BOOST_CHECK_MESSAGE(3 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
355  BOOST_CHECK_MESSAGE(30 == shape.GetNumElements(), "Number of elements is " << shape.GetNumElements());
356  BOOST_CHECK(true == shape.GetDimensionSpecificity(0));
357  BOOST_CHECK(true == shape.GetDimensionSpecificity(1));
358  BOOST_CHECK(true == shape.GetDimensionSpecificity(2));
359  BOOST_CHECK(shape.AreAllDimensionsSpecified());
360  BOOST_CHECK(shape.IsAtLeastOneDimensionSpecified());
361 }
362 
363 BOOST_AUTO_TEST_CASE(Tensor_emptyConstructors)
364 {
365  auto shape = armnn::TensorShape();
366  BOOST_CHECK_MESSAGE( 0 == shape.GetNumDimensions(), "Number of dimensions is " << shape.GetNumDimensions());
367  BOOST_CHECK_MESSAGE( 0 == shape.GetNumElements(), "Number of elements is " << shape.GetNumElements());
368  BOOST_CHECK( armnn::Dimensionality::Specified == shape.GetDimensionality());
369  BOOST_CHECK( shape.AreAllDimensionsSpecified());
370  BOOST_CHECK_THROW( shape[0], InvalidArgumentException );
371 
372  auto tensor = armnn::Tensor();
373  BOOST_CHECK_MESSAGE( 0 == tensor.GetNumDimensions(), "Number of dimensions is " << tensor.GetNumDimensions());
374  BOOST_CHECK_MESSAGE( 0 == tensor.GetNumElements(), "Number of elements is " << tensor.GetNumElements());
375  BOOST_CHECK_MESSAGE( 0 == tensor.GetShape().GetNumDimensions(), "Number of dimensions is " <<
376  tensor.GetShape().GetNumDimensions());
377  BOOST_CHECK_MESSAGE( 0 == tensor.GetShape().GetNumElements(), "Number of dimensions is " <<
378  tensor.GetShape().GetNumElements());
379  BOOST_CHECK( armnn::Dimensionality::Specified == tensor.GetShape().GetDimensionality());
380  BOOST_CHECK( tensor.GetShape().AreAllDimensionsSpecified());
381  BOOST_CHECK_THROW( tensor.GetShape()[0], InvalidArgumentException );
382 }
unsigned int GetNumElements() const
Function that calculates the tensor elements by multiplying all dimension size which are Specified...
Definition: Tensor.cpp:182
BOOST_AUTO_TEST_SUITE(TensorflowLiteParser)
const TensorShape & GetShape() const
Definition: Tensor.hpp:187
bool AreAllDimensionsSpecified() const
Checks if there is at least one dimension not specified.
Definition: Tensor.cpp:242
Dimensionality GetDimensionality() const
Function that returns the tensor type.
Definition: Tensor.hpp:92
BOOST_FIXTURE_TEST_CASE(ConstructShapeUsingListInitialization, TensorInfoFixture)
Definition: TensorTest.cpp:55
const TensorShape & GetShape() const
Definition: Tensor.hpp:268
MemoryType GetMemoryArea() const
Definition: Tensor.hpp:276
Copyright (c) 2020 ARM Limited.
bool GetDimensionSpecificity(unsigned int i) const
Gets information about if the dimension size has been specified or not.
Definition: Tensor.cpp:212
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:189
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:290
void CheckTensor(const ConstTensor &t)
Definition: TensorTest.cpp:102
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:470
float GetQuantizationScale() const
Definition: Tensor.cpp:453
DataType GetDataType() const
Definition: Tensor.hpp:194
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:298
void SetNumDimensions(unsigned int numDimensions, bool initDimensionsSpecificity=false)
Sets the tensor rank and therefore the Dimensionality is set to Specified if it was not...
Definition: Tensor.cpp:220
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:465
BOOST_AUTO_TEST_CASE(CheckConvolution2dLayer)
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:266
void SetDataType(DataType type)
Definition: Tensor.hpp:195
std::ostream & boost_test_print_type(std::ostream &ostr, const TensorInfo &right)
Definition: TensorTest.cpp:14
BOOST_AUTO_TEST_SUITE_END()
unsigned int GetNumDimensions() const
Function that returns the tensor rank.
Definition: Tensor.cpp:175
void SetDimensionSize(unsigned int i, unsigned int dimensionSize)
Sets the size of the indicated dimension and Specificity for that dimension is set to true...
Definition: Tensor.cpp:233
void SetQuantizationOffset(int32_t offset)
Definition: Tensor.cpp:481
void SetQuantizationScales(const std::vector< float > &scales)
Definition: Tensor.cpp:448
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:191
bool IsAtLeastOneDimensionSpecified() const
Checks if there is at least one dimension specified.
Definition: Tensor.cpp:258