Debian Codegraph @

tensilelite/Tensile/Common/DataType.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2022-2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 from rocisa.enum import DataTypeEnum
26 from functools import lru_cache
27 import functools
28
29 @lru_cache
30 def _is8bitFloat(value):
31 return (value == DataTypeEnum.Float8.value \
32 or value == DataTypeEnum.BFloat8.value \
33 or value == DataTypeEnum.Float8BFloat8.value \
34 or value == DataTypeEnum.BFloat8Float8.value \
35 or value == DataTypeEnum.Float8_fnuz.value \
36 or value == DataTypeEnum.BFloat8_fnuz.value \
37 or value == DataTypeEnum.Float8BFloat8_fnuz.value \
38 or value == DataTypeEnum.BFloat8Float8_fnuz.value)
39
40 @functools.total_ordering
41 class DataType:
42 """
43 Data Type (new)
44 Uses a list of dictionaries to organize the DataType and Properties for the kernels
45 Changed older properties list of lists to list of dictionaries
46 The inner keys (char, reg, etc) correspond with the data type properties values
47 Lookup table is used to store row numbers of a specific property
48 """
49
50 properties = [
51 {
52 'enum': DataTypeEnum.Float,
53 'char': 'S',
54 'nameAbbrev': 'f32',
55 'miOutTypeNameAbbrev': 'f32',
56 'reg': 1,
57 'hip': 'float',
58 'isComplex': False,
59 },
60 {
61 'enum': DataTypeEnum.Double,
62 'char': 'D',
63 'nameAbbrev': 'f64',
64 'miOutTypeNameAbbrev': 'f64',
65 'reg': 2,
66 'hip': 'double',
67 'isComplex': False,
68 },
69 {
70 'enum': DataTypeEnum.ComplexFloat,
71 'char': 'C',
72 'nameAbbrev': 'f32c',
73 'miOutTypeNameAbbrev': 'f32',
74 'reg': 2,
75 'hip': 'TensileComplexFloat',
76 'isComplex': True,
77 },
78 {
79 'enum': DataTypeEnum.ComplexDouble,
80 'char': 'Z',
81 'nameAbbrev': 'f64c',
82 'miOutTypeNameAbbrev': 'f64',
83 'reg': 4,
84 'hip': 'TensileComplexDouble',
85 'isComplex': True,
86 },
87 {
88 'enum': DataTypeEnum.Half,
89 'char': 'H',
90 'nameAbbrev': 'f16',
91 'miOutTypeNameAbbrev': 'f32',
92 'reg': 0.5,
93 'hip': 'tensile_half',
94 'isComplex': False,
95 },
96 {
97 'enum': DataTypeEnum.Int8x4,
98 'char': '4xi8',
99 'nameAbbrev': 'i8',
100 'miOutTypeNameAbbrev': 'i32',
101 'reg': 1,
102 'hip': 'uint32_t',
103 'isComplex': False,
104 },
105 {
106 'enum': DataTypeEnum.Int32,
107 'char': 'I',
108 'nameAbbrev': 'i32',
109 'miOutTypeNameAbbrev': 'NONE', # not supported for MI
110 'reg': 1,
111 'hip': 'int32_t',
112 'isComplex': False,
113 },
114 {
115 'enum': DataTypeEnum.BFloat16,
116 'char': 'B',
117 'nameAbbrev': 'bf16',
118 'miOutTypeNameAbbrev': 'f32',
119 'reg': 0.5,
120 'hip': 'tensile_bfloat16',
121 'isComplex': False,
122 },
123 {
124 'enum': DataTypeEnum.Int8,
125 'char': 'I8',
126 'nameAbbrev': 'i8',
127 'miOutTypeNameAbbrev': 'i32',
128 'reg': 0.25,
129 'hip': 'int8_t',
130 'isComplex': False,
131 },
132 {
133 'enum': DataTypeEnum.Int64,
134 'char': 'I64',
135 'nameAbbrev': 'i64',
136 'miOutTypeNameAbbrev': 'NONE', # not supported for MI
137 'reg': 1,
138 'hip': 'int64_t',
139 'isComplex': False,
140 },
141 {
142 'enum': DataTypeEnum.XFloat32,
143 'char': 'X',
144 'nameAbbrev': 'xf32',
145 'miOutTypeNameAbbrev': 'f32',
146 'reg': 1,
147 'hip': 'ERROR',
148 'isComplex': False,
149 },
150 { # NANOO E4M3
151 'enum': DataTypeEnum.Float8_fnuz,
152 'char': 'F8N',
153 'nameAbbrev': 'fp8_fp8', # to match v_mfma inst
154 'miOutTypeNameAbbrev': 'f32',
155 'reg': 0.25,
156 'hip': 'tensile_float8_fnuz',
157 'isComplex': False,
158 },
159 { # NANOO E5M2
160 'enum': DataTypeEnum.BFloat8_fnuz,
161 'char': 'B8N',
162 'nameAbbrev': 'bf8_bf8', # to match v_mfma inst
163 'miOutTypeNameAbbrev': 'f32',
164 'reg': 0.25,
165 'hip': 'tensile_bfloat8_fnuz',
166 'isComplex': False,
167 },
168 { #NANOO
169 'enum': DataTypeEnum.Float8BFloat8_fnuz,
170 'char': 'F8B8N',
171 'nameAbbrev': 'fp8_bf8', # to match v_mfma
172 'miOutTypeNameAbbrev': 'f32',
173 'reg': 0.25,
174 'hip': 'ERROR',
175 'isComplex': False,
176 },
177 { #NANOO
178 'enum': DataTypeEnum.BFloat8Float8_fnuz,
179 'char': 'B8F8N',
180 'nameAbbrev': 'bf8_fp8', # to match v_mfma
181 'miOutTypeNameAbbrev': 'f32',
182 'reg': 0.25,
183 'hip': 'ERROR',
184 'isComplex': False,
185 },
186 { # OCP E4M3
187 'enum': DataTypeEnum.Float8,
188 'char': 'F8',
189 'nameAbbrev': 'fp8_fp8', # to match v_mfma inst
190 'miOutTypeNameAbbrev': 'f32',
191 'reg': 0.25,
192 'hip': 'tensile_float8',
193 'isComplex': False,
194 },
195 { # OCP E5M2
196 'enum': DataTypeEnum.BFloat8,
197 'char': 'B8',
198 'nameAbbrev': 'bf8_bf8', # to match v_mfma inst
199 'miOutTypeNameAbbrev': 'f32',
200 'reg': 0.25,
201 'hip': 'tensile_bfloat8',
202 'isComplex': False,
203 },
204 { #OCP
205 'enum': DataTypeEnum.Float8BFloat8,
206 'char': 'F8B8',
207 'nameAbbrev': 'fp8_bf8', # to match v_mfma
208 'miOutTypeNameAbbrev': 'f32',
209 'reg': 0.25,
210 'hip': 'ERROR',
211 'isComplex': False,
212 },
213 { #OCP
214 'enum': DataTypeEnum.BFloat8Float8,
215 'char': 'B8F8',
216 'nameAbbrev': 'bf8_fp8', # to match v_mfma
217 'miOutTypeNameAbbrev': 'f32',
218 'reg': 0.25,
219 'hip': 'ERROR',
220 'isComplex': False,
221 },
222 ]
223 lookup = {}
224
225 def __init__(self, value):
226 if isinstance(value, DataTypeEnum):
227 self.value = value.value
228 elif isinstance(value, int):
229 self.value = value
230 elif isinstance(value, str):
231 self.value = DataType.lookup[value.lower()]
232 elif isinstance(value, DataType):
233 self.value = value.value
234 else:
235 raise RuntimeError("initializing DataType to {0} {1}".format(str(type(value)), str(value)))
236
237 self.properties = DataType.properties[self.value]
238
239 def toChar(self):
240 return self.properties['char']
241 def toName(self):
242 return self.properties['enum'].name
243 def toNameAbbrev(self):
244 return self.properties['nameAbbrev']
245 def toEnum(self):
246 return self.properties['enum']
247 def toDevice(self, language):
248 if language == "HIP":
249 return self.properties['hip']
250 else:
251 assert 0
252
253 ########################################
254 def zeroString(self, language, vectorWidth):
255 """
256 Returns a string containing the data output format, depending on programming language
257 and in the case of complex numbers, the vector width.
258 """
259 zeroString = "("
260 zeroString += self.toDevice(language)
261 if vectorWidth > 1:
262 zeroString += str(vectorWidth)
263 zeroString += ")("
264
265 """
266 if self.value == DataTypeEnum.Half:
267 single = "0"
268 vectorWidth = 1
269 elif self.value == DataTypeEnum.Float.value:
270 single = "0.f"
271 elif self.value == DataTypeEnum.Double.value:
272 single = "0.0"
273 elif self.value == DataTypeEnum.ComplexSingle.value:
274 single = "0.f, 0.f"
275 elif self.value == DataTypeEnum.ComplexDouble.value:
276 single = "0.0, 0.0"
277 """
278 zeroString += "0"
279 zeroString += ")"
280 return zeroString
281
282 def isReal(self):
283 return not self.isComplex()
284 def isComplex(self):
285 return self.properties['isComplex']
286 def isDoubleComplex(self):
287 return self.value == DataTypeEnum.ComplexDouble.value
288 def isSingleComplex(self):
289 return self.value == DataTypeEnum.ComplexFloat.value
290 def isDouble(self):
291 return self.value == DataTypeEnum.Double.value
292 def isSingle(self):
293 return self.value == DataTypeEnum.Float.value
294 def isHalf(self):
295 return self.value == DataTypeEnum.Half.value
296 def isInt32(self):
297 return self.value == DataTypeEnum.Int32.value
298 def isInt64(self):
299 return self.value == DataTypeEnum.Int64.value
300 def isInt8x4(self):
301 return self.value == DataTypeEnum.Int8x4.value
302 def isInt8(self):
303 return self.value == DataTypeEnum.Int8.value
304 def isBFloat16(self):
305 return self.value == DataTypeEnum.BFloat16.value
306 def isXFloat32(self):
307 return self.value == DataTypeEnum.XFloat32.value
308 def isFloat8(self):
309 return self.value == DataTypeEnum.Float8.value
310 def isFloat8_fnuz(self):
311 return self.value == DataTypeEnum.Float8_fnuz.value
312 def isAnyFloat8(self):
313 return (self.value == DataTypeEnum.Float8.value \
314 or self.value == DataTypeEnum.Float8_fnuz.value)
315 def isBFloat8(self):
316 return self.value == DataTypeEnum.BFloat8.value
317 def isBFloat8_fnuz(self):
318 return self.value == DataTypeEnum.BFloat8_fnuz.value
319 def isAnyBFloat8(self):
320 return (self.value == DataTypeEnum.BFloat8.value \
321 or self.value == DataTypeEnum.BFloat8_fnuz.value)
322 def isFloat8BFloat8(self):
323 return self.value == DataTypeEnum.Float8BFloat8.value
324 def isFloat8BFloat8_fnuz(self):
325 return self.value == DataTypeEnum.Float8BFloat8_fnuz.value
326 def isAnyFloat8BFloat8(self):
327 return (self.value == DataTypeEnum.Float8BFloat8.value \
328 or self.value == DataTypeEnum.Float8BFloat8_fnuz.value)
329 def isBFloat8Float8(self):
330 return self.value == DataTypeEnum.BFloat8Float8.value
331 def isBFloat8Float8_fnuz(self):
332 return self.value == DataTypeEnum.BFloat8Float8_fnuz.value
333 def isAnyBFloat8Float8(self):
334 return (self.value == DataTypeEnum.BFloat8Float8.value \
335 or self.value == DataTypeEnum.BFloat8Float8_fnuz.value)
336 def is8bitFloat(self):
337 return _is8bitFloat(self.value)
338 def isFloat8A(self):
339 return (self.value == DataTypeEnum.Float8.value \
340 or self.value == DataTypeEnum.Float8BFloat8.value)
341 def isFloat8_fnuzA(self):
342 return (self.value == DataTypeEnum.Float8_fnuz.value \
343 or self.value == DataTypeEnum.Float8BFloat8_fnuz.value)
344 def isAnyFloat8A(self):
345 return (self.value == DataTypeEnum.Float8.value \
346 or self.value == DataTypeEnum.Float8BFloat8.value \
347 or self.value == DataTypeEnum.Float8_fnuz.value \
348 or self.value == DataTypeEnum.Float8BFloat8_fnuz.value)
349 def isBFloat8A(self):
350 return (self.value == DataTypeEnum.BFloat8.value \
351 or self.value == DataTypeEnum.BFloat8Float8.value)
352 def isBFloat8_fnuzA(self):
353 return (self.value == DataTypeEnum.BFloat8_fnuz.value \
354 or self.value == DataTypeEnum.BFloat8Float8_fnuz.value)
355 def isAnyBFloat8A(self):
356 return (self.value == DataTypeEnum.BFloat8.value \
357 or self.value == DataTypeEnum.BFloat8Float8.value \
358 or self.value == DataTypeEnum.BFloat8_fnuz.value \
359 or self.value == DataTypeEnum.BFloat8Float8_fnuz.value)
360 def isFloat8B(self):
361 return (self.value == DataTypeEnum.Float8.value \
362 or self.value == DataTypeEnum.BFloat8Float8.value)
363 def isFloat8_fnuzB(self):
364 return (self.value == DataTypeEnum.Float8_fnuz.value \
365 or self.value == DataTypeEnum.BFloat8Float8_fnuz.value)
366 def isAnyFloat8B(self):
367 return (self.value == DataTypeEnum.Float8.value \
368 or self.value == DataTypeEnum.BFloat8Float8.value \
369 or self.value == DataTypeEnum.Float8_fnuz.value \
370 or self.value == DataTypeEnum.BFloat8Float8_fnuz.value)
371 def isBFloat8B(self):
372 return (self.value == DataTypeEnum.BFloat8.value \
373 or self.value == DataTypeEnum.Float8BFloat8.value)
374 def isBFloat8_fnuzB(self):
375 return (self.value == DataTypeEnum.BFloat8_fnuz.value \
376 or self.value == DataTypeEnum.Float8BFloat8_fnuz.value)
377 def isAnyBFloat8B(self):
378 return (self.value == DataTypeEnum.BFloat8.value \
379 or self.value == DataTypeEnum.Float8BFloat8.value \
380 or self.value == DataTypeEnum.BFloat8_fnuz.value \
381 or self.value == DataTypeEnum.Float8BFloat8_fnuz.value)
382 def isNone(self):
383 return self.value == None
384
385 def numRegisters(self):
386 return self.properties['reg']
387 def numBytes(self):
388 return int(self.numRegisters() * 4)
389 def MIOutputTypeNameAbbrev(self):
390 return self.properties['miOutTypeNameAbbrev']
391 def flopsPerMac(self):
392 return 2 if self.isReal() else 8
393
394 def state(self): return self.toName()
395
396 def __str__(self):
397 return self.toChar()
398 def __repr__(self):
399 return self.__str__()
400
401 def getAttributes(self):
402 return (self.value,)
403
404 def __hash__(self):
405 return hash(self.getAttributes())
406
407 def __eq__(self, other):
408 if not isinstance(other, DataType):
409 return NotImplemented
410
411 return self.getAttributes() == other.getAttributes()
412
413 def __lt__(self, other):
414 if not isinstance(other, DataType):
415 return NotImplemented
416
417 return self.getAttributes() < other.getAttributes()
418
419 # Other operands are provided by functools.total_ordering.
420
421 def _populateLookupTable(properties,lookup):
422 """
423 Populates Lookup Table with the corresponding row number for each DataType. The row number
424 is assigned to self.value when a DataType object is called
425 """
426 for i,e in enumerate(properties):
427 if i != int(e['enum'].value):
428 print(e['enum'].name, ":", e['enum'].value, "does not match index", i, "in properties list")
429 raise RuntimeError("Enum value does not match index in properties list")
430 for k in ['enum','char']:
431 lookupKey = e[k].name.lower() if k == 'enum' else e[k].lower()
432 if lookupKey in lookup and lookup[lookupKey] != i:
433 raise RuntimeError("Duplicate key {1} in property '{0}'".format(k,lookupKey))
434 lookup[lookupKey] = i
435
436 _populateLookupTable(DataType.properties,DataType.lookup)