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

164 statements  

« prev     ^ index     » next       coverage.py v7.16.0, created at 2026-09-03 02:41 +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.3.0. Do not modify it directly. 

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

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 isinstance(buf, int): 1efghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

24 ptr = <intptr_t>buf 

25 else: 

26 try: 1defghSTUV23ijklmnopqrstuvwx6789W'abcyzABXYZ045CDEFGHIJKLMNOPQR!#$%1(

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

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

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

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

31 except Exception as e: 

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

33 raise ValueError( 

34 "buf must be either a Python int representing the pointer " 

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

36 f"buffer, of size {size}" 

37 ) from e 

38 else: 

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

40 finally: 

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

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

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

44  

45  

46# <<<< END OF PREAMBLE CONTENT >>>> 

47  

48cimport cython # NOQA 

49  

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

51 get_resource_ptrs) 

52  

53from libcpp.vector cimport vector 

54  

55  

56############################################################################### 

57# Enum 

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

59  

60class Result(_cyb_FastEnum): 

61 """ 

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

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

64  

65 See `nvFatbinResult`. 

66 """ 

67 SUCCESS = NVFATBIN_SUCCESS 

68 ERROR_INTERNAL = NVFATBIN_ERROR_INTERNAL 

69 ERROR_ELF_ARCH_MISMATCH = NVFATBIN_ERROR_ELF_ARCH_MISMATCH 

70 ERROR_ELF_SIZE_MISMATCH = NVFATBIN_ERROR_ELF_SIZE_MISMATCH 

71 ERROR_MISSING_PTX_VERSION = NVFATBIN_ERROR_MISSING_PTX_VERSION 

72 ERROR_NULL_POINTER = NVFATBIN_ERROR_NULL_POINTER 

73 ERROR_COMPRESSION_FAILED = NVFATBIN_ERROR_COMPRESSION_FAILED 

74 ERROR_COMPRESSED_SIZE_EXCEEDED = NVFATBIN_ERROR_COMPRESSED_SIZE_EXCEEDED 

75 ERROR_UNRECOGNIZED_OPTION = NVFATBIN_ERROR_UNRECOGNIZED_OPTION 

76 ERROR_INVALID_ARCH = NVFATBIN_ERROR_INVALID_ARCH 

77 ERROR_INVALID_NVVM = NVFATBIN_ERROR_INVALID_NVVM 

78 ERROR_EMPTY_INPUT = NVFATBIN_ERROR_EMPTY_INPUT 

79 ERROR_MISSING_PTX_ARCH = NVFATBIN_ERROR_MISSING_PTX_ARCH 

80 ERROR_PTX_ARCH_MISMATCH = NVFATBIN_ERROR_PTX_ARCH_MISMATCH 

81 ERROR_MISSING_FATBIN = NVFATBIN_ERROR_MISSING_FATBIN 

82 ERROR_INVALID_INDEX = NVFATBIN_ERROR_INVALID_INDEX 

83 ERROR_IDENTIFIER_REUSE = NVFATBIN_ERROR_IDENTIFIER_REUSE 

84 ERROR_INTERNAL_PTX_OPTION = NVFATBIN_ERROR_INTERNAL_PTX_OPTION 

85  

86  

87############################################################################### 

88# Error handling 

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

90  

91class nvFatbinError(Exception): 

92  

93 def __init__(self, status): 

94 self.status = status 123)45*

95 s = Result(status) 123)45*

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

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

98  

99 def __reduce__(self): 

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

101  

102  

103@cython.profile(False) 

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

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

106 with gil: 123)45*

107 raise nvFatbinError(status) 123)45*

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

109  

110  

111############################################################################### 

112# Wrapper functions 

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

114  

115cpdef destroy(intptr_t handle): 

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

117  

118 Args: 

119 handle (intptr_t): nvFatbin handle. 

120  

121 .. seealso:: `nvFatbinDestroy` 

122 """ 

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

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

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

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

127  

128  

129cpdef str get_error_string(int result): 

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

131  

132 Args: 

133 result (Result): error code. 

134  

135 .. seealso:: `nvFatbinGetErrorString` 

136 """ 

137 cdef const char* _output_ 

138 cdef bytes _output_bytes_ 

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

140  

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

142 return "" 2tbub

143  

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

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

146  

147  

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

149 """nvFatbinCreate creates a new handle. 

150  

151 Args: 

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

153 single option. It can be: 

154  

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

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

157 to a valid sequence of 'char', or 

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

159  

160 options_count (size_t): Number of options. 

161  

162 Returns: 

163 intptr_t: Address of nvFatbin handle. 

164  

165 .. seealso:: `nvFatbinCreate` 

166 """ 

167 cdef nested_resource[ char ] _options_ 

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

169 cdef Handle handle_indirect 

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

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

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

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

174  

175  

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

177 """nvFatbinAddPTX adds PTX to the fatbinary. 

178  

179 Args: 

180 handle (intptr_t): nvFatbin handle. 

181 code (bytes): The PTX code. 

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

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

184 (the XX of any sm_XX, lto_XX, or compute_XX). 

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

186 fatbin with tools like cuobjdump. 

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

188  

189 .. seealso:: `nvFatbinAddPTX` 

190 """ 

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

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

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

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

195 cdef char* _arch_ = _temp_arch_ 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

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

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

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

199 cdef char* _identifier_ = _temp_identifier_ 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

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

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

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

203 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

204 with nogil: 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

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

206 check_status(__status__) 1ijklmnopqrstuvwxabcCDEFGHIJKLMNOPQR

207  

208  

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

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

211  

212 Args: 

213 handle (intptr_t): nvFatbin handle. 

214 code (bytes): The cubin. 

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

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

217 (the XX of any sm_XX, lto_XX, or compute_XX). 

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

219 the fatbin with tools like cuobjdump. 

220  

221 .. seealso:: `nvFatbinAddCubin` 

222 """ 

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

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

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

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

227 cdef char* _arch_ = _temp_arch_ 1STUV23abcXYZ045

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

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

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

231 cdef char* _identifier_ = _temp_identifier_ 1STUV23abcXYZ045

232 with nogil: 1STUV23abcXYZ045

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

234 check_status(__status__) 1STUV23abcXYZ045

235  

236  

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

238 """nvFatbinAddLTOIR adds LTOIR to the fatbinary. 

239  

240 Args: 

241 handle (intptr_t): nvFatbin handle. 

242 code (bytes): The LTOIR code. 

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

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

245 (the XX of any sm_XX, lto_XX, or compute_XX). 

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

247 the fatbin with tools like cuobjdump. 

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

249  

250 .. seealso:: `nvFatbinAddLTOIR` 

251 """ 

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

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

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

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

256 cdef char* _arch_ = _temp_arch_ 1efghyzAB

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

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

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

260 cdef char* _identifier_ = _temp_identifier_ 1efghyzAB

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

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

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

264 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1efghyzAB

265 with nogil: 1efghyzAB

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

267 check_status(__status__) 1efghyzAB

268  

269  

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

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

272  

273 Args: 

274 handle (intptr_t): nvFatbin handle. 

275  

276 Returns: 

277 size_t: The fatbinary's size. 

278  

279 .. seealso:: `nvFatbinSize` 

280 """ 

281 cdef size_t size 

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

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

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

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

286  

287  

288cpdef get(intptr_t handle, buffer): 

289 """nvFatbinGet returns the completed fatbinary. 

290  

291 Args: 

292 handle (intptr_t): nvFatbin handle. 

293 buffer (bytes): memory to store fatbinary. 

294  

295 .. seealso:: `nvFatbinGet` 

296 """ 

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

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

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

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

301  

302  

303cpdef tuple version(): 

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

305  

306 Returns: 

307 A 2-tuple containing: 

308  

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 cdef void* _code_ = <void *>_cyb_get_buffer_pointer(code, size, readonly=True) 

324 if not isinstance(identifier, str): 

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

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

327 cdef char* _identifier_ = _temp_identifier_ 

328 with nogil: 

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

330 check_status(__status__) 

331  

332  

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

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

335  

336 Args: 

337 handle (intptr_t): nvFatbin handle. 

338 code (bytes): The host object image. 

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

340  

341 .. seealso:: `nvFatbinAddReloc` 

342 """ 

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

344 with nogil: 16789!#$%

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

346 check_status(__status__) 16789!#$%

347  

348  

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

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

351  

352 Args: 

353 handle (intptr_t): nvFatbin handle. 

354 code (bytes): The Tile IR. 

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

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

357 the fatbin with tools like cuobjdump. 

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

359  

360 .. seealso:: `nvFatbinAddTileIR` 

361 """ 

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

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

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

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

366 cdef char* _identifier_ = _temp_identifier_ 1W1

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

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

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

370 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1W1

371 with nogil: 1W1

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

373 check_status(__status__) 1W1

374del _cyb_FastEnum