warp.from_dlpack#
- warp.from_dlpack(source, dtype=None, hint=None)[source]#
Return a Warp array or texture sharing storage with a DLPack source.
For source objects with data on a CUDA device, Warp calls the object’s
__dlpack__()method with the current Warp stream on that device. Usewarp.ScopedStreamaround the import and the Warp operations that access the shared data.The source library must ensure its earlier work on the data finishes before new work on the Warp stream accesses it. The source library can record a CUDA event after its work and queue a wait for that event on the Warp stream.
For a raw capsule you must order dependent CUDA accesses to the exported buffer. Queue those accesses on the same stream or use a stream wait. Before using a different Warp stream after importing a source object, make that stream wait for the Warp stream used for the import. See DLPack for more examples.
- Parameters:
source – An object implementing
__dlpack__()and__dlpack_device__()or an unused DLPack capsule. A successful import consumes the capsule.dtype – Warp dtype used to interpret the source elements. Warp infers a scalar dtype from the source by default. For a vector or matrix dtype the source must have a compatible scalar dtype and matching trailing dimensions that are contiguous in row-major order.
hint – Texture type to use for a
cudaArray_thandle exported askDLOpaquePointer. Supported hints arewarp.Texture1D,warp.Texture2D, andwarp.Texture3D.
- Returns:
A view that keeps the exported buffer alive after the source object goes out of scope.
If the exporting library allows external writes to the exported buffer, in-place changes are visible through both views once the accesses are ordered. Otherwise, treat the imported view as read-only and use
warp.clone()if you need a writable copy of an imported array.For vector and matrix dtypes the trailing component dimensions become part of each Warp element. Array imports preserve the outer strides. Import gradient buffers separately when needed.
- Return type:
Example
Import a PyTorch CUDA tensor as a Warp array of 3D points and launch a kernel on the Warp stream used for the import:
import torch import warp as wp @wp.kernel def translate_points(points: wp.array[wp.vec3], translated: wp.array[wp.vec3]): i = wp.tid() translated[i] = points[i] + wp.vec3(0.0, 0.0, 1.0) device = "cuda:0" torch_stream = torch.cuda.Stream(device=device) warp_stream = wp.Stream(wp.get_device(device)) with torch.cuda.stream(torch_stream), wp.ScopedStream(warp_stream): tensor = torch.arange(12, dtype=torch.float32, device=device).reshape(4, 3) points = wp.from_dlpack(tensor, dtype=wp.vec3) translated = wp.empty_like(points) wp.launch(translate_points, dim=len(points), inputs=[points], outputs=[translated], device=device)
If the last reference to the imported buffer could be dropped before the Warp kernel completes, keep
tensororpointsalive until the kernel completes, or calltensor.record_stream(warp.stream_to_torch(warp_stream))before dropping that reference.