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

165 statements  

« prev     ^ index     » next       coverage.py v7.17.2, created at 2026-11-05 01:11 +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=a7b1e6452305b24e9ce950d8adbc14639f922708f50252656f28e0fe2e3cd464 

7  

8  

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

10  

11cimport cpython as _cyb_cpython 

12from cuda.bindings._internal.utils cimport ( 

13 get_nested_resource_ptr as _cyb_get_nested_resource_ptr, 

14 nested_resource as _cyb_nested_resource, 

15) 

16from libc.stdint cimport intptr_t 

17  

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

19  

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

21 cdef intptr_t ptr 

22 cdef int flags = _cyb_cpython.PyBUF_ANY_CONTIGUOUS 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

23 if not readonly: 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

24 flags |= _cyb_cpython.PyBUF_WRITABLE 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

25 cdef int status = -1 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

26 cdef _cyb_cpython.Py_buffer view 

27 if buf is None: 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

28 ptr = 0 

29 elif isinstance(buf, int): 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

30 ptr = <intptr_t>buf 

31 else: 

32 try: 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

33 status = _cyb_cpython.PyObject_GetBuffer(buf, &view, flags) 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

34 if size != -1: 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

35 assert view.len == size 1defgSTUV23hijklmnopqrstuvw6789WabcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1

36 except Exception as e: 

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

38 raise ValueError( 

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

40 f"address to a valid buffer, or a contiguous {adj}" 

41 f"buffer (a ctypes object, NumPy array, bytes, ...), of size {size}" 

42 ) from e 

43 else: 

44 ptr = <intptr_t>view.buf 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

45 finally: 

46 if status == 0: 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

47 _cyb_cpython.PyBuffer_Release(&view) 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

48 return ptr 1defgSTUV23hijklmnopqrstuvw6789W'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1(

49  

50  

51# <<<< END OF PREAMBLE CONTENT >>>> 

52  

53cimport cython # NOQA 

54  

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

56 get_resource_ptrs) 

57  

58from libcpp.vector cimport vector 

59  

60  

61############################################################################### 

62# Enum 

63############################################################################### 

64  

65class Result(_cyb_FastEnum): 

66 """ 

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

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

69  

70 See `nvFatbinResult`. 

71 """ 

72 SUCCESS = NVFATBIN_SUCCESS 

73 ERROR_INTERNAL = NVFATBIN_ERROR_INTERNAL 

74 ERROR_ELF_ARCH_MISMATCH = NVFATBIN_ERROR_ELF_ARCH_MISMATCH 

75 ERROR_ELF_SIZE_MISMATCH = NVFATBIN_ERROR_ELF_SIZE_MISMATCH 

76 ERROR_MISSING_PTX_VERSION = NVFATBIN_ERROR_MISSING_PTX_VERSION 

77 ERROR_NULL_POINTER = NVFATBIN_ERROR_NULL_POINTER 

78 ERROR_COMPRESSION_FAILED = NVFATBIN_ERROR_COMPRESSION_FAILED 

79 ERROR_COMPRESSED_SIZE_EXCEEDED = NVFATBIN_ERROR_COMPRESSED_SIZE_EXCEEDED 

80 ERROR_UNRECOGNIZED_OPTION = NVFATBIN_ERROR_UNRECOGNIZED_OPTION 

81 ERROR_INVALID_ARCH = NVFATBIN_ERROR_INVALID_ARCH 

82 ERROR_INVALID_NVVM = NVFATBIN_ERROR_INVALID_NVVM 

83 ERROR_EMPTY_INPUT = NVFATBIN_ERROR_EMPTY_INPUT 

84 ERROR_MISSING_PTX_ARCH = NVFATBIN_ERROR_MISSING_PTX_ARCH 

85 ERROR_PTX_ARCH_MISMATCH = NVFATBIN_ERROR_PTX_ARCH_MISMATCH 

86 ERROR_MISSING_FATBIN = NVFATBIN_ERROR_MISSING_FATBIN 

87 ERROR_INVALID_INDEX = NVFATBIN_ERROR_INVALID_INDEX 

88 ERROR_IDENTIFIER_REUSE = NVFATBIN_ERROR_IDENTIFIER_REUSE 

89 ERROR_INTERNAL_PTX_OPTION = NVFATBIN_ERROR_INTERNAL_PTX_OPTION 

90  

91  

92############################################################################### 

93# Error handling 

94############################################################################### 

95  

96class nvFatbinError(Exception): 

97  

98 def __init__(self, status): 

99 self.status = status 123)45*

100 s = Result(status) 123)45*

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

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

103  

104 def __reduce__(self): 

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

106  

107  

108@cython.profile(False) 

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

110 if status != 0: 1RdefgSTUV23hijklmnopqrstuvw6789W+'-)abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(.*

111 with gil: 123)45*

112 raise nvFatbinError(status) 123)45*

113 return status 1RdefgSTUV23hijklmnopqrstuvw6789W+'-abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(.

114  

115  

116############################################################################### 

117# Wrapper functions 

118############################################################################### 

119  

120cpdef destroy(intptr_t handle): 

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

122  

123 Args: 

124 handle (intptr_t): nvFatbin handle. 

125  

126 .. seealso:: `nvFatbinDestroy` 

127 """ 

128 cdef Handle h = <Handle>handle 1defgSTUV23hijklmnopqrstuvw6789W+'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(

129 with nogil: 1defgSTUV23hijklmnopqrstuvw6789W+'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(

130 status = nvFatbinDestroy(&h) 1defgSTUV23hijklmnopqrstuvw6789W+'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(

131 check_status(status) 1defgSTUV23hijklmnopqrstuvw6789W+'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(

132  

133  

134cpdef str get_error_string(int result): 

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

136  

137 Args: 

138 result (Result): error code. 

139  

140 .. seealso:: `nvFatbinGetErrorString` 

141 """ 

142 cdef const char* _output_ 

143 cdef bytes _output_bytes_ 

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

145  

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

147 return "" 2tbub

148  

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

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

151  

152  

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

154 """nvFatbinCreate creates a new handle. 

155  

156 Args: 

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

158 option. It can be: 

159  

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

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

162 to a valid sequence of 'char', or 

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

164 options_count (size_t): Number of options. 

165  

166 Returns: 

167 intptr_t: Address of nvFatbin handle. 

168  

169 .. seealso:: `nvFatbinCreate` 

170 """ 

171 cdef _cyb_nested_resource[ char ] _options_ 

172 _cyb_get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1defgSTUV23hijklmnopqrstuvw6789W+')abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(*

173 cdef Handle handle_indirect 

174 with nogil: 1defgSTUV23hijklmnopqrstuvw6789W+')abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(*

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

176 check_status(__status__) 1defgSTUV23hijklmnopqrstuvw6789W+')abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(*

177 return <intptr_t>handle_indirect 1defgSTUV23hijklmnopqrstuvw6789W+'abcxyzAXYZ045BCDEFGHIJKLMNOPQ!#$%1,(

178  

179  

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

181 """nvFatbinAddPTX adds PTX to the fatbinary. 

182  

183 Args: 

184 handle (intptr_t): nvFatbin handle. 

185 code (bytes): The PTX code. 

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

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

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

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

190 fatbin with tools like cuobjdump. 

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

192  

193 .. seealso:: `nvFatbinAddPTX` 

194 """ 

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

196 if not isinstance(arch, str): 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

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

198 cdef bytes _temp_arch_ = (<str>arch).encode() 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

199 cdef char* _arch_ = _temp_arch_ 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

200 if not isinstance(identifier, str): 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

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

202 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

203 cdef char* _identifier_ = _temp_identifier_ 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

204 if not isinstance(options_cmd_line, str): 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

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

206 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

207 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

208 with nogil: 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

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

210 check_status(__status__) 1hijklmnopqrstuvwabcBCDEFGHIJKLMNOPQ

211  

212  

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

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

215  

216 Args: 

217 handle (intptr_t): nvFatbin handle. 

218 code (bytes): The cubin. 

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

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

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

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

223 fatbin with tools like cuobjdump. 

224  

225 .. seealso:: `nvFatbinAddCubin` 

226 """ 

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

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

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

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

231 cdef char* _arch_ = _temp_arch_ 1STUV23abcXYZ045

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

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

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

235 cdef char* _identifier_ = _temp_identifier_ 1STUV23abcXYZ045

236 with nogil: 1STUV23abcXYZ045

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

238 check_status(__status__) 1STUV23abcXYZ045

239  

240  

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

242 """nvFatbinAddLTOIR adds LTOIR to the fatbinary. 

243  

244 Args: 

245 handle (intptr_t): nvFatbin handle. 

246 code (bytes): The LTOIR code. 

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

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

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

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

251 fatbin with tools like cuobjdump. 

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

253  

254 .. seealso:: `nvFatbinAddLTOIR` 

255 """ 

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

257 if not isinstance(arch, str): 1defgxyzA

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

259 cdef bytes _temp_arch_ = (<str>arch).encode() 1defgxyzA

260 cdef char* _arch_ = _temp_arch_ 1defgxyzA

261 if not isinstance(identifier, str): 1defgxyzA

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

263 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1defgxyzA

264 cdef char* _identifier_ = _temp_identifier_ 1defgxyzA

265 if not isinstance(options_cmd_line, str): 1defgxyzA

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

267 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1defgxyzA

268 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1defgxyzA

269 with nogil: 1defgxyzA

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

271 check_status(__status__) 1defgxyzA

272  

273  

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

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

276  

277 Args: 

278 handle (intptr_t): nvFatbin handle. 

279  

280 Returns: 

281 size_t: The fatbinary's size. 

282  

283 .. seealso:: `nvFatbinSize` 

284 """ 

285 cdef size_t size 

286 with nogil: 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

287 __status__ = nvFatbinSize(<Handle>handle, &size) 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

288 check_status(__status__) 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

289 return size 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

290  

291  

292cpdef get(intptr_t handle, buffer): 

293 """nvFatbinGet returns the completed fatbinary. 

294  

295 Args: 

296 handle (intptr_t): nvFatbin handle. 

297 buffer (bytes): memory to store fatbinary. 

298  

299 .. seealso:: `nvFatbinGet` 

300 """ 

301 cdef void* _buffer_ = <void *>_cyb_get_buffer_pointer(buffer, -1, readonly=False) 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

302 with nogil: 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

303 __status__ = nvFatbinGet(<Handle>handle, <void*>_buffer_) 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

304 check_status(__status__) 1defgSTUVhijklmnopqrstuvw6789W'abcxyzAXYZ0BCDEFGHIJKLMNOPQ!#$%1(

305  

306  

307cpdef tuple version(): 

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

309  

310 Returns: 

311 A 2-tuple containing: 

312 - unsigned int: The major version. 

313 - unsigned int: The minor version. 

314  

315 .. seealso:: `nvFatbinVersion` 

316 """ 

317 cdef unsigned int major 

318 cdef unsigned int minor 

319 with nogil: 1R-.

320 __status__ = nvFatbinVersion(&major, &minor) 1R-.

321 check_status(__status__) 1R-.

322 return (major, minor) 1R-.

323  

324  

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

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

327  

328 Args: 

329 handle (intptr_t): nvFatbin handle. 

330 code (bytes): The index. 

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

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

333 fatbin with tools like cuobjdump. 

334  

335 .. seealso:: `nvFatbinAddIndex` 

336 """ 

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

338 if not isinstance(identifier, str): 

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

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

341 cdef char* _identifier_ = _temp_identifier_ 

342 with nogil: 

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

344 check_status(__status__) 

345  

346  

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

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

349  

350 Args: 

351 handle (intptr_t): nvFatbin handle. 

352 code (bytes): The host object image. 

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

354  

355 .. seealso:: `nvFatbinAddReloc` 

356 """ 

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

358 with nogil: 16789!#$%

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

360 check_status(__status__) 16789!#$%

361  

362  

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

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

365  

366 Args: 

367 handle (intptr_t): nvFatbin handle. 

368 code (bytes): The Tile IR. 

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

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

371 fatbin with tools like cuobjdump. 

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

373  

374 .. seealso:: `nvFatbinAddTileIR` 

375 """ 

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

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

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

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

380 cdef char* _identifier_ = _temp_identifier_ 1W1

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

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

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

384 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1W1

385 with nogil: 1W1

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

387 check_status(__status__) 1W1

388del _cyb_FastEnum