-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathtest_ir_types.py
More file actions
360 lines (288 loc) · 10.8 KB
/
Copy pathtest_ir_types.py
File metadata and controls
360 lines (288 loc) · 10.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import copy
import functools
import pickle
import pytest
from cuda.tile import TileValueError
from cuda.tile._exception import TileTypeError
from cuda.tile._ir.type import (
TupleTy, TileTy, NONE, LooselyTypedScalar
)
from cuda.tile._datatype import (
DType,
float16, float32,
float4_e2m1fn, float64, bool_,
float8_e8m0fnu,
int64, int32, int16, int8,
uint64, uint32, uint16, uint8, bfloat16,
tfloat32, float8_e4m3fn, float8_e5m2,
is_boolean, is_integral, is_float, is_unrestricted_float, is_restricted_float, is_signed,
IntegerInfo, opaque_pointer_dtype, pointer_dtype, PointerInfo,
)
from cuda.tile._ir.ops_utils import promote_dtypes
from cuda.tile._ir.typing_support import to_dtype
from cuda.tile._ir.cast_ops import check_implicit_cast
import torch
import numpy as np
from cuda.tile._memory_model import MemorySpace
def test_builtin_types():
assert str(float16) == 'float16'
assert str(int64) == 'int64'
assert str(int32) == 'int32'
assert str(int16) == 'int16'
assert str(int8) == 'int8'
assert str(bool_) == 'bool_'
assert str(bfloat16) == 'bfloat16'
assert str(uint64) == 'uint64'
assert str(uint32) == 'uint32'
assert str(float32) == 'float32'
assert str(float64) == 'float64'
assert str(tfloat32) == 'tfloat32'
assert str(float8_e4m3fn) == 'float8_e4m3fn'
assert str(float8_e5m2) == 'float8_e5m2'
assert str(float8_e8m0fnu) == "float8_e8m0fnu"
assert str(float4_e2m1fn) == "float4_e2m1fn"
assert is_integral(int32)
assert is_signed(int32)
assert is_signed(float32)
assert not is_signed(uint32)
assert not is_signed(bool_)
assert is_boolean(bool_)
assert is_float(bfloat16)
assert not is_float(uint32)
assert is_unrestricted_float(bfloat16)
assert not is_unrestricted_float(tfloat32)
assert is_float(tfloat32)
assert is_restricted_float(tfloat32)
assert is_restricted_float(float8_e4m3fn)
assert is_restricted_float(float8_e5m2)
assert is_restricted_float(float8_e8m0fnu)
assert is_restricted_float(float4_e2m1fn)
# Pickle-unpickle roundtrip
assert pickle.loads(pickle.dumps(float16)) is float16
# Deep copy roundtrip
assert copy.deepcopy(float16) is float16
def test_tuple_type():
# tuple type
shape = TupleTy((5, 4))
assert len(shape) == 2
assert shape[0] == 5
assert shape[1] == 4
def test_tile_type():
# tile type
shape = (5, 4)
tile = TileTy(float16, shape)
assert tile.dtype == float16
assert tile.shape == shape
# tile type equality
tile2 = TileTy(float16, shape)
assert tile == tile2
def test_promote_dtypes():
def assert_no_promote(t1, t2):
with pytest.raises(TileTypeError):
promote_dtypes(t1, t2)
def assert_out_of_range(t1, t2):
with pytest.raises(TileValueError, match="is out of range"):
promote_dtypes(t1, t2)
# Bool
assert promote_dtypes(bool_, uint8) == uint8
assert promote_dtypes(bool_, int16) == int16
assert promote_dtypes(bool_, float32) == float32
assert promote_dtypes(bool_, bfloat16) == bfloat16
assert_no_promote(bool_, tfloat32)
assert_no_promote(bool_, float8_e5m2)
assert_no_promote(bool_, float8_e8m0fnu)
assert_no_promote(bool_, float4_e2m1fn)
# Int
assert promote_dtypes(int8, int16) == int16
assert promote_dtypes(int32, float16) == float16
assert promote_dtypes(int64, bfloat16) == bfloat16
assert_no_promote(int32, tfloat32)
assert_no_promote(int32, float8_e5m2)
assert_no_promote(int32, float8_e8m0fnu)
assert_no_promote(int32, float4_e2m1fn)
# Uint
assert promote_dtypes(uint8, uint16) == uint16
assert promote_dtypes(uint32, float16) == float16
assert promote_dtypes(uint32, bfloat16) == bfloat16
assert_no_promote(uint32, int32)
assert_no_promote(uint32, int64)
assert_no_promote(uint32, tfloat32)
assert_no_promote(uint32, float8_e5m2)
assert_no_promote(uint32, float8_e8m0fnu)
assert_no_promote(uint32, float4_e2m1fn)
# float
assert promote_dtypes(float16, float32) == float32
assert promote_dtypes(bfloat16, float32) == float32
assert_no_promote(float16, bfloat16)
assert_no_promote(float16, tfloat32)
assert_no_promote(float16, float8_e5m2)
assert_no_promote(float16, float8_e8m0fnu)
assert_no_promote(float16, float4_e2m1fn)
# Loosely typed scalars
assert promote_dtypes(int16, LooselyTypedScalar(5)) == int16
assert promote_dtypes(LooselyTypedScalar(5), int8) == int8
assert promote_dtypes(LooselyTypedScalar(5), LooselyTypedScalar(7)) == int32
assert promote_dtypes(LooselyTypedScalar(5), LooselyTypedScalar(7.0)) == float32
assert promote_dtypes(int16, LooselyTypedScalar(5.0)) == float32
assert promote_dtypes(float16, LooselyTypedScalar(5.0)) == float16
assert promote_dtypes(bool_, LooselyTypedScalar(5)) == int32
assert_out_of_range(int8, LooselyTypedScalar(128))
def test_check_implicit_cast():
def allow(src, dst):
if isinstance(src, DType):
src = TileTy(src)
check_implicit_cast(src, dst)
def disallow(src, dst):
if isinstance(src, DType):
src = TileTy(src)
with pytest.raises((TileTypeError, TileValueError)):
check_implicit_cast(src, dst)
# same category
allow(int8, int8)
allow(uint8, uint8)
allow(int8, int16)
disallow(int16, int8)
allow(uint8, uint16)
disallow(uint16, uint8)
allow(float16, float32)
disallow(float32, float16)
allow(bfloat16, float32)
disallow(float32, bfloat16)
disallow(float16, bfloat16)
disallow(bfloat16, float16)
# bool -> int or float
allow(bool_, int32)
disallow(int32, bool_)
allow(bool_, uint32)
disallow(uint32, bool_)
allow(bool_, float32)
disallow(float32, bool_)
allow(bool_, bfloat16)
disallow(bfloat16, bool_)
disallow(bool_, tfloat32)
disallow(tfloat32, bool_)
disallow(bool_, float8_e5m2)
disallow(float8_e5m2, bool_)
disallow(bool_, float8_e8m0fnu)
disallow(float8_e8m0fnu, bool_)
disallow(bool_, float4_e2m1fn)
disallow(float4_e2m1fn, bool_)
# int -> float
allow(uint32, float16)
disallow(float16, uint32)
allow(int32, float16)
disallow(float16, int32)
disallow(uint32, tfloat32)
disallow(tfloat32, uint32)
disallow(uint32, float8_e5m2)
disallow(float8_e5m2, uint32)
disallow(uint32, float8_e8m0fnu)
disallow(float8_e8m0fnu, uint32)
disallow(uint32, float4_e2m1fn)
disallow(float4_e2m1fn, uint32)
disallow(int32, tfloat32)
disallow(tfloat32, int32)
disallow(int32, float8_e5m2)
disallow(float8_e5m2, int32)
disallow(int32, float8_e8m0fnu)
disallow(float8_e8m0fnu, int32)
disallow(int32, float4_e2m1fn)
disallow(float4_e2m1fn, int32)
# signed <> unsigned not allowed
disallow(int32, uint32)
disallow(uint32, int32)
# restricted float not allowed
disallow(float32, tfloat32)
disallow(tfloat32, float32)
disallow(float8_e5m2, tfloat32)
disallow(tfloat32, float8_e5m2)
disallow(float8_e8m0fnu, tfloat32)
disallow(tfloat32, float8_e8m0fnu)
disallow(float4_e2m1fn, tfloat32)
disallow(tfloat32, float4_e2m1fn)
disallow(float8_e8m0fnu, float8_e5m2)
disallow(float8_e5m2, float8_e8m0fnu)
disallow(float4_e2m1fn, float8_e5m2)
disallow(float8_e5m2, float4_e2m1fn)
disallow(float4_e2m1fn, float8_e8m0fnu)
disallow(float8_e8m0fnu, float4_e2m1fn)
# Loosely typed scalars
allow(LooselyTypedScalar(10), int8)
disallow(LooselyTypedScalar(128), int8)
allow(LooselyTypedScalar(10), float32)
allow(LooselyTypedScalar(4.0), float32)
allow(LooselyTypedScalar(4.0), float16)
allow(LooselyTypedScalar(False), bool_)
allow(LooselyTypedScalar(True), bool_)
allow(LooselyTypedScalar(1), bool_)
allow(LooselyTypedScalar(0), bool_)
disallow(LooselyTypedScalar(1.0), bool_)
disallow(LooselyTypedScalar(0.0), bool_)
def test_np_dtype_support():
assert to_dtype(np.float64) == float64
assert to_dtype(np.float32) == float32
assert to_dtype(np.float16) == float16
assert to_dtype(np.int64) == int64
assert to_dtype(np.int32) == int32
assert to_dtype(np.int16) == int16
assert to_dtype(np.int8) == int8
assert to_dtype(np.uint64) == uint64
assert to_dtype(np.uint32) == uint32
assert to_dtype(np.uint16) == uint16
assert to_dtype(np.uint8) == uint8
assert to_dtype(np.bool_) == bool_
assert to_dtype(np.dtype('float64')) == float64
assert to_dtype(np.dtype('float32')) == float32
assert to_dtype(np.dtype('float16')) == float16
assert to_dtype(np.dtype('int64')) == int64
assert to_dtype(np.dtype('int32')) == int32
assert to_dtype(np.dtype('int16')) == int16
assert to_dtype(np.dtype('int8')) == int8
assert to_dtype(np.dtype('uint64')) == uint64
assert to_dtype(np.dtype('uint32')) == uint32
assert to_dtype(np.dtype("uint16")) == uint16
assert to_dtype(np.dtype("uint8")) == uint8
assert to_dtype(np.dtype('bool_')) == bool_
def test_torch_dtype_support():
assert to_dtype(torch.float64) == float64
assert to_dtype(torch.float32) == float32
assert to_dtype(torch.float16) == float16
assert to_dtype(torch.int64) == int64
assert to_dtype(torch.int32) == int32
assert to_dtype(torch.int16) == int16
assert to_dtype(torch.int8) == int8
assert to_dtype(torch.uint64) == uint64
assert to_dtype(torch.uint32) == uint32
assert to_dtype(torch.uint16) == uint16
assert to_dtype(torch.uint8) == uint8
assert to_dtype(torch.bool) == bool_
assert to_dtype(torch.bfloat16) == bfloat16
assert to_dtype(torch.float8_e4m3fn) == float8_e4m3fn
assert to_dtype(torch.float8_e5m2) == float8_e5m2
assert to_dtype(torch.float8_e8m0fnu) == float8_e8m0fnu
def test_type_of_constant_python_value():
from cuda.tile._ir.typing_support import type_of_constant_python_value
from cuda.tile._compile import _TileTypingHooks
tp = functools.partial(type_of_constant_python_value, typing_hooks=_TileTypingHooks())
assert tp(1) == TileTy(int32)
assert tp(1.) == TileTy(float32)
assert tp(True) == TileTy(bool_)
assert tp(None) == NONE
def test_integer_info_equality():
assert IntegerInfo(int32) == IntegerInfo(int32)
assert IntegerInfo(int32) != IntegerInfo(int64)
def test_pointer_info_equality():
dtypes = [
opaque_pointer_dtype(),
opaque_pointer_dtype(MemorySpace.SHARED),
pointer_dtype(int32),
pointer_dtype(int32, MemorySpace.SHARED),
pointer_dtype(int64),
pointer_dtype(int64, MemorySpace.SHARED)
]
for i, a in enumerate(dtypes):
for j, b in enumerate(dtypes):
assert (PointerInfo(a) == PointerInfo(b)) == (i == j)