aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/TypesUtils.hpp
diff options
context:
space:
mode:
authorFrancis Murtagh <francis.murtagh@arm.com>2019-02-22 16:35:13 +0000
committerFrancis Murtagh <francis.murtagh@arm.com>2019-02-25 09:46:01 +0000
commit0d0a78ebd60e058746ea161914b45709245d4e1b (patch)
tree0220a0e885a81dd33e3e770f9dc7c02091eb4159 /include/armnn/TypesUtils.hpp
parentd1ae3a632af09cd065d983a51c40916e2202121d (diff)
downloadarmnn-0d0a78ebd60e058746ea161914b45709245d4e1b.tar.gz
IVGCVSW-2721 Quantize and Dequantize aren't quite right
* Add check for infinity and negative infinity in quantize() * Add assert for NaN value in quantize and dequantize() * Add unit tests for infinity and negative infinity Change-Id: Ie60e1e15b289ccbf99df4a3281f067b82cc9f9bf Signed-off-by: Francis Murtagh <francis.murtagh@arm.com>
Diffstat (limited to 'include/armnn/TypesUtils.hpp')
-rw-r--r--include/armnn/TypesUtils.hpp15
1 files changed, 7 insertions, 8 deletions
diff --git a/include/armnn/TypesUtils.hpp b/include/armnn/TypesUtils.hpp
index c65eefc510..e7652649b0 100644
--- a/include/armnn/TypesUtils.hpp
+++ b/include/armnn/TypesUtils.hpp
@@ -189,18 +189,16 @@ inline std::ostream & operator<<(std::ostream & os, const armnn::TensorShape & s
template<typename QuantizedType>
inline QuantizedType Quantize(float value, float scale, int32_t offset)
{
- // TODO : check we act sensibly for Inf, NaN and -Inf
- // see IVGCVSW-1849
static_assert(IsQuantizedType<QuantizedType>(), "Not an integer type.");
constexpr QuantizedType max = std::numeric_limits<QuantizedType>::max();
constexpr QuantizedType min = std::numeric_limits<QuantizedType>::lowest();
BOOST_ASSERT(scale != 0.f);
- int quantized = boost::numeric_cast<int>(round(value / scale)) + offset;
- QuantizedType quantizedBits = quantized <= min
- ? min
- : quantized >= max
- ? max
- : static_cast<QuantizedType>(quantized);
+ BOOST_ASSERT(!std::isnan(value));
+
+ float clampedValue = std::min(std::max(static_cast<float>(round(value/scale) + offset), static_cast<float>(min)),
+ static_cast<float>(max));
+ auto quantizedBits = static_cast<QuantizedType>(clampedValue);
+
return quantizedBits;
}
@@ -215,6 +213,7 @@ inline float Dequantize(QuantizedType value, float scale, int32_t offset)
{
static_assert(IsQuantizedType<QuantizedType>(), "Not an integer type.");
BOOST_ASSERT(scale != 0.f);
+ BOOST_ASSERT(!std::isnan(value));
float dequantized = boost::numeric_cast<float>(value - offset) * scale;
return dequantized;
}