Coverage for cuda/bindings/nvfatbin.pyx: 86.43%
140 statements
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-13 01:38 +0000
« prev ^ index » next coverage.py v7.14.1, created at 2026-06-13 01:38 +0000
1# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2#
3# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE
4#
5# This code was automatically generated across versions from 12.4.1 to 13.3.0, generator version 0.3.1.dev1719+g565f73f4e. Do not modify it directly.
7cimport cython # NOQA
9from ._internal.utils cimport (get_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr,
10 get_buffer_pointer, get_resource_ptrs)
12from cuda.bindings._internal._fast_enum import FastEnum as _IntEnum
13from libcpp.vector cimport vector
16###############################################################################
17# Enum
18###############################################################################
20class Result(_IntEnum):
21 """
22 The enumerated type nvFatbinResult defines API call result codes.
23 nvFatbin APIs return nvFatbinResult codes to indicate the result.
25 See `nvFatbinResult`.
26 """
27 SUCCESS = NVFATBIN_SUCCESS
28 ERROR_INTERNAL = NVFATBIN_ERROR_INTERNAL
29 ERROR_ELF_ARCH_MISMATCH = NVFATBIN_ERROR_ELF_ARCH_MISMATCH
30 ERROR_ELF_SIZE_MISMATCH = NVFATBIN_ERROR_ELF_SIZE_MISMATCH
31 ERROR_MISSING_PTX_VERSION = NVFATBIN_ERROR_MISSING_PTX_VERSION
32 ERROR_NULL_POINTER = NVFATBIN_ERROR_NULL_POINTER
33 ERROR_COMPRESSION_FAILED = NVFATBIN_ERROR_COMPRESSION_FAILED
34 ERROR_COMPRESSED_SIZE_EXCEEDED = NVFATBIN_ERROR_COMPRESSED_SIZE_EXCEEDED
35 ERROR_UNRECOGNIZED_OPTION = NVFATBIN_ERROR_UNRECOGNIZED_OPTION
36 ERROR_INVALID_ARCH = NVFATBIN_ERROR_INVALID_ARCH
37 ERROR_INVALID_NVVM = NVFATBIN_ERROR_INVALID_NVVM
38 ERROR_EMPTY_INPUT = NVFATBIN_ERROR_EMPTY_INPUT
39 ERROR_MISSING_PTX_ARCH = NVFATBIN_ERROR_MISSING_PTX_ARCH
40 ERROR_PTX_ARCH_MISMATCH = NVFATBIN_ERROR_PTX_ARCH_MISMATCH
41 ERROR_MISSING_FATBIN = NVFATBIN_ERROR_MISSING_FATBIN
42 ERROR_INVALID_INDEX = NVFATBIN_ERROR_INVALID_INDEX
43 ERROR_IDENTIFIER_REUSE = NVFATBIN_ERROR_IDENTIFIER_REUSE
44 ERROR_INTERNAL_PTX_OPTION = NVFATBIN_ERROR_INTERNAL_PTX_OPTION
47###############################################################################
48# Error handling
49###############################################################################
51class nvFatbinError(Exception):
53 def __init__(self, status):
54 self.status = status 1DEK
55 s = Result(status) 1DEK
56 cdef str err = f"{s.name} ({s.value})" 1DEK
57 super(nvFatbinError, self).__init__(err) 1DEK
59 def __reduce__(self):
60 return (type(self), (self.status,))
63@cython.profile(False)
64cdef int check_status(int status) except 1 nogil:
65 if status != 0: 1abcdefghyzABDEijklmnopqrstuvwxFGHICLJMK
66 with gil: 1DEK
67 raise nvFatbinError(status) 1DEK
68 return status 1abcdefghyzABDEijklmnopqrstuvwxFGHICLJM
71###############################################################################
72# Wrapper functions
73###############################################################################
75cpdef destroy(intptr_t handle):
76 """nvFatbinDestroy frees the memory associated with the given handle.
78 Args:
79 handle (intptr_t): nvFatbin handle.
81 .. seealso:: `nvFatbinDestroy`
82 """
83 cdef Handle h = <Handle>handle 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJ
84 with nogil: 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJ
85 status = nvFatbinDestroy(&h) 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJ
86 check_status(status) 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJ
89cpdef str get_error_string(int result):
90 """nvFatbinGetErrorString returns an error description string for each error code.
92 Args:
93 result (Result): error code.
95 .. seealso:: `nvFatbinGetErrorString`
96 """
97 cdef const char* _output_
98 cdef bytes _output_bytes_
99 _output_ = nvFatbinGetErrorString(<_Result>result) 1NOPQRSTUVWXYZ01234
101 if _output_ == NULL: 1NOPQRSTUVWXYZ01234
102 return "" 14
104 _output_bytes_ = <bytes>_output_ 1NOPQRSTUVWXYZ0123
105 return _output_bytes_.decode() 1NOPQRSTUVWXYZ0123
108cpdef intptr_t create(options, size_t options_count) except -1:
109 """nvFatbinCreate creates a new handle.
111 Args:
112 options (object): An array of strings, each containing a single option. It can be:
114 - an :class:`int` as the pointer address to the nested sequence, or
115 - a Python sequence of :class:`int`\s, each of which is a pointer address
116 to a valid sequence of 'char', or
117 - a nested Python sequence of ``str``.
119 options_count (size_t): Number of options.
121 Returns:
122 intptr_t: Address of nvFatbin handle.
124 .. seealso:: `nvFatbinCreate`
125 """
126 cdef nested_resource[ char ] _options_
127 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJK
128 cdef Handle handle_indirect
129 with nogil: 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJK
130 __status__ = nvFatbinCreate(&handle_indirect, <const char**>(_options_.ptrs.data()), options_count) 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJK
131 check_status(__status__) 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJK
132 return <intptr_t>handle_indirect 1bcdefghyzABDEijklmnopqrstuvwxFGHICLJ
135cpdef add_ptx(intptr_t handle, code, size_t size, arch, identifier, options_cmd_line):
136 """nvFatbinAddPTX adds PTX to the fatbinary.
138 Args:
139 handle (intptr_t): nvFatbin handle.
140 code (bytes): The PTX code.
141 size (size_t): The size of the PTX code.
142 arch (str): The numerical architecture that this PTX is for (the XX of any sm_XX, lto_XX, or compute_XX).
143 identifier (str): Name of the PTX, useful when extracting the fatbin with tools like cuobjdump.
144 options_cmd_line (str): Options used during JIT compilation.
146 .. seealso:: `nvFatbinAddPTX`
147 """
148 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1bcdijklmnopqrstuvwx
149 if not isinstance(arch, str): 1bcdijklmnopqrstuvwx
150 raise TypeError("arch must be a Python str")
151 cdef bytes _temp_arch_ = (<str>arch).encode() 1bcdijklmnopqrstuvwx
152 cdef char* _arch_ = _temp_arch_ 1bcdijklmnopqrstuvwx
153 if not isinstance(identifier, str): 1bcdijklmnopqrstuvwx
154 raise TypeError("identifier must be a Python str")
155 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1bcdijklmnopqrstuvwx
156 cdef char* _identifier_ = _temp_identifier_ 1bcdijklmnopqrstuvwx
157 if not isinstance(options_cmd_line, str): 1bcdijklmnopqrstuvwx
158 raise TypeError("options_cmd_line must be a Python str")
159 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1bcdijklmnopqrstuvwx
160 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1bcdijklmnopqrstuvwx
161 with nogil: 1bcdijklmnopqrstuvwx
162 __status__ = nvFatbinAddPTX(<Handle>handle, <const char*>_code_, size, <const char*>_arch_, <const char*>_identifier_, <const char*>_options_cmd_line_) 1bcdijklmnopqrstuvwx
163 check_status(__status__) 1bcdijklmnopqrstuvwx
166cpdef add_cubin(intptr_t handle, code, size_t size, arch, identifier):
167 """nvFatbinAddCubin adds a CUDA binary to the fatbinary.
169 Args:
170 handle (intptr_t): nvFatbin handle.
171 code (bytes): The cubin.
172 size (size_t): The size of the cubin.
173 arch (str): The numerical architecture that this cubin is for (the XX of any sm_XX, lto_XX, or compute_XX).
174 identifier (str): Name of the cubin, useful when extracting the fatbin with tools like cuobjdump.
176 .. seealso:: `nvFatbinAddCubin`
177 """
178 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1bcdyzABDE
179 if not isinstance(arch, str): 1bcdyzABDE
180 raise TypeError("arch must be a Python str")
181 cdef bytes _temp_arch_ = (<str>arch).encode() 1bcdyzABDE
182 cdef char* _arch_ = _temp_arch_ 1bcdyzABDE
183 if not isinstance(identifier, str): 1bcdyzABDE
184 raise TypeError("identifier must be a Python str")
185 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1bcdyzABDE
186 cdef char* _identifier_ = _temp_identifier_ 1bcdyzABDE
187 with nogil: 1bcdyzABDE
188 __status__ = nvFatbinAddCubin(<Handle>handle, <const void*>_code_, size, <const char*>_arch_, <const char*>_identifier_) 1bcdyzABDE
189 check_status(__status__) 1bcdyzABDE
192cpdef add_ltoir(intptr_t handle, code, size_t size, arch, identifier, options_cmd_line):
193 """nvFatbinAddLTOIR adds LTOIR to the fatbinary.
195 Args:
196 handle (intptr_t): nvFatbin handle.
197 code (bytes): The LTOIR code.
198 size (size_t): The size of the LTOIR code.
199 arch (str): The numerical architecture that this LTOIR is for (the XX of any sm_XX, lto_XX, or compute_XX).
200 identifier (str): Name of the LTOIR, useful when extracting the fatbin with tools like cuobjdump.
201 options_cmd_line (str): Options used during JIT compilation.
203 .. seealso:: `nvFatbinAddLTOIR`
204 """
205 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1efgh
206 if not isinstance(arch, str): 1efgh
207 raise TypeError("arch must be a Python str")
208 cdef bytes _temp_arch_ = (<str>arch).encode() 1efgh
209 cdef char* _arch_ = _temp_arch_ 1efgh
210 if not isinstance(identifier, str): 1efgh
211 raise TypeError("identifier must be a Python str")
212 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1efgh
213 cdef char* _identifier_ = _temp_identifier_ 1efgh
214 if not isinstance(options_cmd_line, str): 1efgh
215 raise TypeError("options_cmd_line must be a Python str")
216 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1efgh
217 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1efgh
218 with nogil: 1efgh
219 __status__ = nvFatbinAddLTOIR(<Handle>handle, <const void*>_code_, size, <const char*>_arch_, <const char*>_identifier_, <const char*>_options_cmd_line_) 1efgh
220 check_status(__status__) 1efgh
223cpdef size_t size(intptr_t handle) except? 0:
224 """nvFatbinSize returns the fatbinary's size.
226 Args:
227 handle (intptr_t): nvFatbin handle.
229 Returns:
230 size_t: The fatbinary's size.
232 .. seealso:: `nvFatbinSize`
233 """
234 cdef size_t size
235 with nogil: 1bcdefghyzABijklmnopqrstuvwxFGHICJ
236 __status__ = nvFatbinSize(<Handle>handle, &size) 1bcdefghyzABijklmnopqrstuvwxFGHICJ
237 check_status(__status__) 1bcdefghyzABijklmnopqrstuvwxFGHICJ
238 return size 1bcdefghyzABijklmnopqrstuvwxFGHICJ
241cpdef get(intptr_t handle, buffer):
242 """nvFatbinGet returns the completed fatbinary.
244 Args:
245 handle (intptr_t): nvFatbin handle.
246 buffer (bytes): memory to store fatbinary.
248 .. seealso:: `nvFatbinGet`
249 """
250 cdef void* _buffer_ = get_buffer_pointer(buffer, -1, readonly=False) 1bcdefghyzABijklmnopqrstuvwxFGHICJ
251 with nogil: 1bcdefghyzABijklmnopqrstuvwxFGHICJ
252 __status__ = nvFatbinGet(<Handle>handle, <void*>_buffer_) 1bcdefghyzABijklmnopqrstuvwxFGHICJ
253 check_status(__status__) 1bcdefghyzABijklmnopqrstuvwxFGHICJ
256cpdef tuple version():
257 """nvFatbinVersion returns the current version of nvFatbin.
259 Returns:
260 A 2-tuple containing:
262 - unsigned int: The major version.
263 - unsigned int: The minor version.
265 .. seealso:: `nvFatbinVersion`
266 """
267 cdef unsigned int major
268 cdef unsigned int minor
269 with nogil: 1aM
270 __status__ = nvFatbinVersion(&major, &minor) 1aM
271 check_status(__status__) 1aM
272 return (major, minor) 1aM
275cpdef add_index(intptr_t handle, code, size_t size, identifier):
276 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True)
277 if not isinstance(identifier, str):
278 raise TypeError("identifier must be a Python str")
279 cdef bytes _temp_identifier_ = (<str>identifier).encode()
280 cdef char* _identifier_ = _temp_identifier_
281 with nogil:
282 __status__ = nvFatbinAddIndex(<Handle>handle, <const void*>_code_, size, <const char*>_identifier_)
283 check_status(__status__)
286cpdef add_reloc(intptr_t handle, code, size_t size):
287 """nvFatbinAddReloc adds relocatable PTX entries from a host object to the fatbinary.
289 Args:
290 handle (intptr_t): nvFatbin handle.
291 code (bytes): The host object image.
292 size (size_t): The size of the host object image code.
294 .. seealso:: `nvFatbinAddReloc`
295 """
296 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1FGHI
297 with nogil: 1FGHI
298 __status__ = nvFatbinAddReloc(<Handle>handle, <const void*>_code_, size) 1FGHI
299 check_status(__status__) 1FGHI
302cpdef add_tile_ir(intptr_t handle, code, size_t size, identifier, options_cmd_line):
303 """nvFatbinAddTileIR adds Tile IR to the fatbinary.
305 Args:
306 handle (intptr_t): nvFatbin handle.
307 code (bytes): The Tile IR.
308 size (size_t): The size of the Tile IR.
309 identifier (str): Name of the Tile IR, useful when extracting the fatbin with tools like cuobjdump.
310 options_cmd_line (str): Options used during JIT compilation.
312 .. seealso:: `nvFatbinAddTileIR`
313 """
314 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1C
315 if not isinstance(identifier, str): 1C
316 raise TypeError("identifier must be a Python str")
317 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1C
318 cdef char* _identifier_ = _temp_identifier_ 1C
319 if not isinstance(options_cmd_line, str): 1C
320 raise TypeError("options_cmd_line must be a Python str")
321 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1C
322 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1C
323 with nogil: 1C
324 __status__ = nvFatbinAddTileIR(<Handle>handle, <const void*>_code_, size, <const char*>_identifier_, <const char*>_options_cmd_line_) 1C
325 check_status(__status__) 1C