-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathtest_loose_typing.py
More file actions
205 lines (146 loc) · 6.71 KB
/
test_loose_typing.py
File metadata and controls
205 lines (146 loc) · 6.71 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
# SPDX-FileCopyrightText: Copyright (c) <2025> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import pytest
import torch
import cuda.tile as ct
from cuda.tile import TileValueError
from cuda.tile._ir.op_impl import impl
def raise_error(*args): ...
@impl(raise_error)
def raise_error_impl(args):
msg = " ".join(str(x.get_constant()) for x in args)
raise AssertionError(msg)
@ct.kernel
def propagate_constant_int_then_promote(n: ct.Constant[int], out):
# Do a bunch of operations that should propagate the constant
# n = 5: n = 50:
# ======== ========
t = (n + 2) * 3 # t = 21 t = 156
t = -t # t = -21 t = -156
t ^= n # t = -18 t = -170
# Now combine it with a concretely typed tile to trigger the type promotion logic
a = ct.arange(4, dtype=ct.int8)
b = a - t # b = [18, 19, 20, 21]
# Check that the type of `b` is the same as `a`
if b.dtype != ct.int8:
raise_error("Expected int8, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_propagate_constant_int_then_promote():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), propagate_constant_int_then_promote, (5, a))
assert a.tolist() == [18, 19, 20, 21]
def test_propagate_constant_int_then_promote_out_of_range():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
with pytest.raises(TileValueError, match="Integer constant -170 is out of range of int8"):
ct.launch(torch.cuda.current_stream(), (1,), propagate_constant_int_then_promote, (50, a))
@ct.kernel
def propagate_constant_float_then_promote(n: ct.Constant[int], out):
# Do a bunch of operations that should propagate the constant
t = (n + 2) * 3 # n = 5 -> t = 21
t = -t # t = -21
t += 0.5 # t = -20.5
# Now combine it with a concretely typed tile to trigger the type promotion logic
a = ct.arange(4, dtype=ct.int8)
b = a - t # b = [20.5, 21.5, 22.5, 23.5]
# Check that the result of `a - t` has been promoted to float32
if b.dtype != ct.float32:
raise_error("Expected float32, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_propagate_constant_float_then_promote():
a = torch.zeros((4,), dtype=torch.float32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), propagate_constant_float_then_promote, (5, a))
assert a.tolist() == [20.5, 21.5, 22.5, 23.5]
@ct.kernel
def pack_tuple_then_getitem_and_promote(n: ct.Constant[int], out):
tup = (ct.bid(0), n // 2)
t = tup[1]
# Combine `t` with a concretely typed tile to trigger the type promotion logic
a = ct.arange(4, dtype=ct.int8)
b = a + t
# Check that the type of `b` is the same as `a`
if b.dtype != ct.int8:
raise_error("Expected int8, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_pack_tuple_then_getitem_and_promote():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), pack_tuple_then_getitem_and_promote, (11, a))
assert a.tolist() == [5, 6, 7, 8]
@ct.kernel
def pack_nested_tuple_then_getitem_and_promote(n: ct.Constant[int], out):
tup = (ct.bid(0), (n // 2, ct.bid(0)))
t = tup[1][0]
# Combine `t` with a concretely typed tile to trigger the type promotion logic
a = ct.arange(4, dtype=ct.int8)
b = a + t
# Check that the type of `b` is the same as `a`
if b.dtype != ct.int8:
raise_error("Expected int8, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_pack_nested_tuple_then_getitem_and_promote():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), pack_nested_tuple_then_getitem_and_promote,
(11, a))
assert a.tolist() == [5, 6, 7, 8]
@ct.kernel
def propagate_constant_int_through_if_else_then_promote(n: ct.Constant[int], out):
if ct.bid(0) == 0:
t = n + 2
else:
t = 7 # same constant (assuming n = 5)
# Now combine it with a concretely typed tile to trigger the type promotion logic
a = ct.arange(4, dtype=ct.int8)
b = a + t # [7, 8, 9, 10]
# Check that the type of `b` is the same as `a`
if b.dtype != ct.int8:
raise_error("Expected int8, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_propagate_constant_int_through_if_else_then_promote():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,),
propagate_constant_int_through_if_else_then_promote, (5, a))
assert a.tolist() == [7, 8, 9, 10]
@ct.kernel
def different_constants_in_if_else_then_promote(n: ct.Constant[int], out):
if ct.bid(0) == 0:
t = n
else:
t = 7 # different constant (assuming n = 5)
# Now combine it with a concretely typed tile to trigger the type promotion logic
a = ct.arange(4, dtype=ct.int8)
b = a + t # [5, 6, 7, 8]
# Since `t` is int32 and not loosely typed, the result should be an int32
if b.dtype != ct.int32:
raise_error("Expected int32, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_different_constants_in_if_else_then_promote():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,),
different_constants_in_if_else_then_promote, (5, a))
assert a.tolist() == [5, 6, 7, 8]
@ct.kernel
def combine_loose_and_strict_int(n: ct.Constant[int], out):
t = n + ct.int16(2) # int16 because n is loosely typed
if t != 7:
raise_error("Expected `t` to be a constant 7")
a = ct.arange(4, dtype=ct.int8) # explicitly int8
b = a + t # int16 because `t` is strictly typed
if b.dtype != ct.int16:
raise_error("Expected int16, got", b.dtype)
ct.scatter(out, ct.arange(4, dtype=ct.int32), b)
def test_combine_loose_and_strict_int():
a = torch.zeros((4,), dtype=torch.int32, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), combine_loose_and_strict_int, (5, a))
assert a.tolist() == [7, 8, 9, 10]
@ct.kernel
def call_float_and_store(out):
c1 = float("2.0")
# Since out is f16, this would fail if `c` is a strictly typed f32:
ct.scatter(out, 0, c1)
# Calling float() on a strictly typed constant should produce a loosely typed one:
c2 = float(ct.float32(5.0))
ct.scatter(out, 1, c2)
def test_float_constructor_produces_loosely_typed_constant():
a = torch.zeros((2,), dtype=torch.float16, device="cuda")
ct.launch(torch.cuda.current_stream(), (1,), call_float_and_store, (a,))
assert a.tolist() == [2.0, 5.0]