diff options
author | James Conroy <james.conroy@arm.com> | 2020-04-29 20:01:10 +0100 |
---|---|---|
committer | James Conroy <james.conroy@arm.com> | 2020-05-02 16:44:33 +0000 |
commit | 4f1f899da140bb0490cf7e404daeaf1206f4db8b (patch) | |
tree | dc6d1215440e0efa677d47a4b944882d72e12cc9 /src/backends/backendsCommon | |
parent | 56e1a5f68213c9134826ad14c6e1fb4c0d41fb46 (diff) | |
download | armnn-4f1f899da140bb0490cf7e404daeaf1206f4db8b.tar.gz |
IVGCVSW-4449 Add QLstm ref implementation
* Adds ref implemenation for new HAL 1.3
operator, QLstm.
* Adds Layer and CreateWorkload unit tests.
* Adds WorkloadData validate for QLstm.
Signed-off-by: James Conroy <james.conroy@arm.com>
Change-Id: I8a721f07ff06105e6495a1a0561b9503aa8146dc
Diffstat (limited to 'src/backends/backendsCommon')
3 files changed, 547 insertions, 0 deletions
diff --git a/src/backends/backendsCommon/WorkloadData.cpp b/src/backends/backendsCommon/WorkloadData.cpp index d1249a492f..5796fc7c77 100644 --- a/src/backends/backendsCommon/WorkloadData.cpp +++ b/src/backends/backendsCommon/WorkloadData.cpp @@ -2844,6 +2844,292 @@ void TransposeQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const ValidateTensorDataTypesMatch(inputTensorInfo, outputTensorInfo, descriptorName, "input", "output"); } +void QLstmQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const +{ + const std::string descriptorName{"QLstmQueueDescriptor"}; + + // Validate number of inputs/outputs + ValidateNumInputs(workloadInfo, descriptorName, 3); + ValidateNumOutputs(workloadInfo, descriptorName, 3); + + // Input/output tensor info + auto inputInfo = workloadInfo.m_InputTensorInfos[0]; + auto outputStateInInfo = workloadInfo.m_InputTensorInfos[1]; + auto cellStateInInfo = workloadInfo.m_InputTensorInfos[2]; + + auto outputStateOutInfo = workloadInfo.m_OutputTensorInfos[0]; + auto cellStateOutInfo = workloadInfo.m_OutputTensorInfos[1]; + auto outputInfo = workloadInfo.m_OutputTensorInfos[2]; + + // Supported types for various tensors in QLSTM + std::vector<DataType> inputOutputSupportedTypes = + { + DataType::QAsymmS8 + }; + + std::vector<DataType> cellStateSupportedTypes = + { + DataType::QSymmS16 + }; + + std::vector<DataType> weightsSupportedTypes = + { + DataType::QSymmS8 + }; + + std::vector<DataType> layerNormPeepholeWeightsSupportedTypes = + { + DataType::QSymmS16 + }; + + std::vector<DataType> biasSupportedTypes = + { + DataType::Signed32 + }; + + // Validate types of input/output tensors + ValidateDataTypes(inputInfo, inputOutputSupportedTypes, descriptorName); + ValidateDataTypes(outputStateInInfo, inputOutputSupportedTypes, descriptorName); + ValidateDataTypes(cellStateInInfo, cellStateSupportedTypes, descriptorName); + + ValidateDataTypes(outputStateOutInfo, inputOutputSupportedTypes, descriptorName); + ValidateDataTypes(cellStateOutInfo, cellStateSupportedTypes, descriptorName); + ValidateDataTypes(outputInfo, inputOutputSupportedTypes, descriptorName); + + // Validate matching types of input/output tensors + ValidateTensorDataTypesMatch(inputInfo, outputStateInInfo, descriptorName, "input", "outputStateIn"); + ValidateTensorDataTypesMatch(outputStateInInfo, outputStateOutInfo, descriptorName, + "outputStateIn", "outputStateOut"); + ValidateTensorDataTypesMatch(cellStateInInfo, cellStateOutInfo, descriptorName, "cellStateIn", "cellStateOut"); + + // Infer number of batches, number of units, input size and output size from tensor dimensions + const uint32_t numBatches = inputInfo.GetShape()[0]; + const uint32_t inputSize = inputInfo.GetShape()[1]; + const uint32_t outputSize = outputStateInInfo.GetShape()[1]; + const uint32_t numUnits = cellStateInInfo.GetShape()[1]; + + // Validate number of dimensions and number of elements for input/output tensors + ValidateTensorNumDimNumElem(inputInfo, 2, (numBatches * inputSize), descriptorName + " input"); + ValidateTensorNumDimNumElem(outputStateInInfo, 2, (numBatches * outputSize), descriptorName + " outputStateIn"); + ValidateTensorNumDimNumElem(cellStateInInfo, 2, (numBatches * numUnits), descriptorName + " cellStateIn"); + + ValidateTensorNumDimNumElem(outputStateOutInfo, 2, (numBatches * outputSize), descriptorName + " outputStateOut"); + ValidateTensorNumDimNumElem(cellStateOutInfo, 2, (numBatches * numUnits), descriptorName + " cellStateOut"); + ValidateTensorNumDimNumElem(outputInfo, 2, (numBatches * outputSize), descriptorName + " output"); + + // Validate number of dimensions and number of elements for MANDATORY weight tensors + ValidatePointer(m_InputToForgetWeights, descriptorName, "InputToForgetWeights"); + auto inputToForgetWeightsInfo = m_InputToForgetWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(inputToForgetWeightsInfo, 2, (numUnits * inputSize), " InputToForgetWeights"); + + ValidatePointer(m_InputToCellWeights, descriptorName, "InputToCellWeights"); + auto inputToCellWeightsInfo = m_InputToCellWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(inputToCellWeightsInfo, 2, (numUnits * inputSize), " InputToCellWeights"); + + ValidatePointer(m_InputToOutputWeights, descriptorName, "InputToOutputWeights"); + auto inputToOutputWeightsInfo = m_InputToOutputWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(inputToOutputWeightsInfo, 2, (numUnits * inputSize), " InputToOutputWeights"); + + ValidatePointer(m_RecurrentToForgetWeights, descriptorName, "RecurrentToForgetWeights"); + auto recurrentToForgetWeightsInfo = m_RecurrentToForgetWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(recurrentToForgetWeightsInfo, 2, (numUnits * outputSize), + " RecurrentToForgetWeights"); + + ValidatePointer(m_RecurrentToCellWeights, descriptorName, "RecurrentToCellWeights"); + auto recurrentToCellWeightsInfo = m_RecurrentToCellWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(recurrentToCellWeightsInfo, 2, (numUnits * outputSize), " RecurrentToCellWeights"); + + ValidatePointer(m_RecurrentToOutputWeights, descriptorName, "RecurrentToOutputWeights"); + auto recurrentToOutputWeightsInfo = m_RecurrentToOutputWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(recurrentToOutputWeightsInfo, 2, (numUnits * outputSize), " RecurrentToCellWeights"); + + // Validate data types for MANDATORY weights tensors (all should match each other) + ValidateDataTypes(inputToForgetWeightsInfo, weightsSupportedTypes, descriptorName); + + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, inputToCellWeightsInfo, descriptorName, + "inputToForgetWeights", "inputToCellWeights"); + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, inputToOutputWeightsInfo, descriptorName, + "inputToForgetWeights", "inputToOutputWeights"); + + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, recurrentToForgetWeightsInfo, descriptorName, + "inputToForgetWeights", "recurrentToForgeteights"); + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, recurrentToCellWeightsInfo, descriptorName, + "inputToForgetWeights", "recurrentToCellWeights"); + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, recurrentToOutputWeightsInfo, descriptorName, + "inputToForgetWeights", "recurrentToOutputWeights"); + + // Validate number of dimensions and number of elements for MANDATORY bias tensors + ValidatePointer(m_ForgetGateBias, descriptorName, "ForgetGateBias"); + auto forgetGateBiasInfo = m_ForgetGateBias->GetTensorInfo(); + ValidateTensorNumDimNumElem(forgetGateBiasInfo, 1, numUnits, " ForgetGateBias"); + + ValidatePointer(m_CellBias, descriptorName, "CellBias"); + auto cellBiasInfo = m_CellBias->GetTensorInfo(); + ValidateTensorNumDimNumElem(cellBiasInfo, 1, numUnits, " CellBias"); + + ValidatePointer(m_OutputGateBias, descriptorName, "OutputGateBias"); + auto outputGateBiasInfo = m_OutputGateBias->GetTensorInfo(); + ValidateTensorNumDimNumElem(outputGateBiasInfo, 1, numUnits, " OutputGateBias"); + + // Validate data types for MANDATORY bias tensors + ValidateDataTypes(forgetGateBiasInfo, biasSupportedTypes, descriptorName); + + ValidateTensorDataTypesMatch(forgetGateBiasInfo, cellBiasInfo, descriptorName, + "forgetGateBias", "cellBias"); + ValidateTensorDataTypesMatch(forgetGateBiasInfo, outputGateBiasInfo, descriptorName, + "forgetGateBias", "outputGateBias"); + + // Validate OPTIONAL params: CIFG (inputToInputWeights, recurrentToInputWeights, inputGateBias) + const bool allCifgParamsPresentOrNot = ((m_InputToInputWeights && m_RecurrentToInputWeights && m_InputGateBias && + !m_Parameters.m_CifgEnabled) || + (!m_InputToInputWeights && !m_RecurrentToInputWeights && + !m_InputGateBias && m_Parameters.m_CifgEnabled)); + + if (!allCifgParamsPresentOrNot) + { + throw InvalidArgumentException(descriptorName + + ": InputToInputWeights, RecurrentToInputWeights and InputGateBias must either all be present " + "(CIFG disabled) or not be present at all (CIFG enabled). m_Parameters.m_CifgEnabled should be " + "set appropriately."); + } + + if (!m_Parameters.m_CifgEnabled) + { + // Validate number of dimensions and number of elements + auto inputToInputWeightsInfo = m_InputToInputWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(inputToInputWeightsInfo, 2, (numUnits * inputSize), " InputToInputWeights"); + + auto recurrentToInputWeightsInfo = m_RecurrentToInputWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(recurrentToInputWeightsInfo, 2, (numUnits * outputSize), + " RecurrentToInputWeights"); + + auto inputGateBiasInfo = m_InputGateBias->GetTensorInfo(); + ValidateTensorNumDimNumElem(inputGateBiasInfo, 1, numUnits, " InputGateBias"); + + // Validate data types + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, inputToInputWeightsInfo, descriptorName, + "inputToForgetWeights", "inputToInputWeights"); + ValidateTensorDataTypesMatch(inputToForgetWeightsInfo, recurrentToInputWeightsInfo, descriptorName, + "inputToForgetWeights", "recurrentToInputWeights"); + ValidateTensorDataTypesMatch(forgetGateBiasInfo, inputGateBiasInfo, descriptorName, + "forgetGateBias", "inputGateBias"); + } + + // Validate OPTIONAL params: Peephole (cellToInputWeights, cellToForgetWeights, cellToOutputWeights) + bool allPeepholeWeightsPresentOrNot = + (((m_CellToInputWeights || m_Parameters.m_CifgEnabled) && m_CellToForgetWeights + && m_CellToOutputWeights && m_Parameters.m_PeepholeEnabled) + || (!m_CellToInputWeights && !m_CellToForgetWeights + && !m_CellToOutputWeights && !m_Parameters.m_PeepholeEnabled)); + + if (!allPeepholeWeightsPresentOrNot) + { + throw InvalidArgumentException(descriptorName + + ": CellToInputWeights, CellToForgetWeights and CellToOutputWeights should all be present (Peephole " + "enabled) or not be present at all (Peephole disabled). CellToInputWeights should only be present " + "when Peephole is enabled and CIFG is disabled. m_Parameters.m_PeepholeEnabled should be set " + "appropriately."); + } + + if (m_Parameters.m_PeepholeEnabled) + { + auto cellToForgetWeightsInfo = m_CellToForgetWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(cellToForgetWeightsInfo, 1, numUnits, " cellToForgetWeights"); + ValidateDataTypes(cellToForgetWeightsInfo, layerNormPeepholeWeightsSupportedTypes, descriptorName); + + auto cellToOutputWeightsInfo = m_CellToOutputWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(cellToOutputWeightsInfo, 1, numUnits, " cellToOutputWeights"); + ValidateTensorDataTypesMatch(cellToForgetWeightsInfo, cellToOutputWeightsInfo, descriptorName, + "cellToForgetWeight", "cellToOutputWeights"); + + if (!m_Parameters.m_CifgEnabled) + { + auto cellToInputWeightsInfo = m_CellToInputWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(cellToInputWeightsInfo, 1, numUnits, " cellToInputWeights"); + ValidateTensorDataTypesMatch(cellToForgetWeightsInfo, cellToInputWeightsInfo, descriptorName, + "cellToForgetWeights", "cellToInputWeights"); + } + } + + // Validate OPTIONAL params: Layer Norm Weights + bool allLayerNormWeightsPresentOrNot = + (((m_InputLayerNormWeights || m_Parameters.m_CifgEnabled) && m_ForgetLayerNormWeights + && m_CellLayerNormWeights && m_OutputLayerNormWeights && m_Parameters.m_LayerNormEnabled) + || (!m_InputLayerNormWeights && !m_ForgetLayerNormWeights && !m_CellLayerNormWeights + && !m_OutputLayerNormWeights && !m_Parameters.m_LayerNormEnabled)); + + if (!allLayerNormWeightsPresentOrNot) + { + throw InvalidArgumentException(descriptorName + + ": InputLayerNormWeights, ForgetLayerNormWeights, m_OutputLayerNormWeights " + "and CellLayerNormWeights should all be present (Layer Norm enabled) or not " + "be present at all (Layer Norm disabled). InputLayerNormWeights should " + "only be present when Layer Norm is enabled and CIFG is disabled. " + "m_Parameters.m_LayerNormEnabled should be set appropriately."); + } + + if (m_Parameters.m_LayerNormEnabled) + { + auto forgetLayerNormWeightsInfo = m_ForgetLayerNormWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(forgetLayerNormWeightsInfo, 1, numUnits, " forgetLayerNormWeights"); + ValidateDataTypes(forgetLayerNormWeightsInfo, layerNormPeepholeWeightsSupportedTypes, descriptorName); + + auto cellLayerNormWeightsInfo = m_CellLayerNormWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(cellLayerNormWeightsInfo, 1, numUnits, " cellLayerNormWeights"); + ValidateTensorDataTypesMatch(forgetLayerNormWeightsInfo, cellLayerNormWeightsInfo, descriptorName, + "forgetLayerNormWeights", "cellLayerNormWeights"); + + auto outputLayerNormWeightsInfo = m_OutputLayerNormWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(outputLayerNormWeightsInfo, 1, numUnits, " outputLayerNormWeights"); + ValidateTensorDataTypesMatch(forgetLayerNormWeightsInfo, outputLayerNormWeightsInfo, descriptorName, + "forgetLayerNormWeights", "outputLayerNormWeights"); + + if (!m_Parameters.m_CifgEnabled) + { + auto inputLayerNormWeightsInfo = m_InputLayerNormWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(inputLayerNormWeightsInfo, 1, numUnits, " inputLayerNormWeights"); + ValidateTensorDataTypesMatch(forgetLayerNormWeightsInfo, inputLayerNormWeightsInfo, descriptorName, + "forgetLayerNormWeights", "inputLayerNormWeights"); + } + } + + // Validate OPTIONAL params: Projection (projectionWeights, projectionBias) + bool correctProjectionTensorsPresent = + ((!m_ProjectionWeights && !m_ProjectionBias && !m_Parameters.m_ProjectionEnabled) || + (m_ProjectionWeights && !m_ProjectionBias && m_Parameters.m_ProjectionEnabled) || + (m_ProjectionWeights && m_ProjectionBias && m_Parameters.m_ProjectionEnabled)); + + if (!correctProjectionTensorsPresent) + { + throw InvalidArgumentException(descriptorName + + ": If projection is enabled, ProjectionWeights should be present and " + "ProjectionBias is optional. If projection is disabled, neither " + "ProjectionWeights nor ProjectionBias should be present."); + } + + if (m_Parameters.m_ProjectionEnabled) + { + auto projectionWeightsInfo = m_ProjectionWeights->GetTensorInfo(); + ValidateTensorNumDimNumElem(projectionWeightsInfo, 2, (numUnits * outputSize), "ProjectionWeights"); + ValidateDataTypes(projectionWeightsInfo, weightsSupportedTypes, descriptorName); + + if (m_ProjectionBias) + { + auto projectionBiasInfo = m_ProjectionBias->GetTensorInfo(); + ValidateTensorNumDimNumElem(projectionBiasInfo, 1, numUnits, "ProjectionBias"); + ValidateDataTypes(projectionBiasInfo, biasSupportedTypes, descriptorName); + } + + } + else if ((outputInfo.GetQuantizationScale() != m_Parameters.m_HiddenStateScale) && + outputInfo.GetQuantizationOffset() != m_Parameters.m_HiddenStateZeroPoint) { + throw InvalidArgumentException(descriptorName + + ": If projection is disabled, output quantization info (scale, offset) " + "should match HiddenStateScale and HiddenStateZeroPoint."); + } + +} + void QuantizedLstmQueueDescriptor::Validate(const WorkloadInfo& workloadInfo) const { const std::string descriptorName{"QuantizedLstmQueueDescriptor"}; diff --git a/src/backends/backendsCommon/test/layerTests/LstmTestImpl.cpp b/src/backends/backendsCommon/test/layerTests/LstmTestImpl.cpp index 50ef5c9758..0ae55e4c60 100644 --- a/src/backends/backendsCommon/test/layerTests/LstmTestImpl.cpp +++ b/src/backends/backendsCommon/test/layerTests/LstmTestImpl.cpp @@ -1733,6 +1733,243 @@ LayerTestResult<uint8_t, 2> QuantizedLstmTestImpl( return ret; } +// QLSTM +LayerTestResult<int8_t, 2> QLstmTestImpl( + armnn::IWorkloadFactory& workloadFactory, + const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager, + const boost::multi_array<int8_t, 2>& input, + const boost::multi_array<int8_t, 2>& outputExpected) +{ + IgnoreUnused(memoryManager); + unsigned int numBatches = 2; + unsigned int inputSize = 5; + unsigned int outputSize = 4; + unsigned int numUnits = 4; + + bool cifgEnabled = true; + bool peepholeEnabled = false; + bool projectionEnabled = false; + bool layerNormEnabled = true; + + // Scale/Offset quantization info + float inputScale = 0.0078125f; + int32_t inputOffset = 0; + + int32_t hiddenStateZeroPoint = 0; + float hiddenStateScale = 0.007f; + + // if (!projectionEnabled) outputScale == hiddenStateScale + float outputScale = hiddenStateScale; + int32_t outputOffset = hiddenStateZeroPoint; + + float cellStateScale = 3.05176e-05f; + int32_t cellStateOffset = 0; + + float weightsScale = 0.00784314f; + int32_t weightsOffset = 0; + + float layerNormScale = 3.05182e-05f; + int32_t layerNormOffset = 0; + + float biasScale = layerNormScale / 1024; + int32_t biasOffset = 0; + + float inputIntermediateScale = 0.007059f; + float forgetIntermediateScale = 0.007812f; + float cellIntermediateScale = inputIntermediateScale; + float outputIntermediateScale = forgetIntermediateScale; + + float cellClip = 0.0f; + float projectionClip = 0.0f; + + // Input/Output tensor info + armnn::TensorInfo inputInfo({numBatches , inputSize}, + armnn::DataType::QAsymmS8, + inputScale, + inputOffset); + + armnn::TensorInfo cellStateInfo({numBatches , numUnits}, + armnn::DataType::QSymmS16, + cellStateScale, + cellStateOffset); + + armnn::TensorInfo outputStateInfo({numBatches , outputSize}, + armnn::DataType::QAsymmS8, + outputScale, + outputOffset); + + LayerTestResult<int8_t, 2> ret(outputStateInfo); + + // Input tensors + std::vector<int8_t> inputVector; + inputVector.assign(input.data(), input.data() + (numBatches * inputSize)); + auto inputTensor = MakeTensor<int8_t, 2>(inputInfo, inputVector); + + std::vector<int16_t> cellStateInVector = {0, 0, 0, 0, 0, 0, 0, 0}; + auto cellStateInTensor = MakeTensor<int16_t, 2>(cellStateInfo, cellStateInVector); + + std::vector<int8_t> outputStateInVector = {0, 0, 0, 0, 0, 0, 0, 02}; + auto outputStateInTensor = MakeTensor<int8_t, 2>(outputStateInfo, outputStateInVector); + + // Output tensors + std::vector<int16_t> cellStateOutVector = {-11692, 9960, 5491, 8861, -9422, 7726, 2056, 13149}; + auto cellStateOutTensor = MakeTensor<int16_t, 2>(cellStateInfo, cellStateOutVector); + + std::vector<int8_t> outputVector; + outputVector.assign(outputExpected.data(), outputExpected.data() + (numBatches * outputSize)); + ret.outputExpected = MakeTensor<int8_t, 2>(outputStateInfo, outputVector); + + // Create tensor handles + std::unique_ptr<armnn::ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputInfo); + std::unique_ptr<armnn::ITensorHandle> cellStateInHandle = + workloadFactory.CreateTensorHandle(cellStateInfo); + std::unique_ptr<armnn::ITensorHandle> outputStateInHandle = + workloadFactory.CreateTensorHandle(outputStateInfo); + + std::unique_ptr<armnn::ITensorHandle> outputStateOutHandle = workloadFactory.CreateTensorHandle(outputStateInfo); + std::unique_ptr<armnn::ITensorHandle> cellStateOutHandle = + workloadFactory.CreateTensorHandle(cellStateInfo); + std::unique_ptr<armnn::ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputStateInfo); + + armnn::QLstmQueueDescriptor data; + armnn::WorkloadInfo info; + + // Add inputs and outputs to workload + AddInputToWorkload(data, info, inputInfo, inputHandle.get()); + AddInputToWorkload(data, info, outputStateInfo, outputStateInHandle.get()); + AddInputToWorkload(data, info, cellStateInfo, cellStateInHandle.get()); + + AddOutputToWorkload(data, info, outputStateInfo, outputStateOutHandle.get()); + AddOutputToWorkload(data, info, cellStateInfo, cellStateOutHandle.get()); + AddOutputToWorkload(data, info, outputStateInfo, outputHandle.get()); + + // Weights and bias tensor and quantization info + armnn::TensorInfo inputWeightsInfo({outputSize, inputSize}, + armnn::DataType::QSymmS8, + weightsScale, + weightsOffset); + + armnn::TensorInfo recurrentWeightsInfo({outputSize, outputSize}, + armnn::DataType::QSymmS8, + weightsScale, + weightsOffset); + + armnn::TensorInfo biasInfo({outputSize}, armnn::DataType::Signed32, biasScale, biasOffset); + + armnn::TensorInfo layerNormWeightsInfo({numUnits}, armnn::DataType::QSymmS16, layerNormScale, layerNormOffset); + + // Weights and bias tensor data + auto inputToForgetWeights = MakeTensor<int8_t, 2>(inputWeightsInfo, + {-77, -13, 38, 25, 115, -64, -25, -51, 38, -102, -51, 38, -64, -51, -77, 38, -51, -77, -64, -64}); + auto inputToCellWeights = MakeTensor<int8_t, 2>(inputWeightsInfo, + {-51, -38, -25, -13, -64, 64, -25, -38, -25, -77, 77, -13, -51, -38, -89, 89, -115, -64, 102, 77}); + auto inputToOutputWeights = MakeTensor<int8_t, 2>(inputWeightsInfo, + {-102, -51, -25, -115, -13, -89, 38, -38, -102, -25, 77, -25, 51, -89, -38, -64, 13, 64, -77, -51}); + + auto recurrentToForgetWeights = MakeTensor<int8_t, 2>(recurrentWeightsInfo, + {-64, -38, -64, -25, 77, 51, 115, 38, -13, 25, 64, 25, 25, 38, -13, 51}); + auto recurrentToCellWeights = MakeTensor<int8_t, 2>(recurrentWeightsInfo, + {-38, 25, 13, -38, 102, -10, -25, 38, 102, -77, -13, 25, 38, -13, 25, 64}); + auto recurrentToOutputWeights = MakeTensor<int8_t, 2>(recurrentWeightsInfo, + {38, -13, 13, -25, -64, -89, -25, -77, -13, -51, -89, -25, 13, 64, 25, -38}); + + auto forgetGateBias = MakeTensor<int32_t, 1>(biasInfo, {2147484, -6442451, -4294968, 2147484}); + auto cellBias = MakeTensor<int32_t, 1>(biasInfo, {-1073742, 15461883, 5368709, 1717987}); + auto outputGateBias = MakeTensor<int32_t, 1>(biasInfo, {1073742, -214748, 4294968, 2147484}); + + auto forgetLayerNormWeights = MakeTensor<int16_t, 1>(layerNormWeightsInfo, {6553, 6553, 13107, 9830}); + auto cellLayerNormWeights = MakeTensor<int16_t, 1>(layerNormWeightsInfo, {22937, 6553, 9830, 26214}); + auto outputLayerNormWeights = MakeTensor<int16_t, 1>(layerNormWeightsInfo, {19660, 6553, 6553, 16384}); + + // ScopedCpuTensorHandles + armnn::ScopedCpuTensorHandle inputToForgetWeightsTensor(inputWeightsInfo); + armnn::ScopedCpuTensorHandle inputToCellWeightsTensor(inputWeightsInfo); + armnn::ScopedCpuTensorHandle inputToOutputWeightsTensor(inputWeightsInfo); + + armnn::ScopedCpuTensorHandle recurrentToForgetWeightsTensor(recurrentWeightsInfo); + armnn::ScopedCpuTensorHandle recurrentToCellWeightsTensor(recurrentWeightsInfo); + armnn::ScopedCpuTensorHandle recurrentToOutputWeightsTensor(recurrentWeightsInfo); + + armnn::ScopedCpuTensorHandle forgetGateBiasTensor(biasInfo); + armnn::ScopedCpuTensorHandle cellBiasTensor(biasInfo); + armnn::ScopedCpuTensorHandle outputGateBiasTensor(biasInfo); + + armnn::ScopedCpuTensorHandle forgetLayerNormWeightsTensor(layerNormWeightsInfo); + armnn::ScopedCpuTensorHandle cellLayerNormWeightsTensor(layerNormWeightsInfo); + armnn::ScopedCpuTensorHandle outputLayerNormWeightsTensor(layerNormWeightsInfo); + + // Allocate and copy data + AllocateAndCopyDataToITensorHandle(&inputToForgetWeightsTensor, &inputToForgetWeights[0][0]); + AllocateAndCopyDataToITensorHandle(&inputToCellWeightsTensor, &inputToCellWeights[0][0]); + AllocateAndCopyDataToITensorHandle(&inputToOutputWeightsTensor, &inputToOutputWeights[0][0]); + + AllocateAndCopyDataToITensorHandle(&recurrentToForgetWeightsTensor, &recurrentToForgetWeights[0][0]); + AllocateAndCopyDataToITensorHandle(&recurrentToCellWeightsTensor, &recurrentToCellWeights[0][0]); + AllocateAndCopyDataToITensorHandle(&recurrentToOutputWeightsTensor, &recurrentToOutputWeights[0][0]); + + AllocateAndCopyDataToITensorHandle(&forgetGateBiasTensor, &forgetGateBias[0]); + AllocateAndCopyDataToITensorHandle(&cellBiasTensor, &cellBias[0]); + AllocateAndCopyDataToITensorHandle(&outputGateBiasTensor, &outputGateBias[0]); + + AllocateAndCopyDataToITensorHandle(&forgetLayerNormWeightsTensor, &forgetLayerNormWeights[0]); + AllocateAndCopyDataToITensorHandle(&cellLayerNormWeightsTensor, &cellLayerNormWeights[0]); + AllocateAndCopyDataToITensorHandle(&outputLayerNormWeightsTensor, &outputLayerNormWeights[0]); + + // Setup queue descriptor + data.m_InputToForgetWeights = &inputToForgetWeightsTensor; + data.m_InputToCellWeights = &inputToCellWeightsTensor; + data.m_InputToOutputWeights = &inputToOutputWeightsTensor; + + data.m_RecurrentToForgetWeights = &recurrentToForgetWeightsTensor; + data.m_RecurrentToCellWeights = &recurrentToCellWeightsTensor; + data.m_RecurrentToOutputWeights = &recurrentToOutputWeightsTensor; + + data.m_ForgetGateBias = &forgetGateBiasTensor; + data.m_CellBias = &cellBiasTensor; + data.m_OutputGateBias = &outputGateBiasTensor; + + data.m_ForgetLayerNormWeights = &forgetLayerNormWeightsTensor; + data.m_CellLayerNormWeights = &cellLayerNormWeightsTensor; + data.m_OutputLayerNormWeights = &outputLayerNormWeightsTensor; + + data.m_Parameters.m_CifgEnabled = cifgEnabled; + data.m_Parameters.m_PeepholeEnabled = peepholeEnabled; + data.m_Parameters.m_ProjectionEnabled = projectionEnabled; + data.m_Parameters.m_LayerNormEnabled = layerNormEnabled; + + data.m_Parameters.m_InputIntermediateScale = inputIntermediateScale; + data.m_Parameters.m_ForgetIntermediateScale = forgetIntermediateScale; + data.m_Parameters.m_CellIntermediateScale = cellIntermediateScale; + data.m_Parameters.m_OutputIntermediateScale = outputIntermediateScale; + + data.m_Parameters.m_HiddenStateZeroPoint = hiddenStateZeroPoint; + data.m_Parameters.m_HiddenStateScale = hiddenStateScale; + + data.m_Parameters.m_CellClip = cellClip; + data.m_Parameters.m_ProjectionClip = projectionClip; + + // Create workload and allocate tensor handles + std::unique_ptr<armnn::IWorkload> workload = workloadFactory.CreateQLstm(data, info); + inputHandle->Allocate(); + outputStateInHandle->Allocate(); + cellStateInHandle->Allocate(); + + outputStateOutHandle->Allocate(); + cellStateOutHandle->Allocate(); + outputHandle->Allocate(); + + CopyDataToITensorHandle(inputHandle.get(), &inputTensor[0][0]); + CopyDataToITensorHandle(outputStateInHandle.get(), &outputStateInTensor[0][0]); + CopyDataToITensorHandle(cellStateInHandle.get(), &cellStateInTensor[0][0]); + + workload->Execute(); + + CopyDataFromITensorHandle(&ret.output[0][0], outputHandle.get()); + + return ret; +} + + } // anonymous namespace #if defined(ARMNNREF_ENABLED) @@ -2107,3 +2344,19 @@ LayerTestResult<uint8_t, 2> QuantizedLstmTest( return QuantizedLstmTestImpl(workloadFactory, memoryManager, input, expectedOutput); } + +// QLSTM +LayerTestResult<int8_t, 2> QLstmTest( + armnn::IWorkloadFactory& workloadFactory, + const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager) +{ + armnn::TensorInfo inputDesc({2, 5}, armnn::DataType::QAsymmS8); + boost::multi_array<int8_t, 2> input = MakeTensor<int8_t, 2>(inputDesc, std::vector<int8_t>( + {90, 102, 13, 26, 38, 102, 13, 26, 51, 64})); + + armnn::TensorInfo outputDesc({2, 4}, armnn::DataType::QAsymmS8); + boost::multi_array<int8_t, 2> expectedOutput = MakeTensor<int8_t, 2>(outputDesc, std::vector<int8_t>( + {-15, 21, 14, 20, -15, 15, 5, 27})); + + return QLstmTestImpl(workloadFactory, memoryManager, input, expectedOutput); +} diff --git a/src/backends/backendsCommon/test/layerTests/LstmTestImpl.hpp b/src/backends/backendsCommon/test/layerTests/LstmTestImpl.hpp index dad1760c65..f1180aee16 100644 --- a/src/backends/backendsCommon/test/layerTests/LstmTestImpl.hpp +++ b/src/backends/backendsCommon/test/layerTests/LstmTestImpl.hpp @@ -58,3 +58,11 @@ LayerTestResult<int16_t, 2> LstmLayerInt16NoCifgNoPeepholeNoProjectionInt16Const LayerTestResult<uint8_t, 2> QuantizedLstmTest( armnn::IWorkloadFactory& workloadFactory, const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); + +// +// QLstm +// + +LayerTestResult<int8_t, 2> QLstmTest( + armnn::IWorkloadFactory& workloadFactory, + const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager); |