Clarify memory sharing and synchronization in interoperability APIs
Category
- Report an error in the documentation.
- Request for something to be documented.
- Suggestion to improve the documentation.
- Other (please explain)
Description
Several interoperability docstrings say that a conversion happens "without copying." That establishes that storage is shared, but does not tell users what they can safely do with the source and converted objects afterward.
This affects to_dlpack, from_dlpack, the JAX and Paddle conversion functions, and behavior shared with the PyTorch conversions.
The documentation gaps are:
- The docstrings do not consistently say whether a conversion shares storage or copies it. This may depend on the framework and whether the data is on CPU or CUDA.
- When storage is shared, the docs do not always say whether mutations through one object are visible through the other.
- It is not clear whether the converted object keeps the storage alive or whether the caller must retain the source object.
to_dlpack()does not say that the returned capsule is single-use.from_dlpack()performs a CUDA stream handoff for objects implementing the DLPack protocol, but its docstring does not explain which stream is used or what synchronization the caller can rely on.- Vector and matrix Warp dtypes become additional scalar dimensions in DLPack. The resulting shape and stride behavior is not documented.
- Gradient handling differs by framework. The docs should say whether gradient storage is shared, wrapped separately, allocated, or not transferred.
- Unsupported inputs, such as structured arrays or incompatible inner strides, are often only discoverable from an exception.
from_torch() and to_torch() already describe storage aliasing more clearly than the other framework conversions. to_torch() also warns callers to clone the tensor if Warp may later modify or clear the shared values. The other conversion docstrings could use the same terminology where their behavior matches.
The DLPack functions need a short practical explanation of their lifetime behavior. A capsule returned by to_dlpack() keeps the source Warp array alive until the consumer releases the managed tensor. A Warp object returned by from_dlpack() keeps the imported managed tensor alive. This means callers do not need to keep an otherwise unused source variable solely to preserve the allocation, but the capsule itself can only be consumed once.
to_paddle() also needs attention. Its docstring makes an unconditional zero-copy claim, but the CPU path converts through numpy.asarray() and paddle.to_tensor(), while the CUDA path uses DLPack. The documentation should state whether each path shares storage or copies it after that behavior has been verified.
Longer explanations can remain in the interoperability guide. The API docstrings should state the behavior of each conversion and link to the relevant section.
#1732 tracks incorrect PyTorch gradient behavior and should remain a separate runtime issue. #1737 covered the narrower use of PyTorch interoperability with wp.Tape and is closed. This documentation work is tracked as a child of #1498.
Source: NVIDIA/warp