Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 1 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 2 | // Copyright (c) 2020-2022, ARM Limited. |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 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> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 21 | #include "half.hpp" |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 22 | #include <Eigen/Core> |
| 23 | #include "arith_util.h" |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 24 | |
| 25 | using namespace tosa; |
| 26 | |
| 27 | namespace TosaReference |
| 28 | { |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 29 | // Shorter alias templates for common Eigen::Tensor types |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 30 | template <typename T> |
| 31 | using ETensor0 = Eigen::Tensor<T, 0>; |
| 32 | template <typename T> |
| 33 | using ETensor1 = Eigen::Tensor<T, 1>; |
| 34 | template <typename T> |
| 35 | using ETensor2 = Eigen::Tensor<T, 2>; |
| 36 | template <typename T> |
| 37 | using ETensor3 = Eigen::Tensor<T, 3>; |
| 38 | template <typename T> |
| 39 | using ETensor4 = Eigen::Tensor<T, 4>; |
| 40 | template <typename T> |
| 41 | using ETensor5 = Eigen::Tensor<T, 5>; |
| 42 | template <typename T> |
| 43 | using ETensor6 = Eigen::Tensor<T, 6>; |
| 44 | |
| 45 | // Forward declaration |
| 46 | template <class T> |
| 47 | class TensorTemplate; |
| 48 | |
| 49 | // Shortcut to hide the TensorTemplate class. |
| 50 | // For example, declare Tensor1<float> to get a TensorTemplate |
| 51 | // with an Eigen::Tensor<float, 1> |
| 52 | template <typename T> |
| 53 | using Tensor0 = TensorTemplate<ETensor0<T>>; |
| 54 | template <typename T> |
| 55 | using Tensor1 = TensorTemplate<ETensor1<T>>; |
| 56 | template <typename T> |
| 57 | using Tensor2 = TensorTemplate<ETensor2<T>>; |
| 58 | template <typename T> |
| 59 | using Tensor3 = TensorTemplate<ETensor3<T>>; |
| 60 | template <typename T> |
| 61 | using Tensor4 = TensorTemplate<ETensor4<T>>; |
| 62 | template <typename T> |
| 63 | using Tensor5 = TensorTemplate<ETensor5<T>>; |
| 64 | template <typename T> |
| 65 | using Tensor6 = TensorTemplate<ETensor6<T>>; |
| 66 | |
| 67 | template <DType type> |
| 68 | struct GetEigenType; |
| 69 | template <> |
Jeremy Johnson | bc2a3db | 2022-09-27 13:50:00 +0100 | [diff] [blame] | 70 | struct GetEigenType<DType_FP32> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 71 | { |
| 72 | using type = float; |
| 73 | }; |
| 74 | template <> |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 75 | struct GetEigenType<DType_FP16> |
| 76 | { |
| 77 | // NOTE: full precision used |
| 78 | using type = float; |
| 79 | }; |
| 80 | template <> |
James Ward | 24dbc42 | 2022-10-19 12:20:31 +0100 | [diff] [blame] | 81 | struct GetEigenType<DType_BF16> |
| 82 | { |
| 83 | // NOTE: full precision used |
| 84 | using type = float; |
| 85 | }; |
| 86 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 87 | struct GetEigenType<DType_INT32> |
| 88 | { |
| 89 | using type = int32_t; |
| 90 | }; |
| 91 | template <> |
| 92 | struct GetEigenType<DType_INT48> |
| 93 | { |
| 94 | using type = int64_t; |
| 95 | }; |
| 96 | template <> |
| 97 | struct GetEigenType<DType_BOOL> |
| 98 | { |
| 99 | using type = bool; |
| 100 | }; |
| 101 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 102 | struct GetEigenType<DType_UINT8> |
| 103 | { |
| 104 | using type = int32_t; |
| 105 | }; |
| 106 | template <> |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 107 | struct GetEigenType<DType_UINT16> |
| 108 | { |
| 109 | using type = int32_t; |
| 110 | }; |
| 111 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 112 | struct GetEigenType<DType_INT4> |
| 113 | { |
| 114 | using type = int32_t; |
| 115 | }; |
| 116 | template <> |
| 117 | struct GetEigenType<DType_INT8> |
| 118 | { |
| 119 | using type = int32_t; |
| 120 | }; |
| 121 | template <> |
| 122 | struct GetEigenType<DType_INT16> |
| 123 | { |
| 124 | using type = int32_t; |
| 125 | }; |
| 126 | |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 127 | /* Get Accumulate Eigen Type: |
| 128 | Same behaviour as GetEigenType for all DTypes except the |
| 129 | single specialised case of DType_FP16. */ |
| 130 | template <DType Dtype> |
| 131 | struct GetAccEigenType; |
| 132 | template <> |
| 133 | struct GetAccEigenType<DType_FP16> |
| 134 | { |
| 135 | using type = half_float::half; |
| 136 | }; |
| 137 | template <DType Dtype> |
| 138 | struct GetAccEigenType |
| 139 | { |
| 140 | using type = typename GetEigenType<Dtype>::type; |
| 141 | }; |
| 142 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 143 | // Meta function to get number of bits |
| 144 | template <DType T> |
| 145 | struct GetNumBits |
| 146 | { |
| 147 | static constexpr int32_t value = 0; |
| 148 | }; |
| 149 | template <> |
| 150 | struct GetNumBits<DType_BOOL> |
| 151 | { |
| 152 | static constexpr int32_t value = 1; |
| 153 | }; |
| 154 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 155 | struct GetNumBits<DType_UINT8> |
| 156 | { |
| 157 | static constexpr int32_t value = 8; |
| 158 | }; |
| 159 | template <> |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 160 | struct GetNumBits<DType_UINT16> |
| 161 | { |
| 162 | static constexpr int32_t value = 16; |
| 163 | }; |
| 164 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 165 | struct GetNumBits<DType_INT4> |
| 166 | { |
| 167 | static constexpr int32_t value = 4; |
| 168 | }; |
| 169 | template <> |
| 170 | struct GetNumBits<DType_INT8> |
| 171 | { |
| 172 | static constexpr int32_t value = 8; |
| 173 | }; |
| 174 | template <> |
| 175 | struct GetNumBits<DType_INT16> |
| 176 | { |
| 177 | static constexpr int32_t value = 16; |
| 178 | }; |
| 179 | template <> |
| 180 | struct GetNumBits<DType_INT32> |
| 181 | { |
| 182 | static constexpr int32_t value = 32; |
| 183 | }; |
| 184 | template <> |
| 185 | struct GetNumBits<DType_INT48> |
| 186 | { |
| 187 | static constexpr int32_t value = 48; |
| 188 | }; |
James Ward | 8b39043 | 2022-08-12 20:48:56 +0100 | [diff] [blame] | 189 | template <> |
| 190 | struct GetNumBits<DType_FP16> |
| 191 | { |
| 192 | static constexpr int32_t value = 16; |
| 193 | }; |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 194 | |
| 195 | // Meta function to get quantized min/max in compile time |
| 196 | template <DType T> |
| 197 | struct GetQMin |
| 198 | { |
| 199 | static constexpr int64_t value = 0L; |
| 200 | }; |
| 201 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 202 | struct GetQMin<DType_UINT8> |
| 203 | { |
| 204 | static constexpr int64_t value = 0L; |
| 205 | }; |
| 206 | template <> |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 207 | struct GetQMin<DType_UINT16> |
| 208 | { |
| 209 | static constexpr int64_t value = 0L; |
| 210 | }; |
| 211 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 212 | struct GetQMin<DType_INT4> |
| 213 | { |
| 214 | static constexpr int64_t value = -8L; |
| 215 | }; |
| 216 | template <> |
| 217 | struct GetQMin<DType_INT8> |
| 218 | { |
| 219 | static constexpr int64_t value = -128L; |
| 220 | }; |
| 221 | template <> |
| 222 | struct GetQMin<DType_INT16> |
| 223 | { |
| 224 | static constexpr int64_t value = -32768L; |
| 225 | }; |
| 226 | template <> |
| 227 | struct GetQMin<DType_INT32> |
| 228 | { |
| 229 | static constexpr int64_t value = -(1L << 31); |
| 230 | }; |
| 231 | template <> |
| 232 | struct GetQMin<DType_INT48> |
| 233 | { |
| 234 | static constexpr int64_t value = -(1L << 47); |
| 235 | }; |
| 236 | |
| 237 | template <DType T> |
| 238 | struct GetQMax |
| 239 | { |
| 240 | static constexpr int64_t value = 0L; |
| 241 | }; |
| 242 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 243 | struct GetQMax<DType_UINT8> |
| 244 | { |
| 245 | static constexpr int64_t value = 255L; |
| 246 | }; |
| 247 | template <> |
Jeremy Johnson | f7f78ae | 2022-05-25 15:26:38 +0100 | [diff] [blame] | 248 | struct GetQMax<DType_UINT16> |
| 249 | { |
| 250 | static constexpr int64_t value = 65535L; |
| 251 | }; |
| 252 | template <> |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 253 | struct GetQMax<DType_INT4> |
| 254 | { |
| 255 | static constexpr int64_t value = 7L; |
| 256 | }; |
| 257 | template <> |
| 258 | struct GetQMax<DType_INT8> |
| 259 | { |
| 260 | static constexpr int64_t value = 127L; |
| 261 | }; |
| 262 | template <> |
| 263 | struct GetQMax<DType_INT16> |
| 264 | { |
| 265 | static constexpr int64_t value = 32767L; |
| 266 | }; |
| 267 | template <> |
| 268 | struct GetQMax<DType_INT32> |
| 269 | { |
| 270 | static constexpr int64_t value = (1L << 31) - 1; |
| 271 | }; |
| 272 | template <> |
| 273 | struct GetQMax<DType_INT48> |
| 274 | { |
| 275 | static constexpr int64_t value = (1L << 47) - 1; |
| 276 | }; |
| 277 | |
Eric Kunze | e5e2676 | 2020-10-13 16:11:07 -0700 | [diff] [blame] | 278 | }; // namespace TosaReference |
| 279 | |
| 280 | #endif |