-
Notifications
You must be signed in to change notification settings - Fork 141
Expand file tree
/
Copy pathtest_export_compat.py
More file actions
226 lines (191 loc) · 9.42 KB
/
Copy pathtest_export_compat.py
File metadata and controls
226 lines (191 loc) · 9.42 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
# SPDX-FileCopyrightText: Copyright (c) <2026> NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: Apache-2.0
import ctypes
import re
from ctypes import (c_char_p, POINTER, c_void_p, c_int, c_uint64, pointer, CFUNCTYPE, c_uint,
c_int32, c_float, byref)
from io import BytesIO
import pytest
import torch.cuda
import cuda.tile as ct
from cuda.tile._compile import get_sm_arch
class CudaDriver:
def __init__(self):
from cuda.tile._load_libcuda import _cuGetProcAddress_v2
_cuGetProcAddress_v2.argtypes = [c_char_p, POINTER(c_void_p), c_int, c_uint64, c_void_p]
_cuGetProcAddress_v2.restype = c_int
def get_proc(name: bytes, version: int, ty):
func_ptr_v = c_void_p()
res = _cuGetProcAddress_v2(name, byref(func_ptr_v), version, 0, None)
assert res == 0
assert func_ptr_v.value is not None
return ctypes.cast(func_ptr_v, ty)
functy_cuLibraryLoadData = CFUNCTYPE(
c_int,
POINTER(c_void_p), c_void_p,
c_void_p, c_void_p, c_uint,
c_void_p, c_void_p, c_uint)
self._cuLibraryLoadData = get_proc(b"cuLibraryLoadData", 12000, functy_cuLibraryLoadData)
functy_cuLibraryGetKernel = CFUNCTYPE(c_int, POINTER(c_void_p), c_void_p, c_char_p)
self._cuLibraryGetKernel = get_proc(b"cuLibraryGetKernel", 12000, functy_cuLibraryGetKernel)
functy_cuLaunchKernel = CFUNCTYPE(c_int, c_void_p,
c_uint, c_uint, c_uint,
c_uint, c_uint, c_uint,
c_uint, c_void_p, POINTER(c_void_p), POINTER(c_void_p))
self._cuLaunchKernel = get_proc(b"cuLaunchKernel", 7000, functy_cuLaunchKernel)
def cuLibraryLoadData(self, code: bytes):
library = c_void_p()
res = self._cuLibraryLoadData(byref(library), code, None, None, 0, None, None, 0)
assert res == 0
return library
def cuLibraryGetKernel(self, library, name: str):
kernel = c_void_p()
res = self._cuLibraryGetKernel(byref(kernel), library, name.encode())
assert res == 0
return kernel
def cuLaunchKernel(self, f, grid: tuple[int, int, int], block: tuple[int, int, int],
shared_mem: int, stream: c_void_p, args: list):
args_arr = (c_void_p * len(args))()
for i, x in enumerate(args):
args_arr[i] = ctypes.cast(pointer(x), c_void_p)
res = self._cuLaunchKernel(f, *grid, *block, shared_mem, stream, args_arr, None)
assert res == 0
@ct.kernel
def kernel_1(c1: ct.Constant, s1, c2: ct.Constant, s2,
a1, a2):
for i in range(5):
ct.scatter(a1, (i*2+3, i), c1 * 1000 + s1 * 10 + i)
ct.scatter(a2, (7 - i, i + 2, i), c2 * 1000.0 + s2 * 10 + i)
def _build_kernel_args(runtime_pyargs, is_v2=False) -> list:
args = []
for x in runtime_pyargs:
if isinstance(x, torch.Tensor):
args.append(c_void_p(x.data_ptr()))
for s in x.shape:
args.append(c_int32(s))
for s in x.stride():
args.append(c_int32(s))
elif is_v2 and isinstance(x, tuple):
for elem in x:
if isinstance(elem, int):
args.append(c_int32(elem))
elif isinstance(elem, float):
args.append(c_float(elem))
else:
assert False, f"Unsupported tuple element type: {type(elem)}"
elif isinstance(x, int):
args.append(c_int32(x))
elif isinstance(x, float):
args.append(c_float(x))
else:
assert False
return args
def _call_kernel(cubin: bytes, kernel_name: str, runtime_pyargs, is_v2=False):
driver = CudaDriver()
library = driver.cuLibraryLoadData(cubin)
kernel = driver.cuLibraryGetKernel(library, kernel_name)
stream = torch.cuda.current_stream()
driver.cuLaunchKernel(kernel, (1, 1, 1), (1, 1, 1), 0,
c_void_p(stream.cuda_stream),
_build_kernel_args(runtime_pyargs, is_v2))
def test_export_compat_cutile_python_v1():
sig = ct.compilation.KernelSignature(
parameters=[
13,
ct.compilation.ScalarConstraint(ct.int32),
17.0,
ct.compilation.ScalarConstraint(ct.float32),
ct.compilation.ArrayConstraint(ct.int32, 2, index_dtype=ct.int32,
stride_lower_bound_incl=0,
alias_groups=(), may_alias_internally=False,
stride_divisible_by=(4, 1),
stride_constant=(None, 1)),
ct.compilation.ArrayConstraint(ct.float32, 3, index_dtype=ct.int32,
stride_lower_bound_incl=0,
alias_groups=(), may_alias_internally=False),
],
calling_convention=ct.compilation.CallingConvention.cutile_python_v1(),
)
io = BytesIO()
ct.compilation.export_kernel(kernel_1, [sig], gpu_code=get_sm_arch(), output_file=io,
output_format="cubin")
a1 = torch.zeros((32, 8), dtype=torch.int32, device="cuda")
a2 = torch.zeros((8, 8, 8), dtype=torch.float32, device="cuda")
_call_kernel(io.getvalue(),
"kernel_1_Kt1_I13_Si32_F4031000000000000_Sf32_A2i32_1v4l0_2t1_A3f32_7l0",
(5, 9.0, a1, a2))
a1_cpu = a1.cpu()
a2_cpu = a2.cpu()
for i in range(5):
assert a1_cpu[i*2+3, i] == 13 * 1000 + 5 * 10 + i
assert a2_cpu[7 - i, i + 2, i] == 17.0 * 1000.0 + 9.0 * 10.0 + i
@ct.kernel
def kernel_static_shape(a, out):
t = ct.load(a, (0,), (8,))
ct.store(out, (0,), t + 1)
def test_export_compat_static_shape():
sig = ct.compilation.KernelSignature(
parameters=[
ct.compilation.ArrayConstraint(ct.float32, 1, index_dtype=ct.int32,
shape_constant=(8,),
stride_lower_bound_incl=0,
alias_groups=(), may_alias_internally=False),
ct.compilation.ArrayConstraint(ct.float32, 1, index_dtype=ct.int32,
shape_constant=(8,),
stride_constant=(1,),
stride_lower_bound_incl=0,
alias_groups=(), may_alias_internally=False),
],
calling_convention=ct.compilation.CallingConvention.cutile_python_v2(),
)
io = BytesIO()
ct.compilation.export_kernel(kernel_static_shape, [sig], gpu_code=get_sm_arch(),
output_file=io, output_format="cubin")
a = torch.zeros(8, dtype=torch.float32, device="cuda")
out = torch.zeros(8, dtype=torch.float32, device="cuda")
# shape_constant=(8,): shape is still a runtime CUDA parameter — pass it as usual.
# stride_lower_bound_incl for out is dropped (stride_constant=(1,) makes it redundant).
_call_kernel(io.getvalue(), "kernel_static_shape_Kt2_A1f32_1s8l0_A1f32_1s8t1", (a, out))
assert torch.all(out == 1.0).item()
@ct.kernel
def kernel_2(pair, addend: ct.Constant[tuple], out):
ct.scatter(out, (), pair[0] + pair[1] + addend[0])
def test_export_compat_cutile_python_v2():
sig = ct.compilation.KernelSignature(
parameters=[
ct.compilation.TupleConstraint(
[
ct.compilation.ScalarConstraint(ct.int32),
ct.compilation.ScalarConstraint(ct.int32),
]),
(10,),
ct.compilation.ArrayConstraint(ct.int32, 0, index_dtype=ct.int32,
stride_lower_bound_incl=0,
alias_groups=(), may_alias_internally=False),
],
calling_convention=ct.compilation.CallingConvention.cutile_python_v2(),
)
io = BytesIO()
ct.compilation.export_kernel(kernel_2, [sig], gpu_code=get_sm_arch(), output_file=io,
output_format="cubin")
out = torch.zeros((), dtype=torch.int32, device="cuda")
_call_kernel(io.getvalue(), "kernel_2_Kt2_T2Si32Si32_T1I10_A0i32", ((3, 7), out), is_v2=True)
assert out.item() == 20
def test_static_shape_with_v1_raises():
cconv = ct.compilation.CallingConvention.cutile_python_v1()
expected_message = re.escape("Static array shapes are not supported by calling convention"
" cutile_python_v1; version >= 2 is required")
with pytest.raises(ValueError, match=expected_message):
ct.compilation.KernelSignature(
[ct.compilation.ArrayConstraint(
ct.float32, 1, index_dtype=ct.int32, stride_lower_bound_incl=0,
alias_groups=(), may_alias_internally=False,
shape_constant=(8,))],
cconv)
def test_tuple_with_v1_raises():
cconv = ct.compilation.CallingConvention.cutile_python_v1()
expected_message = re.escape("Tuple parameters are not supported by calling convention"
" cutile_python_v1; version >= 2 is required")
with pytest.raises(ValueError, match=expected_message):
ct.compilation.KernelSignature([(ct.compilation.ScalarConstraint(ct.int32),)], cconv)