ArmNN
 20.02
TensorTest.cpp File Reference
#include <boost/test/unit_test.hpp>
#include <armnn/Tensor.hpp>

Go to the source code of this file.

Namespaces

 armnn
 Copyright (c) 2020 ARM Limited.
 

Functions

std::ostream & boost_test_print_type (std::ostream &ostr, const TensorInfo &right)
 
std::ostream & boost_test_print_type (std::ostream &ostr, const TensorShape &shape)
 
 BOOST_FIXTURE_TEST_CASE (ConstructShapeUsingListInitialization, TensorInfoFixture)
 
 BOOST_FIXTURE_TEST_CASE (ConstructTensorInfo, TensorInfoFixture)
 
 BOOST_FIXTURE_TEST_CASE (CopyConstructTensorInfo, TensorInfoFixture)
 
 BOOST_FIXTURE_TEST_CASE (TensorInfoEquality, TensorInfoFixture)
 
 BOOST_FIXTURE_TEST_CASE (TensorInfoInequality, TensorInfoFixture)
 
 BOOST_FIXTURE_TEST_CASE (TensorInfoAssignmentOperator, TensorInfoFixture)
 
void CheckTensor (const ConstTensor &t)
 
 BOOST_AUTO_TEST_CASE (TensorVsConstTensor)
 
 BOOST_AUTO_TEST_CASE (ModifyTensorInfo)
 
 BOOST_AUTO_TEST_CASE (TensorShapeOperatorBrackets)
 
 BOOST_AUTO_TEST_CASE (TensorInfoPerAxisQuantization)
 

Function Documentation

◆ BOOST_AUTO_TEST_CASE() [1/4]

BOOST_AUTO_TEST_CASE ( TensorVsConstTensor  )

Definition at line 105 of file TensorTest.cpp.

References CheckTensor().

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 }
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
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:199

◆ BOOST_AUTO_TEST_CASE() [2/4]

BOOST_AUTO_TEST_CASE ( ModifyTensorInfo  )

Definition at line 123 of file TensorTest.cpp.

References TensorInfo::GetDataType(), TensorInfo::GetQuantizationOffset(), TensorInfo::GetQuantizationScale(), TensorInfo::GetShape(), armnn::info, armnn::QAsymmU8, TensorInfo::SetDataType(), TensorInfo::SetQuantizationOffset(), TensorInfo::SetQuantizationScale(), and TensorInfo::SetShape().

124 {
126  info.SetShape({ 5, 6, 7, 8 });
127  BOOST_TEST((info.GetShape() == TensorShape({ 5, 6, 7, 8 })));
128  info.SetDataType(DataType::QAsymmU8);
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 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
void SetShape(const TensorShape &newShape)
Definition: Tensor.hpp:90
int32_t GetQuantizationOffset() const
Definition: Tensor.cpp:264
float GetQuantizationScale() const
Definition: Tensor.cpp:247
DataType GetDataType() const
Definition: Tensor.hpp:95
void SetQuantizationScale(float scale)
Definition: Tensor.cpp:259
void SetDataType(DataType type)
Definition: Tensor.hpp:96
void SetQuantizationOffset(int32_t offset)
Definition: Tensor.cpp:275

◆ BOOST_AUTO_TEST_CASE() [3/4]

BOOST_AUTO_TEST_CASE ( TensorShapeOperatorBrackets  )

Definition at line 136 of file TensorTest.cpp.

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 }

◆ BOOST_AUTO_TEST_CASE() [4/4]

BOOST_AUTO_TEST_CASE ( TensorInfoPerAxisQuantization  )

Definition at line 146 of file TensorTest.cpp.

References BOOST_AUTO_TEST_SUITE_END(), BOOST_CHECK(), and armnn::Float32.

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 }
BOOST_CHECK(profilingService.GetCurrentState()==ProfilingState::WaitingForAck)

◆ BOOST_FIXTURE_TEST_CASE() [1/6]

BOOST_FIXTURE_TEST_CASE ( ConstructShapeUsingListInitialization  ,
TensorInfoFixture   
)

Definition at line 53 of file TensorTest.cpp.

54 {
55  TensorShape listInitializedShape{ 6, 7, 8, 9 };
56  BOOST_TEST(listInitializedShape == m_TensorInfo.GetShape());
57 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:88

◆ BOOST_FIXTURE_TEST_CASE() [2/6]

BOOST_FIXTURE_TEST_CASE ( ConstructTensorInfo  ,
TensorInfoFixture   
)

Definition at line 59 of file TensorTest.cpp.

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 }
const TensorShape & GetShape() const
Definition: Tensor.hpp:88
unsigned int GetNumDimensions() const
Definition: Tensor.hpp:92

◆ BOOST_FIXTURE_TEST_CASE() [3/6]

BOOST_FIXTURE_TEST_CASE ( CopyConstructTensorInfo  ,
TensorInfoFixture   
)

Definition at line 68 of file TensorTest.cpp.

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 }

◆ BOOST_FIXTURE_TEST_CASE() [4/6]

BOOST_FIXTURE_TEST_CASE ( TensorInfoEquality  ,
TensorInfoFixture   
)

Definition at line 78 of file TensorTest.cpp.

79 {
80  TensorInfo copyConstructed(m_TensorInfo);
81  BOOST_TEST(copyConstructed == m_TensorInfo);
82 }

◆ BOOST_FIXTURE_TEST_CASE() [5/6]

BOOST_FIXTURE_TEST_CASE ( TensorInfoInequality  ,
TensorInfoFixture   
)

Definition at line 84 of file TensorTest.cpp.

References armnn::Float32.

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 }

◆ BOOST_FIXTURE_TEST_CASE() [6/6]

BOOST_FIXTURE_TEST_CASE ( TensorInfoAssignmentOperator  ,
TensorInfoFixture   
)

Definition at line 93 of file TensorTest.cpp.

94 {
95  TensorInfo copy;
96  copy = m_TensorInfo;
97  BOOST_TEST(copy == m_TensorInfo);
98 }

◆ CheckTensor()

void CheckTensor ( const ConstTensor t)

Definition at line 100 of file TensorTest.cpp.

References BaseTensor< MemoryType >::GetInfo().

Referenced by BOOST_AUTO_TEST_CASE().

101 {
102  t.GetInfo();
103 }
const TensorInfo & GetInfo() const
Definition: Tensor.hpp:167