blob: 470504d2b2efd80bea5dfc13d84fc74c8a080d92 [file] [log] [blame]
Tim Hall79d07d22020-04-27 18:20:16 +01001# Copyright (C) 2020 Arm Limited or its affiliates. All rights reserved.
2#
3# SPDX-License-Identifier: Apache-2.0
4#
5# Licensed under the Apache License, Version 2.0 (the License); you may
6# not use this file except in compliance with the License.
7# You may obtain a copy of the License at
8#
9# www.apache.org/licenses/LICENSE-2.0
10#
11# Unless required by applicable law or agreed to in writing, software
12# distributed under the License is distributed on an AS IS BASIS, WITHOUT
13# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14# See the License for the specific language governing permissions and
15# limitations under the License.
Tim Hall79d07d22020-04-27 18:20:16 +010016# Description:
17# Defines the basic numeric type classes for tensors.
Tim Hall79d07d22020-04-27 18:20:16 +010018import enum
Dwight Lidman9b43f842020-12-08 17:56:44 +010019from typing import Any
Tim Hall79d07d22020-04-27 18:20:16 +010020
James Peet7519d502021-07-19 16:47:58 +010021import numpy as np
22
Diego Russoea6111a2020-04-14 18:41:58 +010023from .numeric_util import round_up_divide
24
Tim Hall79d07d22020-04-27 18:20:16 +010025
26class BaseType(enum.Flag):
27 Signed = 1
28 Unsigned = 2
29 Asymmetric = 4
30 Int = 8
31 SignedInt = Int | Signed
32 UnsignedInt = Int | Unsigned
33 AsymmSInt = Int | Asymmetric | Signed
34 AsymmUInt = Int | Asymmetric | Unsigned
35 Float = 16
36 BFloat = 32
37 Bool = 64
38 String = 128
39 Resource = 256
40 Variant = 512
Jacob Bohlinf767b932020-08-13 15:32:45 +020041 Complex = 1024
Tim Hall79d07d22020-04-27 18:20:16 +010042
43
44class DataType:
45 """Defines a data type. Consists of a base type, and the number of bits used for this type"""
46
47 __slots__ = "type", "bits"
48
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020049 int4: Any
Dwight Lidman9b43f842020-12-08 17:56:44 +010050 int8: Any
51 int16: Any
52 int32: Any
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +020053 int48: Any
Dwight Lidman9b43f842020-12-08 17:56:44 +010054 int64: Any
55 uint8: Any
56 uint16: Any
57 uint32: Any
58 uint64: Any
59 quint4: Any
60 quint8: Any
61 quint12: Any
62 quint16: Any
63 quint32: Any
64 qint4: Any
65 qint8: Any
66 qint12: Any
67 qint16: Any
68 qint32: Any
69 float16: Any
70 float32: Any
71 float64: Any
72 string: Any
73 bool: Any
74 resource: Any
75 variant: Any
76 complex64: Any
77 complex128: Any
78
Tim Hall79d07d22020-04-27 18:20:16 +010079 def __init__(self, type_, bits):
80 self.type = type_
81 self.bits = bits
82
83 def __eq__(self, other):
84 return self.type == other.type and self.bits == other.bits
85
86 def __hash__(self):
87 return hash((self.type, self.bits))
88
89 def size_in_bytes(self):
90 return round_up_divide(self.bits, 8)
91
92 def size_in_bits(self):
93 return self.bits
94
95 def __str__(self):
96 stem, needs_format = DataType.stem_name[self.type]
97 if not needs_format:
98 return stem
99 else:
100 return stem % (self.bits,)
101
102 __repr__ = __str__
103
James Peet7519d502021-07-19 16:47:58 +0100104 def as_numpy_type(self):
105 numpy_dtype_code = {
106 BaseType.UnsignedInt: "u",
107 BaseType.SignedInt: "i",
108 BaseType.Float: "f",
109 BaseType.Complex: "c",
110 }
111 assert self.type in numpy_dtype_code, f"Failed to interpret {self} as a numpy dtype"
112 return np.dtype(numpy_dtype_code[self.type] + str(self.size_in_bytes()))
113
Tim Hall79d07d22020-04-27 18:20:16 +0100114 stem_name = {
115 BaseType.UnsignedInt: ("uint%s", True),
116 BaseType.SignedInt: ("int%s", True),
117 BaseType.AsymmUInt: ("quint%s", True),
118 BaseType.AsymmSInt: ("qint%s", True),
119 BaseType.Float: ("float%s", True),
120 BaseType.BFloat: ("bfloat%s", True),
121 BaseType.Bool: ("bool", False),
122 BaseType.String: ("string", False),
123 BaseType.Resource: ("resource", False),
124 BaseType.Variant: ("variant", False),
Jacob Bohlinf767b932020-08-13 15:32:45 +0200125 BaseType.Complex: ("complex%s", True),
Tim Hall79d07d22020-04-27 18:20:16 +0100126 }
127
128
129# generate the standard set of data types
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200130DataType.int4 = DataType(BaseType.SignedInt, 4)
Tim Hall79d07d22020-04-27 18:20:16 +0100131DataType.int8 = DataType(BaseType.SignedInt, 8)
132DataType.int16 = DataType(BaseType.SignedInt, 16)
133DataType.int32 = DataType(BaseType.SignedInt, 32)
Patrik Gustavsson8f1f9aa2021-06-28 07:41:58 +0200134DataType.int48 = DataType(BaseType.SignedInt, 48)
Tim Hall79d07d22020-04-27 18:20:16 +0100135DataType.int64 = DataType(BaseType.SignedInt, 64)
136
137DataType.uint8 = DataType(BaseType.UnsignedInt, 8)
138DataType.uint16 = DataType(BaseType.UnsignedInt, 16)
139DataType.uint32 = DataType(BaseType.UnsignedInt, 32)
140DataType.uint64 = DataType(BaseType.UnsignedInt, 64)
141
142DataType.quint4 = DataType(BaseType.AsymmUInt, 4)
143DataType.quint8 = DataType(BaseType.AsymmUInt, 8)
144DataType.quint12 = DataType(BaseType.AsymmUInt, 12)
145DataType.quint16 = DataType(BaseType.AsymmUInt, 16)
146DataType.quint32 = DataType(BaseType.AsymmUInt, 32)
147
148DataType.qint4 = DataType(BaseType.AsymmSInt, 4)
149DataType.qint8 = DataType(BaseType.AsymmSInt, 8)
150DataType.qint12 = DataType(BaseType.AsymmSInt, 12)
151DataType.qint16 = DataType(BaseType.AsymmSInt, 16)
152DataType.qint32 = DataType(BaseType.AsymmSInt, 32)
153
154DataType.float16 = DataType(BaseType.Float, 16)
155DataType.float32 = DataType(BaseType.Float, 32)
156DataType.float64 = DataType(BaseType.Float, 64)
157
158DataType.string = DataType(BaseType.String, 64)
159DataType.bool = DataType(BaseType.Bool, 8)
160DataType.resource = DataType(BaseType.Resource, 8)
161DataType.variant = DataType(BaseType.Variant, 8)
Jacob Bohlinf767b932020-08-13 15:32:45 +0200162DataType.complex64 = DataType(BaseType.Complex, 64)
Jacob Bohlin8daf6b72020-09-15 16:28:35 +0200163DataType.complex128 = DataType(BaseType.Complex, 128)