Coverage for cuda / bindings / _internal / nvfatbin.pyx: 67.72%
189 statements
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +0000
« prev ^ index » next coverage.py v7.13.4, created at 2026-03-08 01:07 +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.1.1. Do not modify it directly.
7from libc.stdint cimport intptr_t, uintptr_t
9import threading
10from .utils import FunctionNotFoundError, NotSupportedError
12from cuda.pathfinder import load_nvidia_dynamic_lib
15###############################################################################
16# Extern
17###############################################################################
19# You must 'from .utils import NotSupportedError' before using this template
21cdef extern from "<dlfcn.h>" nogil:
22 void* dlopen(const char*, int)
23 char* dlerror()
24 void* dlsym(void*, const char*)
25 int dlclose(void*)
27 enum:
28 RTLD_LAZY
29 RTLD_NOW
30 RTLD_GLOBAL
31 RTLD_LOCAL
33 const void* RTLD_DEFAULT 'RTLD_DEFAULT'
35cdef int get_cuda_version():
36 cdef void* handle = NULL
37 cdef int err, driver_ver = 0
39 # Load driver to check version
40 handle = dlopen('libcuda.so.1', RTLD_NOW | RTLD_GLOBAL)
41 if handle == NULL:
42 err_msg = dlerror()
43 raise NotSupportedError(f'CUDA driver is not found ({err_msg.decode()})')
44 cuDriverGetVersion = dlsym(handle, "cuDriverGetVersion")
45 if cuDriverGetVersion == NULL:
46 raise RuntimeError('Did not find cuDriverGetVersion symbol in libcuda.so.1')
47 err = (<int (*)(int*) noexcept nogil>cuDriverGetVersion)(&driver_ver)
48 if err != 0:
49 raise RuntimeError(f'cuDriverGetVersion returned error code {err}')
51 return driver_ver
55###############################################################################
56# Wrapper init
57###############################################################################
59cdef object __symbol_lock = threading.Lock()
60cdef bint __py_nvfatbin_init = False
62cdef void* __nvFatbinGetErrorString = NULL
63cdef void* __nvFatbinCreate = NULL
64cdef void* __nvFatbinDestroy = NULL
65cdef void* __nvFatbinAddPTX = NULL
66cdef void* __nvFatbinAddCubin = NULL
67cdef void* __nvFatbinAddLTOIR = NULL
68cdef void* __nvFatbinSize = NULL
69cdef void* __nvFatbinGet = NULL
70cdef void* __nvFatbinVersion = NULL
71cdef void* __nvFatbinAddReloc = NULL
72cdef void* __nvFatbinAddTileIR = NULL
75cdef void* load_library() except* with gil:
76 cdef uintptr_t handle = load_nvidia_dynamic_lib("nvfatbin")._handle_uint
77 return <void*>handle
80cdef int _init_nvfatbin() except -1 nogil:
81 global __py_nvfatbin_init
83 cdef void* handle = NULL
85 with gil, __symbol_lock:
86 # Recheck the flag after obtaining the locks
87 if __py_nvfatbin_init:
88 return 0
90 # Load function
91 global __nvFatbinGetErrorString
92 __nvFatbinGetErrorString = dlsym(RTLD_DEFAULT, 'nvFatbinGetErrorString')
93 if __nvFatbinGetErrorString == NULL:
94 if handle == NULL:
95 handle = load_library()
96 __nvFatbinGetErrorString = dlsym(handle, 'nvFatbinGetErrorString')
98 global __nvFatbinCreate
99 __nvFatbinCreate = dlsym(RTLD_DEFAULT, 'nvFatbinCreate')
100 if __nvFatbinCreate == NULL:
101 if handle == NULL:
102 handle = load_library()
103 __nvFatbinCreate = dlsym(handle, 'nvFatbinCreate')
105 global __nvFatbinDestroy
106 __nvFatbinDestroy = dlsym(RTLD_DEFAULT, 'nvFatbinDestroy')
107 if __nvFatbinDestroy == NULL:
108 if handle == NULL:
109 handle = load_library()
110 __nvFatbinDestroy = dlsym(handle, 'nvFatbinDestroy')
112 global __nvFatbinAddPTX
113 __nvFatbinAddPTX = dlsym(RTLD_DEFAULT, 'nvFatbinAddPTX')
114 if __nvFatbinAddPTX == NULL:
115 if handle == NULL:
116 handle = load_library()
117 __nvFatbinAddPTX = dlsym(handle, 'nvFatbinAddPTX')
119 global __nvFatbinAddCubin
120 __nvFatbinAddCubin = dlsym(RTLD_DEFAULT, 'nvFatbinAddCubin')
121 if __nvFatbinAddCubin == NULL:
122 if handle == NULL:
123 handle = load_library()
124 __nvFatbinAddCubin = dlsym(handle, 'nvFatbinAddCubin')
126 global __nvFatbinAddLTOIR
127 __nvFatbinAddLTOIR = dlsym(RTLD_DEFAULT, 'nvFatbinAddLTOIR')
128 if __nvFatbinAddLTOIR == NULL:
129 if handle == NULL:
130 handle = load_library()
131 __nvFatbinAddLTOIR = dlsym(handle, 'nvFatbinAddLTOIR')
133 global __nvFatbinSize
134 __nvFatbinSize = dlsym(RTLD_DEFAULT, 'nvFatbinSize')
135 if __nvFatbinSize == NULL:
136 if handle == NULL:
137 handle = load_library()
138 __nvFatbinSize = dlsym(handle, 'nvFatbinSize')
140 global __nvFatbinGet
141 __nvFatbinGet = dlsym(RTLD_DEFAULT, 'nvFatbinGet')
142 if __nvFatbinGet == NULL:
143 if handle == NULL: 1aIJKLMNOPQRSTUVWXYZbcdewxyzFGfghijklmnopqrstuvHA01
144 handle = load_library() 1IJKLMNOPQRSTUVWXYZbcdewxyzFGfghijklmnopqrstuvHA01
145 __nvFatbinGet = dlsym(handle, 'nvFatbinGet')
147 global __nvFatbinVersion
148 __nvFatbinVersion = dlsym(RTLD_DEFAULT, 'nvFatbinVersion')
149 if __nvFatbinVersion == NULL:
150 if handle == NULL:
151 handle = load_library()
152 __nvFatbinVersion = dlsym(handle, 'nvFatbinVersion')
154 global __nvFatbinAddReloc
155 __nvFatbinAddReloc = dlsym(RTLD_DEFAULT, 'nvFatbinAddReloc')
156 if __nvFatbinAddReloc == NULL:
157 if handle == NULL:
158 handle = load_library()
159 __nvFatbinAddReloc = dlsym(handle, 'nvFatbinAddReloc')
161 global __nvFatbinAddTileIR
162 __nvFatbinAddTileIR = dlsym(RTLD_DEFAULT, 'nvFatbinAddTileIR')
163 if __nvFatbinAddTileIR == NULL:
164 if handle == NULL:
165 handle = load_library()
166 __nvFatbinAddTileIR = dlsym(handle, 'nvFatbinAddTileIR')
168 __py_nvfatbin_init = True
169 return 0
172cdef inline int _check_or_init_nvfatbin() except -1 nogil:
173 if __py_nvfatbin_init: 1aIJKLMNOPQRSTUVWXYZbcdewxyzFGfghijklmnopqrstuBCDEvHA01
174 return 0 1IJKLMNOPQRSTUVWXYZbcdewxyzFGfghijklmnopqrstuBCDEvHA01
176 return _init_nvfatbin()
178cdef dict func_ptrs = None
181cpdef dict _inspect_function_pointers():
182 global func_ptrs
183 if func_ptrs is not None:
184 return func_ptrs
186 _check_or_init_nvfatbin()
187 cdef dict data = {}
189 global __nvFatbinGetErrorString
190 data["__nvFatbinGetErrorString"] = <intptr_t>__nvFatbinGetErrorString
192 global __nvFatbinCreate
193 data["__nvFatbinCreate"] = <intptr_t>__nvFatbinCreate
195 global __nvFatbinDestroy
196 data["__nvFatbinDestroy"] = <intptr_t>__nvFatbinDestroy
198 global __nvFatbinAddPTX
199 data["__nvFatbinAddPTX"] = <intptr_t>__nvFatbinAddPTX
201 global __nvFatbinAddCubin
202 data["__nvFatbinAddCubin"] = <intptr_t>__nvFatbinAddCubin
204 global __nvFatbinAddLTOIR
205 data["__nvFatbinAddLTOIR"] = <intptr_t>__nvFatbinAddLTOIR
207 global __nvFatbinSize
208 data["__nvFatbinSize"] = <intptr_t>__nvFatbinSize
210 global __nvFatbinGet
211 data["__nvFatbinGet"] = <intptr_t>__nvFatbinGet 1IJKLMNOPQRSTUVWXYZ
213 global __nvFatbinVersion
214 data["__nvFatbinVersion"] = <intptr_t>__nvFatbinVersion 1IJKLMNOPQRSTUVWXYZ
216 global __nvFatbinAddReloc
217 data["__nvFatbinAddReloc"] = <intptr_t>__nvFatbinAddReloc
219 global __nvFatbinAddTileIR
220 data["__nvFatbinAddTileIR"] = <intptr_t>__nvFatbinAddTileIR 1bcdewxyzFGfghijklmnopqrstuvHA1
222 func_ptrs = data
223 return data
226cpdef _inspect_function_pointer(str name):
227 global func_ptrs
228 if func_ptrs is None:
229 func_ptrs = _inspect_function_pointers()
230 return func_ptrs[name] 1bcdewxyzFGfghijklmnopqrstuvHA
233###############################################################################
234# Wrapper functions
235###############################################################################
237cdef const char* _nvFatbinGetErrorString(nvFatbinResult result) except?NULL nogil:
238 global __nvFatbinGetErrorString
239 _check_or_init_nvfatbin() 1IJKLMNOPQRSTUVWXYZ
240 if __nvFatbinGetErrorString == NULL: 1IJKLMNOPQRSTUVWXYZfghijklmnopqrstu
241 with gil: 1fghijklmnopqrstu
242 raise FunctionNotFoundError("function nvFatbinGetErrorString is not found")
243 return (<const char* (*)(nvFatbinResult) noexcept nogil>__nvFatbinGetErrorString)( 1IJKLMNOPQRSTUVWXYZ
244 result)
247cdef nvFatbinResult _nvFatbinCreate(nvFatbinHandle* handle_indirect, const char** options, size_t optionsCount) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
248 global __nvFatbinCreate
249 _check_or_init_nvfatbin() 1bcdewxyzFGfghijklmnopqrstuBCDEvHA1
250 if __nvFatbinCreate == NULL: 1bcdewxyzFGfghijklmnopqrstuBCDEvHA1
251 with gil: 1wxyzFG
252 raise FunctionNotFoundError("function nvFatbinCreate is not found")
253 return (<nvFatbinResult (*)(nvFatbinHandle*, const char**, size_t) noexcept nogil>__nvFatbinCreate)( 1bcdewxyzFGfghijklmnopqrstuBCDEvHA1
254 handle_indirect, options, optionsCount)
257cdef nvFatbinResult _nvFatbinDestroy(nvFatbinHandle* handle_indirect) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
258 global __nvFatbinDestroy
259 _check_or_init_nvfatbin() 1bcdewxyzFGfghijklmnopqrstuBCDEvHA
260 if __nvFatbinDestroy == NULL: 1bcdewxyzFGfghijklmnopqrstuBCDEvHA
261 with gil: 1bcde
262 raise FunctionNotFoundError("function nvFatbinDestroy is not found")
263 return (<nvFatbinResult (*)(nvFatbinHandle*) noexcept nogil>__nvFatbinDestroy)( 1bcdewxyzFGfghijklmnopqrstuBCDEvHA
264 handle_indirect)
267cdef nvFatbinResult _nvFatbinAddPTX(nvFatbinHandle handle, const char* code, size_t size, const char* arch, const char* identifier, const char* optionsCmdLine) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
268 global __nvFatbinAddPTX
269 _check_or_init_nvfatbin() 1fghijklmnopqrstu
270 if __nvFatbinAddPTX == NULL: 1bcdewxyzfghijklmnopqrstuvA
271 with gil: 1bcdewxyzfghijklmnopqrstuvA
272 raise FunctionNotFoundError("function nvFatbinAddPTX is not found")
273 return (<nvFatbinResult (*)(nvFatbinHandle, const char*, size_t, const char*, const char*, const char*) noexcept nogil>__nvFatbinAddPTX)( 1fghijklmnopqrstu
274 handle, code, size, arch, identifier, optionsCmdLine)
277cdef nvFatbinResult _nvFatbinAddCubin(nvFatbinHandle handle, const void* code, size_t size, const char* arch, const char* identifier) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
278 global __nvFatbinAddCubin
279 _check_or_init_nvfatbin() 1wxyzFG
280 if __nvFatbinAddCubin == NULL: 1bcdewxyzFGfghijklmnopqrstuvA
281 with gil: 1bcdewxyzfghijklmnopqrstuvA
282 raise FunctionNotFoundError("function nvFatbinAddCubin is not found")
283 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*, const char*) noexcept nogil>__nvFatbinAddCubin)( 1wxyzFG
284 handle, code, size, arch, identifier)
287cdef nvFatbinResult _nvFatbinAddLTOIR(nvFatbinHandle handle, const void* code, size_t size, const char* arch, const char* identifier, const char* optionsCmdLine) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
288 global __nvFatbinAddLTOIR
289 _check_or_init_nvfatbin() 1bcde
290 if __nvFatbinAddLTOIR == NULL: 1abcde0
291 with gil: 1a0
292 raise FunctionNotFoundError("function nvFatbinAddLTOIR is not found")
293 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*, const char*, const char*) noexcept nogil>__nvFatbinAddLTOIR)( 1bcde
294 handle, code, size, arch, identifier, optionsCmdLine)
297cdef nvFatbinResult _nvFatbinSize(nvFatbinHandle handle, size_t* size) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
298 global __nvFatbinSize
299 _check_or_init_nvfatbin() 1bcdewxyzfghijklmnopqrstuBCDEvA
300 if __nvFatbinSize == NULL: 1bcdewxyzfghijklmnopqrstuBCDEvA
301 with gil:
302 raise FunctionNotFoundError("function nvFatbinSize is not found")
303 return (<nvFatbinResult (*)(nvFatbinHandle, size_t*) noexcept nogil>__nvFatbinSize)( 1bcdewxyzfghijklmnopqrstuBCDEvA
304 handle, size)
307cdef nvFatbinResult _nvFatbinGet(nvFatbinHandle handle, void* buffer) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
308 global __nvFatbinGet
309 _check_or_init_nvfatbin() 1bcdewxyzfghijklmnopqrstuBCDEvA
310 if __nvFatbinGet == NULL: 1bcdewxyzfghijklmnopqrstuBCDEvA
311 with gil: 1v
312 raise FunctionNotFoundError("function nvFatbinGet is not found")
313 return (<nvFatbinResult (*)(nvFatbinHandle, void*) noexcept nogil>__nvFatbinGet)( 1bcdewxyzfghijklmnopqrstuBCDEvA
314 handle, buffer)
317cdef nvFatbinResult _nvFatbinVersion(unsigned int* major, unsigned int* minor) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
318 global __nvFatbinVersion
319 _check_or_init_nvfatbin() 1a0
320 if __nvFatbinVersion == NULL: 1a0
321 with gil:
322 raise FunctionNotFoundError("function nvFatbinVersion is not found")
323 return (<nvFatbinResult (*)(unsigned int*, unsigned int*) noexcept nogil>__nvFatbinVersion)( 1a0
324 major, minor)
327cdef nvFatbinResult _nvFatbinAddReloc(nvFatbinHandle handle, const void* code, size_t size) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
328 global __nvFatbinAddReloc
329 _check_or_init_nvfatbin() 1BCDE
330 if __nvFatbinAddReloc == NULL: 1BCDE
331 with gil:
332 raise FunctionNotFoundError("function nvFatbinAddReloc is not found")
333 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t) noexcept nogil>__nvFatbinAddReloc)( 1BCDE
334 handle, code, size)
337cdef nvFatbinResult _nvFatbinAddTileIR(nvFatbinHandle handle, const void* code, size_t size, const char* identifier, const char* optionsCmdLine) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil:
338 global __nvFatbinAddTileIR
339 _check_or_init_nvfatbin() 1v
340 if __nvFatbinAddTileIR == NULL: 1v
341 with gil:
342 raise FunctionNotFoundError("function nvFatbinAddTileIR is not found")
343 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*, const char*) noexcept nogil>__nvFatbinAddTileIR)( 1v
344 handle, code, size, identifier, optionsCmdLine)