Skip to content

Commit 9d8ecbc

Browse files
committed
integrate ruff changes
1 parent fdc76e8 commit 9d8ecbc

File tree

7 files changed

+406
-2
lines changed

7 files changed

+406
-2
lines changed

cuda_core/cuda/core/experimental/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@
55
from cuda.core.experimental._device import Device
66
from cuda.core.experimental._event import EventOptions
77
from cuda.core.experimental._launcher import LaunchConfig, launch
8+
from cuda.core.experimental._linker import Linker, LinkerOptions
89
from cuda.core.experimental._program import Program
910
from cuda.core.experimental._stream import Stream, StreamOptions
Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
from dataclasses import dataclass
2+
from typing import List, Optional
3+
4+
from cuda.bindings import nvjitlink
5+
from cuda.core.experimental._module import ObjectCode
6+
from cuda.core.experimental._utils import check_or_create_options
7+
8+
9+
@dataclass
10+
class LinkerOptions:
11+
"""Customizable :obj:`LinkerOptions` for nvJitLink.
12+
13+
Attributes
14+
----------
15+
arch : str
16+
Pass SM architecture value. Can use compute_<N> value instead if only generating PTX.
17+
This is a required option.
18+
Acceptable value type: str
19+
Maps to: -arch=sm_<N>
20+
max_register_count : int, optional
21+
Maximum register count.
22+
Default: None
23+
Acceptable value type: int
24+
Maps to: -maxrregcount=<N>
25+
time : bool, optional
26+
Print timing information to InfoLog.
27+
Default: False
28+
Acceptable value type: bool
29+
Maps to: -time
30+
verbose : bool, optional
31+
Print verbose messages to InfoLog.
32+
Default: False
33+
Acceptable value type: bool
34+
Maps to: -verbose
35+
link_time_optimization : bool, optional
36+
Perform link time optimization.
37+
Default: False
38+
Acceptable value type: bool
39+
Maps to: -lto
40+
ptx : bool, optional
41+
Emit PTX after linking instead of CUBIN; only supported with -lto.
42+
Default: False
43+
Acceptable value type: bool
44+
Maps to: -ptx
45+
optimization_level : int, optional
46+
Set optimization level. Only 0 and 3 are accepted.
47+
Default: None
48+
Acceptable value type: int
49+
Maps to: -O<N>
50+
debug : bool, optional
51+
Generate debug information.
52+
Default: False
53+
Acceptable value type: bool
54+
Maps to: -g
55+
lineinfo : bool, optional
56+
Generate line information.
57+
Default: False
58+
Acceptable value type: bool
59+
Maps to: -lineinfo
60+
ftz : bool, optional
61+
Flush denormal values to zero.
62+
Default: False
63+
Acceptable value type: bool
64+
Maps to: -ftz=<n>
65+
prec_div : bool, optional
66+
Use precise division.
67+
Default: True
68+
Acceptable value type: bool
69+
Maps to: -prec-div=<n>
70+
prec_sqrt : bool, optional
71+
Use precise square root.
72+
Default: True
73+
Acceptable value type: bool
74+
Maps to: -prec-sqrt=<n>
75+
fma : bool, optional
76+
Use fast multiply-add.
77+
Default: True
78+
Acceptable value type: bool
79+
Maps to: -fma=<n>
80+
kernels_used : List[str], optional
81+
Pass list of kernels that are used; any not in the list can be removed. This option can be specified multiple
82+
times.
83+
Default: None
84+
Acceptable value type: list of str
85+
Maps to: -kernels-used=<name>
86+
variables_used : List[str], optional
87+
Pass list of variables that are used; any not in the list can be removed. This option can be specified multiple
88+
times.
89+
Default: None
90+
Acceptable value type: list of str
91+
Maps to: -variables-used=<name>
92+
optimize_unused_variables : bool, optional
93+
Assume that if a variable is not referenced in device code, it can be removed.
94+
Default: False
95+
Acceptable value type: bool
96+
Maps to: -optimize-unused-variables
97+
xptxas : List[str], optional
98+
Pass options to PTXAS. This option can be called multiple times.
99+
Default: None
100+
Acceptable value type: list of str
101+
Maps to: -Xptxas=<opt>
102+
split_compile : int, optional
103+
Split compilation maximum thread count. Use 0 to use all available processors. Value of 1 disables split
104+
compilation (default).
105+
Default: 1
106+
Acceptable value type: int
107+
Maps to: -split-compile=<N>
108+
split_compile_extended : int, optional
109+
A more aggressive form of split compilation available in LTO mode only. Accepts a maximum thread count value.
110+
Use 0 to use all available processors. Value of 1 disables extended split compilation (default). Note: This
111+
option can potentially impact performance of the compiled binary.
112+
Default: 1
113+
Acceptable value type: int
114+
Maps to: -split-compile-extended=<N>
115+
jump_table_density : int, optional
116+
When doing LTO, specify the case density percentage in switch statements, and use it as a minimal threshold to
117+
determine whether jump table (brx.idx instruction) will be used to implement a switch statement. Default value
118+
is 101. The percentage ranges from 0 to 101 inclusively.
119+
Default: 101
120+
Acceptable value type: int
121+
Maps to: -jump-table-density=<N>
122+
no_cache : bool, optional
123+
Do not cache the intermediate steps of nvJitLink.
124+
Default: False
125+
Acceptable value type: bool
126+
Maps to: -no-cache
127+
device_stack_protector : bool, optional
128+
Enable stack canaries in device code. Stack canaries make it more difficult to exploit certain types of memory
129+
safety bugs involving stack-local variables. The compiler uses heuristics to assess the risk of such a bug in
130+
each function. Only those functions which are deemed high-risk make use of a stack canary.
131+
Default: False
132+
Acceptable value type: bool
133+
Maps to: -device-stack-protector
134+
"""
135+
136+
arch: str
137+
max_register_count: Optional[int] = None
138+
time: Optional[bool] = None
139+
verbose: Optional[bool] = None
140+
link_time_optimization: Optional[bool] = None
141+
ptx: Optional[bool] = None
142+
optimization_level: Optional[int] = None
143+
debug: Optional[bool] = None
144+
lineinfo: Optional[bool] = None
145+
ftz: Optional[bool] = None
146+
prec_div: Optional[bool] = None
147+
prec_sqrt: Optional[bool] = None
148+
fma: Optional[bool] = None
149+
kernels_used: Optional[List[str]] = None
150+
variables_used: Optional[List[str]] = None
151+
optimize_unused_variables: Optional[bool] = None
152+
xptxas: Optional[List[str]] = None
153+
split_compile: Optional[int] = None
154+
split_compile_extended: Optional[int] = None
155+
jump_table_density: Optional[int] = None
156+
no_cache: Optional[bool] = None
157+
device_stack_protector: Optional[bool] = None
158+
159+
def __post_init__(self):
160+
self.formatted_options = []
161+
if self.arch is not None:
162+
self.formatted_options.append(f"-arch={self.arch}")
163+
if self.max_register_count is not None:
164+
self.formatted_options.append(f"-maxrregcount={self.max_register_count}")
165+
if self.time is not None:
166+
self.formatted_options.append("-time")
167+
if self.verbose is not None:
168+
self.formatted_options.append("-verbose")
169+
if self.link_time_optimization is not None:
170+
self.formatted_options.append("-lto")
171+
if self.ptx is not None:
172+
self.formatted_options.append("-ptx")
173+
if self.optimization_level is not None:
174+
self.formatted_options.append(f"-O{self.optimization_level}")
175+
if self.debug is not None:
176+
self.formatted_options.append("-g")
177+
if self.lineinfo is not None:
178+
self.formatted_options.append("-lineinfo")
179+
if self.ftz is not None:
180+
self.formatted_options.append(f"-ftz={'true' if self.ftz else 'false'}")
181+
if self.prec_div is not None:
182+
self.formatted_options.append(f"-prec-div={'true' if self.prec_div else 'false'}")
183+
if self.prec_sqrt is not None:
184+
self.formatted_options.append(f"-prec-sqrt={'true' if self.prec_sqrt else 'false'}")
185+
if self.fma is not None:
186+
self.formatted_options.append(f"-fma={'true' if self.fma else 'false'}")
187+
if self.kernels_used is not None:
188+
for kernel in self.kernels_used:
189+
self.formatted_options.append(f"-kernels-used={kernel}")
190+
if self.variables_used is not None:
191+
for variable in self.variables_used:
192+
self.formatted_options.append(f"-variables-used={variable}")
193+
if self.optimize_unused_variables is not None:
194+
self.formatted_options.append("-optimize-unused-variables")
195+
if self.xptxas is not None:
196+
for opt in self.xptxas:
197+
self.formatted_options.append(f"-Xptxas={opt}")
198+
if self.split_compile is not None:
199+
self.formatted_options.append(f"-split-compile={self.split_compile}")
200+
if self.split_compile_extended is not None:
201+
self.formatted_options.append(f"-split-compile-extended={self.split_compile_extended}")
202+
if self.jump_table_density is not None:
203+
self.formatted_options.append(f"-jump-table-density={self.jump_table_density}")
204+
if self.no_cache is not None:
205+
self.formatted_options.append("-no-cache")
206+
if self.device_stack_protector is not None:
207+
self.formatted_options.append("-device-stack-protector")
208+
209+
210+
class Linker:
211+
__slots__ = "_handle"
212+
213+
def __init__(self, *object_codes: ObjectCode, options: LinkerOptions = None):
214+
self._handle = None
215+
options = check_or_create_options(LinkerOptions, options, "Linker options")
216+
self._handle = nvjitlink.create(len(options.formatted_options), options.formatted_options)
217+
218+
if object_codes is not None:
219+
for code in object_codes:
220+
assert isinstance(code, ObjectCode)
221+
self._add_code_object(code)
222+
223+
def _add_code_object(self, object_code: ObjectCode):
224+
data = object_code._module
225+
assert isinstance(data, bytes)
226+
nvjitlink.add_data(
227+
self._handle,
228+
self._input_type_from_code_type(object_code._code_type),
229+
data,
230+
len(data),
231+
f"{object_code._handle}_{object_code._code_type}",
232+
)
233+
234+
def link(self, target_type) -> ObjectCode:
235+
nvjitlink.complete(self._handle)
236+
if target_type not in ["cubin", "ptx"]:
237+
raise ValueError(f"Unsupported target type: {target_type}")
238+
code = None
239+
if target_type == "cubin":
240+
cubin_size = nvjitlink.get_linked_cubin_size(self._handle)
241+
code = bytearray(cubin_size)
242+
nvjitlink.get_linked_cubin(self._handle, code)
243+
else:
244+
ptx_size = nvjitlink.get_linked_ptx_size(self._handle)
245+
code = bytearray(ptx_size)
246+
nvjitlink.get_linked_ptx(self._handle, code)
247+
248+
return ObjectCode(bytes(code), target_type)
249+
250+
def get_error_log(self) -> str:
251+
log_size = nvjitlink.get_error_log_size(self._handle)
252+
log = bytearray(log_size)
253+
nvjitlink.get_error_log(self._handle, log)
254+
return log.decode()
255+
256+
def get_info_log(self) -> str:
257+
log_size = nvjitlink.get_info_log_size(self._handle)
258+
log = bytearray(log_size)
259+
nvjitlink.get_info_log(self._handle, log)
260+
return log.decode()
261+
262+
def _input_type_from_code_type(self, code_type: str) -> nvjitlink.InputType:
263+
# this list is based on the supported values for code_type in the ObjectCode class definition.
264+
# nvjitlink supports other options for input type
265+
if code_type == "ptx":
266+
return nvjitlink.InputType.PTX
267+
elif code_type == "cubin":
268+
return nvjitlink.InputType.CUBIN
269+
elif code_type == "fatbin":
270+
return nvjitlink.InputType.FATBIN
271+
elif code_type == "ltoir":
272+
return nvjitlink.InputType.LTOIR
273+
elif code_type == "object":
274+
return nvjitlink.InputType.OBJECT
275+
else:
276+
raise ValueError(f"Unknown code_type associated with ObjectCode: {code_type}")
277+
278+
@property
279+
def handle(self) -> int:
280+
return self._handle
281+
282+
def __del__(self):
283+
if self._handle is not None:
284+
nvjitlink.destroy(self._handle)
285+
self._handle = None

cuda_core/docs/source/api.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,8 @@ CUDA compilation toolchain
3131
:toctree: generated/
3232

3333
Program
34+
Linker
35+
36+
:template: dataclass.rst
37+
38+
LinkerOptions

cuda_core/docs/source/release.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ maxdepth: 3
66
---
77
88
0.1.0 <release/0.1.0-notes>
9+
0.2.0 <release/0.2.0-notes>
910
```

cuda_core/docs/source/release/0.1.0-notes.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# `cuda.core` Release notes
22

3-
Released on Nov 8, 2024
3+
Released on Nov XX, 2024
44

55
## Hightlights
6-
- Initial beta release
6+
- Initial EA1 (early access) release
77
- Supports all platforms that CUDA is supported
88
- Supports all CUDA 11.x/12.x drivers
99
- Supports all CUDA 11.x/12.x Toolkits
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# `cuda.core` Release notes
2+
3+
Released on Nov <TODO>, 2024
4+
5+
## Hightlights
6+
- Addition of the Linker class which gives object oriented and pythonic access to the nvJitLink API.
7+
8+
## Limitations
9+
10+
-The Linker class only supports cuda >=12. For cuda <12, use low level cuLink API.
11+
<TODO>

0 commit comments

Comments
 (0)