aboutsummaryrefslogtreecommitdiff
path: root/include/armnn/TypesUtils.hpp
blob: a851b66b283e1916c37dfe988359baf4b1d4cbfc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
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 <cmath>
#include <ostream>
#include <boost/assert.hpp>
#include <boost/numeric/conversion/cast.hpp>

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 <int N>
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 <typename T>
constexpr DataType GetDataType();

template <>
constexpr DataType GetDataType<float>()
{
    return DataType::Float32;
}

template <>
constexpr DataType GetDataType<uint8_t>()
{
    return DataType::QuantisedAsymm8;
}

template <>
constexpr DataType GetDataType<int32_t>()
{
    return DataType::Signed32;
}

template<typename T>
constexpr bool IsQuantizedType()
{
    return std::is_integral<T>::value;
}


template<DataType DT>
struct ResolveTypeImpl;

template<>
struct ResolveTypeImpl<DataType::QuantisedAsymm8>
{
    using Type = uint8_t;
};

template<>
struct ResolveTypeImpl<DataType::Float32>
{
    using Type = float;
};

template<DataType DT>
using ResolveType = typename ResolveTypeImpl<DT>::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<typename QuantizedType>
inline QuantizedType Quantize(float value, float scale, int32_t offset)
{
    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);
    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 <typename QuantizedType>
inline float Dequantize(QuantizedType value, float scale, int32_t offset)
{
    static_assert(IsQuantizedType<QuantizedType>(), "Not an integer type.");
    BOOST_ASSERT(scale != 0.f);
    float dequantized = boost::numeric_cast<float>(value - offset) * scale;
    return dequantized;
}

} //namespace armnn