blob: 41e606187afaad365375d3c341296e14023a1ad8 [file] [log] [blame]
Eric Kunzee5e26762020-10-13 16:11:07 -07001
Won Jeon2c34b462024-02-06 18:37:00 +00002// Copyright (c) 2020-2024, ARM Limited.
Eric Kunzee5e26762020-10-13 16:11:07 -07003//
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
Tai Lya4d748b2023-03-28 22:06:56 +000019#include "dtype.h"
James Ward8b390432022-08-12 20:48:56 +010020#include "half.hpp"
Tai Lya4d748b2023-03-28 22:06:56 +000021#include <Eigen/CXX11/Tensor>
James Ward24dbc422022-10-19 12:20:31 +010022#include <Eigen/Core>
Eric Kunzee5e26762020-10-13 16:11:07 -070023
24using namespace tosa;
25
26namespace TosaReference
27{
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +010028// Shorter alias templates for common Eigen::Tensor types
Eric Kunzee5e26762020-10-13 16:11:07 -070029template <typename T>
30using ETensor0 = Eigen::Tensor<T, 0>;
31template <typename T>
32using ETensor1 = Eigen::Tensor<T, 1>;
33template <typename T>
34using ETensor2 = Eigen::Tensor<T, 2>;
35template <typename T>
36using ETensor3 = Eigen::Tensor<T, 3>;
37template <typename T>
38using ETensor4 = Eigen::Tensor<T, 4>;
39template <typename T>
40using ETensor5 = Eigen::Tensor<T, 5>;
41template <typename T>
42using ETensor6 = Eigen::Tensor<T, 6>;
43
44// Forward declaration
45template <class T>
46class TensorTemplate;
47
48// Shortcut to hide the TensorTemplate class.
49// For example, declare Tensor1<float> to get a TensorTemplate
50// with an Eigen::Tensor<float, 1>
51template <typename T>
52using Tensor0 = TensorTemplate<ETensor0<T>>;
53template <typename T>
54using Tensor1 = TensorTemplate<ETensor1<T>>;
55template <typename T>
56using Tensor2 = TensorTemplate<ETensor2<T>>;
57template <typename T>
58using Tensor3 = TensorTemplate<ETensor3<T>>;
59template <typename T>
60using Tensor4 = TensorTemplate<ETensor4<T>>;
61template <typename T>
62using Tensor5 = TensorTemplate<ETensor5<T>>;
63template <typename T>
64using Tensor6 = TensorTemplate<ETensor6<T>>;
65
Tai Lya4d748b2023-03-28 22:06:56 +000066template <TOSA_REF_TYPE type>
Eric Kunzee5e26762020-10-13 16:11:07 -070067struct GetEigenType;
68template <>
Tai Lya4d748b2023-03-28 22:06:56 +000069struct GetEigenType<TOSA_REF_TYPE_FP64>
70{
71 using type = double;
72};
73template <>
74struct GetEigenType<TOSA_REF_TYPE_FP32>
Eric Kunzee5e26762020-10-13 16:11:07 -070075{
76 using type = float;
77};
78template <>
Tai Lya4d748b2023-03-28 22:06:56 +000079struct GetEigenType<TOSA_REF_TYPE_FP16>
James Ward8b390432022-08-12 20:48:56 +010080{
81 // NOTE: full precision used
82 using type = float;
83};
84template <>
Tai Lya4d748b2023-03-28 22:06:56 +000085struct GetEigenType<TOSA_REF_TYPE_BF16>
James Ward24dbc422022-10-19 12:20:31 +010086{
87 // NOTE: full precision used
88 using type = float;
89};
90template <>
Won Jeon2c34b462024-02-06 18:37:00 +000091struct GetEigenType<TOSA_REF_TYPE_FP8E4M3>
92{
93 // NOTE: full precision used
94 using type = float;
95};
96template <>
97struct GetEigenType<TOSA_REF_TYPE_FP8E5M2>
98{
99 // NOTE: full precision used
100 using type = float;
101};
102template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000103struct GetEigenType<TOSA_REF_TYPE_INT32>
Eric Kunzee5e26762020-10-13 16:11:07 -0700104{
105 using type = int32_t;
106};
107template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000108struct GetEigenType<TOSA_REF_TYPE_INT48>
Eric Kunzee5e26762020-10-13 16:11:07 -0700109{
110 using type = int64_t;
111};
112template <>
Won Jeona21b2e82023-08-10 10:33:01 +0000113struct GetEigenType<TOSA_REF_TYPE_SHAPE>
114{
115 using type = int64_t;
116};
117template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000118struct GetEigenType<TOSA_REF_TYPE_BOOL>
Eric Kunzee5e26762020-10-13 16:11:07 -0700119{
120 using type = bool;
121};
122template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000123struct GetEigenType<TOSA_REF_TYPE_UINT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700124{
125 using type = int32_t;
126};
127template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000128struct GetEigenType<TOSA_REF_TYPE_UINT16>
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100129{
130 using type = int32_t;
131};
132template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000133struct GetEigenType<TOSA_REF_TYPE_INT4>
Eric Kunzee5e26762020-10-13 16:11:07 -0700134{
135 using type = int32_t;
136};
137template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000138struct GetEigenType<TOSA_REF_TYPE_INT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700139{
140 using type = int32_t;
141};
142template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000143struct GetEigenType<TOSA_REF_TYPE_INT16>
Eric Kunzee5e26762020-10-13 16:11:07 -0700144{
145 using type = int32_t;
146};
147
James Ward8b390432022-08-12 20:48:56 +0100148/* Get Accumulate Eigen Type:
Tai Lya4d748b2023-03-28 22:06:56 +0000149Same behaviour as GetEigenType for all DTYPEs except the
150single specialised case of TOSA_REF_TYPE_FP16. */
151template <TOSA_REF_TYPE Dtype>
James Ward8b390432022-08-12 20:48:56 +0100152struct GetAccEigenType;
153template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000154struct GetAccEigenType<TOSA_REF_TYPE_FP16>
James Ward8b390432022-08-12 20:48:56 +0100155{
156 using type = half_float::half;
157};
Tai Lya4d748b2023-03-28 22:06:56 +0000158template <TOSA_REF_TYPE Dtype>
James Ward8b390432022-08-12 20:48:56 +0100159struct GetAccEigenType
160{
161 using type = typename GetEigenType<Dtype>::type;
162};
163
Eric Kunzee5e26762020-10-13 16:11:07 -0700164// Meta function to get number of bits
Tai Lya4d748b2023-03-28 22:06:56 +0000165template <TOSA_REF_TYPE T>
Eric Kunzee5e26762020-10-13 16:11:07 -0700166struct GetNumBits
167{
168 static constexpr int32_t value = 0;
169};
170template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000171struct GetNumBits<TOSA_REF_TYPE_BOOL>
Eric Kunzee5e26762020-10-13 16:11:07 -0700172{
173 static constexpr int32_t value = 1;
174};
175template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000176struct GetNumBits<TOSA_REF_TYPE_UINT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700177{
178 static constexpr int32_t value = 8;
179};
180template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000181struct GetNumBits<TOSA_REF_TYPE_UINT16>
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100182{
183 static constexpr int32_t value = 16;
184};
185template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000186struct GetNumBits<TOSA_REF_TYPE_INT4>
Eric Kunzee5e26762020-10-13 16:11:07 -0700187{
188 static constexpr int32_t value = 4;
189};
190template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000191struct GetNumBits<TOSA_REF_TYPE_INT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700192{
193 static constexpr int32_t value = 8;
194};
195template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000196struct GetNumBits<TOSA_REF_TYPE_INT16>
Eric Kunzee5e26762020-10-13 16:11:07 -0700197{
198 static constexpr int32_t value = 16;
199};
200template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000201struct GetNumBits<TOSA_REF_TYPE_INT32>
Eric Kunzee5e26762020-10-13 16:11:07 -0700202{
203 static constexpr int32_t value = 32;
204};
205template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000206struct GetNumBits<TOSA_REF_TYPE_INT48>
Eric Kunzee5e26762020-10-13 16:11:07 -0700207{
208 static constexpr int32_t value = 48;
209};
James Ward8b390432022-08-12 20:48:56 +0100210template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000211struct GetNumBits<TOSA_REF_TYPE_FP16>
James Ward8b390432022-08-12 20:48:56 +0100212{
213 static constexpr int32_t value = 16;
214};
Won Jeon2c34b462024-02-06 18:37:00 +0000215template <>
216struct GetNumBits<TOSA_REF_TYPE_FP8E4M3>
217{
218 static constexpr int32_t value = 8;
219};
220template <>
221struct GetNumBits<TOSA_REF_TYPE_FP8E5M2>
222{
223 static constexpr int32_t value = 8;
224};
Eric Kunzee5e26762020-10-13 16:11:07 -0700225
226// Meta function to get quantized min/max in compile time
Tai Lya4d748b2023-03-28 22:06:56 +0000227template <TOSA_REF_TYPE T>
Eric Kunzee5e26762020-10-13 16:11:07 -0700228struct GetQMin
229{
Jerry Gecf305db2023-03-06 13:07:36 -0800230 static constexpr int64_t value = INT64_C(0);
Eric Kunzee5e26762020-10-13 16:11:07 -0700231};
232template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000233struct GetQMin<TOSA_REF_TYPE_UINT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700234{
Jerry Gecf305db2023-03-06 13:07:36 -0800235 static constexpr int64_t value = INT64_C(0);
Eric Kunzee5e26762020-10-13 16:11:07 -0700236};
237template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000238struct GetQMin<TOSA_REF_TYPE_UINT16>
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100239{
Jerry Gecf305db2023-03-06 13:07:36 -0800240 static constexpr int64_t value = INT64_C(0);
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100241};
242template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000243struct GetQMin<TOSA_REF_TYPE_INT4>
Eric Kunzee5e26762020-10-13 16:11:07 -0700244{
Jerry Gecf305db2023-03-06 13:07:36 -0800245 static constexpr int64_t value = INT64_C(-8);
Eric Kunzee5e26762020-10-13 16:11:07 -0700246};
247template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000248struct GetQMin<TOSA_REF_TYPE_INT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700249{
Jerry Gecf305db2023-03-06 13:07:36 -0800250 static constexpr int64_t value = INT64_C(-128);
Eric Kunzee5e26762020-10-13 16:11:07 -0700251};
252template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000253struct GetQMin<TOSA_REF_TYPE_INT16>
Eric Kunzee5e26762020-10-13 16:11:07 -0700254{
Jerry Gecf305db2023-03-06 13:07:36 -0800255 static constexpr int64_t value = INT64_C(-32768);
Eric Kunzee5e26762020-10-13 16:11:07 -0700256};
257template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000258struct GetQMin<TOSA_REF_TYPE_INT32>
Eric Kunzee5e26762020-10-13 16:11:07 -0700259{
Jerry Gecf305db2023-03-06 13:07:36 -0800260 static constexpr int64_t value = -(INT64_C(1) << 31);
Eric Kunzee5e26762020-10-13 16:11:07 -0700261};
262template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000263struct GetQMin<TOSA_REF_TYPE_INT48>
Eric Kunzee5e26762020-10-13 16:11:07 -0700264{
Jerry Gecf305db2023-03-06 13:07:36 -0800265 static constexpr int64_t value = -(INT64_C(1) << 47);
Eric Kunzee5e26762020-10-13 16:11:07 -0700266};
267
Tai Lya4d748b2023-03-28 22:06:56 +0000268template <TOSA_REF_TYPE T>
Eric Kunzee5e26762020-10-13 16:11:07 -0700269struct GetQMax
270{
Jerry Gecf305db2023-03-06 13:07:36 -0800271 static constexpr int64_t value = INT64_C(0);
Eric Kunzee5e26762020-10-13 16:11:07 -0700272};
273template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000274struct GetQMax<TOSA_REF_TYPE_UINT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700275{
Jerry Gecf305db2023-03-06 13:07:36 -0800276 static constexpr int64_t value = INT64_C(255);
Eric Kunzee5e26762020-10-13 16:11:07 -0700277};
278template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000279struct GetQMax<TOSA_REF_TYPE_UINT16>
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100280{
Jerry Gecf305db2023-03-06 13:07:36 -0800281 static constexpr int64_t value = INT64_C(65535);
Jeremy Johnsonf7f78ae2022-05-25 15:26:38 +0100282};
283template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000284struct GetQMax<TOSA_REF_TYPE_INT4>
Eric Kunzee5e26762020-10-13 16:11:07 -0700285{
Jerry Gecf305db2023-03-06 13:07:36 -0800286 static constexpr int64_t value = INT64_C(7);
Eric Kunzee5e26762020-10-13 16:11:07 -0700287};
288template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000289struct GetQMax<TOSA_REF_TYPE_INT8>
Eric Kunzee5e26762020-10-13 16:11:07 -0700290{
Jerry Gecf305db2023-03-06 13:07:36 -0800291 static constexpr int64_t value = INT64_C(127);
Eric Kunzee5e26762020-10-13 16:11:07 -0700292};
293template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000294struct GetQMax<TOSA_REF_TYPE_INT16>
Eric Kunzee5e26762020-10-13 16:11:07 -0700295{
Jerry Gecf305db2023-03-06 13:07:36 -0800296 static constexpr int64_t value = INT64_C(32767);
Eric Kunzee5e26762020-10-13 16:11:07 -0700297};
298template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000299struct GetQMax<TOSA_REF_TYPE_INT32>
Eric Kunzee5e26762020-10-13 16:11:07 -0700300{
Jerry Gecf305db2023-03-06 13:07:36 -0800301 static constexpr int64_t value = (INT64_C(1) << 31) - 1;
Eric Kunzee5e26762020-10-13 16:11:07 -0700302};
303template <>
Tai Lya4d748b2023-03-28 22:06:56 +0000304struct GetQMax<TOSA_REF_TYPE_INT48>
Eric Kunzee5e26762020-10-13 16:11:07 -0700305{
Jerry Gecf305db2023-03-06 13:07:36 -0800306 static constexpr int64_t value = (INT64_C(1) << 47) - 1;
Eric Kunzee5e26762020-10-13 16:11:07 -0700307};
308
Eric Kunzee5e26762020-10-13 16:11:07 -0700309}; // namespace TosaReference
310
311#endif