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