warp-lang 0.11.0__py3-none-manylinux2014_x86_64.whl → 1.0.0__py3-none-manylinux2014_x86_64.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 warp-lang might be problematic. Click here for more details.

Files changed (170) hide show
  1. warp/__init__.py +8 -0
  2. warp/bin/warp-clang.so +0 -0
  3. warp/bin/warp.so +0 -0
  4. warp/build.py +7 -6
  5. warp/build_dll.py +70 -79
  6. warp/builtins.py +10 -6
  7. warp/codegen.py +51 -19
  8. warp/config.py +7 -8
  9. warp/constants.py +3 -0
  10. warp/context.py +948 -245
  11. warp/dlpack.py +198 -113
  12. warp/examples/assets/bunny.usd +0 -0
  13. warp/examples/assets/cartpole.urdf +110 -0
  14. warp/examples/assets/crazyflie.usd +0 -0
  15. warp/examples/assets/cube.usda +42 -0
  16. warp/examples/assets/nv_ant.xml +92 -0
  17. warp/examples/assets/nv_humanoid.xml +183 -0
  18. warp/examples/assets/quadruped.urdf +268 -0
  19. warp/examples/assets/rocks.nvdb +0 -0
  20. warp/examples/assets/rocks.usd +0 -0
  21. warp/examples/assets/sphere.usda +56 -0
  22. warp/examples/assets/torus.usda +105 -0
  23. warp/examples/benchmarks/benchmark_api.py +383 -0
  24. warp/examples/benchmarks/benchmark_cloth.py +279 -0
  25. warp/examples/benchmarks/benchmark_cloth_cupy.py +88 -0
  26. warp/examples/benchmarks/benchmark_cloth_jax.py +100 -0
  27. warp/examples/benchmarks/benchmark_cloth_numba.py +142 -0
  28. warp/examples/benchmarks/benchmark_cloth_numpy.py +77 -0
  29. warp/examples/benchmarks/benchmark_cloth_pytorch.py +86 -0
  30. warp/examples/benchmarks/benchmark_cloth_taichi.py +112 -0
  31. warp/examples/benchmarks/benchmark_cloth_warp.py +146 -0
  32. warp/examples/benchmarks/benchmark_launches.py +295 -0
  33. warp/examples/core/example_dem.py +221 -0
  34. warp/examples/core/example_fluid.py +267 -0
  35. warp/examples/core/example_graph_capture.py +129 -0
  36. warp/examples/core/example_marching_cubes.py +177 -0
  37. warp/examples/core/example_mesh.py +154 -0
  38. warp/examples/core/example_mesh_intersect.py +193 -0
  39. warp/examples/core/example_nvdb.py +169 -0
  40. warp/examples/core/example_raycast.py +89 -0
  41. warp/examples/core/example_raymarch.py +178 -0
  42. warp/examples/core/example_render_opengl.py +141 -0
  43. warp/examples/core/example_sph.py +389 -0
  44. warp/examples/core/example_torch.py +181 -0
  45. warp/examples/core/example_wave.py +249 -0
  46. warp/examples/fem/bsr_utils.py +380 -0
  47. warp/examples/fem/example_apic_fluid.py +391 -0
  48. warp/examples/fem/example_convection_diffusion.py +168 -0
  49. warp/examples/fem/example_convection_diffusion_dg.py +209 -0
  50. warp/examples/fem/example_convection_diffusion_dg0.py +194 -0
  51. warp/examples/fem/example_deformed_geometry.py +159 -0
  52. warp/examples/fem/example_diffusion.py +173 -0
  53. warp/examples/fem/example_diffusion_3d.py +152 -0
  54. warp/examples/fem/example_diffusion_mgpu.py +214 -0
  55. warp/examples/fem/example_mixed_elasticity.py +222 -0
  56. warp/examples/fem/example_navier_stokes.py +243 -0
  57. warp/examples/fem/example_stokes.py +192 -0
  58. warp/examples/fem/example_stokes_transfer.py +249 -0
  59. warp/examples/fem/mesh_utils.py +109 -0
  60. warp/examples/fem/plot_utils.py +287 -0
  61. warp/examples/optim/example_bounce.py +248 -0
  62. warp/examples/optim/example_cloth_throw.py +210 -0
  63. warp/examples/optim/example_diffray.py +535 -0
  64. warp/examples/optim/example_drone.py +850 -0
  65. warp/examples/optim/example_inverse_kinematics.py +169 -0
  66. warp/examples/optim/example_inverse_kinematics_torch.py +170 -0
  67. warp/examples/optim/example_spring_cage.py +234 -0
  68. warp/examples/optim/example_trajectory.py +201 -0
  69. warp/examples/sim/example_cartpole.py +128 -0
  70. warp/examples/sim/example_cloth.py +184 -0
  71. warp/examples/sim/example_granular.py +113 -0
  72. warp/examples/sim/example_granular_collision_sdf.py +185 -0
  73. warp/examples/sim/example_jacobian_ik.py +213 -0
  74. warp/examples/sim/example_particle_chain.py +106 -0
  75. warp/examples/sim/example_quadruped.py +179 -0
  76. warp/examples/sim/example_rigid_chain.py +191 -0
  77. warp/examples/sim/example_rigid_contact.py +176 -0
  78. warp/examples/sim/example_rigid_force.py +126 -0
  79. warp/examples/sim/example_rigid_gyroscopic.py +97 -0
  80. warp/examples/sim/example_rigid_soft_contact.py +124 -0
  81. warp/examples/sim/example_soft_body.py +178 -0
  82. warp/fabric.py +29 -20
  83. warp/fem/cache.py +0 -1
  84. warp/fem/dirichlet.py +0 -2
  85. warp/fem/integrate.py +0 -1
  86. warp/jax.py +45 -0
  87. warp/jax_experimental.py +339 -0
  88. warp/native/builtin.h +12 -0
  89. warp/native/bvh.cu +18 -18
  90. warp/native/clang/clang.cpp +8 -3
  91. warp/native/cuda_util.cpp +94 -5
  92. warp/native/cuda_util.h +35 -6
  93. warp/native/cutlass_gemm.cpp +1 -1
  94. warp/native/cutlass_gemm.cu +4 -1
  95. warp/native/error.cpp +66 -0
  96. warp/native/error.h +27 -0
  97. warp/native/mesh.cu +2 -2
  98. warp/native/reduce.cu +4 -4
  99. warp/native/runlength_encode.cu +2 -2
  100. warp/native/scan.cu +2 -2
  101. warp/native/sparse.cu +0 -1
  102. warp/native/temp_buffer.h +2 -2
  103. warp/native/warp.cpp +95 -60
  104. warp/native/warp.cu +1053 -218
  105. warp/native/warp.h +49 -32
  106. warp/optim/linear.py +33 -16
  107. warp/render/render_opengl.py +202 -101
  108. warp/render/render_usd.py +82 -40
  109. warp/sim/__init__.py +13 -4
  110. warp/sim/articulation.py +4 -5
  111. warp/sim/collide.py +320 -175
  112. warp/sim/import_mjcf.py +25 -30
  113. warp/sim/import_urdf.py +94 -63
  114. warp/sim/import_usd.py +51 -36
  115. warp/sim/inertia.py +3 -2
  116. warp/sim/integrator.py +233 -0
  117. warp/sim/integrator_euler.py +447 -469
  118. warp/sim/integrator_featherstone.py +1991 -0
  119. warp/sim/integrator_xpbd.py +1420 -640
  120. warp/sim/model.py +765 -487
  121. warp/sim/particles.py +2 -1
  122. warp/sim/render.py +35 -13
  123. warp/sim/utils.py +222 -11
  124. warp/stubs.py +8 -0
  125. warp/tape.py +16 -1
  126. warp/tests/aux_test_grad_customs.py +23 -0
  127. warp/tests/test_array.py +190 -1
  128. warp/tests/test_async.py +656 -0
  129. warp/tests/test_bool.py +50 -0
  130. warp/tests/test_dlpack.py +164 -11
  131. warp/tests/test_examples.py +166 -74
  132. warp/tests/test_fem.py +8 -1
  133. warp/tests/test_generics.py +15 -5
  134. warp/tests/test_grad.py +1 -1
  135. warp/tests/test_grad_customs.py +172 -12
  136. warp/tests/test_jax.py +254 -0
  137. warp/tests/test_large.py +29 -6
  138. warp/tests/test_launch.py +25 -0
  139. warp/tests/test_linear_solvers.py +20 -3
  140. warp/tests/test_matmul.py +61 -16
  141. warp/tests/test_matmul_lite.py +13 -13
  142. warp/tests/test_mempool.py +186 -0
  143. warp/tests/test_multigpu.py +3 -0
  144. warp/tests/test_options.py +16 -2
  145. warp/tests/test_peer.py +137 -0
  146. warp/tests/test_print.py +3 -1
  147. warp/tests/test_quat.py +23 -0
  148. warp/tests/test_sim_kinematics.py +97 -0
  149. warp/tests/test_snippet.py +126 -3
  150. warp/tests/test_streams.py +108 -79
  151. warp/tests/test_torch.py +16 -8
  152. warp/tests/test_utils.py +32 -27
  153. warp/tests/test_verify_fp.py +65 -0
  154. warp/tests/test_volume.py +1 -1
  155. warp/tests/unittest_serial.py +2 -0
  156. warp/tests/unittest_suites.py +12 -0
  157. warp/tests/unittest_utils.py +14 -7
  158. warp/thirdparty/unittest_parallel.py +15 -3
  159. warp/torch.py +10 -8
  160. warp/types.py +363 -246
  161. warp/utils.py +143 -19
  162. warp_lang-1.0.0.dist-info/LICENSE.md +126 -0
  163. warp_lang-1.0.0.dist-info/METADATA +394 -0
  164. {warp_lang-0.11.0.dist-info → warp_lang-1.0.0.dist-info}/RECORD +167 -86
  165. warp/sim/optimizer.py +0 -138
  166. warp_lang-0.11.0.dist-info/LICENSE.md +0 -36
  167. warp_lang-0.11.0.dist-info/METADATA +0 -238
  168. /warp/tests/{walkthough_debug.py → walkthrough_debug.py} +0 -0
  169. {warp_lang-0.11.0.dist-info → warp_lang-1.0.0.dist-info}/WHEEL +0 -0
  170. {warp_lang-0.11.0.dist-info → warp_lang-1.0.0.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,656 @@
1
+ # Copyright (c) 2023 NVIDIA CORPORATION. All rights reserved.
2
+ # NVIDIA CORPORATION and its licensors retain all intellectual property
3
+ # and proprietary rights in and to this software, related documentation
4
+ # and any modifications thereto. Any use, reproduction, disclosure or
5
+ # distribution of this software and related documentation without an express
6
+ # license agreement from NVIDIA CORPORATION is strictly prohibited.
7
+
8
+ import unittest
9
+
10
+ import numpy as np
11
+
12
+ import warp as wp
13
+ from warp.utils import check_iommu
14
+ from warp.tests.unittest_utils import *
15
+
16
+ wp.init()
17
+
18
+
19
+ class Capturable:
20
+ def __init__(self, use_graph=True, stream=None):
21
+ self.use_graph = use_graph
22
+ self.stream = stream
23
+
24
+ def __enter__(self):
25
+ if self.use_graph:
26
+ wp.capture_begin(stream=self.stream)
27
+
28
+ def __exit__(self, exc_type, exc_value, traceback):
29
+ if self.use_graph:
30
+ try:
31
+ # need to call capture_end() to terminate the CUDA stream capture
32
+ graph = wp.capture_end(stream=self.stream)
33
+ except:
34
+ # capture_end() will raise if there was an error during capture, but we squash it here
35
+ # if we already had an exception so that the original exception percolates to the caller
36
+ if exc_type is None:
37
+ raise
38
+ else:
39
+ # capture can succeed despite some errors during capture (e.g. cudaInvalidValue during copy)
40
+ # but if we had an exception during capture, don't launch the graph
41
+ if exc_type is None:
42
+ wp.capture_launch(graph, stream=self.stream)
43
+
44
+
45
+ @wp.kernel
46
+ def inc(a: wp.array(dtype=float)):
47
+ tid = wp.tid()
48
+ a[tid] = a[tid] + 1.0
49
+
50
+
51
+ def test_async_empty(test, device, use_mempools, use_graph):
52
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
53
+ n = 100
54
+
55
+ with Capturable(use_graph):
56
+ a = wp.empty(n, dtype=float)
57
+
58
+ test.assertIsInstance(a, wp.array)
59
+ test.assertIsNotNone(a.ptr)
60
+ test.assertEqual(a.size, n)
61
+ test.assertEqual(a.dtype, wp.float32)
62
+ test.assertEqual(a.device, device)
63
+
64
+
65
+ def test_async_zeros(test, device, use_mempools, use_graph):
66
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
67
+ n = 100
68
+
69
+ with Capturable(use_graph):
70
+ a = wp.zeros(n, dtype=float)
71
+
72
+ assert_np_equal(a.numpy(), np.zeros(n, dtype=np.float32))
73
+
74
+
75
+ def test_async_zero_v1(test, device, use_mempools, use_graph):
76
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
77
+ n = 100
78
+
79
+ with Capturable(use_graph):
80
+ a = wp.empty(n, dtype=float)
81
+ a.zero_()
82
+
83
+ assert_np_equal(a.numpy(), np.zeros(n, dtype=np.float32))
84
+
85
+
86
+ def test_async_zero_v2(test, device, use_mempools, use_graph):
87
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
88
+ n = 100
89
+
90
+ a = wp.empty(n, dtype=float)
91
+
92
+ with Capturable(use_graph):
93
+ a.zero_()
94
+
95
+ assert_np_equal(a.numpy(), np.zeros(n, dtype=np.float32))
96
+
97
+
98
+ def test_async_full(test, device, use_mempools, use_graph):
99
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
100
+ n = 100
101
+ value = 42
102
+
103
+ with Capturable(use_graph):
104
+ a = wp.full(n, value, dtype=float)
105
+
106
+ assert_np_equal(a.numpy(), np.full(n, value, dtype=np.float32))
107
+
108
+
109
+ def test_async_fill_v1(test, device, use_mempools, use_graph):
110
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
111
+ n = 100
112
+ value = 17
113
+
114
+ with Capturable(use_graph):
115
+ a = wp.empty(n, dtype=float)
116
+ a.fill_(value)
117
+
118
+ assert_np_equal(a.numpy(), np.full(n, value, dtype=np.float32))
119
+
120
+
121
+ def test_async_fill_v2(test, device, use_mempools, use_graph):
122
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
123
+ n = 100
124
+ value = 17
125
+
126
+ a = wp.empty(n, dtype=float)
127
+
128
+ with Capturable(use_graph):
129
+ a.fill_(value)
130
+
131
+ assert_np_equal(a.numpy(), np.full(n, value, dtype=np.float32))
132
+
133
+
134
+ def test_async_kernels_v1(test, device, use_mempools, use_graph):
135
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
136
+ n = 100
137
+ num_iters = 10
138
+
139
+ with Capturable(use_graph):
140
+ a = wp.zeros(n, dtype=float)
141
+ for i in range(num_iters):
142
+ wp.launch(inc, dim=a.size, inputs=[a])
143
+
144
+ assert_np_equal(a.numpy(), np.full(n, num_iters, dtype=np.float32))
145
+
146
+
147
+ def test_async_kernels_v2(test, device, use_mempools, use_graph):
148
+ with wp.ScopedDevice(device), wp.ScopedMempool(device, use_mempools):
149
+ n = 100
150
+ num_iters = 10
151
+
152
+ a = wp.zeros(n, dtype=float)
153
+
154
+ with Capturable(use_graph):
155
+ for i in range(num_iters):
156
+ wp.launch(inc, dim=a.size, inputs=[a])
157
+
158
+ assert_np_equal(a.numpy(), np.full(n, num_iters, dtype=np.float32))
159
+
160
+
161
+ class TestAsync(unittest.TestCase):
162
+ pass
163
+
164
+
165
+ # get all CUDA devices
166
+ cuda_devices = wp.get_cuda_devices()
167
+
168
+ # get CUDA devices that support mempools
169
+ cuda_devices_with_mempools = []
170
+ for d in cuda_devices:
171
+ if d.is_mempool_supported:
172
+ cuda_devices_with_mempools.append(d)
173
+
174
+ # get a pair of CUDA devices that support mempool access
175
+ cuda_devices_with_mempool_access = []
176
+ for target_device in cuda_devices_with_mempools:
177
+ for peer_device in cuda_devices_with_mempools:
178
+ if peer_device != target_device:
179
+ if wp.is_mempool_access_supported(target_device, peer_device):
180
+ cuda_devices_with_mempool_access = [target_device, peer_device]
181
+ break
182
+ if cuda_devices_with_mempool_access:
183
+ break
184
+
185
+ def add_test_variants(
186
+ func,
187
+ device_count=1,
188
+ graph_allocs=False,
189
+ requires_mempool_access_with_graph=False,
190
+ ):
191
+
192
+ # test that works with default allocators
193
+ if not graph_allocs and device_count <= len(cuda_devices):
194
+ devices = cuda_devices[:device_count]
195
+ func1 = lambda t, d: func(t, *devices, False, False)
196
+ func2 = lambda t, d: func(t, *devices, False, True)
197
+ name1 = f"{func.__name__}_DefaultAlloc_NoGraph"
198
+ name2 = f"{func.__name__}_DefaultAlloc_WithGraph"
199
+ if device_count == 1:
200
+ add_function_test(TestAsync, name1, func1, devices=devices)
201
+ add_function_test(TestAsync, name2, func2, devices=devices)
202
+ else:
203
+ add_function_test(TestAsync, name1, func1)
204
+ add_function_test(TestAsync, name2, func2)
205
+
206
+ # test that works with mempool allocators
207
+ if device_count <= len(cuda_devices_with_mempools):
208
+ devices = cuda_devices_with_mempools[:device_count]
209
+ func3 = lambda t, d: func(t, *devices, True, False)
210
+ name3 = f"{func.__name__}_MempoolAlloc_NoGraph"
211
+ if device_count == 1:
212
+ add_function_test(TestAsync, name3, func3, devices=devices)
213
+ else:
214
+ add_function_test(TestAsync, name3, func3)
215
+
216
+ # test that requires devices with mutual mempool access during graph capture (e.g., p2p memcpy limitation)
217
+ if requires_mempool_access_with_graph:
218
+ suitable_devices = cuda_devices_with_mempool_access
219
+ else:
220
+ suitable_devices = cuda_devices_with_mempools
221
+
222
+ if device_count <= len(suitable_devices):
223
+ devices = suitable_devices[:device_count]
224
+ func4 = lambda t, d: func(t, *devices, True, True)
225
+ name4 = f"{func.__name__}_MempoolAlloc_WithGraph"
226
+ if device_count == 1:
227
+ add_function_test(TestAsync, name4, func4, devices=devices)
228
+ else:
229
+ add_function_test(TestAsync, name4, func4)
230
+
231
+ add_test_variants(test_async_empty, graph_allocs=True)
232
+ add_test_variants(test_async_zeros, graph_allocs=True)
233
+ add_test_variants(test_async_zero_v1, graph_allocs=True)
234
+ add_test_variants(test_async_zero_v2, graph_allocs=False)
235
+ add_test_variants(test_async_full, graph_allocs=True)
236
+ add_test_variants(test_async_fill_v1, graph_allocs=True)
237
+ add_test_variants(test_async_fill_v2, graph_allocs=False)
238
+ add_test_variants(test_async_kernels_v1, graph_allocs=True)
239
+ add_test_variants(test_async_kernels_v2, graph_allocs=False)
240
+
241
+
242
+ #=================================================================================
243
+ # wp.copy() tests
244
+ #=================================================================================
245
+
246
+ def as_contiguous_array(data, device=None, grad_data=None):
247
+ a = wp.array(data=data, device=device, copy=True)
248
+ if grad_data is not None:
249
+ a.grad = as_contiguous_array(grad_data, device=device)
250
+ return a
251
+
252
+
253
+ def as_strided_array(data, device=None, grad_data=None):
254
+ a = wp.array(data=data, device=device)
255
+ # make a copy with non-contiguous strides
256
+ strides = (*a.strides[:-1], 2 * a.strides[-1])
257
+ strided_a = wp.zeros(shape=a.shape, strides=strides, dtype=a.dtype, device=device)
258
+ wp.copy(strided_a, a)
259
+ if grad_data is not None:
260
+ strided_a.grad = as_strided_array(grad_data, device=device)
261
+ return strided_a
262
+
263
+
264
+ def as_indexed_array(data, device=None, **kwargs):
265
+ a = wp.array(data=data, device=device)
266
+ # allocate double the elements so we can index half of them
267
+ shape = (*a.shape[:-1], 2 * a.shape[-1])
268
+ big_a = wp.zeros(shape=shape, dtype=a.dtype, device=device)
269
+ indices = wp.array(data=np.arange(0, shape[-1], 2, dtype=np.int32), device=device)
270
+ indexed_a = big_a[indices]
271
+ wp.copy(indexed_a, a)
272
+ return indexed_a
273
+
274
+
275
+ def as_fabric_array(data, device=None, **kwargs):
276
+ from warp.tests.test_fabricarray import _create_fabric_array_interface
277
+ a = wp.array(data=data, device=device)
278
+ iface = _create_fabric_array_interface(a, "foo")
279
+ fa = wp.fabricarray(data=iface, attrib="foo")
280
+ fa._iface = iface # save data reference
281
+ return fa
282
+
283
+
284
+ def as_indexed_fabric_array(data, device=None, **kwargs):
285
+ from warp.tests.test_fabricarray import _create_fabric_array_interface
286
+ a = wp.array(data=data, device=device)
287
+ shape = (*a.shape[:-1], 2 * a.shape[-1])
288
+ # allocate double the elements so we can index half of them
289
+ big_a = wp.zeros(shape=shape, dtype=a.dtype, device=device)
290
+ indices = wp.array(data=np.arange(0, shape[-1], 2, dtype=np.int32), device=device)
291
+ iface = _create_fabric_array_interface(big_a, "foo", copy=True)
292
+ fa = wp.fabricarray(data=iface, attrib="foo")
293
+ fa._iface = iface # save data reference
294
+ indexed_fa = fa[indices]
295
+ wp.copy(indexed_fa, a)
296
+ return indexed_fa
297
+
298
+
299
+ class CopyParams:
300
+ def __init__(self,
301
+ with_grad=False, # whether to use arrays with gradients (contiguous and strided only)
302
+ src_use_mempool=False, # whether to enable memory pool on source device
303
+ dst_use_mempool=False, # whether to enable memory pool on destination device
304
+ access_dst_src=False, # whether destination device has access to the source mempool
305
+ access_src_dst=False, # whether source device has access to the destination mempool
306
+ stream_device=None, # the device for the stream (None for default behaviour)
307
+ use_graph=False, # whether to use a graph
308
+ value_offset=0, # unique offset for generated data values per test
309
+ ):
310
+ self.with_grad = with_grad
311
+ self.src_use_mempool = src_use_mempool
312
+ self.dst_use_mempool = dst_use_mempool
313
+ self.access_dst_src = access_dst_src
314
+ self.access_src_dst = access_src_dst
315
+ self.stream_device = stream_device
316
+ self.use_graph = use_graph
317
+ self.value_offset = value_offset
318
+
319
+
320
+ def copy_template(
321
+ test,
322
+ src_ctor,
323
+ dst_ctor,
324
+ src_device,
325
+ dst_device,
326
+ n,
327
+ params: CopyParams
328
+ ):
329
+
330
+ # activate the given memory pool configuration
331
+ with wp.ScopedMempool(src_device, params.src_use_mempool), \
332
+ wp.ScopedMempool(dst_device, params.dst_use_mempool), \
333
+ wp.ScopedMempoolAccess(dst_device, src_device, params.access_dst_src), \
334
+ wp.ScopedMempoolAccess(src_device, dst_device, params.access_src_dst):
335
+
336
+ # make sure the data are different between tests by adding a unique offset
337
+ # this avoids aliasing issues with older memory
338
+ src_data = np.arange(params.value_offset, params.value_offset + n, dtype=np.float32)
339
+ dst_data = np.zeros(n, dtype=np.float32)
340
+
341
+ if params.with_grad:
342
+ src_grad_data = -np.arange(params.value_offset, params.value_offset + n, dtype=np.float32)
343
+ dst_grad_data = np.zeros(n, dtype=np.float32)
344
+ else:
345
+ src_grad_data = None
346
+ dst_grad_data = None
347
+
348
+ # create Warp arrays for the copy
349
+ src = src_ctor(src_data, device=src_device, grad_data=src_grad_data)
350
+ dst = dst_ctor(dst_data, device=dst_device, grad_data=dst_grad_data)
351
+
352
+ # determine the stream argument to pass to wp.copy()
353
+ if params.stream_device is not None:
354
+ stream_arg = wp.Stream(params.stream_device)
355
+ else:
356
+ stream_arg = None
357
+
358
+ # determine the actual stream used for the copy
359
+ if stream_arg is not None:
360
+ stream = stream_arg
361
+ else:
362
+ if dst_device.is_cuda:
363
+ stream = dst_device.stream
364
+ elif src_device.is_cuda:
365
+ stream = src_device.stream
366
+ else:
367
+ stream = None
368
+
369
+ # check if an exception is expected given the arguments and system configuration
370
+ expected_error_type = None
371
+ expected_error_regex = None
372
+
373
+ # restrictions on copying between different devices during graph capture
374
+ if params.use_graph and src_device != dst_device:
375
+
376
+ # errors with allocating staging buffer on source device
377
+ if not src.is_contiguous:
378
+ if src_device.is_cuda and not src_device.is_mempool_enabled:
379
+ # can't allocate staging buffer using default CUDA allocator during capture
380
+ expected_error_type, expected_error_regex = RuntimeError, r"^Failed to allocate"
381
+ elif src_device.is_cpu:
382
+ # can't allocate CPU staging buffer during capture
383
+ expected_error_type, expected_error_regex = RuntimeError, r"^Failed to allocate"
384
+
385
+ # errors with allocating staging buffer on destination device
386
+ if expected_error_type is None:
387
+ if not dst.is_contiguous:
388
+ if dst_device.is_cuda and not dst_device.is_mempool_enabled:
389
+ # can't allocate staging buffer using default CUDA allocator during capture
390
+ expected_error_type, expected_error_regex = RuntimeError, r"^Failed to allocate"
391
+ elif dst_device.is_cpu and src_device.is_cuda:
392
+ # can't allocate CPU staging buffer during capture
393
+ expected_error_type, expected_error_regex = RuntimeError, r"^Failed to allocate"
394
+
395
+ # p2p copies and mempool access
396
+ if expected_error_type is None and src_device.is_cuda and dst_device.is_cuda:
397
+
398
+ # If the source is a contiguous mempool allocation or a non-contiguous array
399
+ # AND the destination is a contiguous mempool allocation or a non-contiguous array,
400
+ # then memory pool access needs to be enabled EITHER from src_device to dst_device
401
+ # OR from dst_device to src_device.
402
+ if (((src.is_contiguous and params.src_use_mempool) or not src.is_contiguous) and
403
+ ((dst.is_contiguous and params.dst_use_mempool) or not dst.is_contiguous) and
404
+ not wp.is_mempool_access_enabled(src_device, dst_device) and
405
+ not wp.is_mempool_access_enabled(dst_device, src_device)
406
+ ):
407
+ expected_error_type, expected_error_regex = RuntimeError, r"^Warp copy error"
408
+
409
+ # synchronize before test
410
+ wp.synchronize()
411
+
412
+ if expected_error_type is not None:
413
+ # disable error output from Warp if we expect an exception
414
+ try:
415
+ saved_error_output_enabled = wp.context.runtime.core.is_error_output_enabled()
416
+ wp.context.runtime.core.set_error_output_enabled(False)
417
+ with test.assertRaisesRegex(expected_error_type, expected_error_regex):
418
+ with Capturable(use_graph=params.use_graph, stream=stream):
419
+ wp.copy(dst, src, stream=stream_arg)
420
+ finally:
421
+ wp.context.runtime.core.set_error_output_enabled(saved_error_output_enabled)
422
+ wp.synchronize()
423
+
424
+ # print(f"SUCCESSFUL ERROR PREDICTION: {expected_error_regex}")
425
+
426
+ else:
427
+ with Capturable(use_graph=params.use_graph, stream=stream):
428
+ wp.copy(dst, src, stream=stream_arg)
429
+
430
+ # synchronize the stream where the copy was running (None for h2h copies)
431
+ if stream is not None:
432
+ wp.synchronize_stream(stream)
433
+
434
+ assert_np_equal(dst.numpy(), src.numpy())
435
+
436
+ if params.with_grad:
437
+ assert_np_equal(dst.grad.numpy(), src.grad.numpy())
438
+
439
+ # print("SUCCESSFUL COPY")
440
+
441
+
442
+ array_constructors = {
443
+ "contiguous": as_contiguous_array,
444
+ "strided": as_strided_array,
445
+ "indexed": as_indexed_array,
446
+ "fabric": as_fabric_array,
447
+ "indexedfabric": as_indexed_fabric_array,
448
+ }
449
+
450
+ array_type_codes = {
451
+ "contiguous": "c",
452
+ "strided": "s",
453
+ "indexed": "i",
454
+ "fabric": "f",
455
+ "indexedfabric": "fi",
456
+ }
457
+
458
+ device_pairs = {}
459
+ cpu = None
460
+ cuda0 = None
461
+ cuda1 = None
462
+ cuda2 = None
463
+ if wp.is_cpu_available():
464
+ cpu = wp.get_device("cpu")
465
+ device_pairs["h2h"] = (cpu, cpu)
466
+ if wp.is_cuda_available():
467
+ cuda0 = wp.get_device("cuda:0")
468
+ device_pairs["d2d"] = (cuda0, cuda0)
469
+ if wp.is_cpu_available():
470
+ device_pairs["h2d"] = (cpu, cuda0)
471
+ device_pairs["d2h"] = (cuda0, cpu)
472
+ if wp.get_cuda_device_count() > 1:
473
+ cuda1 = wp.get_device("cuda:1")
474
+ device_pairs["p2p"] = (cuda0, cuda1)
475
+ if wp.get_cuda_device_count() > 2:
476
+ cuda2 = wp.get_device("cuda:2")
477
+
478
+ num_copy_elems = 1000000
479
+ num_copy_tests = 0
480
+
481
+
482
+ def add_copy_test(test_name, src_ctor, dst_ctor, src_device, dst_device, n, params):
483
+ test_func = \
484
+ lambda test, device, src_ctor=src_ctor, dst_ctor=dst_ctor, src_device=src_device, dst_device=dst_device, n=n, params=params: \
485
+ copy_template(test, src_ctor, dst_ctor, src_device, dst_device, n, params)
486
+ add_function_test(TestAsync, test_name, test_func, check_output=False)
487
+
488
+
489
+ # Procedurally add tests with argument combinations supported by the system.
490
+ for src_type, src_ctor in array_constructors.items():
491
+ for dst_type, dst_ctor in array_constructors.items():
492
+
493
+ copy_type = f"{array_type_codes[src_type]}2{array_type_codes[dst_type]}"
494
+
495
+ for transfer_type, device_pair in device_pairs.items():
496
+
497
+ # skip p2p tests if IOMMU is enabled on Linux
498
+ if transfer_type == "p2p" and not check_iommu():
499
+ continue
500
+
501
+ src_device = device_pair[0]
502
+ dst_device = device_pair[1]
503
+
504
+ # basic copy arguments
505
+ copy_args = (src_ctor, dst_ctor, src_device, dst_device, num_copy_elems)
506
+
507
+ if src_device.is_cuda and src_device.is_mempool_supported:
508
+ src_mempool_flags = [False, True]
509
+ else:
510
+ src_mempool_flags = [False]
511
+
512
+ if dst_device.is_cuda and dst_device.is_mempool_supported:
513
+ dst_mempool_flags = [False, True]
514
+ else:
515
+ dst_mempool_flags = [False]
516
+
517
+ # stream options
518
+ if src_device.is_cuda:
519
+ if dst_device.is_cuda:
520
+ if src_device == dst_device:
521
+ # d2d
522
+ assert src_device == cuda0 and dst_device == cuda0
523
+ if cuda1 is not None:
524
+ stream_devices = [None, cuda0, cuda1]
525
+ else:
526
+ stream_devices = [None, cuda0]
527
+ else:
528
+ # p2p
529
+ assert src_device == cuda0 and dst_device == cuda1
530
+ if cuda2 is not None:
531
+ stream_devices = [None, cuda0, cuda1, cuda2]
532
+ else:
533
+ stream_devices = [None, cuda0, cuda1]
534
+ else:
535
+ # d2h
536
+ assert src_device == cuda0
537
+ if cuda1 is not None:
538
+ stream_devices = [None, cuda0, cuda1]
539
+ else:
540
+ stream_devices = [None, cuda0]
541
+ else:
542
+ if dst_device.is_cuda:
543
+ # h2d
544
+ assert dst_device == cuda0
545
+ if cuda1 is not None:
546
+ stream_devices = [None, cuda0, cuda1]
547
+ else:
548
+ stream_devices = [None, cuda0]
549
+ else:
550
+ # h2h
551
+ stream_devices = [None]
552
+
553
+ # gradient options (only supported with contiguous and strided arrays)
554
+ if src_type in ("contiguous", "strided") and dst_type in ("contiguous", "strided"):
555
+ grad_flags = [False, True]
556
+ else:
557
+ grad_flags = [False]
558
+
559
+ # graph capture options (only supported with CUDA devices)
560
+ if src_device.is_cuda or dst_device.is_cuda:
561
+ graph_flags = [False, True]
562
+ else:
563
+ graph_flags = [False]
564
+
565
+ # access from destination device to source mempool
566
+ if wp.is_mempool_access_supported(dst_device, src_device):
567
+ access_dst_src_flags = [False, True]
568
+ else:
569
+ access_dst_src_flags = [False]
570
+
571
+ # access from source device to destination mempool
572
+ if wp.is_mempool_access_supported(src_device, dst_device):
573
+ access_src_dst_flags = [False, True]
574
+ else:
575
+ access_src_dst_flags = [False]
576
+
577
+ for src_use_mempool in src_mempool_flags:
578
+ for dst_use_mempool in dst_mempool_flags:
579
+ for stream_device in stream_devices:
580
+ for access_dst_src in access_dst_src_flags:
581
+ for access_src_dst in access_src_dst_flags:
582
+ for with_grad in grad_flags:
583
+ for use_graph in graph_flags:
584
+
585
+ test_name = f"test_copy_{copy_type}_{transfer_type}"
586
+
587
+ if src_use_mempool:
588
+ test_name += "_SrcPoolOn"
589
+ else:
590
+ test_name += "_SrcPoolOff"
591
+
592
+ if dst_use_mempool:
593
+ test_name += "_DstPoolOn"
594
+ else:
595
+ test_name += "_DstPoolOff"
596
+
597
+ if stream_device is None:
598
+ test_name += "_NoStream"
599
+ elif stream_device == cuda0:
600
+ test_name += "_Stream0"
601
+ elif stream_device == cuda1:
602
+ test_name += "_Stream1"
603
+ elif stream_device == cuda2:
604
+ test_name += "_Stream2"
605
+ else:
606
+ assert False
607
+
608
+ if with_grad:
609
+ test_name += "_Grad"
610
+ else:
611
+ test_name += "_NoGrad"
612
+
613
+ if use_graph:
614
+ test_name += "_Graph"
615
+ else:
616
+ test_name += "_NoGraph"
617
+
618
+ if access_dst_src and access_src_dst:
619
+ test_name += "_AccessBoth"
620
+ elif access_dst_src and not access_src_dst:
621
+ test_name += "_AccessDstSrc"
622
+ elif not access_dst_src and access_src_dst:
623
+ test_name += "_AccessSrcDst"
624
+ else:
625
+ test_name += "_AccessNone"
626
+
627
+ copy_params = CopyParams(
628
+ src_use_mempool=src_use_mempool,
629
+ dst_use_mempool=dst_use_mempool,
630
+ access_dst_src=access_dst_src,
631
+ access_src_dst=access_src_dst,
632
+ stream_device=stream_device,
633
+ with_grad=with_grad,
634
+ use_graph=use_graph,
635
+ value_offset=num_copy_tests,
636
+ )
637
+
638
+ add_copy_test(test_name, *copy_args, copy_params)
639
+
640
+ num_copy_tests += 1
641
+
642
+ # Specify individual test(s) for debugging purposes
643
+ # add_copy_test("test_a", as_contiguous_array, as_strided_array, cuda0, cuda1, num_copy_elems,
644
+ # CopyParams(
645
+ # src_use_mempool=True,
646
+ # dst_use_mempool=True,
647
+ # access_dst_src=False,
648
+ # access_src_dst=False,
649
+ # stream_device=cuda0,
650
+ # with_grad=False,
651
+ # use_graph=True,
652
+ # value_offset=0))
653
+
654
+ if __name__ == "__main__":
655
+ wp.build.clear_kernel_cache()
656
+ unittest.main(verbosity=2)