blob: bb4c55898a688da1580cb446c3b89a63796a73fb [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
19
Diego Russoea6111a2020-04-14 18:41:58 +010020from .numeric_util import round_up_divide
21
Tim Hall79d07d22020-04-27 18:20:16 +010022
23class BaseType(enum.Flag):
24 Signed = 1
25 Unsigned = 2
26 Asymmetric = 4
27 Int = 8
28 SignedInt = Int | Signed
29 UnsignedInt = Int | Unsigned
30 AsymmSInt = Int | Asymmetric | Signed
31 AsymmUInt = Int | Asymmetric | Unsigned
32 Float = 16
33 BFloat = 32
34 Bool = 64
35 String = 128
36 Resource = 256
37 Variant = 512
38
39
40class DataType:
41 """Defines a data type. Consists of a base type, and the number of bits used for this type"""
42
43 __slots__ = "type", "bits"
44
45 def __init__(self, type_, bits):
46 self.type = type_
47 self.bits = bits
48
49 def __eq__(self, other):
50 return self.type == other.type and self.bits == other.bits
51
52 def __hash__(self):
53 return hash((self.type, self.bits))
54
55 def size_in_bytes(self):
56 return round_up_divide(self.bits, 8)
57
58 def size_in_bits(self):
59 return self.bits
60
61 def __str__(self):
62 stem, needs_format = DataType.stem_name[self.type]
63 if not needs_format:
64 return stem
65 else:
66 return stem % (self.bits,)
67
68 __repr__ = __str__
69
70 stem_name = {
71 BaseType.UnsignedInt: ("uint%s", True),
72 BaseType.SignedInt: ("int%s", True),
73 BaseType.AsymmUInt: ("quint%s", True),
74 BaseType.AsymmSInt: ("qint%s", True),
75 BaseType.Float: ("float%s", True),
76 BaseType.BFloat: ("bfloat%s", True),
77 BaseType.Bool: ("bool", False),
78 BaseType.String: ("string", False),
79 BaseType.Resource: ("resource", False),
80 BaseType.Variant: ("variant", False),
81 }
82
83
84# generate the standard set of data types
85DataType.int8 = DataType(BaseType.SignedInt, 8)
86DataType.int16 = DataType(BaseType.SignedInt, 16)
87DataType.int32 = DataType(BaseType.SignedInt, 32)
88DataType.int64 = DataType(BaseType.SignedInt, 64)
89
90DataType.uint8 = DataType(BaseType.UnsignedInt, 8)
91DataType.uint16 = DataType(BaseType.UnsignedInt, 16)
92DataType.uint32 = DataType(BaseType.UnsignedInt, 32)
93DataType.uint64 = DataType(BaseType.UnsignedInt, 64)
94
95DataType.quint4 = DataType(BaseType.AsymmUInt, 4)
96DataType.quint8 = DataType(BaseType.AsymmUInt, 8)
97DataType.quint12 = DataType(BaseType.AsymmUInt, 12)
98DataType.quint16 = DataType(BaseType.AsymmUInt, 16)
99DataType.quint32 = DataType(BaseType.AsymmUInt, 32)
100
101DataType.qint4 = DataType(BaseType.AsymmSInt, 4)
102DataType.qint8 = DataType(BaseType.AsymmSInt, 8)
103DataType.qint12 = DataType(BaseType.AsymmSInt, 12)
104DataType.qint16 = DataType(BaseType.AsymmSInt, 16)
105DataType.qint32 = DataType(BaseType.AsymmSInt, 32)
106
107DataType.float16 = DataType(BaseType.Float, 16)
108DataType.float32 = DataType(BaseType.Float, 32)
109DataType.float64 = DataType(BaseType.Float, 64)
110
111DataType.string = DataType(BaseType.String, 64)
112DataType.bool = DataType(BaseType.Bool, 8)
113DataType.resource = DataType(BaseType.Resource, 8)
114DataType.variant = DataType(BaseType.Variant, 8)