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
222 lines (176 loc) · 7.42 KB
/
Copy pathtest_program.py
File metadata and controls
222 lines (176 loc) · 7.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
# 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()
nvvm_ir = """
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
"""
@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_options = [
ProgramOptions(name="nvvm_test"),
ProgramOptions(device_code_optimize=True),
ProgramOptions(arch="sm_90"),
ProgramOptions(debug=True),
]
@pytest.mark.parametrize("options", nvvm_options)
def test_nvvm_program_with_various_options(init_cuda, options):
program = Program(nvvm_ir, "nvvm", options)
assert program.backend == "NVVM"
program.compile("ptx")
program.close()
assert program.handle is None
def test_nvvm_program_creation():
program = Program(nvvm_ir, "nvvm")
assert program.backend == "NVVM"
assert program.handle is not None
def test_nvvm_compile_invalid_target():
program = Program(nvvm_ir, "nvvm")
with pytest.raises(ValueError):
program.compile("cubin")
def test_nvvm_compile_valid_target_type(init_cuda):
program = Program(nvvm_ir, "nvvm", options={"name": "nvvm_test"})
ptx_object_code = program.compile("ptx")
assert isinstance(ptx_object_code, ObjectCode)
assert ptx_object_code.name == "nvvm_test"
ptx_kernel = ptx_object_code.get_kernel("nvvm_kernel")
assert isinstance(ptx_kernel, Kernel)
program.close()