numba-cuda 0.19.1__py3-none-any.whl → 0.20.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of numba-cuda might be problematic. Click here for more details.

Files changed (172) hide show
  1. numba_cuda/VERSION +1 -1
  2. numba_cuda/numba/cuda/__init__.py +1 -1
  3. numba_cuda/numba/cuda/_internal/cuda_bf16.py +12706 -1470
  4. numba_cuda/numba/cuda/_internal/cuda_fp16.py +2653 -8769
  5. numba_cuda/numba/cuda/api.py +6 -1
  6. numba_cuda/numba/cuda/bf16.py +285 -2
  7. numba_cuda/numba/cuda/cgutils.py +2 -2
  8. numba_cuda/numba/cuda/cloudpickle/__init__.py +21 -0
  9. numba_cuda/numba/cuda/cloudpickle/cloudpickle.py +1598 -0
  10. numba_cuda/numba/cuda/cloudpickle/cloudpickle_fast.py +17 -0
  11. numba_cuda/numba/cuda/codegen.py +1 -1
  12. numba_cuda/numba/cuda/compiler.py +373 -30
  13. numba_cuda/numba/cuda/core/analysis.py +319 -0
  14. numba_cuda/numba/cuda/core/annotations/__init__.py +0 -0
  15. numba_cuda/numba/cuda/core/annotations/type_annotations.py +304 -0
  16. numba_cuda/numba/cuda/core/base.py +1289 -0
  17. numba_cuda/numba/cuda/core/bytecode.py +727 -0
  18. numba_cuda/numba/cuda/core/caching.py +2 -2
  19. numba_cuda/numba/cuda/core/compiler.py +6 -14
  20. numba_cuda/numba/cuda/core/compiler_machinery.py +497 -0
  21. numba_cuda/numba/cuda/core/config.py +747 -0
  22. numba_cuda/numba/cuda/core/consts.py +124 -0
  23. numba_cuda/numba/cuda/core/cpu.py +370 -0
  24. numba_cuda/numba/cuda/core/environment.py +68 -0
  25. numba_cuda/numba/cuda/core/event.py +511 -0
  26. numba_cuda/numba/cuda/core/funcdesc.py +330 -0
  27. numba_cuda/numba/cuda/core/inline_closurecall.py +1889 -0
  28. numba_cuda/numba/cuda/core/interpreter.py +48 -26
  29. numba_cuda/numba/cuda/core/ir_utils.py +15 -26
  30. numba_cuda/numba/cuda/core/options.py +262 -0
  31. numba_cuda/numba/cuda/core/postproc.py +249 -0
  32. numba_cuda/numba/cuda/core/pythonapi.py +1868 -0
  33. numba_cuda/numba/cuda/core/rewrites/__init__.py +26 -0
  34. numba_cuda/numba/cuda/core/rewrites/ir_print.py +90 -0
  35. numba_cuda/numba/cuda/core/rewrites/registry.py +104 -0
  36. numba_cuda/numba/cuda/core/rewrites/static_binop.py +40 -0
  37. numba_cuda/numba/cuda/core/rewrites/static_getitem.py +187 -0
  38. numba_cuda/numba/cuda/core/rewrites/static_raise.py +98 -0
  39. numba_cuda/numba/cuda/core/ssa.py +496 -0
  40. numba_cuda/numba/cuda/core/targetconfig.py +329 -0
  41. numba_cuda/numba/cuda/core/tracing.py +231 -0
  42. numba_cuda/numba/cuda/core/transforms.py +952 -0
  43. numba_cuda/numba/cuda/core/typed_passes.py +738 -7
  44. numba_cuda/numba/cuda/core/typeinfer.py +1948 -0
  45. numba_cuda/numba/cuda/core/unsafe/__init__.py +0 -0
  46. numba_cuda/numba/cuda/core/unsafe/bytes.py +67 -0
  47. numba_cuda/numba/cuda/core/unsafe/eh.py +66 -0
  48. numba_cuda/numba/cuda/core/unsafe/refcount.py +98 -0
  49. numba_cuda/numba/cuda/core/untyped_passes.py +1983 -0
  50. numba_cuda/numba/cuda/cpython/cmathimpl.py +560 -0
  51. numba_cuda/numba/cuda/cpython/mathimpl.py +499 -0
  52. numba_cuda/numba/cuda/cpython/numbers.py +1474 -0
  53. numba_cuda/numba/cuda/cuda_paths.py +422 -246
  54. numba_cuda/numba/cuda/cudadecl.py +1 -1
  55. numba_cuda/numba/cuda/cudadrv/__init__.py +1 -1
  56. numba_cuda/numba/cuda/cudadrv/devicearray.py +2 -1
  57. numba_cuda/numba/cuda/cudadrv/driver.py +11 -140
  58. numba_cuda/numba/cuda/cudadrv/dummyarray.py +111 -24
  59. numba_cuda/numba/cuda/cudadrv/libs.py +5 -5
  60. numba_cuda/numba/cuda/cudadrv/mappings.py +1 -1
  61. numba_cuda/numba/cuda/cudadrv/nvrtc.py +19 -8
  62. numba_cuda/numba/cuda/cudadrv/nvvm.py +1 -4
  63. numba_cuda/numba/cuda/cudadrv/runtime.py +1 -1
  64. numba_cuda/numba/cuda/cudaimpl.py +5 -1
  65. numba_cuda/numba/cuda/debuginfo.py +85 -2
  66. numba_cuda/numba/cuda/decorators.py +3 -3
  67. numba_cuda/numba/cuda/descriptor.py +3 -4
  68. numba_cuda/numba/cuda/deviceufunc.py +66 -2
  69. numba_cuda/numba/cuda/dispatcher.py +18 -39
  70. numba_cuda/numba/cuda/flags.py +141 -1
  71. numba_cuda/numba/cuda/fp16.py +0 -2
  72. numba_cuda/numba/cuda/include/13/cuda_bf16.h +5118 -0
  73. numba_cuda/numba/cuda/include/13/cuda_bf16.hpp +3865 -0
  74. numba_cuda/numba/cuda/include/13/cuda_fp16.h +5363 -0
  75. numba_cuda/numba/cuda/include/13/cuda_fp16.hpp +3483 -0
  76. numba_cuda/numba/cuda/lowering.py +7 -144
  77. numba_cuda/numba/cuda/mathimpl.py +2 -1
  78. numba_cuda/numba/cuda/memory_management/nrt.py +43 -17
  79. numba_cuda/numba/cuda/misc/findlib.py +75 -0
  80. numba_cuda/numba/cuda/models.py +9 -1
  81. numba_cuda/numba/cuda/np/npdatetime_helpers.py +217 -0
  82. numba_cuda/numba/cuda/np/npyfuncs.py +1807 -0
  83. numba_cuda/numba/cuda/np/numpy_support.py +553 -0
  84. numba_cuda/numba/cuda/np/ufunc/ufuncbuilder.py +59 -0
  85. numba_cuda/numba/cuda/nvvmutils.py +1 -1
  86. numba_cuda/numba/cuda/printimpl.py +12 -1
  87. numba_cuda/numba/cuda/random.py +1 -1
  88. numba_cuda/numba/cuda/serialize.py +1 -1
  89. numba_cuda/numba/cuda/simulator/__init__.py +1 -1
  90. numba_cuda/numba/cuda/simulator/api.py +1 -1
  91. numba_cuda/numba/cuda/simulator/compiler.py +4 -0
  92. numba_cuda/numba/cuda/simulator/cudadrv/devicearray.py +1 -1
  93. numba_cuda/numba/cuda/simulator/kernelapi.py +1 -1
  94. numba_cuda/numba/cuda/simulator/memory_management/nrt.py +14 -2
  95. numba_cuda/numba/cuda/target.py +35 -17
  96. numba_cuda/numba/cuda/testing.py +7 -19
  97. numba_cuda/numba/cuda/tests/__init__.py +1 -1
  98. numba_cuda/numba/cuda/tests/cloudpickle_main_class.py +9 -0
  99. numba_cuda/numba/cuda/tests/core/test_serialize.py +4 -4
  100. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_devicerecord.py +1 -1
  101. numba_cuda/numba/cuda/tests/cudadrv/test_cuda_libraries.py +1 -1
  102. numba_cuda/numba/cuda/tests/cudadrv/test_deallocations.py +1 -1
  103. numba_cuda/numba/cuda/tests/cudadrv/test_detect.py +6 -3
  104. numba_cuda/numba/cuda/tests/cudadrv/test_emm_plugins.py +1 -1
  105. numba_cuda/numba/cuda/tests/cudadrv/test_linker.py +18 -2
  106. numba_cuda/numba/cuda/tests/cudadrv/test_module_callbacks.py +2 -1
  107. numba_cuda/numba/cuda/tests/cudadrv/test_nvjitlink.py +1 -1
  108. numba_cuda/numba/cuda/tests/cudadrv/test_ptds.py +1 -1
  109. numba_cuda/numba/cuda/tests/cudapy/extensions_usecases.py +1 -1
  110. numba_cuda/numba/cuda/tests/cudapy/test_array.py +2 -1
  111. numba_cuda/numba/cuda/tests/cudapy/test_atomics.py +1 -1
  112. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16.py +539 -2
  113. numba_cuda/numba/cuda/tests/cudapy/test_bfloat16_bindings.py +81 -1
  114. numba_cuda/numba/cuda/tests/cudapy/test_caching.py +1 -3
  115. numba_cuda/numba/cuda/tests/cudapy/test_complex.py +1 -1
  116. numba_cuda/numba/cuda/tests/cudapy/test_constmem.py +1 -1
  117. numba_cuda/numba/cuda/tests/cudapy/test_cooperative_groups.py +2 -3
  118. numba_cuda/numba/cuda/tests/cudapy/test_copy_propagate.py +130 -0
  119. numba_cuda/numba/cuda/tests/cudapy/test_datetime.py +1 -1
  120. numba_cuda/numba/cuda/tests/cudapy/test_debug.py +1 -1
  121. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo.py +293 -4
  122. numba_cuda/numba/cuda/tests/cudapy/test_debuginfo_types.py +1 -1
  123. numba_cuda/numba/cuda/tests/cudapy/test_dispatcher.py +1 -1
  124. numba_cuda/numba/cuda/tests/cudapy/test_errors.py +1 -1
  125. numba_cuda/numba/cuda/tests/cudapy/test_exception.py +1 -1
  126. numba_cuda/numba/cuda/tests/cudapy/test_extending.py +2 -1
  127. numba_cuda/numba/cuda/tests/cudapy/test_inline.py +18 -8
  128. numba_cuda/numba/cuda/tests/cudapy/test_intrinsics.py +23 -21
  129. numba_cuda/numba/cuda/tests/cudapy/test_ir_utils.py +10 -37
  130. numba_cuda/numba/cuda/tests/cudapy/test_laplace.py +1 -1
  131. numba_cuda/numba/cuda/tests/cudapy/test_math.py +1 -1
  132. numba_cuda/numba/cuda/tests/cudapy/test_matmul.py +1 -1
  133. numba_cuda/numba/cuda/tests/cudapy/test_operator.py +1 -1
  134. numba_cuda/numba/cuda/tests/cudapy/test_print.py +20 -0
  135. numba_cuda/numba/cuda/tests/cudapy/test_record_dtype.py +1 -1
  136. numba_cuda/numba/cuda/tests/cudapy/test_reduction.py +1 -1
  137. numba_cuda/numba/cuda/tests/cudapy/test_serialize.py +1 -1
  138. numba_cuda/numba/cuda/tests/cudapy/test_sm.py +1 -1
  139. numba_cuda/numba/cuda/tests/cudapy/test_ssa.py +453 -0
  140. numba_cuda/numba/cuda/tests/cudapy/test_sync.py +1 -1
  141. numba_cuda/numba/cuda/tests/cudapy/test_typeinfer.py +538 -0
  142. numba_cuda/numba/cuda/tests/cudapy/test_ufuncs.py +263 -2
  143. numba_cuda/numba/cuda/tests/cudapy/test_userexc.py +1 -1
  144. numba_cuda/numba/cuda/tests/cudapy/test_vector_type.py +1 -1
  145. numba_cuda/numba/cuda/tests/cudapy/test_vectorize_decor.py +112 -6
  146. numba_cuda/numba/cuda/tests/cudapy/test_warning.py +1 -1
  147. numba_cuda/numba/cuda/tests/cudapy/test_warp_ops.py +1 -1
  148. numba_cuda/numba/cuda/tests/doc_examples/test_cg.py +0 -2
  149. numba_cuda/numba/cuda/tests/doc_examples/test_ffi.py +3 -2
  150. numba_cuda/numba/cuda/tests/doc_examples/test_laplace.py +0 -2
  151. numba_cuda/numba/cuda/tests/doc_examples/test_sessionize.py +0 -2
  152. numba_cuda/numba/cuda/tests/nocuda/test_import.py +3 -1
  153. numba_cuda/numba/cuda/tests/nocuda/test_library_lookup.py +24 -12
  154. numba_cuda/numba/cuda/tests/nrt/test_nrt.py +2 -1
  155. numba_cuda/numba/cuda/tests/support.py +55 -15
  156. numba_cuda/numba/cuda/tests/test_tracing.py +200 -0
  157. numba_cuda/numba/cuda/types.py +56 -0
  158. numba_cuda/numba/cuda/typing/__init__.py +9 -1
  159. numba_cuda/numba/cuda/typing/cffi_utils.py +55 -0
  160. numba_cuda/numba/cuda/typing/context.py +751 -0
  161. numba_cuda/numba/cuda/typing/enumdecl.py +74 -0
  162. numba_cuda/numba/cuda/typing/npydecl.py +658 -0
  163. numba_cuda/numba/cuda/typing/templates.py +7 -6
  164. numba_cuda/numba/cuda/ufuncs.py +3 -3
  165. numba_cuda/numba/cuda/utils.py +6 -112
  166. {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/METADATA +4 -3
  167. {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/RECORD +171 -116
  168. numba_cuda/numba/cuda/tests/cudadrv/test_mvc.py +0 -60
  169. {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/WHEEL +0 -0
  170. {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/licenses/LICENSE +0 -0
  171. {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/licenses/LICENSE.numba +0 -0
  172. {numba_cuda-0.19.1.dist-info → numba_cuda-0.20.1.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,329 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: BSD-2-Clause
3
+
4
+ """
5
+ This module contains utils for manipulating target configurations such as
6
+ compiler flags.
7
+ """
8
+
9
+ import re
10
+ import zlib
11
+ import base64
12
+
13
+ from types import MappingProxyType
14
+ from numba.cuda import utils
15
+
16
+
17
+ class Option:
18
+ """An option to be used in ``TargetConfig``."""
19
+
20
+ __slots__ = "_type", "_default", "_doc"
21
+
22
+ def __init__(self, type, *, default, doc):
23
+ """
24
+ Parameters
25
+ ----------
26
+ type :
27
+ Type of the option value. It can be a callable.
28
+ The setter always calls ``self._type(value)``.
29
+ default :
30
+ The default value for the option.
31
+ doc : str
32
+ Docstring for the option.
33
+ """
34
+ self._type = type
35
+ self._default = default
36
+ self._doc = doc
37
+
38
+ @property
39
+ def type(self):
40
+ return self._type
41
+
42
+ @property
43
+ def default(self):
44
+ return self._default
45
+
46
+ @property
47
+ def doc(self):
48
+ return self._doc
49
+
50
+
51
+ try:
52
+ from numba.core.targetconfig import ConfigStack, _FlagsStack
53
+ except ImportError:
54
+
55
+ class _FlagsStack(utils.ThreadLocalStack, stack_name="flags"):
56
+ pass
57
+
58
+ class ConfigStack:
59
+ """A stack for tracking target configurations in the compiler.
60
+
61
+ It stores the stack in a thread-local class attribute. All instances in the
62
+ same thread will see the same stack.
63
+ """
64
+
65
+ @classmethod
66
+ def top_or_none(cls):
67
+ """Get the TOS or return None if no config is set."""
68
+ self = cls()
69
+ if self:
70
+ flags = self.top()
71
+ else:
72
+ # Note: should this be the default flag for the target instead?
73
+ flags = None
74
+ return flags
75
+
76
+ def __init__(self):
77
+ self._stk = _FlagsStack()
78
+
79
+ def top(self):
80
+ return self._stk.top()
81
+
82
+ def __len__(self):
83
+ return len(self._stk)
84
+
85
+ def enter(self, flags):
86
+ """Returns a contextmanager that performs ``push(flags)`` on enter and
87
+ ``pop()`` on exit.
88
+ """
89
+ return self._stk.enter(flags)
90
+
91
+
92
+ class _MetaTargetConfig(type):
93
+ """Metaclass for ``TargetConfig``.
94
+
95
+ When a subclass of ``TargetConfig`` is created, all ``Option`` defined
96
+ as class members will be parsed and corresponding getters, setters, and
97
+ delters will be inserted.
98
+ """
99
+
100
+ def __init__(cls, name, bases, dct):
101
+ """Invoked when subclass is created.
102
+
103
+ Insert properties for each ``Option`` that are class members.
104
+ All the options will be grouped inside the ``.options`` class
105
+ attribute.
106
+ """
107
+ # Gather options from base classes and class dict
108
+ opts = {}
109
+ # Reversed scan into the base classes to follow MRO ordering such that
110
+ # the closest base class is overriding
111
+ for base_cls in reversed(bases):
112
+ opts.update(base_cls.options)
113
+ opts.update(cls.find_options(dct))
114
+ # Store the options into class attribute as a ready-only mapping.
115
+ cls.options = MappingProxyType(opts)
116
+
117
+ # Make properties for each of the options
118
+ def make_prop(name, option):
119
+ def getter(self):
120
+ return self._values.get(name, option.default)
121
+
122
+ def setter(self, val):
123
+ self._values[name] = option.type(val)
124
+
125
+ def delter(self):
126
+ del self._values[name]
127
+
128
+ return property(getter, setter, delter, option.doc)
129
+
130
+ for name, option in cls.options.items():
131
+ setattr(cls, name, make_prop(name, option))
132
+
133
+ def find_options(cls, dct):
134
+ """Returns a new dict with all the items that are a mapping to an
135
+ ``Option``.
136
+ """
137
+ return {k: v for k, v in dct.items() if isinstance(v, Option)}
138
+
139
+
140
+ class _NotSetType:
141
+ def __repr__(self):
142
+ return "<NotSet>"
143
+
144
+
145
+ _NotSet = _NotSetType()
146
+
147
+
148
+ class TargetConfig(metaclass=_MetaTargetConfig):
149
+ """Base class for ``TargetConfig``.
150
+
151
+ Subclass should fill class members with ``Option``. For example:
152
+
153
+ >>> class MyTargetConfig(TargetConfig):
154
+ >>> a_bool_option = Option(type=bool, default=False, doc="a bool")
155
+ >>> an_int_option = Option(type=int, default=0, doc="an int")
156
+
157
+ The metaclass will insert properties for each ``Option``. For example:
158
+
159
+ >>> tc = MyTargetConfig()
160
+ >>> tc.a_bool_option = True # invokes the setter
161
+ >>> print(tc.an_int_option) # print the default
162
+ """
163
+
164
+ __slots__ = ["_values"]
165
+
166
+ # Used for compression in mangling.
167
+ # Set to -15 to disable the header and checksum for smallest output.
168
+ _ZLIB_CONFIG = {"wbits": -15}
169
+
170
+ def __init__(self, copy_from=None):
171
+ """
172
+ Parameters
173
+ ----------
174
+ copy_from : TargetConfig or None
175
+ if None, creates an empty ``TargetConfig``.
176
+ Otherwise, creates a copy.
177
+ """
178
+ self._values = {}
179
+ if copy_from is not None:
180
+ assert isinstance(copy_from, TargetConfig)
181
+ self._values.update(copy_from._values)
182
+
183
+ def __repr__(self):
184
+ # NOTE: default options will be placed at the end and grouped inside
185
+ # a square bracket; i.e. [optname=optval, ...]
186
+ args = []
187
+ defs = []
188
+ for k in self.options:
189
+ msg = f"{k}={getattr(self, k)}"
190
+ if not self.is_set(k):
191
+ defs.append(msg)
192
+ else:
193
+ args.append(msg)
194
+ clsname = self.__class__.__name__
195
+ return f"{clsname}({', '.join(args)}, [{', '.join(defs)}])"
196
+
197
+ def __hash__(self):
198
+ return hash(tuple(sorted(self.values())))
199
+
200
+ def __eq__(self, other):
201
+ if isinstance(other, TargetConfig):
202
+ return self.values() == other.values()
203
+ else:
204
+ return NotImplemented
205
+
206
+ def values(self):
207
+ """Returns a dict of all the values"""
208
+ return {k: getattr(self, k) for k in self.options}
209
+
210
+ def is_set(self, name):
211
+ """Is the option set?"""
212
+ self._guard_option(name)
213
+ return name in self._values
214
+
215
+ def discard(self, name):
216
+ """Remove the option by name if it is defined.
217
+
218
+ After this, the value for the option will be set to its default value.
219
+ """
220
+ self._guard_option(name)
221
+ self._values.pop(name, None)
222
+
223
+ def inherit_if_not_set(self, name, default=_NotSet):
224
+ """Inherit flag from ``ConfigStack``.
225
+
226
+ Parameters
227
+ ----------
228
+ name : str
229
+ Option name.
230
+ default : optional
231
+ When given, it overrides the default value.
232
+ It is only used when the flag is not defined locally and there is
233
+ no entry in the ``ConfigStack``.
234
+ """
235
+ self._guard_option(name)
236
+ if not self.is_set(name):
237
+ cstk = ConfigStack()
238
+ if cstk:
239
+ # inherit
240
+ top = cstk.top()
241
+ setattr(self, name, getattr(top, name))
242
+ elif default is not _NotSet:
243
+ setattr(self, name, default)
244
+
245
+ def copy(self):
246
+ """Clone this instance."""
247
+ return type(self)(self)
248
+
249
+ def summary(self) -> str:
250
+ """Returns a ``str`` that summarizes this instance.
251
+
252
+ In contrast to ``__repr__``, only options that are explicitly set will
253
+ be shown.
254
+ """
255
+ args = [f"{k}={v}" for k, v in self._summary_args()]
256
+ clsname = self.__class__.__name__
257
+ return f"{clsname}({', '.join(args)})"
258
+
259
+ def _guard_option(self, name):
260
+ if name not in self.options:
261
+ msg = f"{name!r} is not a valid option for {type(self)}"
262
+ raise ValueError(msg)
263
+
264
+ def _summary_args(self):
265
+ """returns a sorted sequence of 2-tuple containing the
266
+ ``(flag_name, flag_value)`` for flag that are set with a non-default
267
+ value.
268
+ """
269
+ args = []
270
+ for k in sorted(self.options):
271
+ opt = self.options[k]
272
+ if self.is_set(k):
273
+ flagval = getattr(self, k)
274
+ if opt.default != flagval:
275
+ v = (k, flagval)
276
+ args.append(v)
277
+ return args
278
+
279
+ @classmethod
280
+ def _make_compression_dictionary(cls) -> bytes:
281
+ """Returns a ``bytes`` object suitable for use as a dictionary for
282
+ compression.
283
+ """
284
+ buf = []
285
+ # include package name
286
+ buf.append("numba")
287
+ # include class name
288
+ buf.append(cls.__class__.__name__)
289
+ # include common values
290
+ buf.extend(["True", "False"])
291
+ # include all options name and their default value
292
+ for k, opt in cls.options.items():
293
+ buf.append(k)
294
+ buf.append(str(opt.default))
295
+ return "".join(buf).encode()
296
+
297
+ def get_mangle_string(self) -> str:
298
+ """Return a string suitable for symbol mangling."""
299
+ zdict = self._make_compression_dictionary()
300
+
301
+ comp = zlib.compressobj(
302
+ zdict=zdict, level=zlib.Z_BEST_COMPRESSION, **self._ZLIB_CONFIG
303
+ )
304
+ # The mangled string is a compressed and base64 encoded version of the
305
+ # summary
306
+ buf = [comp.compress(self.summary().encode())]
307
+ buf.append(comp.flush())
308
+ return base64.b64encode(b"".join(buf)).decode()
309
+
310
+ @classmethod
311
+ def demangle(cls, mangled: str) -> str:
312
+ """Returns the demangled result from ``.get_mangle_string()``"""
313
+
314
+ # unescape _XX sequence
315
+ def repl(x):
316
+ return chr(int("0x" + x.group(0)[1:], 16))
317
+
318
+ unescaped = re.sub(r"_[a-zA-Z0-9][a-zA-Z0-9]", repl, mangled)
319
+ # decode base64
320
+ raw = base64.b64decode(unescaped)
321
+ # decompress
322
+ zdict = cls._make_compression_dictionary()
323
+ dc = zlib.decompressobj(zdict=zdict, **cls._ZLIB_CONFIG)
324
+ buf = []
325
+ while raw:
326
+ buf.append(dc.decompress(raw))
327
+ raw = dc.unconsumed_tail
328
+ buf.append(dc.flush())
329
+ return b"".join(buf).decode()
@@ -0,0 +1,231 @@
1
+ # SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2
+ # SPDX-License-Identifier: BSD-2-Clause
3
+
4
+ import inspect
5
+ import logging
6
+ import sys
7
+ import threading
8
+ from functools import wraps
9
+ from itertools import chain
10
+
11
+ from numba.core import config
12
+
13
+
14
+ class TLS(threading.local):
15
+ """Use a subclass to properly initialize the TLS variables in all threads.""" # noqa: E501
16
+
17
+ def __init__(self):
18
+ self.tracing = False
19
+ self.indent = 0
20
+
21
+
22
+ tls = TLS()
23
+
24
+
25
+ def find_function_info(func, spec, args):
26
+ """Return function meta-data in a tuple.
27
+
28
+ (name, type)"""
29
+
30
+ module = getattr(func, "__module__", None)
31
+ name = getattr(func, "__name__", None)
32
+ self = getattr(func, "__self__", None)
33
+ cname = None
34
+ if self:
35
+ cname = self.__name__
36
+ # cname = self.__class__.__name__
37
+ # Try to deduce the class' name even for unbound methods from their
38
+ # first argument, which we assume to be a class instance if named 'self'...
39
+ elif len(spec.args) and spec.args[0] == "self":
40
+ cname = args[0].__class__.__name__
41
+ # ...or a class object if named 'cls'
42
+ elif len(spec.args) and spec.args[0] == "cls":
43
+ cname = args[0].__name__
44
+ if name:
45
+ qname = []
46
+ if module and module != "__main__":
47
+ qname.append(module)
48
+ qname.append(".")
49
+ if cname:
50
+ qname.append(cname)
51
+ qname.append(".")
52
+ qname.append(name)
53
+ name = "".join(qname)
54
+ return name, None
55
+
56
+
57
+ def chop(value):
58
+ MAX_SIZE = 320
59
+ s = repr(value)
60
+ if len(s) > MAX_SIZE:
61
+ return s[:MAX_SIZE] + "..." + s[-1]
62
+ else:
63
+ return s
64
+
65
+
66
+ def create_events(fname, spec, args, kwds):
67
+ values = dict()
68
+ if spec.defaults:
69
+ values = dict(zip(spec.args[-len(spec.defaults) :], spec.defaults))
70
+ values.update(kwds)
71
+ values.update(list(zip(spec.args[: len(args)], args)))
72
+ positional = ["%s=%r" % (a, values.pop(a)) for a in spec.args]
73
+ anonymous = [str(a) for a in args[len(positional) :]]
74
+ keywords = ["%s=%r" % (k, values[k]) for k in sorted(values.keys())]
75
+ params = ", ".join([f for f in chain(positional, anonymous, keywords) if f])
76
+
77
+ enter = [">> ", tls.indent * " ", fname, "(", params, ")"]
78
+ leave = ["<< ", tls.indent * " ", fname]
79
+ return enter, leave
80
+
81
+
82
+ def dotrace(*args, **kwds):
83
+ """Function decorator to trace a function's entry and exit.
84
+
85
+ *args: categories in which to trace this function. Example usage:
86
+
87
+ @trace
88
+ def function(...):...
89
+
90
+ @trace('mycategory')
91
+ def function(...):...
92
+
93
+
94
+ """
95
+
96
+ recursive = kwds.get("recursive", False)
97
+
98
+ def decorator(func):
99
+ spec = None
100
+ logger = logging.getLogger("trace")
101
+
102
+ def wrapper(*args, **kwds):
103
+ if not logger.isEnabledFor(logging.INFO) or tls.tracing:
104
+ return func(*args, **kwds)
105
+
106
+ fname, ftype = find_function_info(func, spec, args)
107
+
108
+ try:
109
+ tls.tracing = True
110
+ enter, leave = create_events(fname, spec, args, kwds)
111
+
112
+ try:
113
+ logger.info("".join(enter))
114
+ tls.indent += 1
115
+ try:
116
+ try:
117
+ tls.tracing = False
118
+ result = func(*args, **kwds)
119
+ finally:
120
+ tls.tracing = True
121
+ except: # noqa: E722
122
+ type, value, traceback = sys.exc_info()
123
+ leave.append(" => exception thrown\n\traise ")
124
+ mname = type.__module__
125
+ if mname != "__main__":
126
+ leave.append(mname)
127
+ leave.append(".")
128
+ leave.append(type.__name__)
129
+ if value.args:
130
+ leave.append("(")
131
+ leave.append(", ".join(chop(v) for v in value.args))
132
+ leave.append(")")
133
+ else:
134
+ leave.append("()")
135
+ raise
136
+ else:
137
+ if result is not None:
138
+ leave.append(" -> ")
139
+ leave.append(chop(result))
140
+ finally:
141
+ tls.indent -= 1
142
+ logger.info("".join(leave))
143
+ finally:
144
+ tls.tracing = False
145
+ return result
146
+
147
+ # wrapper end
148
+
149
+ rewrap = lambda x: x
150
+ # Unwrap already wrapped functions
151
+ # (to be rewrapped again later)
152
+ if isinstance(func, classmethod):
153
+ rewrap = type(func)
154
+ # Note: 'func.__func__' only works in Python 3
155
+ func = func.__get__(True).__func__
156
+ elif isinstance(func, staticmethod):
157
+ rewrap = type(func)
158
+ # Note: 'func.__func__' only works in Python 3
159
+ func = func.__get__(True)
160
+ elif isinstance(func, property):
161
+ raise NotImplementedError
162
+
163
+ spec = inspect.getfullargspec(func)
164
+ return rewrap(wraps(func)(wrapper))
165
+
166
+ arg0 = len(args) and args[0] or None
167
+ # not supported yet...
168
+ if recursive:
169
+ raise NotImplementedError
170
+ if inspect.ismodule(arg0):
171
+ for n, f in inspect.getmembers(arg0, inspect.isfunction):
172
+ setattr(arg0, n, decorator(f))
173
+ for n, c in inspect.getmembers(arg0, inspect.isclass):
174
+ dotrace(c, *args, recursive=recursive)
175
+ elif inspect.isclass(arg0):
176
+ for n, f in inspect.getmembers(
177
+ arg0, lambda x: (inspect.isfunction(x) or inspect.ismethod(x))
178
+ ):
179
+ setattr(arg0, n, decorator(f))
180
+
181
+ if callable(arg0) or type(arg0) in (classmethod, staticmethod):
182
+ return decorator(arg0)
183
+ elif isinstance(arg0, property):
184
+ # properties combine up to three functions: 'get', 'set', 'del',
185
+ # so let's wrap them all.
186
+ pget, pset, pdel = None, None, None
187
+ if arg0.fget:
188
+ pget = decorator(arg0.fget)
189
+ if arg0.fset:
190
+ pset = decorator(arg0.fset)
191
+ if arg0.fdel:
192
+ pdel = decorator(arg0.fdel)
193
+ return property(pget, pset, pdel)
194
+
195
+ else:
196
+ return decorator
197
+
198
+
199
+ def notrace(*args, **kwds):
200
+ """Just a no-op in case tracing is disabled."""
201
+
202
+ def decorator(func):
203
+ return func
204
+
205
+ arg0 = len(args) and args[0] or None
206
+
207
+ if callable(arg0) or type(arg0) in (classmethod, staticmethod):
208
+ return decorator(arg0)
209
+ else:
210
+ return decorator
211
+
212
+
213
+ def doevent(msg):
214
+ msg = ["== ", tls.indent * " ", msg]
215
+ logger = logging.getLogger("trace")
216
+ logger.info("".join(msg))
217
+
218
+
219
+ def noevent(msg):
220
+ pass
221
+
222
+
223
+ if config.TRACE:
224
+ logger = logging.getLogger("trace")
225
+ logger.setLevel(logging.INFO)
226
+ logger.handlers = [logging.StreamHandler()]
227
+ trace = dotrace
228
+ event = doevent
229
+ else:
230
+ trace = notrace
231
+ event = noevent