nvmath-python 0.5.0__cp313-cp313-manylinux_2_28_aarch64.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.
Files changed (183) hide show
  1. nvmath/__init__.pxd +0 -0
  2. nvmath/__init__.py +37 -0
  3. nvmath/_internal/__init__.py +0 -0
  4. nvmath/_internal/layout.py +58 -0
  5. nvmath/_utils.py +363 -0
  6. nvmath/bindings/__init__.pxd +0 -0
  7. nvmath/bindings/__init__.py +44 -0
  8. nvmath/bindings/_internal/__init__.pxd +0 -0
  9. nvmath/bindings/_internal/__init__.py +0 -0
  10. nvmath/bindings/_internal/cublas.cpython-313-aarch64-linux-gnu.so +0 -0
  11. nvmath/bindings/_internal/cublas.pxd +518 -0
  12. nvmath/bindings/_internal/cublasLt.cpython-313-aarch64-linux-gnu.so +0 -0
  13. nvmath/bindings/_internal/cublasLt.pxd +54 -0
  14. nvmath/bindings/_internal/cudss.cpython-313-aarch64-linux-gnu.so +0 -0
  15. nvmath/bindings/_internal/cudss.pxd +44 -0
  16. nvmath/bindings/_internal/cufft.cpython-313-aarch64-linux-gnu.so +0 -0
  17. nvmath/bindings/_internal/cufft.pxd +69 -0
  18. nvmath/bindings/_internal/cufftMp.cpython-313-aarch64-linux-gnu.so +0 -0
  19. nvmath/bindings/_internal/cufftMp.pxd +77 -0
  20. nvmath/bindings/_internal/curand.cpython-313-aarch64-linux-gnu.so +0 -0
  21. nvmath/bindings/_internal/curand.pxd +42 -0
  22. nvmath/bindings/_internal/cusolver.cpython-313-aarch64-linux-gnu.so +0 -0
  23. nvmath/bindings/_internal/cusolver.pxd +15 -0
  24. nvmath/bindings/_internal/cusolverDn.cpython-313-aarch64-linux-gnu.so +0 -0
  25. nvmath/bindings/_internal/cusolverDn.pxd +386 -0
  26. nvmath/bindings/_internal/cusparse.cpython-313-aarch64-linux-gnu.so +0 -0
  27. nvmath/bindings/_internal/cusparse.pxd +270 -0
  28. nvmath/bindings/_internal/mathdx.cpython-313-aarch64-linux-gnu.so +0 -0
  29. nvmath/bindings/_internal/mathdx.pxd +79 -0
  30. nvmath/bindings/_internal/nvshmem.cpython-313-aarch64-linux-gnu.so +0 -0
  31. nvmath/bindings/_internal/nvshmem.pxd +29 -0
  32. nvmath/bindings/_internal/utils.cpython-313-aarch64-linux-gnu.so +0 -0
  33. nvmath/bindings/_internal/utils.pxd +186 -0
  34. nvmath/bindings/cublas.cpython-313-aarch64-linux-gnu.so +0 -0
  35. nvmath/bindings/cublas.pxd +545 -0
  36. nvmath/bindings/cublas.pyi +18 -0
  37. nvmath/bindings/cublasLt.cpython-313-aarch64-linux-gnu.so +0 -0
  38. nvmath/bindings/cublasLt.pxd +99 -0
  39. nvmath/bindings/cudss.cpython-313-aarch64-linux-gnu.so +0 -0
  40. nvmath/bindings/cudss.pxd +83 -0
  41. nvmath/bindings/cudss.pyi +249 -0
  42. nvmath/bindings/cufft.cpython-313-aarch64-linux-gnu.so +0 -0
  43. nvmath/bindings/cufft.pxd +114 -0
  44. nvmath/bindings/cufftMp.cpython-313-aarch64-linux-gnu.so +0 -0
  45. nvmath/bindings/cufftMp.pxd +124 -0
  46. nvmath/bindings/curand.cpython-313-aarch64-linux-gnu.so +0 -0
  47. nvmath/bindings/curand.pxd +71 -0
  48. nvmath/bindings/cusolver.cpython-313-aarch64-linux-gnu.so +0 -0
  49. nvmath/bindings/cusolver.pxd +60 -0
  50. nvmath/bindings/cusolverDn.cpython-313-aarch64-linux-gnu.so +0 -0
  51. nvmath/bindings/cusolverDn.pxd +410 -0
  52. nvmath/bindings/cusparse.cpython-313-aarch64-linux-gnu.so +0 -0
  53. nvmath/bindings/cusparse.pxd +329 -0
  54. nvmath/bindings/cycublas.cpython-313-aarch64-linux-gnu.so +0 -0
  55. nvmath/bindings/cycublas.pxd +657 -0
  56. nvmath/bindings/cycublasLt.cpython-313-aarch64-linux-gnu.so +0 -0
  57. nvmath/bindings/cycublasLt.pxd +1010 -0
  58. nvmath/bindings/cycudss.cpython-313-aarch64-linux-gnu.so +0 -0
  59. nvmath/bindings/cycudss.pxd +191 -0
  60. nvmath/bindings/cycufft.cpython-313-aarch64-linux-gnu.so +0 -0
  61. nvmath/bindings/cycufft.pxd +320 -0
  62. nvmath/bindings/cycufftMp.cpython-313-aarch64-linux-gnu.so +0 -0
  63. nvmath/bindings/cycufftMp.pxd +333 -0
  64. nvmath/bindings/cycurand.cpython-313-aarch64-linux-gnu.so +0 -0
  65. nvmath/bindings/cycurand.pxd +146 -0
  66. nvmath/bindings/cycusolver.cpython-313-aarch64-linux-gnu.so +0 -0
  67. nvmath/bindings/cycusolver.pxd +143 -0
  68. nvmath/bindings/cycusolverDn.cpython-313-aarch64-linux-gnu.so +0 -0
  69. nvmath/bindings/cycusolverDn.pxd +414 -0
  70. nvmath/bindings/cycusparse.cpython-313-aarch64-linux-gnu.so +0 -0
  71. nvmath/bindings/cycusparse.pxd +476 -0
  72. nvmath/bindings/cymathdx.cpython-313-aarch64-linux-gnu.so +0 -0
  73. nvmath/bindings/cymathdx.pxd +363 -0
  74. nvmath/bindings/cynvshmem.cpython-313-aarch64-linux-gnu.so +0 -0
  75. nvmath/bindings/cynvshmem.pxd +126 -0
  76. nvmath/bindings/mathdx.cpython-313-aarch64-linux-gnu.so +0 -0
  77. nvmath/bindings/mathdx.pxd +131 -0
  78. nvmath/bindings/mathdx.pyi +686 -0
  79. nvmath/bindings/nvpl/__init__.pxd +0 -0
  80. nvmath/bindings/nvpl/__init__.py +11 -0
  81. nvmath/bindings/nvpl/_internal/__init__.pxd +0 -0
  82. nvmath/bindings/nvpl/_internal/__init__.py +0 -0
  83. nvmath/bindings/nvpl/_internal/fft.cpython-313-aarch64-linux-gnu.so +0 -0
  84. nvmath/bindings/nvpl/_internal/fft.pxd +36 -0
  85. nvmath/bindings/nvpl/cyfft.cpython-313-aarch64-linux-gnu.so +0 -0
  86. nvmath/bindings/nvpl/cyfft.pxd +87 -0
  87. nvmath/bindings/nvpl/fft.cpython-313-aarch64-linux-gnu.so +0 -0
  88. nvmath/bindings/nvpl/fft.pxd +100 -0
  89. nvmath/bindings/nvshmem.cpython-313-aarch64-linux-gnu.so +0 -0
  90. nvmath/bindings/nvshmem.pxd +54 -0
  91. nvmath/device/__init__.py +23 -0
  92. nvmath/device/_deprecated.py +33 -0
  93. nvmath/device/caching.py +51 -0
  94. nvmath/device/common.py +131 -0
  95. nvmath/device/common_backend.py +125 -0
  96. nvmath/device/common_cuda.py +156 -0
  97. nvmath/device/common_mathdx.py +97 -0
  98. nvmath/device/common_numba.py +159 -0
  99. nvmath/device/common_opaque_tensor.py +128 -0
  100. nvmath/device/cublasdx.py +1085 -0
  101. nvmath/device/cublasdx_backend.py +552 -0
  102. nvmath/device/cublasdx_numba.py +528 -0
  103. nvmath/device/cufftdx.py +551 -0
  104. nvmath/device/cufftdx_backend.py +194 -0
  105. nvmath/device/cufftdx_numba.py +123 -0
  106. nvmath/device/curand_kernel.py +9147 -0
  107. nvmath/device/nvrtc.py +95 -0
  108. nvmath/device/patch.py +35 -0
  109. nvmath/device/random.py +417 -0
  110. nvmath/device/random_helpers.py +23 -0
  111. nvmath/device/random_states.py +189 -0
  112. nvmath/device/types.py +11 -0
  113. nvmath/device/vector_types_numba.py +203 -0
  114. nvmath/distributed/__init__.py +117 -0
  115. nvmath/distributed/_internal/__init__.py +0 -0
  116. nvmath/distributed/_internal/nvshmem.py +243 -0
  117. nvmath/distributed/_internal/tensor_ifc_cupy.py +111 -0
  118. nvmath/distributed/_internal/tensor_ifc_numpy.py +53 -0
  119. nvmath/distributed/_internal/tensor_ifc_torch.py +108 -0
  120. nvmath/distributed/_internal/tensor_wrapper.py +59 -0
  121. nvmath/distributed/_utils.py +152 -0
  122. nvmath/distributed/fft/__init__.py +6 -0
  123. nvmath/distributed/fft/_configuration.py +86 -0
  124. nvmath/distributed/fft/fft.py +1737 -0
  125. nvmath/distributed/reshape/__init__.py +6 -0
  126. nvmath/distributed/reshape/_configuration.py +39 -0
  127. nvmath/distributed/reshape/reshape.py +1249 -0
  128. nvmath/fft/__init__.py +7 -0
  129. nvmath/fft/_configuration.py +208 -0
  130. nvmath/fft/_exec_utils.py +116 -0
  131. nvmath/fft/_helpers.py +260 -0
  132. nvmath/fft/fft.py +2346 -0
  133. nvmath/internal/__init__.py +5 -0
  134. nvmath/internal/enum_utils.py +142 -0
  135. nvmath/internal/formatters.py +87 -0
  136. nvmath/internal/mem_limit.py +51 -0
  137. nvmath/internal/package_ifc.py +119 -0
  138. nvmath/internal/package_ifc_cuda.py +43 -0
  139. nvmath/internal/package_ifc_cupy.py +49 -0
  140. nvmath/internal/package_ifc_torch.py +31 -0
  141. nvmath/internal/package_wrapper.py +14 -0
  142. nvmath/internal/tensor_ifc.py +143 -0
  143. nvmath/internal/tensor_ifc_cupy.py +185 -0
  144. nvmath/internal/tensor_ifc_numpy.py +157 -0
  145. nvmath/internal/tensor_ifc_torch.py +115 -0
  146. nvmath/internal/tensor_wrapper.py +160 -0
  147. nvmath/internal/typemaps.py +154 -0
  148. nvmath/internal/utils.py +634 -0
  149. nvmath/linalg/__init__.py +11 -0
  150. nvmath/linalg/_internal/__init__.py +3 -0
  151. nvmath/linalg/_internal/algo_cap_ifc.py +86 -0
  152. nvmath/linalg/_internal/algo_config_ifc.py +87 -0
  153. nvmath/linalg/_internal/enum_to_tuples.py +64 -0
  154. nvmath/linalg/_internal/epilog_protocol.py +738 -0
  155. nvmath/linalg/_internal/matmul_desc_ifc.py +72 -0
  156. nvmath/linalg/_internal/matmul_pref_ifc.py +65 -0
  157. nvmath/linalg/_internal/matrix_layout_ifc.py +59 -0
  158. nvmath/linalg/_internal/typemaps.py +135 -0
  159. nvmath/linalg/_internal/utils.py +96 -0
  160. nvmath/linalg/advanced/__init__.py +8 -0
  161. nvmath/linalg/advanced/_algorithmmod.py +142 -0
  162. nvmath/linalg/advanced/_configuration.py +320 -0
  163. nvmath/linalg/advanced/helpers/__init__.py +5 -0
  164. nvmath/linalg/advanced/helpers/matmul.py +173 -0
  165. nvmath/linalg/advanced/matmulmod.py +2855 -0
  166. nvmath/memory.py +266 -0
  167. nvmath/sparse/__init__.py +18 -0
  168. nvmath/sparse/_internal/__init__.py +21 -0
  169. nvmath/sparse/_internal/common_utils.py +81 -0
  170. nvmath/sparse/_internal/cudss_config_ifc.py +584 -0
  171. nvmath/sparse/_internal/cudss_data_ifc.py +291 -0
  172. nvmath/sparse/_internal/cudss_utils.py +398 -0
  173. nvmath/sparse/_internal/sparse_csr_ifc.py +154 -0
  174. nvmath/sparse/_internal/sparse_format_helpers.py +88 -0
  175. nvmath/sparse/_internal/sparse_tensor_ifc.py +80 -0
  176. nvmath/sparse/advanced/__init__.py +7 -0
  177. nvmath/sparse/advanced/_configuration.py +150 -0
  178. nvmath/sparse/advanced/direct_solver.py +1705 -0
  179. nvmath_python-0.5.0.dist-info/METADATA +122 -0
  180. nvmath_python-0.5.0.dist-info/RECORD +183 -0
  181. nvmath_python-0.5.0.dist-info/WHEEL +5 -0
  182. nvmath_python-0.5.0.dist-info/licenses/LICENSE +177 -0
  183. nvmath_python-0.5.0.dist-info/top_level.txt +1 -0
nvmath/__init__.pxd ADDED
File without changes
nvmath/__init__.py ADDED
@@ -0,0 +1,37 @@
1
+ # Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ import importlib.metadata
6
+
7
+
8
+ # Attempt to preload libraries. Fail silently if preload fails.
9
+ def _force_lib_load():
10
+ from nvmath._utils import module_init_force_cupy_lib_load
11
+
12
+ module_init_force_cupy_lib_load()
13
+
14
+
15
+ _force_lib_load()
16
+
17
+ from nvmath import bindings # noqa: E402
18
+ from nvmath._utils import ComputeType # noqa: E402
19
+ from nvmath._utils import CudaDataType # noqa: E402
20
+ from nvmath._utils import LibraryPropertyType # noqa: E402
21
+
22
+ from nvmath import fft, linalg, sparse # noqa: E402
23
+ from nvmath.memory import BaseCUDAMemoryManager, MemoryPointer # noqa: E402
24
+
25
+ __all__ = [
26
+ "BaseCUDAMemoryManager",
27
+ "bindings",
28
+ "ComputeType",
29
+ "CudaDataType",
30
+ "fft",
31
+ "LibraryPropertyType",
32
+ "linalg",
33
+ "MemoryPointer",
34
+ "sparse",
35
+ ]
36
+
37
+ __version__ = importlib.metadata.version("nvmath-python")
File without changes
@@ -0,0 +1,58 @@
1
+ # Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ """
6
+ A collection of (internal use) helper functions for shape/stride
7
+ validation and manipulation.
8
+ """
9
+
10
+ from collections.abc import Sequence
11
+
12
+
13
+ def is_contiguous_layout(sorted_shape: Sequence[int], sorted_strides: Sequence[int]) -> bool:
14
+ return all(sorted_shape[s - 1] * sorted_strides[s - 1] == sorted_strides[s] for s in range(1, len(sorted_strides)))
15
+
16
+
17
+ def is_contiguous_in_memory(shape: Sequence[int], strides: Sequence[int]) -> bool:
18
+ """
19
+ Check if the provided (shape, strides) result in a contiguous memory layout.
20
+ """
21
+ sorted_strides, sorted_shape = zip(*sorted(zip(strides, shape, strict=True)), strict=True)
22
+ return is_contiguous_layout(sorted_shape, sorted_strides)
23
+
24
+
25
+ def is_contiguous_and_dense(shape: Sequence[int], strides: Sequence[int]) -> bool:
26
+ """
27
+ Check if the provided (shape, strides) result in a contiguous memory layout
28
+ with no extra stride in least strided dimension.
29
+ """
30
+ sorted_strides, sorted_shape = zip(*sorted(zip(strides, shape, strict=True)), strict=True)
31
+ if len(sorted_strides) > 0 and sorted_strides[0] != 1:
32
+ return False
33
+ return is_contiguous_layout(sorted_shape, sorted_strides)
34
+
35
+
36
+ def is_overlapping_layout(shape: Sequence[int], strides: Sequence[int]) -> bool:
37
+ """
38
+ For a tensor `t`, if `not is_overlapping_layout(t.shape, t.strides)`,
39
+ there are no two different valid nd-indices `idxs` such that
40
+ `t[idxs_0]` and `t[idxs_1]` map to the same offset in the memory.
41
+ Checks that the strides:
42
+ 1. are positive
43
+ 2. any n - 1 extents maximal offset is smaller than the stride
44
+ of the n-th extent.
45
+ The check should return False for contiguous
46
+ or contiguous and sliced tensors.
47
+ """
48
+ sorted_strides, sorted_shape = zip(*sorted(zip(strides, shape, strict=True)), strict=True)
49
+ cur_max_offset = 0
50
+ for s in range(1, len(sorted_strides)):
51
+ stride = sorted_strides[s - 1]
52
+ extent = sorted_shape[s - 1]
53
+ if stride <= 0:
54
+ return True
55
+ cur_max_offset += stride * (extent - 1)
56
+ if cur_max_offset >= sorted_strides[s]:
57
+ return True
58
+ return False
nvmath/_utils.py ADDED
@@ -0,0 +1,363 @@
1
+ # Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ import ctypes
6
+ from enum import IntEnum
7
+ import logging
8
+ import os
9
+ import re
10
+ import site
11
+ import sys
12
+
13
+ #
14
+ # Note: This module should not depend on anything from the nvmath namespace!
15
+ #
16
+
17
+
18
+ # The (subset of) compute types below are shared by cuStateVec and cuTensorNet
19
+ class ComputeType(IntEnum):
20
+ """An enumeration of CUDA compute types."""
21
+
22
+ COMPUTE_DEFAULT = 0
23
+ COMPUTE_16F = 1 << 0
24
+ COMPUTE_32F = 1 << 2
25
+ COMPUTE_64F = 1 << 4
26
+ COMPUTE_8U = 1 << 6
27
+ COMPUTE_8I = 1 << 8
28
+ COMPUTE_32U = 1 << 7
29
+ COMPUTE_32I = 1 << 9
30
+ COMPUTE_16BF = 1 << 10
31
+ COMPUTE_TF32 = 1 << 12
32
+
33
+
34
+ # TODO: use those exposed by CUDA Python instead, but before removing these
35
+ # duplicates, check if they are fixed to inherit IntEnum instead of Enum.
36
+ class CudaDataType(IntEnum):
37
+ """An enumeration of `cudaDataType_t`."""
38
+
39
+ CUDA_R_16F = 2
40
+ CUDA_C_16F = 6
41
+ CUDA_R_16BF = 14
42
+ CUDA_C_16BF = 15
43
+ CUDA_R_32F = 0
44
+ CUDA_C_32F = 4
45
+ CUDA_R_64F = 1
46
+ CUDA_C_64F = 5
47
+ CUDA_R_4I = 16
48
+ CUDA_C_4I = 17
49
+ CUDA_R_4U = 18
50
+ CUDA_C_4U = 19
51
+ CUDA_R_8I = 3
52
+ CUDA_C_8I = 7
53
+ CUDA_R_8U = 8
54
+ CUDA_C_8U = 9
55
+ CUDA_R_16I = 20
56
+ CUDA_C_16I = 21
57
+ CUDA_R_16U = 22
58
+ CUDA_C_16U = 23
59
+ CUDA_R_32I = 10
60
+ CUDA_C_32I = 11
61
+ CUDA_R_32U = 12
62
+ CUDA_C_32U = 13
63
+ CUDA_R_64I = 24
64
+ CUDA_C_64I = 25
65
+ CUDA_R_64U = 26
66
+ CUDA_C_64U = 27
67
+ CUDA_R_8F_E4M3 = 28
68
+ CUDA_R_8F_E5M2 = 29
69
+
70
+
71
+ class LibraryPropertyType(IntEnum):
72
+ """An enumeration of library version information."""
73
+
74
+ MAJOR_VERSION = 0
75
+ MINOR_VERSION = 1
76
+ PATCH_LEVEL = 2
77
+
78
+
79
+ del IntEnum
80
+
81
+
82
+ PLATFORM_LINUX = sys.platform.startswith("linux")
83
+ PLATFORM_WIN = sys.platform.startswith("win32")
84
+
85
+
86
+ # TODO: unify all loading helpers into one
87
+ _nvrtc_obj: list[ctypes.CDLL] = []
88
+
89
+
90
+ def force_loading_nvrtc(cu_ver):
91
+ # this logic should live in CUDA Python...
92
+ # TODO: remove this function once NVIDIA/cuda-python#62 is resolved
93
+ # This logic handles all cases - wheel, conda, and system installations
94
+ global _nvrtc_obj
95
+ if len(_nvrtc_obj) > 0:
96
+ return
97
+
98
+ cu_ver = cu_ver.split(".")
99
+ major = cu_ver[0]
100
+ if major == "11":
101
+ # CUDA 11.2+ supports minor ver compat
102
+ if PLATFORM_LINUX:
103
+ cu_ver = "11.2"
104
+ elif PLATFORM_WIN:
105
+ cu_ver = "112"
106
+ elif major == "12":
107
+ if PLATFORM_LINUX:
108
+ cu_ver = "12"
109
+ elif PLATFORM_WIN:
110
+ cu_ver = "120"
111
+ else:
112
+ raise NotImplementedError(f"CUDA {major} is not supported")
113
+
114
+ site_paths = [site.getusersitepackages()] + site.getsitepackages() + [None]
115
+ for sp in site_paths:
116
+ if PLATFORM_LINUX:
117
+ dso_dir = "lib"
118
+ dso_path = f"libnvrtc.so.{cu_ver}"
119
+ elif PLATFORM_WIN:
120
+ dso_dir = "bin"
121
+ dso_path = f"nvrtc64_{cu_ver}_0.dll"
122
+ else:
123
+ raise AssertionError()
124
+
125
+ if sp is not None:
126
+ dso_dir = os.path.join(sp, "nvidia", "cuda_nvrtc", dso_dir)
127
+ dso_path = os.path.join(dso_dir, dso_path)
128
+ try:
129
+ _nvrtc_obj.append(ctypes.CDLL(dso_path, mode=ctypes.RTLD_GLOBAL))
130
+ except OSError:
131
+ continue
132
+ else:
133
+ if PLATFORM_WIN:
134
+ import win32api
135
+
136
+ # This absolute path will always be correct regardless of the package source
137
+ nvrtc_path = win32api.GetModuleFileNameW(_nvrtc_obj[0]._handle)
138
+ dso_dir = os.path.dirname(nvrtc_path)
139
+ dso_path = os.path.join(dso_dir, [f for f in os.listdir(dso_dir) if re.match("^nvrtc-builtins.*.dll$", f)][0])
140
+ _nvrtc_obj.append(ctypes.CDLL(dso_path))
141
+ break
142
+ else:
143
+ raise RuntimeError(
144
+ f"NVRTC from CUDA {major} not found. Depending on how you install nvmath-python and other CUDA packages,\n"
145
+ f"you may need to perform one of the steps below:\n"
146
+ f" - pip install nvidia-cuda-nvrtc-cu{major}\n"
147
+ f" - conda install -c conda-forge cuda-nvrtc cuda-version={major}\n"
148
+ " - export LD_LIBRARY_PATH=/path/to/CUDA/Toolkit/lib64:$LD_LIBRARY_PATH"
149
+ )
150
+
151
+
152
+ # TODO: unify all loading helpers into one
153
+ _libmathdx_obj: list[ctypes.CDLL] = []
154
+
155
+
156
+ def force_loading_libmathdx(cu_ver):
157
+ # this logic should live in CUDA Python...
158
+ # TODO: remove this function once NVIDIA/cuda-python#62 is resolved
159
+ # This logic handles all cases - wheel, conda, and system installations
160
+ global _libmathdx_obj
161
+ if len(_libmathdx_obj) > 0:
162
+ return
163
+
164
+ cu_ver = cu_ver.split(".")
165
+ major = cu_ver[0]
166
+ if major != "12":
167
+ raise NotImplementedError(f"CUDA {major} is not supported")
168
+
169
+ site_paths = [site.getusersitepackages()] + site.getsitepackages() + [None]
170
+ for sp in site_paths:
171
+ if PLATFORM_LINUX:
172
+ dso_dir = "lib"
173
+ dso_path = "libmathdx.so.0"
174
+ elif PLATFORM_WIN:
175
+ dso_dir = "bin"
176
+ dso_path = "libmathdx.dll"
177
+ else:
178
+ raise AssertionError("Unsupported Platform! Only Linux and Windows are supported.")
179
+
180
+ if sp is not None:
181
+ dso_dir = os.path.join(sp, "nvidia", f"cu{major}", dso_dir)
182
+ dso_path = os.path.join(dso_dir, dso_path)
183
+ try:
184
+ _libmathdx_obj.append(ctypes.CDLL(dso_path, mode=ctypes.RTLD_GLOBAL))
185
+ except OSError:
186
+ continue
187
+ else:
188
+ if PLATFORM_WIN:
189
+ # TODO: untested in context of libmathdx.
190
+ import win32api
191
+
192
+ # This absolute path will always be correct regardless of the package source
193
+ nvrtc_path = win32api.GetModuleFileNameW(_libmathdx_obj[0]._handle)
194
+ dso_dir = os.path.dirname(nvrtc_path)
195
+ dso_path = os.path.join(dso_dir, [f for f in os.listdir(dso_dir) if re.match("^nvrtc-builtins.*.dll$", f)][0])
196
+ _libmathdx_obj.append(ctypes.CDLL(dso_path))
197
+ break
198
+ else:
199
+ raise RuntimeError(
200
+ f"libmathdx not found. Depending on how you install nvmath-python and other CUDA packages,\n"
201
+ f"you may need to perform one of the steps below:\n"
202
+ f" - pip install nvidia-libmathdx-cu{major}\n"
203
+ f" - conda install -c conda-forge libmathdx cuda-version={major}"
204
+ )
205
+
206
+ from nvmath.bindings import mathdx
207
+
208
+ version = mathdx.get_version()
209
+ if version < 201 or version >= 300:
210
+ raise ValueError(f"libmathdx version must be >= 0.2.1 and < 0.3.0 ; got {version}")
211
+
212
+
213
+ # TODO: unify all loading helpers into one
214
+ _nvvm_obj: list[ctypes.CDLL] = []
215
+
216
+
217
+ def force_loading_nvvm():
218
+ # this logic should live in CUDA Python...
219
+ # This logic handles all cases - wheel, conda, and system installations
220
+ global _nvvm_obj
221
+ if len(_nvvm_obj) > 0:
222
+ return
223
+
224
+ site_paths = [site.getusersitepackages()] + site.getsitepackages() + ["conda", None]
225
+ for sp in site_paths:
226
+ # The SONAME is taken based on public CTK 12.x releases
227
+ if PLATFORM_LINUX:
228
+ dso_dir = "lib64"
229
+ # Hack: libnvvm from Linux wheel does not have any soname (CUDAINST-3183)
230
+ dso_path = "libnvvm.so"
231
+ if sp == "conda" or sp is None:
232
+ dso_path += ".4"
233
+ elif PLATFORM_WIN:
234
+ dso_dir = "bin"
235
+ dso_path = "nvvm64_40_0.dll"
236
+ else:
237
+ raise AssertionError()
238
+
239
+ if sp == "conda" and "CONDA_PREFIX" in os.environ:
240
+ # nvvm is not under $CONDA_PREFIX/lib, so it's not in the default search path
241
+ if PLATFORM_LINUX:
242
+ dso_dir = os.path.join(os.environ["CONDA_PREFIX"], "nvvm", dso_dir)
243
+ elif PLATFORM_WIN:
244
+ dso_dir = os.path.join(os.environ["CONDA_PREFIX"], "Library", "nvvm", dso_dir)
245
+ dso_path = os.path.join(dso_dir, dso_path)
246
+ elif sp is not None:
247
+ dso_dir = os.path.join(sp, "nvidia", "cuda_nvcc", "nvvm", dso_dir)
248
+ dso_path = os.path.join(dso_dir, dso_path)
249
+ try:
250
+ _nvvm_obj.append(ctypes.CDLL(dso_path, mode=ctypes.RTLD_GLOBAL))
251
+ except OSError:
252
+ continue
253
+ else:
254
+ break
255
+ else:
256
+ raise RuntimeError(
257
+ "NVVM from CUDA 12 not found. Depending on how you install nvmath-python and other CUDA packages,\n"
258
+ "you may need to perform one of the steps below:\n"
259
+ " - pip install nvidia-cuda-nvcc-cu12\n"
260
+ " - conda install -c conda-forge cuda-nvvm cuda-version=12\n"
261
+ " - export LD_LIBRARY_PATH=/path/to/CUDA/Toolkit/nvvm/lib64:$LD_LIBRARY_PATH"
262
+ )
263
+
264
+
265
+ # TODO: unify all loading helpers into one
266
+ _libcudss_obj: list[ctypes.CDLL] = []
267
+
268
+
269
+ def force_loading_cudss(cu_ver):
270
+ # this logic should live in CUDA Python...
271
+ global _libcudss_obj
272
+ if len(_libcudss_obj) > 0:
273
+ return
274
+
275
+ cu_ver = cu_ver.split(".")
276
+ major = cu_ver[0]
277
+ if major != "12":
278
+ raise NotImplementedError(f"CUDA {major} is not supported")
279
+
280
+ site_paths = [site.getusersitepackages()] + site.getsitepackages() + [None]
281
+ for sp in site_paths:
282
+ if PLATFORM_LINUX:
283
+ dso_dir = "lib"
284
+ dso_path = "libcudss.so.0"
285
+ elif PLATFORM_WIN:
286
+ dso_dir = "bin"
287
+ dso_path = "cudss64_0.dll"
288
+ else:
289
+ raise AssertionError("Unsupported Platform! Only Linux and Windows are supported.")
290
+
291
+ if sp is not None:
292
+ dso_dir = os.path.join(sp, "nvidia", f"cu{major}", dso_dir)
293
+ dso_path = os.path.join(dso_dir, dso_path)
294
+ try:
295
+ _libcudss_obj.append(ctypes.CDLL(dso_path, mode=ctypes.RTLD_GLOBAL))
296
+ logging.debug("Loaded %s", dso_path)
297
+ except OSError as e:
298
+ logging.debug("%s", e)
299
+ continue
300
+ else:
301
+ break
302
+ else:
303
+ raise RuntimeError(
304
+ f"libcudss not found. Depending on how you install nvmath-python and other CUDA packages,\n"
305
+ f"you may need to perform one of the steps below:\n"
306
+ f" - pip install nvidia-cudss-cu{major}\n"
307
+ f" - conda install -c conda-forge libcudss cuda-version={major}"
308
+ )
309
+
310
+
311
+ def module_init_force_cupy_lib_load():
312
+ """
313
+ Attempt to preload libraries at module import time.
314
+ Fail silently if preload fails.
315
+ """
316
+ from nvmath.bindings import _internal
317
+
318
+ for lib in ("cublas", "cufft", "curand", "cusolverDn", "cusparse"):
319
+ try:
320
+ mod = getattr(_internal, lib)
321
+ mod._inspect_function_pointers()
322
+ except (_internal.utils.NotSupportedError, RuntimeError):
323
+ pass
324
+ for cu_ver in ("12", "11"):
325
+ try:
326
+ force_loading_nvrtc(cu_ver)
327
+ return
328
+ except RuntimeError:
329
+ pass
330
+
331
+
332
+ def get_nvrtc_build_id():
333
+ from cuda.core.experimental import ObjectCode, Program, ProgramOptions
334
+
335
+ code = r"""
336
+ extern "C" __global__ void get_build_id(int* build_id) {
337
+
338
+ *build_id = __CUDACC_VER_BUILD__;
339
+ }
340
+ """
341
+
342
+ prog = Program(code, "c++", ProgramOptions(std="c++17", minimal=True, arch="compute_75"))
343
+ obj = prog.compile("ptx")
344
+ assert isinstance(obj, ObjectCode)
345
+
346
+ pattern = re.compile(r"mov\.u32\s+%\w+,\s+(\d+)")
347
+ m = pattern.search(obj.code.decode())
348
+ assert m is not None
349
+
350
+ return int(m.group(1))
351
+
352
+
353
+ def get_nvrtc_version():
354
+ """
355
+ Returns the NVRTC version as a tuple of (major, minor, build).
356
+ """
357
+ from cuda.bindings import nvrtc
358
+
359
+ err, major, minor = nvrtc.nvrtcVersion()
360
+ if err != nvrtc.nvrtcResult.NVRTC_SUCCESS:
361
+ raise RuntimeError(f"nvrtcVersion error: {err}")
362
+ build = get_nvrtc_build_id()
363
+ return major, minor, build
File without changes
@@ -0,0 +1,44 @@
1
+ # Copyright (c) 2024-2025, NVIDIA CORPORATION & AFFILIATES. ALL RIGHTS RESERVED.
2
+ #
3
+ # SPDX-License-Identifier: Apache-2.0
4
+
5
+ # type: ignore
6
+
7
+ from nvmath.bindings import cublas
8
+ from nvmath.bindings import cudss
9
+ from nvmath.bindings import cufft
10
+ from nvmath.bindings import curand
11
+ from nvmath.bindings import cusolver
12
+ from nvmath.bindings import cusolverDn
13
+ from nvmath.bindings import cusparse
14
+
15
+ try:
16
+ # nvpl is Linux-only.
17
+ from nvmath.bindings import nvpl
18
+ except ImportError:
19
+ nvpl = None
20
+
21
+ try:
22
+ # cufftMp is Linux-only.
23
+ from nvmath.bindings import cufftMp
24
+ except ImportError:
25
+ cufftMp = None
26
+
27
+ try:
28
+ # nvshmem is Linux-only.
29
+ from nvmath.bindings import nvshmem
30
+ except ImportError:
31
+ nvshmem = None
32
+
33
+ __all__ = [
34
+ "cublas",
35
+ "cudss",
36
+ "cufft",
37
+ "cufftMp",
38
+ "curand",
39
+ "cusolver",
40
+ "cusolverDn",
41
+ "cusparse",
42
+ "nvpl",
43
+ "nvshmem",
44
+ ]
File without changes
File without changes