ArmNN
 20.08
QuantizedLstmEndToEndTestImpl.cpp
Go to the documentation of this file.
1 //
2 // Copyright © 2019 Arm Ltd. All rights reserved.
3 // SPDX-License-Identifier: MIT
4 //
5 
7 
8 #include "CommonTestUtils.hpp"
9 #include "EndToEndTestImpl.hpp"
10 
11 #include <ResolveType.hpp>
12 
13 #include <armnn/INetwork.hpp>
15 
16 #include <test/TensorHelpers.hpp>
17 
18 #include <boost/test/unit_test.hpp>
19 
20 #include <type_traits>
21 
22 namespace
23 {
24 
25 using MultiArray = const boost::multi_array<uint8_t, 2>&;
26 
27 armnn::INetworkPtr CreateQuantizedLstmNetwork(MultiArray input,
28  MultiArray expectedOutput)
29 {
30  auto batchSize = boost::numeric_cast<unsigned int>(input.shape()[0]);
31  auto inputSize = boost::numeric_cast<unsigned int>(input.shape()[1]);
32  auto outputSize = boost::numeric_cast<unsigned int>(expectedOutput.shape()[1]);
33 
34  float inputOutputScale = 0.0078125f;
35  int32_t inputOutputOffset = 128;
36 
37  float weightsScale = 0.00408021f;
38  int32_t weightsOffset = 100;
39 
40  float biasScale = 3.1876640625e-05f;
41  int32_t biasOffset = 0;
42 
43  float cellStateScale = 0.00048828125f;
44  int32_t cellStateOffset = 0;
45 
46  armnn::TensorInfo inputWeightsInfo({outputSize, inputSize},
48  weightsScale,
49  weightsOffset);
50 
51  armnn::TensorInfo recurrentWeightsInfo({outputSize, outputSize},
53  weightsScale,
54  weightsOffset);
55 
56  armnn::TensorInfo biasInfo({outputSize}, armnn::DataType::Signed32, biasScale, biasOffset);
57 
59 
60  const std::vector<uint8_t> inputToInputWeightsVector = {146, 250, 235, 171, 10, 218, 171, 108};
61  armnn::ConstTensor inputToInputWeightsTensor(inputWeightsInfo, inputToInputWeightsVector.data());
62 
63  const std::vector<uint8_t> inputToForgetWeightsVector = {24, 50, 132, 179, 158, 110, 3, 169};
64  armnn::ConstTensor inputToForgetWeightsTensor(inputWeightsInfo, inputToForgetWeightsVector.data());
65 
66  const std::vector<uint8_t> inputToCellWeightsTensorVector = {133, 34, 29, 49, 206, 109, 54, 183};
67  armnn::ConstTensor inputToCellWeightsTensor(inputWeightsInfo, inputToCellWeightsTensorVector.data());
68 
69  const std::vector<uint8_t> inputToOutputWeightsTensorVector = {195, 187, 11, 99, 109, 10, 218, 48};
70  armnn::ConstTensor inputToOutputWeightsTensor(inputWeightsInfo, inputToOutputWeightsTensorVector.data());
71 
72  const std::vector<uint8_t> recurrentToInputWeightsTensorVector =
73  {254, 206, 77, 168, 71, 20, 215, 6, 223, 7, 118, 225, 59, 130, 174, 26};
74  armnn::ConstTensor recurrentToInputWeightsTensor(recurrentWeightsInfo, recurrentToInputWeightsTensorVector.data());
75 
76  const std::vector<uint8_t> recurrentToForgetWeightsTensorVector =
77  {137, 240, 103, 52, 68, 51, 237, 112, 0, 220, 89, 23, 69, 4, 207, 253};
78  armnn::ConstTensor recurrentToForgetWeightsTensor(recurrentWeightsInfo,
79  recurrentToForgetWeightsTensorVector.data());
80 
81  const std::vector<uint8_t> recurrentToCellWeightsTensorVector =
82  {172, 60, 205, 65, 14, 0, 140, 168, 240, 223, 133, 56, 142, 64, 246, 216};
83  armnn::ConstTensor recurrentToCellWeightsTensor(recurrentWeightsInfo, recurrentToCellWeightsTensorVector.data());
84 
85  const std::vector<uint8_t> recurrentToOutputWeightsTensorVector =
86  {106, 214, 67, 23, 59, 158, 45, 3, 119, 132, 49, 205, 129, 218, 11, 98};
87  armnn::ConstTensor recurrentToOutputWeightsTensor(recurrentWeightsInfo,
88  recurrentToOutputWeightsTensorVector.data());
89 
90  const std::vector<int32_t> inputGateBiasTensorVector = {-7876, 13488, -726, 32839};
91  armnn::ConstTensor inputGateBiasTensor(biasInfo, inputGateBiasTensorVector.data());
92 
93  const std::vector<int32_t> forgetGateBiasTensorVector = {9206, -46884, -11693, -38724};
94  armnn::ConstTensor forgetGateBiasTensor(biasInfo, forgetGateBiasTensorVector.data());
95 
96  const std::vector<int32_t> cellBiasTensorVector = {39481, 48624, 48976, -21419};
97  armnn::ConstTensor cellBiasTensor(biasInfo, cellBiasTensorVector.data());
98 
99  const std::vector<int32_t> outputGateBiasTensorVector = {-58999, -17050, -41852, -40538};
100  armnn::ConstTensor outputGateBiasTensor(biasInfo, outputGateBiasTensorVector.data());
101 
102  data.m_InputToInputWeights = &inputToInputWeightsTensor;
103  data.m_InputToForgetWeights = &inputToForgetWeightsTensor;
104  data.m_InputToCellWeights = &inputToCellWeightsTensor;
105  data.m_InputToOutputWeights = &inputToOutputWeightsTensor;
106  data.m_RecurrentToInputWeights = &recurrentToInputWeightsTensor;
107  data.m_RecurrentToForgetWeights = &recurrentToForgetWeightsTensor;
108  data.m_RecurrentToCellWeights = &recurrentToCellWeightsTensor;
109  data.m_RecurrentToOutputWeights = &recurrentToOutputWeightsTensor;
110  data.m_InputGateBias = &inputGateBiasTensor;
111  data.m_ForgetGateBias = &forgetGateBiasTensor;
112  data.m_CellBias = &cellBiasTensor;
113  data.m_OutputGateBias = &outputGateBiasTensor;
114 
116 
117  armnn::IConnectableLayer* const inputLayer = net->AddInputLayer(0);
118  armnn::IConnectableLayer* const cellStateIn = net->AddInputLayer(1);
119  armnn::IConnectableLayer* const outputStateIn = net->AddInputLayer(2);
120  armnn::IConnectableLayer* const quantizedLstmLayer = net->AddQuantizedLstmLayer(data, "quantizedLstm");
121  armnn::IConnectableLayer* const cellStateOut = net->AddOutputLayer(0);
122  armnn::IConnectableLayer* const outputStateOut = net->AddOutputLayer(1);
123 
124  armnn::TensorInfo inputTensorInfo({batchSize , inputSize},
126  inputOutputScale,
127  inputOutputOffset);
128 
129  armnn::TensorInfo cellStateInTensorInfo({batchSize , outputSize},
131  cellStateScale,
132  cellStateOffset);
133 
134  armnn::TensorInfo outputStateInTensorInfo({batchSize , outputSize},
136  inputOutputScale,
137  inputOutputOffset);
138 
139  armnn::TensorInfo cellStateOutTensorInfo({batchSize, outputSize},
141  cellStateScale,
142  cellStateOffset);
143 
144  armnn::TensorInfo outputTensorInfo({batchSize, outputSize},
146  inputOutputScale,
147  inputOutputOffset);
148 
149  // connect up
150  // inputs
151  Connect(inputLayer, quantizedLstmLayer, inputTensorInfo, 0, 0);
152  Connect(cellStateIn, quantizedLstmLayer, cellStateInTensorInfo, 0, 1);
153  Connect(outputStateIn, quantizedLstmLayer, outputStateInTensorInfo, 0, 2);
154 
155  // outputs
156  Connect(quantizedLstmLayer, cellStateOut, cellStateOutTensorInfo, 0, 0);
157  Connect(quantizedLstmLayer, outputStateOut, outputTensorInfo, 1, 0);
158 
159  return net;
160 }
161 
162 // Checks if two values of an arithmetic type are close enough to each other
163 // with regard to a given tolerance value.
164 template<typename T>
165 typename std::enable_if<std::is_arithmetic<T>::value, bool>::type
166 IsCloseEnough(T value1, T value2, T tolerance)
167 {
168  if (tolerance < 0)
169  {
170  throw armnn::InvalidArgumentException("Tolerance cannot be < 0");
171  }
172 
173  T diff = value1 >= value2 ? static_cast<T>(value1 - value2) : static_cast<T>(value2 - value1);
174  return diff <= tolerance;
175 }
176 
177 } // anonymous namespace
178 
179 void QuantizedLstmEndToEnd(const std::vector<armnn::BackendId>& backends)
180 {
181  std::vector<uint8_t> inputVector = {166, 179, 50, 150};
182  armnn::TensorInfo inputDesc({2, 2}, armnn::DataType::QAsymmU8);
183  boost::multi_array<uint8_t, 2> input = MakeTensor<uint8_t, 2>(inputDesc, inputVector);
184 
185  std::vector<int16_t> cellStateInVector = {876, 1034, 955, -909, 761, 1029, 796, -1036};
186  armnn::TensorInfo cellStateInDesc({2, 4}, armnn::DataType::QSymmS16);
187  boost::multi_array<int16_t, 2> cellStateIn = MakeTensor<int16_t, 2>(cellStateInDesc, cellStateInVector);
188 
189  std::vector<uint8_t> outputStateInVector = {136, 150, 140, 115, 135, 152, 138, 112};
190  armnn::TensorInfo outputStateInDesc({2, 4}, armnn::DataType::QAsymmU8);
191  boost::multi_array<uint8_t, 2> outputStateIn = MakeTensor<uint8_t, 2>(outputStateInDesc, outputStateInVector);
192 
193  std::vector<int16_t> cellStateOutVector = {1485, 1177, 1373, -1023, 1019, 1355, 1097, -1235};
194  armnn::TensorInfo cellStateOutVectorDesc({2, 4}, armnn::DataType::QSymmS16);
195  boost::multi_array<int16_t, 2> cellStateOut = MakeTensor<int16_t, 2>(cellStateOutVectorDesc, cellStateOutVector);
196 
197  std::vector<uint8_t> outputStateOutVector = {140, 151, 146, 112, 136, 156, 142, 112};
198  armnn::TensorInfo outputDesc({2, 4}, armnn::DataType::QAsymmU8);
199  boost::multi_array<uint8_t, 2> outputStateOut = MakeTensor<uint8_t, 2>(outputDesc, outputStateOutVector);
200 
201  // Builds up the structure of the network
202  armnn::INetworkPtr net = CreateQuantizedLstmNetwork(input, outputStateOut);
203 
204  BOOST_TEST_CHECKPOINT("create a network");
205 
207  IRuntimePtr runtime(IRuntime::Create(options));
208 
209  // optimize the network
210  IOptimizedNetworkPtr optNet = Optimize(*net, backends, runtime->GetDeviceSpec());
211 
212  // Loads it into the runtime.
213  NetworkId netId;
214  runtime->LoadNetwork(netId, std::move(optNet));
215 
216  InputTensors inputTensors;
217  inputTensors.reserve(3);
218 
219  // input
220  inputTensors.push_back({0, ConstTensor(runtime->GetInputTensorInfo(netId, 0), inputVector.data())});
221  inputTensors.push_back({1, ConstTensor(runtime->GetInputTensorInfo(netId, 1), cellStateInVector.data())});
222  inputTensors.push_back({2, ConstTensor(runtime->GetInputTensorInfo(netId, 2), outputStateInVector.data())});
223 
224  OutputTensors outputTensors;
225  outputTensors.reserve(2);
226 
227  //output
228  std::vector<int16_t > cellStateOutResult(cellStateOutVector.size());
229  std::vector<uint8_t > outputStateOutResult(outputStateOutVector.size());
230  outputTensors.push_back({0, Tensor(runtime->GetOutputTensorInfo(netId, 0), cellStateOutResult.data())});
231  outputTensors.push_back({1, Tensor(runtime->GetOutputTensorInfo(netId, 1), outputStateOutResult.data())});
232 
233  // Does the inference.
234  runtime->EnqueueWorkload(netId, inputTensors, outputTensors);
235 
236  // Checks the results
237  constexpr int16_t toleranceInt16 = 2;
238  for (unsigned int i = 0u; i < cellStateOutResult.size(); ++i)
239  {
240  BOOST_CHECK(IsCloseEnough(cellStateOutVector[i], cellStateOutResult[i], toleranceInt16));
241  }
242 
243  constexpr uint8_t toleranceUint8 = 1;
244  for (unsigned int i = 0u; i < outputStateOutResult.size(); ++i)
245  {
246  BOOST_TEST(IsCloseEnough(outputStateOutVector[i], outputStateOutResult[i], toleranceUint8));
247  }
248 }
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
Definition: INetwork.hpp:61
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
Definition: IRuntime.hpp:25
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
Definition: Tensor.hpp:324
int NetworkId
Definition: IRuntime.hpp:20
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
Definition: Tensor.hpp:290
IOptimizedNetworkPtr Optimize(const INetwork &network, const std::vector< BackendId > &backendPreferences, const IDeviceSpec &deviceSpec, const OptimizerOptions &options=OptimizerOptions(), Optional< std::vector< std::string > &> messages=EmptyOptional())
Create an optimized version of the network.
Definition: Network.cpp:1014
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
Definition: Tensor.hpp:298
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
Definition: Tensor.hpp:325
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
Definition: INetwork.hpp:593
std::enable_if_t< std::is_unsigned< Source >::value &&std::is_unsigned< Dest >::value, Dest > numeric_cast(Source source)
Definition: NumericCast.hpp:33
void QuantizedLstmEndToEnd(const std::vector< armnn::BackendId > &backends)
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
Definition: TestUtils.cpp:12
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
Definition: INetwork.hpp:101
static INetworkPtr Create(NetworkOptions networkOptions={})
Definition: Network.cpp:50