Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame^] | 1 | |
| 2 | // Copyright (c) 2020, ARM Limited. |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | |
| 16 | #ifndef OP_TEMPLATE_TYPES_H |
| 17 | #define OP_TEMPLATE_TYPES_H |
| 18 | |
| 19 | #include "tosa_generated.h" |
| 20 | #include <Eigen/CXX11/Tensor> |
| 21 | |
| 22 | using namespace tosa; |
| 23 | |
| 24 | namespace TosaReference |
| 25 | { |
| 26 | // Shorter aliase templates for common Eigen::Tensor types |
| 27 | template <typename T> |
| 28 | using ETensor0 = Eigen::Tensor<T, 0>; |
| 29 | template <typename T> |
| 30 | using ETensor1 = Eigen::Tensor<T, 1>; |
| 31 | template <typename T> |
| 32 | using ETensor2 = Eigen::Tensor<T, 2>; |
| 33 | template <typename T> |
| 34 | using ETensor3 = Eigen::Tensor<T, 3>; |
| 35 | template <typename T> |
| 36 | using ETensor4 = Eigen::Tensor<T, 4>; |
| 37 | template <typename T> |
| 38 | using ETensor5 = Eigen::Tensor<T, 5>; |
| 39 | template <typename T> |
| 40 | using ETensor6 = Eigen::Tensor<T, 6>; |
| 41 | |
| 42 | // Forward declaration |
| 43 | template <class T> |
| 44 | class TensorTemplate; |
| 45 | |
| 46 | // Shortcut to hide the TensorTemplate class. |
| 47 | // For example, declare Tensor1<float> to get a TensorTemplate |
| 48 | // with an Eigen::Tensor<float, 1> |
| 49 | template <typename T> |
| 50 | using Tensor0 = TensorTemplate<ETensor0<T>>; |
| 51 | template <typename T> |
| 52 | using Tensor1 = TensorTemplate<ETensor1<T>>; |
| 53 | template <typename T> |
| 54 | using Tensor2 = TensorTemplate<ETensor2<T>>; |
| 55 | template <typename T> |
| 56 | using Tensor3 = TensorTemplate<ETensor3<T>>; |
| 57 | template <typename T> |
| 58 | using Tensor4 = TensorTemplate<ETensor4<T>>; |
| 59 | template <typename T> |
| 60 | using Tensor5 = TensorTemplate<ETensor5<T>>; |
| 61 | template <typename T> |
| 62 | using Tensor6 = TensorTemplate<ETensor6<T>>; |
| 63 | |
| 64 | template <DType type> |
| 65 | struct GetEigenType; |
| 66 | template <> |
| 67 | struct GetEigenType<DType_FLOAT> |
| 68 | { |
| 69 | using type = float; |
| 70 | }; |
| 71 | template <> |
| 72 | struct GetEigenType<DType_INT32> |
| 73 | { |
| 74 | using type = int32_t; |
| 75 | }; |
| 76 | template <> |
| 77 | struct GetEigenType<DType_INT48> |
| 78 | { |
| 79 | using type = int64_t; |
| 80 | }; |
| 81 | template <> |
| 82 | struct GetEigenType<DType_BOOL> |
| 83 | { |
| 84 | using type = bool; |
| 85 | }; |
| 86 | template <> |
| 87 | struct GetEigenType<DType_AINT8> |
| 88 | { |
| 89 | using type = int32_t; |
| 90 | }; |
| 91 | template <> |
| 92 | struct GetEigenType<DType_UINT8> |
| 93 | { |
| 94 | using type = int32_t; |
| 95 | }; |
| 96 | template <> |
| 97 | struct GetEigenType<DType_INT4> |
| 98 | { |
| 99 | using type = int32_t; |
| 100 | }; |
| 101 | template <> |
| 102 | struct GetEigenType<DType_INT8> |
| 103 | { |
| 104 | using type = int32_t; |
| 105 | }; |
| 106 | template <> |
| 107 | struct GetEigenType<DType_INT16> |
| 108 | { |
| 109 | using type = int32_t; |
| 110 | }; |
| 111 | |
| 112 | // Meta function to get number of bits |
| 113 | template <DType T> |
| 114 | struct GetNumBits |
| 115 | { |
| 116 | static constexpr int32_t value = 0; |
| 117 | }; |
| 118 | template <> |
| 119 | struct GetNumBits<DType_BOOL> |
| 120 | { |
| 121 | static constexpr int32_t value = 1; |
| 122 | }; |
| 123 | template <> |
| 124 | struct GetNumBits<DType_AINT8> |
| 125 | { |
| 126 | static constexpr int32_t value = 8; |
| 127 | }; |
| 128 | template <> |
| 129 | struct GetNumBits<DType_UINT8> |
| 130 | { |
| 131 | static constexpr int32_t value = 8; |
| 132 | }; |
| 133 | template <> |
| 134 | struct GetNumBits<DType_INT4> |
| 135 | { |
| 136 | static constexpr int32_t value = 4; |
| 137 | }; |
| 138 | template <> |
| 139 | struct GetNumBits<DType_INT8> |
| 140 | { |
| 141 | static constexpr int32_t value = 8; |
| 142 | }; |
| 143 | template <> |
| 144 | struct GetNumBits<DType_INT16> |
| 145 | { |
| 146 | static constexpr int32_t value = 16; |
| 147 | }; |
| 148 | template <> |
| 149 | struct GetNumBits<DType_INT32> |
| 150 | { |
| 151 | static constexpr int32_t value = 32; |
| 152 | }; |
| 153 | template <> |
| 154 | struct GetNumBits<DType_INT48> |
| 155 | { |
| 156 | static constexpr int32_t value = 48; |
| 157 | }; |
| 158 | |
| 159 | // Meta function to get quantized min/max in compile time |
| 160 | template <DType T> |
| 161 | struct GetQMin |
| 162 | { |
| 163 | static constexpr int64_t value = 0L; |
| 164 | }; |
| 165 | template <> |
| 166 | struct GetQMin<DType_AINT8> |
| 167 | { |
| 168 | static constexpr int64_t value = -128L; |
| 169 | }; |
| 170 | template <> |
| 171 | struct GetQMin<DType_UINT8> |
| 172 | { |
| 173 | static constexpr int64_t value = 0L; |
| 174 | }; |
| 175 | template <> |
| 176 | struct GetQMin<DType_INT4> |
| 177 | { |
| 178 | static constexpr int64_t value = -8L; |
| 179 | }; |
| 180 | template <> |
| 181 | struct GetQMin<DType_INT8> |
| 182 | { |
| 183 | static constexpr int64_t value = -128L; |
| 184 | }; |
| 185 | template <> |
| 186 | struct GetQMin<DType_INT16> |
| 187 | { |
| 188 | static constexpr int64_t value = -32768L; |
| 189 | }; |
| 190 | template <> |
| 191 | struct GetQMin<DType_INT32> |
| 192 | { |
| 193 | static constexpr int64_t value = -(1L << 31); |
| 194 | }; |
| 195 | template <> |
| 196 | struct GetQMin<DType_INT48> |
| 197 | { |
| 198 | static constexpr int64_t value = -(1L << 47); |
| 199 | }; |
| 200 | |
| 201 | template <DType T> |
| 202 | struct GetQMax |
| 203 | { |
| 204 | static constexpr int64_t value = 0L; |
| 205 | }; |
| 206 | template <> |
| 207 | struct GetQMax<DType_AINT8> |
| 208 | { |
| 209 | static constexpr int64_t value = 127L; |
| 210 | }; |
| 211 | template <> |
| 212 | struct GetQMax<DType_UINT8> |
| 213 | { |
| 214 | static constexpr int64_t value = 255L; |
| 215 | }; |
| 216 | template <> |
| 217 | struct GetQMax<DType_INT4> |
| 218 | { |
| 219 | static constexpr int64_t value = 7L; |
| 220 | }; |
| 221 | template <> |
| 222 | struct GetQMax<DType_INT8> |
| 223 | { |
| 224 | static constexpr int64_t value = 127L; |
| 225 | }; |
| 226 | template <> |
| 227 | struct GetQMax<DType_INT16> |
| 228 | { |
| 229 | static constexpr int64_t value = 32767L; |
| 230 | }; |
| 231 | template <> |
| 232 | struct GetQMax<DType_INT32> |
| 233 | { |
| 234 | static constexpr int64_t value = (1L << 31) - 1; |
| 235 | }; |
| 236 | template <> |
| 237 | struct GetQMax<DType_INT48> |
| 238 | { |
| 239 | static constexpr int64_t value = (1L << 47) - 1; |
| 240 | }; |
| 241 | |
| 242 | template <DType TIn1, DType TIn2> |
| 243 | struct GetAccDType; |
| 244 | template <> |
| 245 | struct GetAccDType<DType_AINT8, DType_AINT8> |
| 246 | { |
| 247 | static constexpr DType value = DType_INT32; |
| 248 | }; |
| 249 | template <> |
| 250 | struct GetAccDType<DType_AINT8, DType_INT4> |
| 251 | { |
| 252 | static constexpr DType value = DType_INT32; |
| 253 | }; |
| 254 | template <> |
| 255 | struct GetAccDType<DType_AINT8, DType_INT8> |
| 256 | { |
| 257 | static constexpr DType value = DType_INT32; |
| 258 | }; |
| 259 | template <> |
| 260 | struct GetAccDType<DType_INT16, DType_INT8> |
| 261 | { |
| 262 | static constexpr DType value = DType_INT48; |
| 263 | }; |
| 264 | template <> |
| 265 | struct GetAccDType<DType_INT16, DType_INT16> |
| 266 | { |
| 267 | static constexpr DType value = DType_INT48; |
| 268 | }; |
| 269 | template <> |
| 270 | struct GetAccDType<DType_FLOAT, DType_FLOAT> |
| 271 | { |
| 272 | static constexpr DType value = DType_FLOAT; |
| 273 | }; |
| 274 | |
| 275 | }; // namespace TosaReference |
| 276 | |
| 277 | #endif |