Coverage for cuda/bindings/nvfatbin.pyx: 83.73%

166 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-16 01:23 +0000

1# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved. 

2# 

3# SPDX-License-Identifier: Apache-2.0 

4# 

5# This code was automatically generated across versions from 12.4.1 to 13.4.1. Do not modify it directly. 

6# CYTHON-BINDINGS-GENERATED-DO-NOT-MODIFY-THIS-FILE: format=1; content-sha256=a7028b3fc77724f1ea7c5c49302cfe8524b85554b73eaf754231ac9bfc26c0f7 

7  

8  

9# <<<< PREAMBLE CONTENT >>>> 

10  

11cimport cpython as _cyb_cpython 

12from libc.stdint cimport intptr_t 

13  

14from cuda.bindings._internal._fast_enum import FastEnum as _cyb_FastEnum 

15  

16cdef intptr_t _cyb_get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except?-1: 

17 cdef intptr_t ptr 

18 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

19 if not readonly: 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

20 flags |= _cyb_cpython.PyBUF_WRITABLE 1defghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

21 cdef int status = -1 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

22 cdef _cyb_cpython.Py_buffer view 

23 if buf is None: 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

24 ptr = 0 

25 elif isinstance(buf, int): 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

26 ptr = <intptr_t>buf 

27 else: 

28 try: 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

29 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

30 if size != -1: 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

31 assert view.len == size 1efghSTUV23ijklmnopqrstuvwx6789WabcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1

32 assert view.ndim == 1 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

33 except Exception as e: 

34 adj = "writable " if not readonly else "" 

35 raise ValueError( 

36 "buf must be None, a Python int representing the pointer " 

37 f"address to a valid buffer, or a 1D contiguous {adj}" 

38 f"buffer, of size {size}" 

39 ) from e 

40 else: 

41 ptr = <intptr_t>view.buf 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

42 finally: 

43 if status == 0: 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

44 _cyb_cpython.PyBuffer_Release(&view) 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

45 return ptr 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

46  

47  

48# <<<< END OF PREAMBLE CONTENT >>>> 

49  

50cimport cython # NOQA 

51  

52from ._internal.utils cimport (get_resource_ptr, get_nested_resource_ptr, nested_resource, nullable_unique_ptr, 

53 get_resource_ptrs) 

54  

55from libcpp.vector cimport vector 

56  

57  

58############################################################################### 

59# Enum 

60############################################################################### 

61  

62class Result(_cyb_FastEnum): 

63 """ 

64 The enumerated type `nvFatbinResult` defines API call result codes. 

65 nvFatbin APIs return `nvFatbinResult` codes to indicate the result. 

66  

67 See `nvFatbinResult`. 

68 """ 

69 SUCCESS = NVFATBIN_SUCCESS 

70 ERROR_INTERNAL = NVFATBIN_ERROR_INTERNAL 

71 ERROR_ELF_ARCH_MISMATCH = NVFATBIN_ERROR_ELF_ARCH_MISMATCH 

72 ERROR_ELF_SIZE_MISMATCH = NVFATBIN_ERROR_ELF_SIZE_MISMATCH 

73 ERROR_MISSING_PTX_VERSION = NVFATBIN_ERROR_MISSING_PTX_VERSION 

74 ERROR_NULL_POINTER = NVFATBIN_ERROR_NULL_POINTER 

75 ERROR_COMPRESSION_FAILED = NVFATBIN_ERROR_COMPRESSION_FAILED 

76 ERROR_COMPRESSED_SIZE_EXCEEDED = NVFATBIN_ERROR_COMPRESSED_SIZE_EXCEEDED 

77 ERROR_UNRECOGNIZED_OPTION = NVFATBIN_ERROR_UNRECOGNIZED_OPTION 

78 ERROR_INVALID_ARCH = NVFATBIN_ERROR_INVALID_ARCH 

79 ERROR_INVALID_NVVM = NVFATBIN_ERROR_INVALID_NVVM 

80 ERROR_EMPTY_INPUT = NVFATBIN_ERROR_EMPTY_INPUT 

81 ERROR_MISSING_PTX_ARCH = NVFATBIN_ERROR_MISSING_PTX_ARCH 

82 ERROR_PTX_ARCH_MISMATCH = NVFATBIN_ERROR_PTX_ARCH_MISMATCH 

83 ERROR_MISSING_FATBIN = NVFATBIN_ERROR_MISSING_FATBIN 

84 ERROR_INVALID_INDEX = NVFATBIN_ERROR_INVALID_INDEX 

85 ERROR_IDENTIFIER_REUSE = NVFATBIN_ERROR_IDENTIFIER_REUSE 

86 ERROR_INTERNAL_PTX_OPTION = NVFATBIN_ERROR_INTERNAL_PTX_OPTION 

87  

88  

89############################################################################### 

90# Error handling 

91############################################################################### 

92  

93class nvFatbinError(Exception): 

94  

95 def __init__(self, status): 

96 self.status = status 123)45*

97 s = Result(status) 123)45*

98 cdef str err = f"{s.name} ({s.value})" 123)45*

99 super(nvFatbinError, self).__init__(err) 123)45*

100  

101 def __reduce__(self): 

102 return (type(self), (self.status,)) 

103  

104  

105@cython.profile(False) 

106cdef int check_status(int status) except 1 nogil: 

107 if status != 0: 1defghSTUV23ijklmnopqrstuvwx6789W+'-)abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(.*

108 with gil: 123)45*

109 raise nvFatbinError(status) 123)45*

110 return status 1defghSTUV23ijklmnopqrstuvwx6789W+'-abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(.

111  

112  

113############################################################################### 

114# Wrapper functions 

115############################################################################### 

116  

117cpdef destroy(intptr_t handle): 

118 """nvFatbinDestroy frees the memory associated with the given handle. 

119  

120 Args: 

121 handle (intptr_t): nvFatbin handle. 

122  

123 .. seealso:: `nvFatbinDestroy` 

124 """ 

125 cdef Handle h = <Handle>handle 1efghSTUV23ijklmnopqrstuvwx6789W+'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(

126 with nogil: 1efghSTUV23ijklmnopqrstuvwx6789W+'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(

127 status = nvFatbinDestroy(&h) 1efghSTUV23ijklmnopqrstuvwx6789W+'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(

128 check_status(status) 1efghSTUV23ijklmnopqrstuvwx6789W+'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(

129  

130  

131cpdef str get_error_string(int result): 

132 """nvFatbinGetErrorString returns an error description string for each error code. 

133  

134 Args: 

135 result (Result): error code. 

136  

137 .. seealso:: `nvFatbinGetErrorString` 

138 """ 

139 cdef const char* _output_ 

140 cdef bytes _output_bytes_ 

141 _output_ = nvFatbinGetErrorString(<_Result>result) 2/ : ; = ? @ [ ] ^ _ ` { | } ~ abbbtbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbub

142  

143 if _output_ == NULL: 2/ : ; = ? @ [ ] ^ _ ` { | } ~ abbbtbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsbub

144 return "" 2tbub

145  

146 _output_bytes_ = <bytes>_output_ 2/ : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsb

147 return _output_bytes_.decode() 2/ : ; = ? @ [ ] ^ _ ` { | } ~ abbbcbdbebfbgbhbibjbkblbmbnbobpbqbrbsb

148  

149  

150cpdef intptr_t create(options, size_t options_count) except -1: 

151 """nvFatbinCreate creates a new handle. 

152  

153 Args: 

154 options (object): An array of strings, each containing a single 

155 option. It can be: 

156  

157 - an :class:`int` as the pointer address to the nested sequence, or 

158 - a Python sequence of :class:`int`\s, each of which is a pointer address 

159 to a valid sequence of 'char', or 

160 - a nested Python sequence of ``str``. 

161 options_count (size_t): Number of options. 

162  

163 Returns: 

164 intptr_t: Address of nvFatbin handle. 

165  

166 .. seealso:: `nvFatbinCreate` 

167 """ 

168 cdef nested_resource[ char ] _options_ 

169 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1efghSTUV23ijklmnopqrstuvwx6789W+')abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(*

170 cdef Handle handle_indirect 

171 with nogil: 1efghSTUV23ijklmnopqrstuvwx6789W+')abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(*

172 __status__ = nvFatbinCreate(&handle_indirect, <const char**>(_options_.ptrs.data()), options_count) 1efghSTUV23ijklmnopqrstuvwx6789W+')abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(*

173 check_status(__status__) 1efghSTUV23ijklmnopqrstuvwx6789W+')abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(*

174 return <intptr_t>handle_indirect 1efghSTUV23ijklmnopqrstuvwx6789W+'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1,(

175  

176  

177cpdef add_ptx(intptr_t handle, code, size_t size, arch, identifier, options_cmd_line): 

178 """nvFatbinAddPTX adds PTX to the fatbinary. 

179  

180 Args: 

181 handle (intptr_t): nvFatbin handle. 

182 code (bytes): The PTX code. 

183 size (size_t): The size of the PTX code. 

184 arch (str): The numerical architecture that this PTX is for (the 

185 XX of any sm_XX, lto_XX, or compute_XX). 

186 identifier (str): Name of the PTX, useful when extracting the 

187 fatbin with tools like cuobjdump. 

188 options_cmd_line (str): Options used during JIT compilation. 

189  

190 .. seealso:: `nvFatbinAddPTX` 

191 """ 

192 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

193 if not isinstance(arch, str): 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

194 raise TypeError("arch must be a Python str") 

195 cdef bytes _temp_arch_ = (<str>arch).encode() 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

196 cdef char* _arch_ = _temp_arch_ 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

197 if not isinstance(identifier, str): 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

198 raise TypeError("identifier must be a Python str") 

199 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

200 cdef char* _identifier_ = _temp_identifier_ 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

201 if not isinstance(options_cmd_line, str): 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

202 raise TypeError("options_cmd_line must be a Python str") 

203 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

204 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

205 with nogil: 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

206 __status__ = nvFatbinAddPTX(<Handle>handle, <const char*>_code_, size, <const char*>_arch_, <const char*>_identifier_, <const char*>_options_cmd_line_) 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

207 check_status(__status__) 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

208  

209  

210cpdef add_cubin(intptr_t handle, code, size_t size, arch, identifier): 

211 """nvFatbinAddCubin adds a CUDA binary to the fatbinary. 

212  

213 Args: 

214 handle (intptr_t): nvFatbin handle. 

215 code (bytes): The cubin. 

216 size (size_t): The size of the cubin. 

217 arch (str): The numerical architecture that this cubin is for (the 

218 XX of any sm_XX, lto_XX, or compute_XX). 

219 identifier (str): Name of the cubin, useful when extracting the 

220 fatbin with tools like cuobjdump. 

221  

222 .. seealso:: `nvFatbinAddCubin` 

223 """ 

224 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 1STUV23abcXYZ045

225 if not isinstance(arch, str): 1STUV23abcXYZ045

226 raise TypeError("arch must be a Python str") 

227 cdef bytes _temp_arch_ = (<str>arch).encode() 1STUV23abcXYZ045

228 cdef char* _arch_ = _temp_arch_ 1STUV23abcXYZ045

229 if not isinstance(identifier, str): 1STUV23abcXYZ045

230 raise TypeError("identifier must be a Python str") 

231 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1STUV23abcXYZ045

232 cdef char* _identifier_ = _temp_identifier_ 1STUV23abcXYZ045

233 with nogil: 1STUV23abcXYZ045

234 __status__ = nvFatbinAddCubin(<Handle>handle, <const void*>_code_, size, <const char*>_arch_, <const char*>_identifier_) 1STUV23abcXYZ045

235 check_status(__status__) 1STUV23abcXYZ045

236  

237  

238cpdef add_ltoir(intptr_t handle, code, size_t size, arch, identifier, options_cmd_line): 

239 """nvFatbinAddLTOIR adds LTOIR to the fatbinary. 

240  

241 Args: 

242 handle (intptr_t): nvFatbin handle. 

243 code (bytes): The LTOIR code. 

244 size (size_t): The size of the LTOIR code. 

245 arch (str): The numerical architecture that this LTOIR is for (the 

246 XX of any sm_XX, lto_XX, or compute_XX). 

247 identifier (str): Name of the LTOIR, useful when extracting the 

248 fatbin with tools like cuobjdump. 

249 options_cmd_line (str): Options used during JIT compilation. 

250  

251 .. seealso:: `nvFatbinAddLTOIR` 

252 """ 

253 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 1efghyzAB

254 if not isinstance(arch, str): 1efghyzAB

255 raise TypeError("arch must be a Python str") 

256 cdef bytes _temp_arch_ = (<str>arch).encode() 1efghyzAB

257 cdef char* _arch_ = _temp_arch_ 1efghyzAB

258 if not isinstance(identifier, str): 1efghyzAB

259 raise TypeError("identifier must be a Python str") 

260 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1efghyzAB

261 cdef char* _identifier_ = _temp_identifier_ 1efghyzAB

262 if not isinstance(options_cmd_line, str): 1efghyzAB

263 raise TypeError("options_cmd_line must be a Python str") 

264 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1efghyzAB

265 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1efghyzAB

266 with nogil: 1efghyzAB

267 __status__ = nvFatbinAddLTOIR(<Handle>handle, <const void*>_code_, size, <const char*>_arch_, <const char*>_identifier_, <const char*>_options_cmd_line_) 1efghyzAB

268 check_status(__status__) 1efghyzAB

269  

270  

271cpdef size_t size(intptr_t handle) except? 0: 

272 """nvFatbinSize returns the fatbinary's size. 

273  

274 Args: 

275 handle (intptr_t): nvFatbin handle. 

276  

277 Returns: 

278 size_t: The fatbinary's size. 

279  

280 .. seealso:: `nvFatbinSize` 

281 """ 

282 cdef size_t size 

283 with nogil: 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

284 __status__ = nvFatbinSize(<Handle>handle, &size) 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

285 check_status(__status__) 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

286 return size 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

287  

288  

289cpdef get(intptr_t handle, buffer): 

290 """nvFatbinGet returns the completed fatbinary. 

291  

292 Args: 

293 handle (intptr_t): nvFatbin handle. 

294 buffer (bytes): memory to store fatbinary. 

295  

296 .. seealso:: `nvFatbinGet` 

297 """ 

298 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

299 with nogil: 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

300 __status__ = nvFatbinGet(<Handle>handle, <void*>_buffer_) 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

301 check_status(__status__) 1efghSTUVijklmnopqrstuvwx6789W'abcyzABXYZ0CDEFGHIJKLMNOPQR!#$%1(

302  

303  

304cpdef tuple version(): 

305 """nvFatbinVersion returns the current version of nvFatbin. 

306  

307 Returns: 

308 A 2-tuple containing: 

309 - unsigned int: The major version. 

310 - unsigned int: The minor version. 

311  

312 .. seealso:: `nvFatbinVersion` 

313 """ 

314 cdef unsigned int major 

315 cdef unsigned int minor 

316 with nogil: 1d-.

317 __status__ = nvFatbinVersion(&major, &minor) 1d-.

318 check_status(__status__) 1d-.

319 return (major, minor) 1d-.

320  

321  

322cpdef add_index(intptr_t handle, code, size_t size, identifier): 

323 """nvFatbinAddIndex adds an index file to the fatbinary. 

324  

325 Args: 

326 handle (intptr_t): nvFatbin handle. 

327 code (bytes): The index. 

328 size (size_t): The size of the index. 

329 identifier (str): Name of the index, useful when extracting the 

330 fatbin with tools like cuobjdump. 

331  

332 .. seealso:: `nvFatbinAddIndex` 

333 """ 

334 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 

335 if not isinstance(identifier, str): 

336 raise TypeError("identifier must be a Python str") 

337 cdef bytes _temp_identifier_ = (<str>identifier).encode() 

338 cdef char* _identifier_ = _temp_identifier_ 

339 with nogil: 

340 __status__ = nvFatbinAddIndex(<Handle>handle, <const void*>_code_, size, <const char*>_identifier_) 

341 check_status(__status__) 

342  

343  

344cpdef add_reloc(intptr_t handle, code, size_t size): 

345 """nvFatbinAddReloc adds relocatable PTX entries from a host object to the fatbinary. 

346  

347 Args: 

348 handle (intptr_t): nvFatbin handle. 

349 code (bytes): The host object image. 

350 size (size_t): The size of the host object image code. 

351  

352 .. seealso:: `nvFatbinAddReloc` 

353 """ 

354 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 16789!#$%

355 with nogil: 16789!#$%

356 __status__ = nvFatbinAddReloc(<Handle>handle, <const void*>_code_, size) 16789!#$%

357 check_status(__status__) 16789!#$%

358  

359  

360cpdef add_tile_ir(intptr_t handle, code, size_t size, identifier, options_cmd_line): 

361 """nvFatbinAddTileIR adds Tile IR to the fatbinary. 

362  

363 Args: 

364 handle (intptr_t): nvFatbin handle. 

365 code (bytes): The Tile IR. 

366 size (size_t): The size of the Tile IR. 

367 identifier (str): Name of the Tile IR, useful when extracting the 

368 fatbin with tools like cuobjdump. 

369 options_cmd_line (str): Options used during JIT compilation. 

370  

371 .. seealso:: `nvFatbinAddTileIR` 

372 """ 

373 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 1W1

374 if not isinstance(identifier, str): 1W1

375 raise TypeError("identifier must be a Python str") 

376 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1W1

377 cdef char* _identifier_ = _temp_identifier_ 1W1

378 if not isinstance(options_cmd_line, str): 1W1

379 raise TypeError("options_cmd_line must be a Python str") 

380 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1W1

381 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1W1

382 with nogil: 1W1

383 __status__ = nvFatbinAddTileIR(<Handle>handle, <const void*>_code_, size, <const char*>_identifier_, <const char*>_options_cmd_line_) 1W1

384 check_status(__status__) 1W1

385del _cyb_FastEnum