QuantisedAsymm8 = 2,
Signed32 = 3,
Boolean = 4,
- QuantisedSymm16 = 5
+ QuantisedSymm16 = 5,
+ QuantizedSymm8PerAxis = 6
};
enum class DataLayout
return dequantized;
}
+/// Explicit specialization of Quantize for int8_t
+template
+int8_t armnn::Quantize<int8_t>(float value, float scale, int32_t offset);
+
/// Explicit specialization of Quantize for uint8_t
template
uint8_t armnn::Quantize<uint8_t>(float value, float scale, int32_t offset);
template
int32_t armnn::Quantize<int32_t>(float value, float scale, int32_t offset);
+/// Explicit specialization of Dequantize for int8_t
+template
+float armnn::Dequantize<int8_t>(int8_t value, float scale, int32_t offset);
+
/// Explicit specialization of Dequantize for uint8_t
template
float armnn::Dequantize<uint8_t>(uint8_t value, float scale, int32_t offset);
#include <armnn/TypesUtils.hpp>
+#include <boost/assert.hpp>
+
namespace armnnUtils
{
armnn::TensorShape GetTensorShape(unsigned int numberOfBatches,
unsigned int GetUnsignedAxis(const unsigned int inputDimension, const int axis);
+inline unsigned int GetNumElementsAfter(const armnn::TensorShape& shape,
+ unsigned int axis)
+{
+ unsigned int numDim = shape.GetNumDimensions();
+ BOOST_ASSERT(0 >= axis);
+ BOOST_ASSERT(axis < numDim - 1);
+ unsigned int count = 1;
+ for (unsigned int i = axis; i < numDim; i++)
+ {
+ count *= shape[i];
+ }
+ return count;
+}
+
+inline std::pair<unsigned int, std::vector<float>> GetPerAxisParams(const armnn::TensorInfo& info)
+{
+ const std::vector<float>& scales = info.GetQuantizationScales();
+ armnn::Optional<unsigned int> quantizationDim = info.GetQuantizationDim();
+ if (scales.size() < 1 || !quantizationDim.has_value())
+ {
+ throw armnn::InvalidArgumentException(
+ "We currently support only per-axis symmetric quantization for QuantizedSymm8.");
+ }
+ unsigned int axisFactor = GetNumElementsAfter(info.GetShape(), quantizationDim.value());
+
+ return {axisFactor, scales};
+}
+
} // namespace armnnUtils
}
};
-} //namespace armnn
+// PerAxisIterator for per-axis quantization
+template<typename T, typename Base>
+class PerAxisIterator : public Base
+{
+public:
+ // axisFactor is used to calculate axisIndex
+ PerAxisIterator(T* data = nullptr, unsigned int axisFactor = 0)
+ : m_Iterator(data), m_Start(data), m_AxisIndex(0), m_AxisFactor(axisFactor)
+ {}
+
+ // This should be called to set index for per-axis Encoder/Decoder
+ PerAxisIterator& SetIndex(unsigned int index, unsigned int axisIndex)
+ {
+ BOOST_ASSERT(m_Iterator);
+ m_Iterator = m_Start + index;
+ m_AxisIndex = axisIndex;
+ return *this;
+ }
+
+ void Reset(void* data) override
+ {
+ m_Iterator = reinterpret_cast<T*>(data);
+ m_Start = m_Iterator;
+ m_AxisIndex = 0;
+ }
+
+ PerAxisIterator& operator++() override
+ {
+ BOOST_ASSERT(m_Iterator);
+ ++m_Iterator;
+ m_AxisIndex = static_cast<unsigned int>(*m_Iterator) % m_AxisFactor;
+ return *this;
+ }
+
+ PerAxisIterator& operator+=(const unsigned int increment) override
+ {
+ BOOST_ASSERT(m_Iterator);
+ m_Iterator += increment;
+ m_AxisIndex = static_cast<unsigned int>(*m_Iterator) % m_AxisFactor;
+ return *this;
+ }
+
+ PerAxisIterator& operator-=(const unsigned int decrement) override
+ {
+ BOOST_ASSERT(m_Iterator);
+ m_Iterator -= decrement;
+ m_AxisIndex = static_cast<unsigned int>(*m_Iterator) % m_AxisFactor;
+ return *this;
+ }
+
+ PerAxisIterator& operator[](const unsigned int index) override
+ {
+ BOOST_ASSERT(m_Iterator);
+ m_Iterator = m_Start + index;
+ m_AxisIndex = static_cast<unsigned int>(*m_Iterator) % m_AxisFactor;
+ return *this;
+ }
+
+ protected:
+ T* m_Iterator;
+ T* m_Start;
+ unsigned int m_AxisIndex;
+ unsigned int m_AxisFactor;
+};
+
+class QSymm8PerAxisDecoder : public PerAxisIterator<const int8_t, Decoder<float>>
+{
+public:
+ QSymm8PerAxisDecoder(const int8_t* data, const std::vector<float>& scale, unsigned int axisFactor)
+ : PerAxisIterator(data, axisFactor), m_Scale(scale) {}
+
+ float Get() const override
+ {
+ return armnn::Dequantize(*m_Iterator, m_Scale[m_AxisIndex], 0);
+ }
+
+ // Get scale of the current value
+ float GetScale() const
+ {
+ return m_Scale[m_AxisIndex];
+ }
+
+private:
+ std::vector<float> m_Scale;
+};
+
+class QSymm8PerAxisEncoder : public PerAxisIterator<int8_t, Encoder<float>>
+{
+public:
+ QSymm8PerAxisEncoder(int8_t* data, const std::vector<float>& scale, unsigned int axisFactor)
+ : PerAxisIterator(data, axisFactor), m_Scale(scale) {}
+
+ void Set(float right)
+ {
+ *m_Iterator = armnn::Quantize<int8_t>(right, m_Scale[m_AxisIndex], 0);
+ }
+
+ float Get() const
+ {
+ return armnn::Dequantize(*m_Iterator, m_Scale[m_AxisIndex], 0);
+ }
+
+ // Get scale of the current value
+ float GetScale() const
+ {
+ return m_Scale[m_AxisIndex];
+ }
+
+private:
+ std::vector<float> m_Scale;
+};
+
+} //namespace armnn
\ No newline at end of file
#include "BaseIterator.hpp"
#include "FloatingPointConverter.hpp"
+#include "TensorUtils.hpp"
#include <boost/assert.hpp>
{
switch(info.GetDataType())
{
+ case armnn::DataType::QuantizedSymm8PerAxis:
+ {
+ std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
+ return std::make_unique<QSymm8PerAxisDecoder>(
+ static_cast<const int8_t*>(data),
+ params.second,
+ params.first);
+ }
case DataType::QuantisedAsymm8:
{
return std::make_unique<QASymm8Decoder>(
}
default:
{
- BOOST_ASSERT_MSG(false, "Not supported Data Type!");
+ BOOST_ASSERT_MSG(false, "Unsupported Data Type!");
break;
}
}
#pragma once
#include "BaseIterator.hpp"
+#include "TensorUtils.hpp"
#include <boost/assert.hpp>
{
switch(info.GetDataType())
{
+ case armnn::DataType::QuantizedSymm8PerAxis:
+ {
+ std::pair<unsigned int, std::vector<float>> params = armnnUtils::GetPerAxisParams(info);
+ return std::make_unique<QSymm8PerAxisEncoder>(
+ static_cast<int8_t*>(data),
+ params.second,
+ params.first);
+ }
case armnn::DataType::QuantisedAsymm8:
{
return std::make_unique<QASymm8Encoder>(
}
default:
{
- BOOST_ASSERT_MSG(false, "Cannot encode from float. Not supported target Data Type!");
+ BOOST_ASSERT_MSG(false, "Unsupported target Data Type!");
break;
}
}