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

131 statements  

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

6  

7cimport cython # NOQA 

8  

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

10 get_buffer_pointer, get_resource_ptrs) 

11  

12from cuda.bindings._internal._fast_enum import FastEnum as _IntEnum 

13from libcpp.vector cimport vector 

14  

15  

16############################################################################### 

17# Enum 

18############################################################################### 

19  

20class Result(_IntEnum): 

21 """ 

22 The enumerated type nvFatbinResult defines API call result codes. 

23 nvFatbin APIs return nvFatbinResult codes to indicate the result. 

24  

25 See `nvFatbinResult`. 

26 """ 

27 SUCCESS = NVFATBIN_SUCCESS 

28 ERROR_INTERNAL = NVFATBIN_ERROR_INTERNAL 

29 ERROR_ELF_ARCH_MISMATCH = NVFATBIN_ERROR_ELF_ARCH_MISMATCH 

30 ERROR_ELF_SIZE_MISMATCH = NVFATBIN_ERROR_ELF_SIZE_MISMATCH 

31 ERROR_MISSING_PTX_VERSION = NVFATBIN_ERROR_MISSING_PTX_VERSION 

32 ERROR_NULL_POINTER = NVFATBIN_ERROR_NULL_POINTER 

33 ERROR_COMPRESSION_FAILED = NVFATBIN_ERROR_COMPRESSION_FAILED 

34 ERROR_COMPRESSED_SIZE_EXCEEDED = NVFATBIN_ERROR_COMPRESSED_SIZE_EXCEEDED 

35 ERROR_UNRECOGNIZED_OPTION = NVFATBIN_ERROR_UNRECOGNIZED_OPTION 

36 ERROR_INVALID_ARCH = NVFATBIN_ERROR_INVALID_ARCH 

37 ERROR_INVALID_NVVM = NVFATBIN_ERROR_INVALID_NVVM 

38 ERROR_EMPTY_INPUT = NVFATBIN_ERROR_EMPTY_INPUT 

39 ERROR_MISSING_PTX_ARCH = NVFATBIN_ERROR_MISSING_PTX_ARCH 

40 ERROR_PTX_ARCH_MISMATCH = NVFATBIN_ERROR_PTX_ARCH_MISMATCH 

41 ERROR_MISSING_FATBIN = NVFATBIN_ERROR_MISSING_FATBIN 

42 ERROR_INVALID_INDEX = NVFATBIN_ERROR_INVALID_INDEX 

43 ERROR_IDENTIFIER_REUSE = NVFATBIN_ERROR_IDENTIFIER_REUSE 

44 ERROR_INTERNAL_PTX_OPTION = NVFATBIN_ERROR_INTERNAL_PTX_OPTION 

45  

46  

47############################################################################### 

48# Error handling 

49############################################################################### 

50  

51class nvFatbinError(Exception): 

52  

53 def __init__(self, status): 

54 self.status = status 1ABH

55 s = Result(status) 1ABH

56 cdef str err = f"{s.name} ({s.value})" 1ABH

57 super(nvFatbinError, self).__init__(err) 1ABH

58  

59 def __reduce__(self): 

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

61  

62  

63@cython.profile(False) 

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

65 if status != 0: 1abcdevwxyABfghijklmnopqrstuCDEFzIGJH

66 with gil: 1ABH

67 raise nvFatbinError(status) 1ABH

68 return status 1abcdevwxyABfghijklmnopqrstuCDEFzIGJ

69  

70  

71############################################################################### 

72# Wrapper functions 

73############################################################################### 

74  

75cpdef destroy(intptr_t handle): 

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

77  

78 Args: 

79 handle (intptr_t): nvFatbin handle. 

80  

81 .. seealso:: `nvFatbinDestroy` 

82 """ 

83 cdef Handle h = <Handle>handle 1bcdevwxyABfghijklmnopqrstuCDEFzIG

84 with nogil: 1bcdevwxyABfghijklmnopqrstuCDEFzIG

85 status = nvFatbinDestroy(&h) 1bcdevwxyABfghijklmnopqrstuCDEFzIG

86 check_status(status) 1bcdevwxyABfghijklmnopqrstuCDEFzIG

87  

88  

89cpdef str get_error_string(int result): 

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

91  

92 Args: 

93 result (Result): error code. 

94  

95 .. seealso:: `nvFatbinGetErrorString` 

96 """ 

97 cdef const char* _output_ 

98 cdef bytes _output_bytes_ 

99 _output_ = nvFatbinGetErrorString(<_Result>result) 1KLMNOPQRSTUVWXYZ01

100  

101 if _output_ == NULL: 1KLMNOPQRSTUVWXYZ01

102 return "" 11

103  

104 _output_bytes_ = <bytes>_output_ 1KLMNOPQRSTUVWXYZ0

105 return _output_bytes_.decode() 1KLMNOPQRSTUVWXYZ0

106  

107  

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

109 """nvFatbinCreate creates a new handle. 

110  

111 Args: 

112 options (object): An array of strings, each containing a single option. It can be: 

113  

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

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

116 to a valid sequence of 'char', or 

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

118  

119 options_count (size_t): Number of options. 

120  

121 Returns: 

122 intptr_t: Address of nvFatbin handle. 

123  

124 .. seealso:: `nvFatbinCreate` 

125 """ 

126 cdef nested_resource[ char ] _options_ 

127 get_nested_resource_ptr[char](_options_, options, <char*>NULL) 1bcdevwxyABfghijklmnopqrstuCDEFzIGH

128 cdef Handle handle_indirect 

129 with nogil: 1bcdevwxyABfghijklmnopqrstuCDEFzIGH

130 __status__ = nvFatbinCreate(&handle_indirect, <const char**>(_options_.ptrs.data()), options_count) 1bcdevwxyABfghijklmnopqrstuCDEFzIGH

131 check_status(__status__) 1bcdevwxyABfghijklmnopqrstuCDEFzIGH

132 return <intptr_t>handle_indirect 1bcdevwxyABfghijklmnopqrstuCDEFzIG

133  

134  

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

136 """nvFatbinAddPTX adds PTX to the fatbinary. 

137  

138 Args: 

139 handle (intptr_t): nvFatbin handle. 

140 code (bytes): The PTX code. 

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

142 arch (str): The numerical architecture that this PTX is for (the XX of any sm_XX, lto_XX, or compute_XX). 

143 identifier (str): Name of the PTX, useful when extracting the fatbin with tools like cuobjdump. 

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

145  

146 .. seealso:: `nvFatbinAddPTX` 

147 """ 

148 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1fghijklmnopqrstu

149 if not isinstance(arch, str): 1fghijklmnopqrstu

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

151 cdef bytes _temp_arch_ = (<str>arch).encode() 1fghijklmnopqrstu

152 cdef char* _arch_ = _temp_arch_ 1fghijklmnopqrstu

153 if not isinstance(identifier, str): 1fghijklmnopqrstu

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

155 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1fghijklmnopqrstu

156 cdef char* _identifier_ = _temp_identifier_ 1fghijklmnopqrstu

157 if not isinstance(options_cmd_line, str): 1fghijklmnopqrstu

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

159 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1fghijklmnopqrstu

160 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1fghijklmnopqrstu

161 with nogil: 1fghijklmnopqrstu

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

163 check_status(__status__) 1fghijklmnopqrstu

164  

165  

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

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

168  

169 Args: 

170 handle (intptr_t): nvFatbin handle. 

171 code (bytes): The cubin. 

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

173 arch (str): The numerical architecture that this cubin is for (the XX of any sm_XX, lto_XX, or compute_XX). 

174 identifier (str): Name of the cubin, useful when extracting the fatbin with tools like cuobjdump. 

175  

176 .. seealso:: `nvFatbinAddCubin` 

177 """ 

178 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1vwxyAB

179 if not isinstance(arch, str): 1vwxyAB

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

181 cdef bytes _temp_arch_ = (<str>arch).encode() 1vwxyAB

182 cdef char* _arch_ = _temp_arch_ 1vwxyAB

183 if not isinstance(identifier, str): 1vwxyAB

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

185 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1vwxyAB

186 cdef char* _identifier_ = _temp_identifier_ 1vwxyAB

187 with nogil: 1vwxyAB

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

189 check_status(__status__) 1vwxyAB

190  

191  

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

193 """nvFatbinAddLTOIR adds LTOIR to the fatbinary. 

194  

195 Args: 

196 handle (intptr_t): nvFatbin handle. 

197 code (bytes): The LTOIR code. 

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

199 arch (str): The numerical architecture that this LTOIR is for (the XX of any sm_XX, lto_XX, or compute_XX). 

200 identifier (str): Name of the LTOIR, useful when extracting the fatbin with tools like cuobjdump. 

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

202  

203 .. seealso:: `nvFatbinAddLTOIR` 

204 """ 

205 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1bcde

206 if not isinstance(arch, str): 1bcde

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

208 cdef bytes _temp_arch_ = (<str>arch).encode() 1bcde

209 cdef char* _arch_ = _temp_arch_ 1bcde

210 if not isinstance(identifier, str): 1bcde

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

212 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1bcde

213 cdef char* _identifier_ = _temp_identifier_ 1bcde

214 if not isinstance(options_cmd_line, str): 1bcde

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

216 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1bcde

217 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1bcde

218 with nogil: 1bcde

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

220 check_status(__status__) 1bcde

221  

222  

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

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

225  

226 Args: 

227 handle (intptr_t): nvFatbin handle. 

228  

229 Returns: 

230 size_t: The fatbinary's size. 

231  

232 .. seealso:: `nvFatbinSize` 

233 """ 

234 cdef size_t size 

235 with nogil: 1bcdevwxyfghijklmnopqrstuCDEFzG

236 __status__ = nvFatbinSize(<Handle>handle, &size) 1bcdevwxyfghijklmnopqrstuCDEFzG

237 check_status(__status__) 1bcdevwxyfghijklmnopqrstuCDEFzG

238 return size 1bcdevwxyfghijklmnopqrstuCDEFzG

239  

240  

241cpdef get(intptr_t handle, buffer): 

242 """nvFatbinGet returns the completed fatbinary. 

243  

244 Args: 

245 handle (intptr_t): nvFatbin handle. 

246 buffer (bytes): memory to store fatbinary. 

247  

248 .. seealso:: `nvFatbinGet` 

249 """ 

250 cdef void* _buffer_ = get_buffer_pointer(buffer, -1, readonly=False) 1bcdevwxyfghijklmnopqrstuCDEFzG

251 with nogil: 1bcdevwxyfghijklmnopqrstuCDEFzG

252 __status__ = nvFatbinGet(<Handle>handle, <void*>_buffer_) 1bcdevwxyfghijklmnopqrstuCDEFzG

253 check_status(__status__) 1bcdevwxyfghijklmnopqrstuCDEFzG

254  

255  

256cpdef tuple version(): 

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

258  

259 Returns: 

260 A 2-tuple containing: 

261  

262 - unsigned int: The major version. 

263 - unsigned int: The minor version. 

264  

265 .. seealso:: `nvFatbinVersion` 

266 """ 

267 cdef unsigned int major 

268 cdef unsigned int minor 

269 with nogil: 1aJ

270 __status__ = nvFatbinVersion(&major, &minor) 1aJ

271 check_status(__status__) 1aJ

272 return (major, minor) 1aJ

273  

274  

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

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

277  

278 Args: 

279 handle (intptr_t): nvFatbin handle. 

280 code (bytes): The host object image. 

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

282  

283 .. seealso:: `nvFatbinAddReloc` 

284 """ 

285 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1CDEF

286 with nogil: 1CDEF

287 __status__ = nvFatbinAddReloc(<Handle>handle, <const void*>_code_, size) 1CDEF

288 check_status(__status__) 1CDEF

289  

290  

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

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

293  

294 Args: 

295 handle (intptr_t): nvFatbin handle. 

296 code (bytes): The Tile IR. 

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

298 identifier (str): Name of the Tile IR, useful when extracting the fatbin with tools like cuobjdump. 

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

300  

301 .. seealso:: `nvFatbinAddTileIR` 

302 """ 

303 cdef void* _code_ = get_buffer_pointer(code, size, readonly=True) 1z

304 if not isinstance(identifier, str): 1z

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

306 cdef bytes _temp_identifier_ = (<str>identifier).encode() 1z

307 cdef char* _identifier_ = _temp_identifier_ 1z

308 if not isinstance(options_cmd_line, str): 1z

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

310 cdef bytes _temp_options_cmd_line_ = (<str>options_cmd_line).encode() 1z

311 cdef char* _options_cmd_line_ = _temp_options_cmd_line_ 1z

312 with nogil: 1z

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

314 check_status(__status__) 1z