14 #include <doctest/doctest.h> 22 typename std::enable_if<std::is_arithmetic<T>::value,
bool>::type
23 IsCloseEnough(T value1, T value2, T tolerance)
30 T diff = value1 >= value2 ?
static_cast<T
>(value1 - value2) : static_cast<T>(value2 - value1);
31 return diff <= tolerance;
38 const unsigned int numBatches = 2;
39 const unsigned int inputSize = 5;
40 const unsigned int outputSize = 4;
41 const unsigned int numUnits = 4;
43 bool cifgEnabled =
true;
44 bool peepholeEnabled =
false;
45 bool projectionEnabled =
false;
46 bool layerNormEnabled =
true;
49 const float inputScale = 0.0078125f;
50 const int32_t inputOffset = 0;
52 const int32_t hiddenStateZeroPoint = 0;
53 const float hiddenStateScale = 0.007f;
56 const float outputScale = hiddenStateScale;
57 const int32_t outputOffset = hiddenStateZeroPoint;
59 const float cellStateScale = 3.05176e-05f;
60 const int32_t cellStateOffset = 0;
62 const float weightsScale = 0.00784314f;
63 const int32_t weightsOffset = 0;
65 const float layerNormScale = 3.05182e-05f;
66 const int32_t layerNormOffset = 0;
68 const float biasScale = layerNormScale / 1024;
69 const int32_t biasOffset = 0;
71 const float inputIntermediateScale = 0.007059f;
72 const float forgetIntermediateScale = 0.007812f;
73 const float cellIntermediateScale = inputIntermediateScale;
74 const float outputIntermediateScale = forgetIntermediateScale;
76 const float cellClip = 0.0f;
77 const float projectionClip = 0.0f;
105 const std::vector<int8_t> inputToForgetWeightsVector =
106 {-77, -13, 38, 25, 115, -64, -25, -51, 38, -102, -51, 38, -64, -51, -77, 38, -51, -77, -64, -64};
107 const std::vector<int8_t> inputToCellWeightsTensorVector =
108 {-51, -38, -25, -13, -64, 64, -25, -38, -25, -77, 77, -13, -51, -38, -89, 89, -115, -64, 102, 77};
109 const std::vector<int8_t> inputToOutputWeightsTensorVector =
110 {-102, -51, -25, -115, -13, -89, 38, -38, -102, -25, 77, -25, 51, -89, -38, -64, 13, 64, -77, -51};
112 armnn::ConstTensor inputToForgetWeightsTensor(inputWeightsInfo, inputToForgetWeightsVector.data());
113 armnn::ConstTensor inputToCellWeightsTensor(inputWeightsInfo, inputToCellWeightsTensorVector.data());
114 armnn::ConstTensor inputToOutputWeightsTensor(inputWeightsInfo, inputToOutputWeightsTensorVector.data());
116 const std::vector<int8_t> recurrentToForgetWeightsTensorVector =
117 {-64, -38, -64, -25, 77, 51, 115, 38, -13, 25, 64, 25, 25, 38, -13, 51};
118 const std::vector<int8_t> recurrentToCellWeightsTensorVector =
119 {-38, 25, 13, -38, 102, -10, -25, 38, 102, -77, -13, 25, 38, -13, 25, 64};
120 const std::vector<int8_t> recurrentToOutputWeightsTensorVector =
121 {38, -13, 13, -25, -64, -89, -25, -77, -13, -51, -89, -25, 13, 64, 25, -38};
124 recurrentToForgetWeightsTensorVector.data());
126 recurrentToCellWeightsTensorVector.data());
128 recurrentToOutputWeightsTensorVector.data());
130 const std::vector<int32_t> forgetGateBiasTensorVector = {2147484, -6442451, -4294968, 2147484};
131 const std::vector<int32_t> cellBiasTensorVector = {-1073742, 15461883, 5368709, 1717987};
132 const std::vector<int32_t> outputGateBiasTensorVector = {1073742, -214748, 4294968, 2147484};
139 const std::vector<int16_t> forgetLayerNormWeightsVector = {6553, 6553, 13107, 9830};
140 const std::vector<int16_t> cellLayerNormWeightsVector = {22937, 6553, 9830, 26214};
141 const std::vector<int16_t> outputLayerNormWeightsVector = {19660, 6553, 6553, 16384};
143 armnn::ConstTensor forgetLayerNormWeights(layerNormWeightsInfo, forgetLayerNormWeightsVector.data());
144 armnn::ConstTensor cellLayerNormWeights(layerNormWeightsInfo, cellLayerNormWeightsVector.data());
145 armnn::ConstTensor outputLayerNormWeights(layerNormWeightsInfo, outputLayerNormWeightsVector.data());
150 params.m_InputToCellWeights = &inputToCellWeightsTensor;
151 params.m_InputToOutputWeights = &inputToOutputWeightsTensor;
153 params.m_RecurrentToForgetWeights = &recurrentToForgetWeightsTensor;
154 params.m_RecurrentToCellWeights = &recurrentToCellWeightsTensor;
155 params.m_RecurrentToOutputWeights = &recurrentToOutputWeightsTensor;
157 params.m_ForgetGateBias = &forgetGateBiasTensor;
158 params.m_CellBias = &cellBiasTensor;
159 params.m_OutputGateBias = &outputGateBiasTensor;
161 params.m_ForgetLayerNormWeights = &forgetLayerNormWeights;
162 params.m_CellLayerNormWeights = &cellLayerNormWeights;
163 params.m_OutputLayerNormWeights = &outputLayerNormWeights;
202 const std::vector<int8_t> inputVector = {90, 102, 13, 26, 38, 102, 13, 26, 51, 64};
203 const std::vector<int8_t> outputStateInVector = {0, 0, 0, 0, 0, 0, 0, 0};
204 const std::vector<int16_t> cellStateInVector = {0, 0, 0, 0, 0, 0, 0, 0};
207 const std::vector<int8_t> outputStateOutVector = {-15, 21, 14, 20, -15, 15, 5, 27};
208 const std::vector<int16_t> cellStateOutVector = {-11692, 9960, 5491, 8861, -9422, 7726, 2056, 13149};
209 const std::vector<int8_t> outputVector = {-15, 21, 14, 20, -15, 15, 5, 27};
225 Connect(input, qLstmLayer, inputInfo, 0, 0);
226 Connect(outputStateIn, qLstmLayer, outputStateInfo, 0, 1);
227 Connect(cellStateIn, qLstmLayer, cellStateInfo, 0, 2);
229 Connect(qLstmLayer, outputStateOut, outputStateInfo, 0, 0);
230 Connect(qLstmLayer, cellStateOut, cellStateInfo, 1, 0);
231 Connect(qLstmLayer, output, outputStateInfo, 2, 0);
242 runtime->LoadNetwork(netId, std::move(optNet));
246 inputTensors.reserve(3);
248 inputTensors.push_back({0,
ConstTensor(runtime->GetInputTensorInfo(netId, 0), inputVector.data())});
249 inputTensors.push_back({1,
ConstTensor(runtime->GetInputTensorInfo(netId, 1), outputStateInVector.data())});
250 inputTensors.push_back({2,
ConstTensor(runtime->GetInputTensorInfo(netId, 2), cellStateInVector.data())});
254 outputTensors.reserve(3);
256 std::vector<int8_t> outputStateOutResult(outputStateOutVector.size());
257 std::vector<int16_t> cellStateOutResult(cellStateOutVector.size());
258 std::vector<int8_t> outputResult(outputStateOutVector.size());
260 outputTensors.push_back({0,
Tensor(runtime->GetOutputTensorInfo(netId, 0), outputStateOutResult.data())});
261 outputTensors.push_back({1,
Tensor(runtime->GetOutputTensorInfo(netId, 1), cellStateOutResult.data())});
262 outputTensors.push_back({2,
Tensor(runtime->GetOutputTensorInfo(netId, 2), outputResult.data())});
265 runtime->EnqueueWorkload(netId, inputTensors, outputTensors);
267 constexpr int8_t toleranceInt8 = 1;
268 for (
unsigned int i = 0u; i < outputStateOutResult.size(); ++i)
270 CHECK(IsCloseEnough(outputStateOutVector[i], outputStateOutResult[i], toleranceInt8));
273 for (
unsigned int i = 0u; i < outputResult.size(); ++i)
275 CHECK(IsCloseEnough(outputVector[i], outputResult[i], toleranceInt8));
Interface for a layer that is connectable to other layers via InputSlots and OutputSlots.
bool m_PeepholeEnabled
Enable/disable peephole.
float m_HiddenStateScale
Hidden State quantization scale.
float m_OutputIntermediateScale
Output intermediate quantization scale.
std::unique_ptr< IRuntime, void(*)(IRuntime *runtime)> IRuntimePtr
std::vector< std::pair< LayerBindingId, class ConstTensor > > InputTensors
void QLstmEndToEnd(const std::vector< armnn::BackendId > &backends)
A tensor defined by a TensorInfo (shape and data type) and a mutable backing store.
bool m_LayerNormEnabled
Enable/disable layer normalization.
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.
float m_ProjectionClip
Clipping threshold value for the projection.
A tensor defined by a TensorInfo (shape and data type) and an immutable backing store.
float m_InputIntermediateScale
Input intermediate quantization scale.
std::vector< std::pair< LayerBindingId, class Tensor > > OutputTensors
std::unique_ptr< IOptimizedNetwork, void(*)(IOptimizedNetwork *network)> IOptimizedNetworkPtr
A QLstmDescriptor for the QLstmLayer.
float m_ForgetIntermediateScale
Forget intermediate quantization scale.
float m_CellClip
Clipping threshold value for the cell state.
bool m_ProjectionEnabled
Enable/disable the projection layer.
void Connect(armnn::IConnectableLayer *from, armnn::IConnectableLayer *to, const armnn::TensorInfo &tensorInfo, unsigned int fromIndex, unsigned int toIndex)
std::unique_ptr< INetwork, void(*)(INetwork *network)> INetworkPtr
float m_CellIntermediateScale
Cell intermediate quantization scale.
static INetworkPtr Create(NetworkOptions networkOptions={})
bool m_CifgEnabled
Enable/disable CIFG (coupled input & forget gate).
int32_t m_HiddenStateZeroPoint
Hidden State zero point.