From 0063380ca6e43d04722707c707e610b59e1f8dde Mon Sep 17 00:00:00 2001 From: Michel Iwaniec Date: Thu, 12 Oct 2017 14:14:15 +0100 Subject: IVGCVSW-619: Support for Cl u8 bounded Relu Change-Id: I3c39ecbd36f06d5376c35ed4eb38dd73533ef97e Reviewed-on: http://mpd-gerrit.cambridge.arm.com/93686 Tested-by: Kaizen Reviewed-by: Anthony Barbier --- arm_compute/core/ITensorInfo.h | 12 +++++++++++ arm_compute/core/SubTensorInfo.h | 10 ++++++++++ arm_compute/core/TensorInfo.h | 43 ++++++++++++++++++++++++++++++---------- arm_compute/core/Types.h | 41 ++++++++++++++++++++++++++++++++++++++ arm_compute/core/Utils.h | 2 ++ 5 files changed, 97 insertions(+), 11 deletions(-) (limited to 'arm_compute') diff --git a/arm_compute/core/ITensorInfo.h b/arm_compute/core/ITensorInfo.h index bb3ac6e35e..09351522dd 100644 --- a/arm_compute/core/ITensorInfo.h +++ b/arm_compute/core/ITensorInfo.h @@ -190,6 +190,18 @@ public: * @param[in] valid_region Valid region to set. */ virtual void set_valid_region(ValidRegion valid_region) = 0; + + /** Get the quantization settings (scale and offset) of the tensor. + * + * @return A QuantizationInfo containing the scale and offset. + */ + virtual QuantizationInfo quantization_info() const = 0; + + /** Set the quantization settings (scale and offset) of the tensor. + * + * @param[in] quantization_info QuantizationInfo containing the scale and offset. + */ + virtual void set_quantization_info(QuantizationInfo quantization_info) = 0; }; } #endif /*__ARM_COMPUTE_TENSORINFO_H__ */ diff --git a/arm_compute/core/SubTensorInfo.h b/arm_compute/core/SubTensorInfo.h index 81a27026e7..3a88ebae5a 100644 --- a/arm_compute/core/SubTensorInfo.h +++ b/arm_compute/core/SubTensorInfo.h @@ -186,6 +186,16 @@ public: } _valid_region = std::move(valid_region); } + QuantizationInfo quantization_info() const override + { + ARM_COMPUTE_ERROR_ON(_parent == nullptr); + return _parent->quantization_info(); + } + void set_quantization_info(QuantizationInfo quantization_info) override + { + ARM_COMPUTE_ERROR_ON(_parent == nullptr); + _parent->set_quantization_info(quantization_info); + } private: ITensorInfo *_parent; diff --git a/arm_compute/core/TensorInfo.h b/arm_compute/core/TensorInfo.h index 35b9ccb9ff..5d1ee7c578 100644 --- a/arm_compute/core/TensorInfo.h +++ b/arm_compute/core/TensorInfo.h @@ -26,6 +26,7 @@ #include "arm_compute/core/ITensorInfo.h" +#include "ITensorInfo.h" #include "arm_compute/core/Coordinates.h" #include "arm_compute/core/Strides.h" #include "arm_compute/core/TensorShape.h" @@ -97,6 +98,16 @@ public: * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16. */ TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0); + + /** Constructor + * + * @param[in] tensor_shape It specifies the size for each dimension of the tensor in number of elements. + * @param[in] num_channels It indicates the number of channels for each tensor element + * @param[in] data_type Data type to use for each tensor element + * @param[in] quantization_info The quantization settings for the tensor data. + */ + TensorInfo(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, QuantizationInfo quantization_info); + /** Constructor * * @param[in] hog_info HOG's metadata used to allocate normalized HOG space @@ -147,6 +158,7 @@ public: * @param[in] fixed_point_position (Optional) Fixed point position that expresses the number of bits for the fractional part of the number when the tensor's data type is QS8 or QS16. */ void init(const TensorShape &tensor_shape, size_t num_channels, DataType data_type, int fixed_point_position = 0); + /** Initialize the metadata structure with the given parameters * * @param[in] tensor_shape Size for each dimension of the tensor in number of elements. @@ -276,6 +288,14 @@ public: { _valid_region = std::move(valid_region); } + QuantizationInfo quantization_info() const override + { + return _quantization_info; + } + void set_quantization_info(QuantizationInfo quantization_info) override + { + _quantization_info = quantization_info; + } private: /** Calculates strides, offset and total size resulting from the specified padding around the XY plane. @@ -284,17 +304,18 @@ private: */ std::tuple calculate_padding_requirements(const PaddingSize &padding); - size_t _total_size; - int _fixed_point_position; - size_t _offset_first_element_in_bytes; - Strides _strides_in_bytes; - size_t _num_channels; - TensorShape _tensor_shape; - DataType _data_type; - Format _format; - bool _is_resizable; - ValidRegion _valid_region; - PaddingSize _padding; + size_t _total_size; + int _fixed_point_position; + size_t _offset_first_element_in_bytes; + Strides _strides_in_bytes; + size_t _num_channels; + TensorShape _tensor_shape; + DataType _data_type; + Format _format; + bool _is_resizable; + ValidRegion _valid_region; + PaddingSize _padding; + QuantizationInfo _quantization_info; }; } #endif /*__ARM_COMPUTE_TENSORINFO_H__ */ diff --git a/arm_compute/core/Types.h b/arm_compute/core/Types.h index f52dd12597..e567bac860 100644 --- a/arm_compute/core/Types.h +++ b/arm_compute/core/Types.h @@ -67,6 +67,7 @@ enum class DataType U8, S8, QS8, + QASYMM8, U16, S16, QS16, @@ -90,6 +91,46 @@ constexpr float SCALE_PYRAMID_HALF = 0.5f; /* Constant value used to indicate a ORB scaled pyramid */ constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01; +/** Quantization settings (used for QASYMM8 data type) */ +struct QuantizationInfo +{ + QuantizationInfo() + : scale(0.0f), offset(0) + { + } + + QuantizationInfo(float scale, int offset) + : scale(scale), offset(offset) + { + } + + float scale; /**< scale */ + int offset; /**< offset */ + + /** Quantizes a value using the scale/offset in this QuantizationInfo */ + uint8_t quantize(float value) const + { + ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::quantize: scale == 0"); + int quantized = static_cast(value / scale + offset); + quantized = std::max(0, std::min(quantized, 255)); + return quantized; + } + + /** Dequantizes a value using the scale/offset in this QuantizationInfo */ + float dequantize(uint8_t value) const + { + ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::dequantize: scale == 0"); + float dequantized = (value - offset) * scale; + return dequantized; + } + + /** Indicates whether this QuantizationInfo has valid settings or not */ + bool empty() const + { + return scale == 0; + } +}; + struct ValidRegion { ValidRegion() diff --git a/arm_compute/core/Utils.h b/arm_compute/core/Utils.h index 7f53bec2c5..149e404f5b 100644 --- a/arm_compute/core/Utils.h +++ b/arm_compute/core/Utils.h @@ -92,6 +92,7 @@ inline size_t data_size_from_type(DataType data_type) case DataType::U8: case DataType::S8: case DataType::QS8: + case DataType::QASYMM8: return 1; case DataType::U16: case DataType::S16: @@ -166,6 +167,7 @@ inline size_t element_size_from_data_type(DataType dt) case DataType::S8: case DataType::U8: case DataType::QS8: + case DataType::QASYMM8: return 1; case DataType::U16: case DataType::S16: -- cgit v1.2.1