bool HasMultipleQuantizationScales() const { return m_Quantization.m_Scales.size() > 1; }
+ bool HasPerAxisQuantization() const;
+
std::vector<float> GetQuantizationScales() const;
void SetQuantizationScales(const std::vector<float>& scales);
{
switch (dataType)
{
- case DataType::Float16: return 2U;
+ case DataType::Float16: return 2U;
case DataType::Float32:
- case DataType::Signed32: return 4U;
- case DataType::QuantisedAsymm8: return 1U;
- case DataType::QuantisedSymm16: return 2U;
- case DataType::Boolean: return 1U;
- default: return 0U;
+ case DataType::Signed32: return 4U;
+ case DataType::QuantisedAsymm8: return 1U;
+ case DataType::QuantizedSymm8PerAxis: return 1U;
+ case DataType::QuantisedSymm16: return 2U;
+ case DataType::Boolean: return 1U;
+ default: return 0U;
}
}
return match;
}
+bool TensorInfo::HasPerAxisQuantization() const
+{
+ return HasMultipleQuantizationScales() || m_Quantization.m_QuantizationDim.has_value();
+}
+
std::vector<float> TensorInfo::GetQuantizationScales() const
{
return m_Quantization.m_Scales;
{
unsigned int numDim = shape.GetNumDimensions();
BOOST_ASSERT(0 >= axis);
- BOOST_ASSERT(axis < numDim - 1);
+ BOOST_ASSERT(axis <= numDim - 1);
unsigned int count = 1;
for (unsigned int i = axis; i < numDim; i++)
{
{
const std::vector<float>& scales = info.GetQuantizationScales();
armnn::Optional<unsigned int> quantizationDim = info.GetQuantizationDim();
- if (scales.size() < 1 || !quantizationDim.has_value())
+ if (!info.HasPerAxisQuantization())
{
throw armnn::InvalidArgumentException(
std::string("Per-axis quantization params not set for tensor of type ") +
return { axisFactor, scales };
}
-
} // namespace armnnUtils
}
}
+void ValidateWeightDataType(const TensorInfo& inputInfo,
+ const TensorInfo& weightInfo,
+ const std::string& descName)
+{
+ const DataType inputType = inputInfo.GetDataType();
+ if (inputType == DataType::QuantisedAsymm8)
+ {
+ const std::vector<DataType> validTypes =
+ {
+ DataType::QuantisedAsymm8,
+ DataType::QuantizedSymm8PerAxis
+ };
+
+ ValidateDataTypes(weightInfo, validTypes, descName);
+ }
+ else
+ {
+ ValidateTensorDataTypesMatch(inputInfo, weightInfo, descName, "input", "weight");
+ }
+}
+
+void ValidatePerAxisQuantizationDimension(const TensorInfo& tensorInfo,
+ const std::string& descName,
+ const std::string& tensorName)
+{
+ const Optional<unsigned int>& quantizationDim = tensorInfo.GetQuantizationDim();
+ if (!quantizationDim.has_value())
+ {
+ throw InvalidArgumentException(boost::str(
+ boost::format("%1%: Quantization dimension for per-axis quantization not set on tensor %2%.")
+ % descName % tensorName));
+ }
+
+ if (quantizationDim.value() != 0)
+ {
+ throw InvalidArgumentException(boost::str(
+ boost::format("%1%: Quantization dimension for per-axis quantization expected to be 0 on tensor %2%, "
+ "but got: %3%") % descName % tensorName % quantizationDim.value()));
+ }
+}
+
+void ValidatePerAxisQuantizationOffset(const TensorInfo& tensorInfo,
+ const std::string& descName,
+ const std::string& tensorName)
+{
+ int32_t quantizationOffset = tensorInfo.GetQuantizationOffset();
+ if (quantizationOffset != 0)
+ {
+ throw InvalidArgumentException(boost::str(
+ boost::format("%1%: Quantization offset for per-axis quantization expected to be 0 on tensor %2%, "
+ "but got: %3%") % descName % tensorName % quantizationOffset));
+ }
+}
+
+void ValidatePerAxisQuantization(const TensorInfo& inputInfo,
+ const TensorInfo& outputInfo,
+ const TensorInfo& weightInfo,
+ const Optional<TensorInfo>& optionalBiasInfo,
+ const std::string& descName)
+{
+ if (weightInfo.HasPerAxisQuantization())
+ {
+ const DataType inputDataType = inputInfo.GetDataType();
+ const DataType outputDataType = outputInfo.GetDataType();
+
+ const bool canHavePerAxisQuantization =
+ inputDataType == DataType::QuantisedAsymm8 && inputDataType == outputDataType;
+
+ if (!canHavePerAxisQuantization)
+ {
+ throw InvalidArgumentException(boost::str(
+ boost::format("%1%: Per-axis quantization parameters set on tensor %2%, "
+ "but data type does not support per-axis quantization.") % descName % "weight"));
+ }
+
+ ValidateTensorDataType(weightInfo, DataType::QuantizedSymm8PerAxis, descName, "weight");
+ ValidatePerAxisQuantizationDimension(weightInfo, descName, "weight");
+ ValidatePerAxisQuantizationOffset(weightInfo, descName, "weight");
+
+ if (optionalBiasInfo.has_value())
+ {
+ const TensorInfo& biasInfo = optionalBiasInfo.value();
+ if (!biasInfo.HasPerAxisQuantization())
+ {
+ throw InvalidArgumentException(boost::str(
+ boost::format("%1%: Per-axis quantization parameters not set on bias tensor, despite being set on "
+ "weight tensor.") % descName));
+ }
+
+ ValidateTensorDataType(biasInfo, DataType::Signed32, descName, "bias");
+ ValidatePerAxisQuantizationDimension(biasInfo, descName, "bias");
+ ValidatePerAxisQuantizationOffset(biasInfo, descName, "bias");
+ }
+ }
+}
+
} // anonymous namespace
void QueueDescriptor::ValidateInputsOutputs(const std::string& descName,
const TensorInfo& weightTensorInfo = m_Weight->GetTensorInfo();
ValidateTensorNumDimensions(weightTensorInfo, descriptorName, 4, "weight");
- ValidateTensorDataTypesMatch(inputTensorInfo, weightTensorInfo, descriptorName, "input", "weight");
+ ValidateWeightDataType(inputTensorInfo, weightTensorInfo, descriptorName);
+ Optional<TensorInfo> optionalBiasTensorInfo;
if (m_Parameters.m_BiasEnabled)
{
ValidatePointer(m_Bias, descriptorName, "bias");
- const TensorInfo& biasTensorInfo = m_Bias->GetTensorInfo();
- ValidateTensorNumDimensions(biasTensorInfo, descriptorName, 1, "bias");
+ optionalBiasTensorInfo = MakeOptional<TensorInfo>(m_Bias->GetTensorInfo());
+ const TensorInfo& biasTensorInfo = optionalBiasTensorInfo.value();
ValidateTensorDataType(biasTensorInfo, GetBiasDataType(inputTensorInfo.GetDataType()), descriptorName, "bias");
ValidateBiasTensorQuantization(biasTensorInfo, inputTensorInfo, weightTensorInfo, descriptorName);
}
+ ValidatePerAxisQuantization(inputTensorInfo,
+ outputTensorInfo,
+ weightTensorInfo,
+ optionalBiasTensorInfo,
+ descriptorName);
+
std::vector<DataType> supportedTypes =
{
DataType::Float32,
const TensorShape weightShape{ cOutput, cInput, hInput, wInput };
const TensorShape biasShape { cOutput };
- constexpr DataType dataType = DataType::QuantisedAsymm8;
- constexpr DataType biasType = DataType::Signed32;
+ constexpr DataType inputType = DataType::QuantisedAsymm8;
+ constexpr DataType weightType = DataType::QuantizedSymm8PerAxis;
+ constexpr DataType biasType = DataType::Signed32;
constexpr float perTensorScale = 1.5f;
- const TensorInfo inputInfo (inputShape, dataType, perTensorScale);
- const TensorInfo outputInfo(outputShape, dataType, perTensorScale);
+ const TensorInfo inputInfo (inputShape, inputType, perTensorScale);
+ const TensorInfo outputInfo(outputShape, inputType, perTensorScale);
const std::vector<float> weightPerAxisScales = { 2.50f, 3.50f };
- const TensorInfo weightInfo(weightShape, dataType, weightPerAxisScales, 0);
+ const TensorInfo weightInfo(weightShape, weightType, weightPerAxisScales, 0);
Convolution2dQueueDescriptor queueDescriptor;
queueDescriptor.m_Parameters.m_BiasEnabled = true;
#include <backendsCommon/CpuTensorHandle.hpp>
+#include <backendsCommon/test/DataLayoutUtils.hpp>
#include <backendsCommon/test/TensorCopyUtils.hpp>
#include <backendsCommon/test/WorkloadTestUtils.hpp>
workloadFactory, memoryManager, 0.1f, 128, biasEnabled);
}
+LayerTestResult<uint8_t, 4> Convolution2dPerAxisQuantTest(
+ armnn::IWorkloadFactory& workloadFactory,
+ const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
+ const armnn::DataLayout layout)
+{
+ using namespace armnn;
+
+ const DataType inputType = DataType::QuantisedAsymm8;
+ const DataType kernelType = DataType::QuantizedSymm8PerAxis;
+ const DataType biasType = DataType::Signed32;
+
+ TensorInfo inputInfo ({ 1, 3, 1, 2 }, inputType, 0.5f, 128);
+ TensorInfo outputInfo({ 1, 3, 1, 3 }, inputType, 1.0f, 128);
+
+ const std::vector<float> quantScales{ 0.5f, 0.75f, 1.0f };
+ constexpr unsigned int quantDimension = 0;
+
+ TensorInfo kernelInfo({ 3, 1, 1, 2 }, kernelType, quantScales, quantDimension);
+
+ const std::vector<float> biasQuantScales{ 0.25f, 0.375f, 0.5f };
+ TensorInfo biasInfo({ 3 }, biasType, biasQuantScales, quantDimension);
+
+ std::vector<uint8_t> inputData =
+ {
+ 138, 108, 138, 108, 138, 108
+ };
+
+ std::vector<int8_t> kernelData =
+ {
+ 1, 2, 1, 2, 1, 2
+ };
+
+ std::vector<int32_t> biasData =
+ {
+ 4, 4, 4
+ };
+
+ std::vector<uint8_t> expectedOutputData =
+ {
+ 121, 118, 115, 121, 118, 115, 121, 118, 115
+ };
+
+ if (layout == DataLayout::NCHW)
+ {
+ PermuteTensorNhwcToNchw(inputInfo, inputData);
+ PermuteTensorNhwcToNchw(kernelInfo, kernelData);
+ PermuteTensorNhwcToNchw(outputInfo, expectedOutputData);
+ }
+
+ Convolution2dDescriptor descriptor;
+ descriptor.m_StrideX = 1;
+ descriptor.m_StrideY = 1;
+ descriptor.m_PadLeft = 0;
+ descriptor.m_PadRight = 0;
+ descriptor.m_PadTop = 0;
+ descriptor.m_PadBottom = 0;
+ descriptor.m_BiasEnabled = true;
+ descriptor.m_DataLayout = layout;
+
+ std::unique_ptr<ITensorHandle> inputHandle = workloadFactory.CreateTensorHandle(inputInfo);
+ std::unique_ptr<ITensorHandle> outputHandle = workloadFactory.CreateTensorHandle(outputInfo);
+
+ WorkloadInfo workloadInfo;
+ ScopedCpuTensorHandle weightTensor(kernelInfo);
+ ScopedCpuTensorHandle biasTensor(biasInfo);
+
+ AllocateAndCopyDataToITensorHandle(&weightTensor, kernelData.data());
+ AllocateAndCopyDataToITensorHandle(&biasTensor, biasData.data());
+
+ Convolution2dQueueDescriptor queueDescriptor;
+ queueDescriptor.m_Parameters = descriptor;
+ queueDescriptor.m_Weight = &weightTensor;
+ queueDescriptor.m_Bias = &biasTensor;
+
+ AddInputToWorkload(queueDescriptor, workloadInfo, inputInfo, inputHandle.get());
+ AddOutputToWorkload(queueDescriptor, workloadInfo, outputInfo, outputHandle.get());
+
+ std::unique_ptr<IWorkload> workload = workloadFactory.CreateConvolution2d(queueDescriptor, workloadInfo);
+ inputHandle->Allocate();
+ outputHandle->Allocate();
+
+ CopyDataToITensorHandle(inputHandle.get(), inputData.data());
+
+ ExecuteWorkload(*workload, memoryManager);
+
+ LayerTestResult<uint8_t, 4> ret(outputInfo);
+ CopyDataFromITensorHandle(ret.output.origin(), outputHandle.get());
+ ret.outputExpected = MakeTensor<uint8_t, 4>(outputInfo, expectedOutputData);
+
+ return ret;
+}
+
LayerTestResult<float,4> CompareConvolution2dTest(
armnn::IWorkloadFactory& workloadFactory,
const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
armnn::IWorkloadFactory& refWorkloadFactory);
+LayerTestResult<uint8_t, 4> Convolution2dPerAxisQuantTest(
+ armnn::IWorkloadFactory& workloadFactory,
+ const armnn::IBackendInternal::IMemoryManagerSharedPtr& memoryManager,
+ const armnn::DataLayout layout);
+
//
// DepthwiseConvolution2d
//
bool supported = true;
// Define supported types.
- std::array<DataType,4> supportedTypes = {
- DataType::Float32,
- DataType::Float16,
- DataType::QuantisedAsymm8,
- DataType::QuantisedSymm16
+ std::array<DataType,4> supportedTypes =
+ {
+ DataType::Float32,
+ DataType::Float16,
+ DataType::QuantisedAsymm8,
+ DataType::QuantisedSymm16
};
supported &= CheckSupportRule(TypeAnyOf(input, supportedTypes), reasonIfUnsupported,
supported &= CheckSupportRule(TypeAnyOf(output, supportedTypes), reasonIfUnsupported,
"Reference convolution2d: output is not a supported type.");
- supported &= CheckSupportRule(TypeAnyOf(weights, supportedTypes), reasonIfUnsupported,
- "Reference convolution2d: weights is not a supported type.");
-
supported &= CheckSupportRule(TypesAreEqual(input, output), reasonIfUnsupported,
"Reference convolution2d: input and output types mismatched.");
- supported &= CheckSupportRule(TypesAreEqual(input, weights), reasonIfUnsupported,
- "Reference convolution2d: input and weights types mismatched.");
+ const DataType inputType = input.GetDataType();
+ if (inputType == DataType::QuantisedAsymm8)
+ {
+ std::array<DataType, 2> supportedWeightTypes =
+ {
+ DataType::QuantisedAsymm8,
+ DataType::QuantizedSymm8PerAxis
+ };
+
+ supported &= CheckSupportRule(TypeAnyOf(weights, supportedWeightTypes), reasonIfUnsupported,
+ "Reference convolution2d: weights type not supported for quantized input.");
+ }
+ else
+ {
+ supported &= CheckSupportRule(TypeAnyOf(weights, supportedTypes), reasonIfUnsupported,
+ "Reference convolution2d: weights is not a supported type.");
+
+ supported &= CheckSupportRule(TypesAreEqual(input, weights), reasonIfUnsupported,
+ "Reference convolution2d: input and weights types mismatched.");
+ }
if (biases.has_value())
{
- std::array<DataType,3> biasesSupportedTypes = {
- DataType::Float32,
- DataType::Float16,
- DataType::Signed32
+ std::array<DataType,3> biasesSupportedTypes =
+ {
+ DataType::Float32,
+ DataType::Float16,
+ DataType::Signed32
};
+
supported &= CheckSupportRule(TypeAnyOf(biases.value(), biasesSupportedTypes), reasonIfUnsupported,
"Reference convolution2d: biases is not a supported type.");
}
false,
DataLayout::NHWC)
+ARMNN_AUTO_TEST_CASE(Convolution2dPerAxisQuantTestNchw, Convolution2dPerAxisQuantTest, DataLayout::NCHW);
+ARMNN_AUTO_TEST_CASE(Convolution2dPerAxisQuantTestNhwc, Convolution2dPerAxisQuantTest, DataLayout::NHWC);
// Depthwise Convolution
ARMNN_AUTO_TEST_CASE(DepthwiseConvolution2d, DepthwiseConvolution2dTest, true, DataLayout::NCHW)
#include <ResolveType.hpp>
#include <boost/assert.hpp>
+#include <boost/core/ignore_unused.hpp>
namespace armnn
{
virtual ~BaseIterator() {}
+ virtual BaseIterator& SetIndex(unsigned int index, unsigned int axisIndex = 0) = 0;
+
virtual BaseIterator& operator++() = 0;
virtual BaseIterator& operator+=(const unsigned int increment) = 0;
return *this;
}
+ TypedIterator& SetIndex(unsigned int index, unsigned int axisIndex = 0) override
+ {
+ boost::ignore_unused(axisIndex);
+ BOOST_ASSERT(m_Iterator);
+ m_Iterator = m_Start + index;
+ return *this;
+ }
+
protected:
T* m_Iterator;
T* m_Start;
{}
// This should be called to set index for per-axis Encoder/Decoder
- PerAxisIterator& SetIndex(unsigned int index, unsigned int axisIndex)
+ PerAxisIterator& SetIndex(unsigned int index, unsigned int axisIndex) override
{
BOOST_ASSERT(m_Iterator);
m_Iterator = m_Start + index;
}
}
- rFilterDecoder[filterIndex];
+ rFilterDecoder.SetIndex(filterIndex, cOutput);
float filterValue = rFilterDecoder.Get();
unsigned int yInput = yOutput * yStride + yFilter * yDilation;
if (biasEnabled)
{
- (*pBiasDecoder)[cOutput];
+ (*pBiasDecoder).SetIndex(cOutput, cOutput);
sum += pBiasDecoder->Get();
}
}
}
-} //namespace armnn
+} // namespace armnn