From 4fcda0101ec3d110c1d6d7bee5c83416b645528a Mon Sep 17 00:00:00 2001 From: telsoa01 Date: Fri, 9 Mar 2018 14:13:49 +0000 Subject: Release 18.02 Change-Id: Id3c11dc5ee94ef664374a988fcc6901e9a232fa6 --- include/armnn/TypesUtils.hpp | 182 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 182 insertions(+) create mode 100644 include/armnn/TypesUtils.hpp (limited to 'include/armnn/TypesUtils.hpp') diff --git a/include/armnn/TypesUtils.hpp b/include/armnn/TypesUtils.hpp new file mode 100644 index 0000000000..a851b66b28 --- /dev/null +++ b/include/armnn/TypesUtils.hpp @@ -0,0 +1,182 @@ +// +// Copyright © 2017 Arm Ltd. All rights reserved. +// See LICENSE file in the project root for full license information. +// +#pragma once + +#include "Types.hpp" +#include +#include +#include +#include + +namespace armnn +{ + +constexpr char const* GetStatusAsCString(Status compute) +{ + switch (compute) + { + case armnn::Status::Success: return "Status::Success"; + case armnn::Status::Failure: return "Status::Failure"; + default: return "Unknown"; + } +} + +constexpr char const* GetComputeDeviceAsCString(Compute compute) +{ + switch (compute) + { + case armnn::Compute::CpuRef: return "CpuRef"; + case armnn::Compute::CpuAcc: return "CpuAcc"; + case armnn::Compute::GpuAcc: return "GpuAcc"; + default: return "Unknown"; + } +} + +constexpr unsigned int GetDataTypeSize(DataType dataType) +{ + switch (dataType) + { + case DataType::Signed32: + case DataType::Float32: return 4U; + case DataType::QuantisedAsymm8: return 1U; + default: return 0U; + } +} + +template +constexpr bool StrEqual(const char* strA, const char (&strB)[N]) +{ + bool isEqual = true; + for (int i = 0; isEqual && (i < N); ++i) + { + isEqual = (strA[i] == strB[i]); + } + return isEqual; +} + +constexpr Compute ParseComputeDevice(const char* str) +{ + if (StrEqual(str, "CpuAcc")) + { + return armnn::Compute::CpuAcc; + } + else if (StrEqual(str, "CpuRef")) + { + return armnn::Compute::CpuRef; + } + else if (StrEqual(str, "GpuAcc")) + { + return armnn::Compute::GpuAcc; + } + else + { + return armnn::Compute::Undefined; + } +} + +constexpr const char* GetDataTypeName(DataType dataType) +{ + switch (dataType) + { + case DataType::Float32: return "Float32"; + case DataType::QuantisedAsymm8: return "Unsigned8"; + case DataType::Signed32: return "Signed32"; + default: return "Unknown"; + } +} + +template +constexpr DataType GetDataType(); + +template <> +constexpr DataType GetDataType() +{ + return DataType::Float32; +} + +template <> +constexpr DataType GetDataType() +{ + return DataType::QuantisedAsymm8; +} + +template <> +constexpr DataType GetDataType() +{ + return DataType::Signed32; +} + +template +constexpr bool IsQuantizedType() +{ + return std::is_integral::value; +} + + +template +struct ResolveTypeImpl; + +template<> +struct ResolveTypeImpl +{ + using Type = uint8_t; +}; + +template<> +struct ResolveTypeImpl +{ + using Type = float; +}; + +template +using ResolveType = typename ResolveTypeImpl
::Type; + + +inline std::ostream& operator<<(std::ostream& os, Status stat) +{ + os << GetStatusAsCString(stat); + return os; +} + +inline std::ostream& operator<<(std::ostream& os, Compute compute) +{ + os << GetComputeDeviceAsCString(compute); + return os; +} + +/// Quantize a floating point data type into an 8-bit data type +/// @param value The value to quantize +/// @param scale The scale (must be non-zero) +/// @param offset The offset +/// @return The quantized value calculated as round(value/scale)+offset +/// +template +inline QuantizedType Quantize(float value, float scale, int32_t offset) +{ + static_assert(IsQuantizedType(), "Not an integer type."); + constexpr QuantizedType max = std::numeric_limits::max(); + constexpr QuantizedType min = std::numeric_limits::lowest(); + BOOST_ASSERT(scale != 0.f); + int quantized = boost::numeric_cast(round(value / scale)) + offset; + QuantizedType quantizedBits = quantized < min ? min : quantized > max ? max : static_cast(quantized); + return quantizedBits; +} + +/// Dequantize an 8-bit data type into a floating point data type +/// @param value The value to dequantize +/// @param scale The scale (must be non-zero) +/// @param offset The offset +/// @return The dequantized value calculated as (value-offset)*scale +/// +template +inline float Dequantize(QuantizedType value, float scale, int32_t offset) +{ + static_assert(IsQuantizedType(), "Not an integer type."); + BOOST_ASSERT(scale != 0.f); + float dequantized = boost::numeric_cast(value - offset) * scale; + return dequantized; +} + +} //namespace armnn \ No newline at end of file -- cgit v1.2.1