forked from NVIDIA/cuda-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_program.py
More file actions
304 lines (243 loc) · 9.99 KB
/
Copy pathtest_program.py
File metadata and controls
304 lines (243 loc) · 9.99 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
# SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
#
# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
import warnings
import pytest
from cuda.core.experimental import _linker
from cuda.core.experimental._module import Kernel, ObjectCode
from cuda.core.experimental._program import Program, ProgramOptions
is_culink_backend = _linker._decide_nvjitlink_or_driver()
def _is_nvvm_available():
"""Check if NVVM is available."""
try:
from cuda.core.experimental._program import _get_nvvm_module
_get_nvvm_module()
return True
except ImportError:
return False
nvvm_available = pytest.mark.skipif(
not _is_nvvm_available(), reason="NVVM not available (libNVVM not found or cuda-bindings < 12.9.0)"
)
@pytest.fixture(scope="session")
def nvvm_ir():
"""Generate working NVVM IR with proper version metadata.
The try clause here is used for older nvvm modules which
might not have an ir_version() method. In which case the
fallback assumes no version metadata will be present in
the input nvvm ir
"""
try:
from cuda.core.experimental._program import _get_nvvm_module
nvvm = _get_nvvm_module()
major, minor, debug_major, debug_minor = nvvm.ir_version()
nvvm_ir_template = """target triple = "nvptx64-unknown-cuda"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-" \
"f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
define i32 @ave(i32 %a, i32 %b) {{
entry:
%add = add nsw i32 %a, %b
%div = sdiv i32 %add, 2
ret i32 %div
}}
define void @simple(i32* %data) {{
entry:
%0 = call i32 @llvm.nvvm.read.ptx.sreg.ctaid.x()
%1 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
%mul = mul i32 %0, %1
%2 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%add = add i32 %mul, %2
%call = call i32 @ave(i32 %add, i32 %add)
%idxprom = sext i32 %add to i64
store i32 %call, i32* %data, align 4
ret void
}}
declare i32 @llvm.nvvm.read.ptx.sreg.ctaid.x() nounwind readnone
declare i32 @llvm.nvvm.read.ptx.sreg.ntid.x() nounwind readnone
declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() nounwind readnone
!nvvm.annotations = !{{!0}}
!0 = !{{void (i32*)* @simple, !"kernel", i32 1}}
!nvvmir.version = !{{!1}}
!1 = !{{i32 {major}, i32 0, i32 {debug_major}, i32 0}}
"""
return nvvm_ir_template.format(major=major, debug_major=debug_major)
except Exception:
return """target triple = "nvptx64-unknown-cuda"
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-i128:128:128-" \
"f32:32:32-f64:64:64-v16:16:16-v32:32:32-v64:64:64-v128:128:128-n16:32:64"
define i32 @ave(i32 %a, i32 %b) {
entry:
%add = add nsw i32 %a, %b
%div = sdiv i32 %add, 2
ret i32 %div
}
define void @simple(i32* %data) {
entry:
%0 = call i32 @llvm.nvvm.read.ptx.sreg.ctaid.x()
%1 = call i32 @llvm.nvvm.read.ptx.sreg.ntid.x()
%mul = mul i32 %0, %1
%2 = call i32 @llvm.nvvm.read.ptx.sreg.tid.x()
%add = add i32 %mul, %2
%call = call i32 @ave(i32 %add, i32 %add)
%idxprom = sext i32 %add to i64
store i32 %call, i32* %data, align 4
ret void
}
declare i32 @llvm.nvvm.read.ptx.sreg.ctaid.x() nounwind readnone
declare i32 @llvm.nvvm.read.ptx.sreg.ntid.x() nounwind readnone
declare i32 @llvm.nvvm.read.ptx.sreg.tid.x() nounwind readnone
!nvvm.annotations = !{!0}
!0 = !{void (i32*)* @simple, !"kernel", i32 1}
"""
@pytest.fixture(scope="module")
def ptx_code_object():
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++")
ptx_object_code = program.compile("ptx")
return ptx_object_code
@pytest.mark.parametrize(
"options",
[
ProgramOptions(name="abc"),
ProgramOptions(device_code_optimize=True, debug=True),
ProgramOptions(relocatable_device_code=True, max_register_count=32),
ProgramOptions(ftz=True, prec_sqrt=False, prec_div=False),
ProgramOptions(fma=False, use_fast_math=True),
ProgramOptions(extra_device_vectorization=True),
ProgramOptions(link_time_optimization=True),
ProgramOptions(define_macro="MY_MACRO"),
ProgramOptions(define_macro=("MY_MACRO", "99")),
ProgramOptions(define_macro=[("MY_MACRO", "99")]),
ProgramOptions(define_macro=[("MY_MACRO", "99"), ("MY_OTHER_MACRO", "100")]),
ProgramOptions(undefine_macro=["MY_MACRO", "MY_OTHER_MACRO"]),
ProgramOptions(undefine_macro="MY_MACRO", include_path="/usr/local/include"),
ProgramOptions(builtin_initializer_list=False, disable_warnings=True),
ProgramOptions(restrict=True, device_as_default_execution_space=True),
ProgramOptions(device_int128=True, optimization_info="inline"),
ProgramOptions(no_display_error_number=True),
ProgramOptions(diag_error=1234, diag_suppress=1234),
ProgramOptions(diag_error=[1234, 1223], diag_suppress=(1234, 1223)),
ProgramOptions(diag_warn=1000),
ProgramOptions(std="c++11", ptxas_options=["-v"]),
ProgramOptions(std="c++11", ptxas_options=["-v", "-O2"]),
],
)
def test_cpp_program_with_various_options(init_cuda, options):
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++", options)
assert program.backend == "NVRTC"
program.compile("ptx")
program.close()
assert program.handle is None
options = [
ProgramOptions(max_register_count=32),
ProgramOptions(debug=True),
ProgramOptions(lineinfo=True),
ProgramOptions(ftz=True),
ProgramOptions(prec_div=True),
ProgramOptions(prec_sqrt=True),
ProgramOptions(fma=True),
]
if not is_culink_backend:
options += [
ProgramOptions(time=True),
ProgramOptions(split_compile=True),
]
@pytest.mark.parametrize("options", options)
def test_ptx_program_with_various_options(init_cuda, ptx_code_object, options):
program = Program(ptx_code_object._module.decode(), "ptx", options=options)
assert program.backend == ("driver" if is_culink_backend else "nvJitLink")
program.compile("cubin")
program.close()
assert program.handle is None
def test_program_init_valid_code_type():
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++")
assert program.backend == "NVRTC"
assert program.handle is not None
def test_program_init_invalid_code_type():
code = "goto 100"
with pytest.raises(
RuntimeError, match=r"^Unsupported code_type='fortran' \(supported_code_types=\('c\+\+', 'ptx', 'nvvm'\)\)$"
):
Program(code, "FORTRAN")
def test_program_init_invalid_code_format():
code = 12345
with pytest.raises(TypeError):
Program(code, "c++")
# This is tested against the current device's arch
def test_program_compile_valid_target_type(init_cuda):
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++", options={"name": "42"})
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always")
ptx_object_code = program.compile("ptx")
assert isinstance(ptx_object_code, ObjectCode)
assert ptx_object_code.name == "42"
if any("The CUDA driver version is older than the backend version" in str(warning.message) for warning in w):
pytest.skip("PTX version too new for current driver")
ptx_kernel = ptx_object_code.get_kernel("my_kernel")
assert isinstance(ptx_kernel, Kernel)
program = Program(ptx_object_code._module.decode(), "ptx", options={"name": "24"})
cubin_object_code = program.compile("cubin")
assert isinstance(cubin_object_code, ObjectCode)
assert cubin_object_code.name == "24"
cubin_kernel = cubin_object_code.get_kernel("my_kernel")
assert isinstance(cubin_kernel, Kernel)
def test_program_compile_invalid_target_type():
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++")
with pytest.raises(ValueError):
program.compile("invalid_target")
def test_program_backend_property():
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++")
assert program.backend == "NVRTC"
def test_program_handle_property():
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++")
assert program.handle is not None
def test_program_close():
code = 'extern "C" __global__ void my_kernel() {}'
program = Program(code, "c++")
program.close()
assert program.handle is None
@nvvm_available
def test_nvvm_deferred_import():
"""Test that our deferred NVVM import works correctly"""
from cuda.core.experimental._program import _get_nvvm_module
nvvm = _get_nvvm_module()
assert nvvm is not None
@nvvm_available
def test_nvvm_program_creation(nvvm_ir):
"""Test basic NVVM program creation"""
program = Program(nvvm_ir, "nvvm")
assert program.backend == "NVVM"
assert program.handle is not None
program.close()
@nvvm_available
def test_nvvm_compile_invalid_target(nvvm_ir):
"""Test that NVVM programs reject invalid compilation targets"""
program = Program(nvvm_ir, "nvvm")
with pytest.raises(ValueError, match='NVVM backend only supports target_type="ptx"'):
program.compile("cubin")
program.close()
@nvvm_available
@pytest.mark.parametrize(
"options",
[
ProgramOptions(name="test1", arch="sm_90", device_code_optimize=False),
ProgramOptions(name="test2", arch="sm_100", device_code_optimize=False),
ProgramOptions(name="test3", arch="sm_110", device_code_optimize=True),
],
)
def test_nvvm_program_options(init_cuda, nvvm_ir, options):
"""Test NVVM programs with different options"""
program = Program(nvvm_ir, "nvvm", options)
assert program.backend == "NVVM"
ptx_code = program.compile("ptx")
assert isinstance(ptx_code, ObjectCode)
assert ptx_code.name == options.name
code_content = ptx_code.code
ptx_text = code_content.decode() if isinstance(code_content, bytes) else str(code_content)
assert ".visible .entry simple(" in ptx_text
program.close()