warp.to_dlpack#
- warp.to_dlpack(wp_array)[source]#
Return a DLPack capsule sharing a Warp array’s storage.
For arrays on a CUDA device, callers must order dependent accesses to the shared storage. Queue those accesses on the same CUDA stream or use a stream wait to order them across streams.
Pass the Warp array directly to a DLPack import function to enable a stream handoff. Use
torch.from_dlpack(wp_array)in PyTorch orjax.dlpack.from_dlpack(wp_array)in JAX.The importing library can pass its CUDA stream to
wp_array.__dlpack__(). Warp uses its current stream on the array’s device for the handoff. Usewarp.ScopedStreamaround the import call to select the Warp stream with the earlier work on the array.Work queued on the importing library’s stream after the handoff waits for work already queued on the current Warp stream.
Warp uses a CUDA event when the streams differ. Warp records the event on its current stream and queues a wait for that event on the importing library’s stream. See DLPack for more examples.
- Parameters:
wp_array (array) – The Warp array to export. Arrays with a dtype defined by
@warp.structare unsupported.- Returns:
A single-use capsule that keeps the source array alive until the consumer releases the managed tensor or the unused capsule is discarded. For vector and matrix dtypes, the capsule represents the components as trailing scalar dimensions and preserves the outer array strides.
Example
Generate points with a Warp kernel and compute their centroid on a separate PyTorch stream:
import torch import warp as wp @wp.kernel def sample_parabola(points: wp.array[wp.vec3]): i = wp.tid() x = wp.float32(i) points[i] = wp.vec3(x, x * x, 0.0) device = "cuda:0" warp_stream = wp.Stream(wp.get_device(device)) torch_stream = torch.cuda.Stream(device=device) with wp.ScopedStream(warp_stream), torch.cuda.stream(torch_stream): points = wp.empty(4, dtype=wp.vec3, device=device) wp.launch(sample_parabola, dim=len(points), outputs=[points], device=device) tensor = torch.from_dlpack(points) centroid = tensor.mean(dim=0)
If the PyTorch read could still be pending when the last reference to the exported buffer is dropped, keep
pointsortensoralive until the read completes, or queue a wait fortorch_streamon a blocking Warp stream before dropping that reference (see Blocking and Non-Blocking Streams).