Coverage for cuda/bindings/_internal/nvfatbin.pyx: 60.40%

202 statements  

« 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. 

6  

7from libc.stdint cimport intptr_t, uintptr_t 

8  

9import threading 

10from .utils import FunctionNotFoundError, NotSupportedError 

11  

12from cuda.pathfinder import load_nvidia_dynamic_lib 

13  

14  

15############################################################################### 

16# Extern 

17############################################################################### 

18  

19# You must 'from .utils import NotSupportedError' before using this template 

20  

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*) 

26  

27 enum: 

28 RTLD_LAZY 

29 RTLD_NOW 

30 RTLD_GLOBAL 

31 RTLD_LOCAL 

32  

33 const void* RTLD_DEFAULT 'RTLD_DEFAULT' 

34  

35cdef int get_cuda_version(): 

36 cdef void* handle = NULL 

37 cdef int err, driver_ver = 0 

38  

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}') 

50  

51 return driver_ver 

52  

53  

54  

55############################################################################### 

56# Wrapper init 

57############################################################################### 

58  

59cdef object __symbol_lock = threading.Lock() 

60cdef bint __py_nvfatbin_init = False 

61  

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* __nvFatbinAddIndex = NULL 

72cdef void* __nvFatbinAddReloc = NULL 

73cdef void* __nvFatbinAddTileIR = NULL 

74  

75  

76cdef void* load_library() except* with gil: 

77 cdef uintptr_t handle = load_nvidia_dynamic_lib("nvfatbin")._handle_uint 

78 return <void*>handle 

79  

80  

81cdef int _init_nvfatbin() except -1 nogil: 

82 global __py_nvfatbin_init 

83  

84 cdef void* handle = NULL 

85  

86 with gil, __symbol_lock: 

87 # Recheck the flag after obtaining the locks 

88 if __py_nvfatbin_init: 

89 return 0 

90  

91 # Load function 

92 global __nvFatbinGetErrorString 

93 __nvFatbinGetErrorString = dlsym(RTLD_DEFAULT, 'nvFatbinGetErrorString') 

94 if __nvFatbinGetErrorString == NULL: 

95 if handle == NULL: 

96 handle = load_library() 

97 __nvFatbinGetErrorString = dlsym(handle, 'nvFatbinGetErrorString') 

98  

99 global __nvFatbinCreate 

100 __nvFatbinCreate = dlsym(RTLD_DEFAULT, 'nvFatbinCreate') 

101 if __nvFatbinCreate == NULL: 

102 if handle == NULL: 

103 handle = load_library() 

104 __nvFatbinCreate = dlsym(handle, 'nvFatbinCreate') 

105  

106 global __nvFatbinDestroy 

107 __nvFatbinDestroy = dlsym(RTLD_DEFAULT, 'nvFatbinDestroy') 

108 if __nvFatbinDestroy == NULL: 

109 if handle == NULL: 

110 handle = load_library() 

111 __nvFatbinDestroy = dlsym(handle, 'nvFatbinDestroy') 

112  

113 global __nvFatbinAddPTX 

114 __nvFatbinAddPTX = dlsym(RTLD_DEFAULT, 'nvFatbinAddPTX') 

115 if __nvFatbinAddPTX == NULL: 

116 if handle == NULL: 

117 handle = load_library() 

118 __nvFatbinAddPTX = dlsym(handle, 'nvFatbinAddPTX') 

119  

120 global __nvFatbinAddCubin 

121 __nvFatbinAddCubin = dlsym(RTLD_DEFAULT, 'nvFatbinAddCubin') 

122 if __nvFatbinAddCubin == NULL: 

123 if handle == NULL: 

124 handle = load_library() 

125 __nvFatbinAddCubin = dlsym(handle, 'nvFatbinAddCubin') 

126  

127 global __nvFatbinAddLTOIR 

128 __nvFatbinAddLTOIR = dlsym(RTLD_DEFAULT, 'nvFatbinAddLTOIR') 

129 if __nvFatbinAddLTOIR == NULL: 

130 if handle == NULL: 

131 handle = load_library() 

132 __nvFatbinAddLTOIR = dlsym(handle, 'nvFatbinAddLTOIR') 

133  

134 global __nvFatbinSize 

135 __nvFatbinSize = dlsym(RTLD_DEFAULT, 'nvFatbinSize') 

136 if __nvFatbinSize == NULL: 

137 if handle == NULL: 

138 handle = load_library() 

139 __nvFatbinSize = dlsym(handle, 'nvFatbinSize') 

140  

141 global __nvFatbinGet 

142 __nvFatbinGet = dlsym(RTLD_DEFAULT, 'nvFatbinGet') 

143 if __nvFatbinGet == NULL: 

144 if handle == NULL: 

145 handle = load_library() 

146 __nvFatbinGet = dlsym(handle, 'nvFatbinGet') 

147  

148 global __nvFatbinVersion 

149 __nvFatbinVersion = dlsym(RTLD_DEFAULT, 'nvFatbinVersion') 

150 if __nvFatbinVersion == NULL: 

151 if handle == NULL: 

152 handle = load_library() 

153 __nvFatbinVersion = dlsym(handle, 'nvFatbinVersion') 

154  

155 global __nvFatbinAddIndex 

156 __nvFatbinAddIndex = dlsym(RTLD_DEFAULT, 'nvFatbinAddIndex') 

157 if __nvFatbinAddIndex == NULL: 

158 if handle == NULL: 

159 handle = load_library() 

160 __nvFatbinAddIndex = dlsym(handle, 'nvFatbinAddIndex') 

161  

162 global __nvFatbinAddReloc 

163 __nvFatbinAddReloc = dlsym(RTLD_DEFAULT, 'nvFatbinAddReloc') 

164 if __nvFatbinAddReloc == NULL: 

165 if handle == NULL: 

166 handle = load_library() 

167 __nvFatbinAddReloc = dlsym(handle, 'nvFatbinAddReloc') 

168  

169 global __nvFatbinAddTileIR 

170 __nvFatbinAddTileIR = dlsym(RTLD_DEFAULT, 'nvFatbinAddTileIR') 

171 if __nvFatbinAddTileIR == NULL: 

172 if handle == NULL: 

173 handle = load_library() 

174 __nvFatbinAddTileIR = dlsym(handle, 'nvFatbinAddTileIR') 

175  

176 __py_nvfatbin_init = True 

177 return 0 

178  

179  

180cdef inline int _check_or_init_nvfatbin() except -1 nogil: 

181 if __py_nvfatbin_init: 1aBCDNOPQRSTUVWXYZ01234cdefwxyzIJghijklmnopqrstuvEFGHbKALM

182 return 0 1BCDNOPQRSTUVWXYZ01234cdefwxyzIJghijklmnopqrstuvEFGHbKALM

183  

184 return _init_nvfatbin() 

185  

186cdef dict func_ptrs = None 

187  

188  

189cpdef dict _inspect_function_pointers(): 

190 global func_ptrs 

191 if func_ptrs is not None: 

192 return func_ptrs 

193  

194 _check_or_init_nvfatbin() 

195 cdef dict data = {} 

196  

197 global __nvFatbinGetErrorString 

198 data["__nvFatbinGetErrorString"] = <intptr_t>__nvFatbinGetErrorString 

199  

200 global __nvFatbinCreate 

201 data["__nvFatbinCreate"] = <intptr_t>__nvFatbinCreate 

202  

203 global __nvFatbinDestroy 

204 data["__nvFatbinDestroy"] = <intptr_t>__nvFatbinDestroy 

205  

206 global __nvFatbinAddPTX 

207 data["__nvFatbinAddPTX"] = <intptr_t>__nvFatbinAddPTX 

208  

209 global __nvFatbinAddCubin 

210 data["__nvFatbinAddCubin"] = <intptr_t>__nvFatbinAddCubin 

211  

212 global __nvFatbinAddLTOIR 

213 data["__nvFatbinAddLTOIR"] = <intptr_t>__nvFatbinAddLTOIR 

214  

215 global __nvFatbinSize 

216 data["__nvFatbinSize"] = <intptr_t>__nvFatbinSize 

217  

218 global __nvFatbinGet 

219 data["__nvFatbinGet"] = <intptr_t>__nvFatbinGet 

220  

221 global __nvFatbinVersion 

222 data["__nvFatbinVersion"] = <intptr_t>__nvFatbinVersion 

223  

224 global __nvFatbinAddIndex 

225 data["__nvFatbinAddIndex"] = <intptr_t>__nvFatbinAddIndex 

226  

227 global __nvFatbinAddReloc 

228 data["__nvFatbinAddReloc"] = <intptr_t>__nvFatbinAddReloc 1cdefwxyzIJghijklmnopqrstuvbKAM

229  

230 global __nvFatbinAddTileIR 

231 data["__nvFatbinAddTileIR"] = <intptr_t>__nvFatbinAddTileIR 1cdefwxyzIJghijklmnopqrstuvbKAM

232  

233 func_ptrs = data 

234 return data 

235  

236  

237cpdef _inspect_function_pointer(str name): 1acdefwxyzIJghijklmnopqrstuvbKA

238 global func_ptrs 

239 if func_ptrs is None: 

240 func_ptrs = _inspect_function_pointers() 

241 return func_ptrs[name] 1cdefwxyzIJghijklmnopqrstuvbKA

242  

243  

244############################################################################### 

245# Wrapper functions 

246############################################################################### 

247  

248cdef const char* _nvFatbinGetErrorString(nvFatbinResult result) except?NULL nogil: 1aghijklmnopqrstuv

249 global __nvFatbinGetErrorString 

250 _check_or_init_nvfatbin() 1NOPQRSTUVWXYZ01234

251 if __nvFatbinGetErrorString == NULL: 1NOPQRSTUVWXYZ01234ghijklmnopqrstuv

252 with gil: 

253 raise FunctionNotFoundError("function nvFatbinGetErrorString is not found") 

254 return (<const char* (*)(nvFatbinResult) noexcept nogil>__nvFatbinGetErrorString)( 1NOPQRSTUVWXYZ01234

255 result) 

256  

257  

258cdef nvFatbinResult _nvFatbinCreate(nvFatbinHandle* handle_indirect, const char** options, size_t optionsCount) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 1awxyzIJ

259 global __nvFatbinCreate 

260 _check_or_init_nvfatbin() 1BCDcdefwxyzIJghijklmnopqrstuvEFGHbKAM

261 if __nvFatbinCreate == NULL: 1BCDcdefwxyzIJghijklmnopqrstuvEFGHbKAM

262 with gil: 

263 raise FunctionNotFoundError("function nvFatbinCreate is not found") 

264 return (<nvFatbinResult (*)(nvFatbinHandle*, const char**, size_t) noexcept nogil>__nvFatbinCreate)( 1BCDcdefwxyzIJghijklmnopqrstuvEFGHbKAM

265 handle_indirect, options, optionsCount) 

266  

267  

268cdef nvFatbinResult _nvFatbinDestroy(nvFatbinHandle* handle_indirect) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 1acdef

269 global __nvFatbinDestroy 

270 _check_or_init_nvfatbin() 1BCDcdefwxyzIJghijklmnopqrstuvEFGHbKA

271 if __nvFatbinDestroy == NULL: 1BCDcdefwxyzIJghijklmnopqrstuvEFGHbKA

272 with gil: 

273 raise FunctionNotFoundError("function nvFatbinDestroy is not found") 

274 return (<nvFatbinResult (*)(nvFatbinHandle*) noexcept nogil>__nvFatbinDestroy)( 1BCDcdefwxyzIJghijklmnopqrstuvEFGHbKA

275 handle_indirect) 

276  

277  

278cdef 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: 1acdefwxyzghijklmnopqrstuvbA

279 global __nvFatbinAddPTX 

280 _check_or_init_nvfatbin() 1BCDghijklmnopqrstuv

281 if __nvFatbinAddPTX == NULL: 1BCDcdefwxyzghijklmnopqrstuvbA

282 with gil: 

283 raise FunctionNotFoundError("function nvFatbinAddPTX is not found") 

284 return (<nvFatbinResult (*)(nvFatbinHandle, const char*, size_t, const char*, const char*, const char*) noexcept nogil>__nvFatbinAddPTX)( 1BCDghijklmnopqrstuv

285 handle, code, size, arch, identifier, optionsCmdLine) 

286  

287  

288cdef nvFatbinResult _nvFatbinAddCubin(nvFatbinHandle handle, const void* code, size_t size, const char* arch, const char* identifier) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 1acdefwxyzghijklmnopqrstuvbA

289 global __nvFatbinAddCubin 

290 _check_or_init_nvfatbin() 1BCDwxyzIJ

291 if __nvFatbinAddCubin == NULL: 1BCDcdefwxyzIJghijklmnopqrstuvbA

292 with gil: 

293 raise FunctionNotFoundError("function nvFatbinAddCubin is not found") 

294 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*, const char*) noexcept nogil>__nvFatbinAddCubin)( 1BCDwxyzIJ

295 handle, code, size, arch, identifier) 

296  

297  

298cdef 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: 1aL

299 global __nvFatbinAddLTOIR 

300 _check_or_init_nvfatbin() 1cdef

301 if __nvFatbinAddLTOIR == NULL: 1acdefL

302 with gil: 

303 raise FunctionNotFoundError("function nvFatbinAddLTOIR is not found") 

304 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*, const char*, const char*) noexcept nogil>__nvFatbinAddLTOIR)( 1cdef

305 handle, code, size, arch, identifier, optionsCmdLine) 

306  

307  

308cdef nvFatbinResult _nvFatbinSize(nvFatbinHandle handle, size_t* size) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 

309 global __nvFatbinSize 

310 _check_or_init_nvfatbin() 1BCDcdefwxyzghijklmnopqrstuvEFGHbA

311 if __nvFatbinSize == NULL: 1BCDcdefwxyzghijklmnopqrstuvEFGHbA

312 with gil: 

313 raise FunctionNotFoundError("function nvFatbinSize is not found") 

314 return (<nvFatbinResult (*)(nvFatbinHandle, size_t*) noexcept nogil>__nvFatbinSize)( 1BCDcdefwxyzghijklmnopqrstuvEFGHbA

315 handle, size) 

316  

317  

318cdef nvFatbinResult _nvFatbinGet(nvFatbinHandle handle, void* buffer) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 

319 global __nvFatbinGet 

320 _check_or_init_nvfatbin() 1BCDcdefwxyzghijklmnopqrstuvEFGHbA

321 if __nvFatbinGet == NULL: 1BCDcdefwxyzghijklmnopqrstuvEFGHbA

322 with gil: 

323 raise FunctionNotFoundError("function nvFatbinGet is not found") 

324 return (<nvFatbinResult (*)(nvFatbinHandle, void*) noexcept nogil>__nvFatbinGet)( 1BCDcdefwxyzghijklmnopqrstuvEFGHbA

325 handle, buffer) 

326  

327  

328cdef nvFatbinResult _nvFatbinVersion(unsigned int* major, unsigned int* minor) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 1ab

329 global __nvFatbinVersion 

330 _check_or_init_nvfatbin() 1aL

331 if __nvFatbinVersion == NULL: 1abL

332 with gil: 

333 raise FunctionNotFoundError("function nvFatbinVersion is not found") 

334 return (<nvFatbinResult (*)(unsigned int*, unsigned int*) noexcept nogil>__nvFatbinVersion)( 1aL

335 major, minor) 

336  

337  

338cdef nvFatbinResult _nvFatbinAddIndex(nvFatbinHandle handle, const void* code, size_t size, const char* identifier) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 

339 global __nvFatbinAddIndex 

340 _check_or_init_nvfatbin() 

341 if __nvFatbinAddIndex == NULL: 

342 with gil: 

343 raise FunctionNotFoundError("function nvFatbinAddIndex is not found") 

344 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*) noexcept nogil>__nvFatbinAddIndex)( 

345 handle, code, size, identifier) 

346  

347  

348cdef nvFatbinResult _nvFatbinAddReloc(nvFatbinHandle handle, const void* code, size_t size) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 

349 global __nvFatbinAddReloc 

350 _check_or_init_nvfatbin() 1EFGH

351 if __nvFatbinAddReloc == NULL: 1EFGH

352 with gil: 

353 raise FunctionNotFoundError("function nvFatbinAddReloc is not found") 

354 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t) noexcept nogil>__nvFatbinAddReloc)( 1EFGH

355 handle, code, size) 

356  

357  

358cdef nvFatbinResult _nvFatbinAddTileIR(nvFatbinHandle handle, const void* code, size_t size, const char* identifier, const char* optionsCmdLine) except?_NVFATBINRESULT_INTERNAL_LOADING_ERROR nogil: 

359 global __nvFatbinAddTileIR 

360 _check_or_init_nvfatbin() 1b

361 if __nvFatbinAddTileIR == NULL: 1b

362 with gil: 

363 raise FunctionNotFoundError("function nvFatbinAddTileIR is not found") 

364 return (<nvFatbinResult (*)(nvFatbinHandle, const void*, size_t, const char*, const char*) noexcept nogil>__nvFatbinAddTileIR)( 1b

365 handle, code, size, identifier, optionsCmdLine)