Coverage for cuda / bindings / _internal / utils.pyx: 66.28%

86 statements  

« prev     ^ index     » next       coverage.py v7.13.5, created at 2026-03-25 01:07 +0000

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

2# 

3# SPDX-License-Identifier: LicenseRef-NVIDIA-SOFTWARE-LICENSE 

4  

5cimport cpython 

6from libc.stdint cimport intptr_t 

7from libcpp.utility cimport move 

8from cython.operator cimport dereference as deref 

9  

10  

11cdef extern from *: 

12 """ 

13 #if defined(__clang__) 

14 #define _COMPILER_VERSION ("Clang " __clang_version__) 

15 #elif defined(__GNUC__) || defined(__GNUG__) 

16 #define _COMPILER_VERSION ("GCC " __VERSION__) 

17 #elif defined(_MSC_VER) 

18 #define _COMPILER_VERSION ("MSVC " Py_STRINGIFY(_MSC_VER)) 

19 #else 

20 #define _COMPILER_VERSION ("Unknown Compiler") 

21 #endif 

22 """ 

23 cdef char *_COMPILER_VERSION 

24  

25  

26cpdef str get_c_compiler(): 

27 """ 

28 Returns a string describing the C compiler used to build cuda.bindings 

29 """ 

30 return _COMPILER_VERSION.decode() 2hb

31  

32  

33cdef bint is_nested_sequence(data): 

34 if not cpython.PySequence_Check(data): 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

35 return False 

36 else: 

37 for i in data: 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

38 if not cpython.PySequence_Check(i): 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

39 return False 

40 else: 

41 return True 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

42  

43  

44cdef void* get_buffer_pointer(buf, Py_ssize_t size, readonly=True) except*: 

45 """The caller must ensure ``buf`` is alive when the returned pointer is in use.""" 

46 cdef void* bufPtr 

47 cdef int flags = cpython.PyBUF_ANY_CONTIGUOUS 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o fbgbp q r s $ % ' t u b ( v w c dbeb, F G - H I

48 if not readonly: 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o fbgbp q r s $ % ' t u b ( v w c dbeb, F G - H I

49 flags |= cpython.PyBUF_WRITABLE 2a J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb

50 cdef int status = -1 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o fbgbp q r s $ % ' t u b ( v w c dbeb, F G - H I

51 cdef cpython.Py_buffer view 

52  

53 if isinstance(buf, int): 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o fbgbp q r s $ % ' t u b ( v w c dbeb, F G - H I

54 bufPtr = <void*><intptr_t>buf 

55 else: # try buffer protocol 

56 try: 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o fbgbp q r s $ % ' t u b ( v w c dbeb, F G - H I

57 status = cpython.PyObject_GetBuffer(buf, &view, flags) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o fbgbp q r s $ % ' t u b ( v w c dbeb, F G - H I

58 # when the caller does not provide a size, it is set to -1 at generate-time by cybind 

59 if size != -1: 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb, F G - H I

60 assert view.len == size 1aJKLMNOPQ)*RSTUVWXYZ0123456789!#xyzAdefghijklmnopqrs$%'tub(vwc,FG-HI

61 assert view.ndim == 1 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb, F G - H I

62 except Exception as e: 2fbgb

63 adj = "writable " if not readonly else "" 2fbgb

64 raise ValueError( 2fbgb

65 "buf must be either a Python int representing the pointer " 2fbgb

66 f"address to a valid buffer, or a 1D contiguous {adj}" 2fbgb

67 "buffer, of size bytes") from e 2fbgb

68 else: 

69 bufPtr = view.buf 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb, F G - H I

70 finally: 

71 if status == 0: 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb, F G - H I

72 cpython.PyBuffer_Release(&view) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb, F G - H I

73  

74 return bufPtr 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # + x y z A B C D E d e f g h i j k l m n o p q r s $ % ' t u b ( v w c dbeb, F G - H I

75  

76  

77# Cython can't infer the ResT overload when it is wrapped in nullable_unique_ptr, 

78# so we need a dummy (__unused) input argument to help it 

79cdef int get_resource_ptr(nullable_unique_ptr[vector[ResT]] &in_out_ptr, object obj, ResT* __unused) except 1: 

80 if cpython.PySequence_Check(obj): 

81 vec = new vector[ResT](len(obj)) 

82 # set the ownership immediately to avoid leaking the `vec` memory in 

83 # case of exception in the following loop 

84 in_out_ptr.reset(vec, True) 

85 for i in range(len(obj)): 

86 deref(vec)[i] = obj[i] 

87 else: 

88 in_out_ptr.reset(<vector[ResT]*><intptr_t>obj, False) 

89 return 0 

90  

91  

92cdef int get_resource_ptrs(nullable_unique_ptr[ vector[PtrT*] ] &in_out_ptr, object obj, PtrT* __unused) except 1: 

93 if cpython.PySequence_Check(obj): 

94 vec = new vector[PtrT*](len(obj)) 

95 # set the ownership immediately to avoid leaking the `vec` memory in 

96 # case of exception in the following loop 

97 in_out_ptr.reset(vec, True) 

98 for i in range(len(obj)): 

99 deref(vec)[i] = <PtrT*><intptr_t>(obj[i]) 

100 else: 

101 in_out_ptr.reset(<vector[PtrT*]*><intptr_t>obj, False) 

102 return 0 

103  

104  

105cdef int get_nested_resource_ptr(nested_resource[ResT] &in_out_ptr, object obj, ResT* __unused) except 1: 

106 cdef nullable_unique_ptr[ vector[intptr_t] ] nested_ptr 

107 cdef nullable_unique_ptr[ vector[vector[ResT]] ] nested_res_ptr 

108 cdef vector[intptr_t]* nested_vec = NULL 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

109 cdef vector[vector[ResT]]* nested_res_vec = NULL 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

110 cdef size_t i = 0, length = 0 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

111 cdef intptr_t addr 

112  

113 if is_nested_sequence(obj): 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

114 length = len(obj) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

115 nested_res_vec = new vector[vector[ResT]](length) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

116 nested_vec = new vector[intptr_t](length) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

117 # set the ownership immediately to avoid leaking memory in case of 

118 # exception in the following loop 

119 nested_res_ptr.reset(nested_res_vec, True) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

120 nested_ptr.reset(nested_vec, True) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

121 for i, obj_i in enumerate(obj): 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

122 if ResT is char: 

123 obj_i_type = type(obj_i) 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

124 if obj_i_type is str: 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

125 obj_i_bytes = obj_i.encode("utf-8") 1a/xyzA:;=?@[]^_`{|BCDEdefghijklmno}~pqrstuvwFGHI

126 elif obj_i_type is bytes: 1.bc

127 obj_i_bytes = obj_i 1.bc

128 else: 

129 raise TypeError( 

130 f"Expected str or bytes, got {obj_i_type.__name__}") 

131 str_len = <size_t>(len(obj_i_bytes)) + 1 # including null termination 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

132 deref(nested_res_vec)[i].resize(str_len) 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

133 obj_i_ptr = <char*>(obj_i_bytes) 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

134 # cast to size_t explicitly to work around a potentially Cython bug 

135 deref(nested_res_vec)[i].assign(obj_i_ptr, obj_i_ptr + <size_t>str_len) 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

136 else: 

137 deref(nested_res_vec)[i] = obj_i 

138 deref(nested_vec)[i] = <intptr_t>(deref(nested_res_vec)[i].data()) 1a/xyzA:;=?@[]^_`{|.BCDEdefghijklmno}~pqrstubvwcFGHI

139 elif cpython.PySequence_Check(obj): 

140 length = len(obj) 

141 nested_vec = new vector[intptr_t](length) 

142 nested_ptr.reset(nested_vec, True) 

143 for i, addr in enumerate(obj): 

144 deref(nested_vec)[i] = addr 

145 nested_res_ptr.reset(NULL, False) 

146 else: 

147 # obj is an int (ResT**) 

148 nested_res_ptr.reset(NULL, False) 

149 nested_ptr.reset(<vector[intptr_t]*><intptr_t>obj, False) 

150  

151 in_out_ptr.ptrs = move(nested_ptr) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

152 in_out_ptr.nested_resource_ptr = move(nested_res_ptr) 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

153 return 0 2a J K L M N O P Q ) * R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9 ! # ab+ / x y z A : ; = ? @ [ ] ^ _ ` { | . B C D E d e f g h i j k l m n o } ~ p q r s $ % bbcb' t u b ( v w c , F G - H I

154  

155  

156class FunctionNotFoundError(RuntimeError): pass 

157  

158class NotSupportedError(RuntimeError): pass