-
Notifications
You must be signed in to change notification settings - Fork 630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JAX zero copy #5703
JAX zero copy #5703
Changes from all commits
d10a227
c5d6d2e
050a4aa
b3cc6aa
80a6b42
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,31 +16,46 @@ | |
import jax.dlpack | ||
|
||
from nvidia.dali.backend import TensorGPU | ||
from packaging.version import Version | ||
|
||
|
||
|
||
_jax_version_pre_0_4_16 = None | ||
_jax_has_old_dlpack = Version(jax.__version__) < Version("0.4.16") | ||
|
||
|
||
def _jax_has_old_dlpack(): | ||
global _jax_version_pre_0_4_16 | ||
if _jax_version_pre_0_4_16 is not None: | ||
return _jax_version_pre_0_4_16 | ||
if Version(jax.__version__) >= Version("0.4.31"): | ||
|
||
from packaging.version import Version | ||
def _jax_device(jax_array): | ||
return jax_array.device | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I couldn't find the documentation for old JAX releases, but looking at the code, the method version used to raise an error for multi-dev array, while this property (according to the docs) returns sharding. So this call works fine with the multi-dev arrays while the other two variants don't. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's kind of the problem with jax: it keeps changing and the documentation is hard to find. |
||
|
||
_jax_version_pre_0_4_16 = Version(jax.__version__) < Version("0.4.16") | ||
return _jax_version_pre_0_4_16 | ||
elif Version(jax.__version__) >= Version("0.4.27"): | ||
|
||
def _jax_device(jax_array): | ||
|
||
devs = jax_array.devices() | ||
if len(devs) != 1: | ||
raise RuntimeError("The array must be associated with exactly one device") | ||
for d in devs: | ||
return d | ||
|
||
def _to_jax_array(dali_tensor: TensorGPU) -> jax.Array: | ||
else: | ||
|
||
def _jax_device(jax_array): | ||
return jax_array.device() | ||
|
||
|
||
def _to_jax_array(dali_tensor: TensorGPU, copy: bool) -> jax.Array: | ||
"""Converts input DALI tensor to JAX array. | ||
|
||
Args: | ||
dali_tensor (TensorGPU): DALI GPU tensor to be converted to JAX array. | ||
dali_tensor (TensorGPU): | ||
DALI GPU tensor to be converted to JAX array. | ||
|
||
copy (bool): | ||
If True, the output is copied; | ||
if False, the output may wrap DLPack capsule obtained from `dali_tensor`. | ||
|
||
Note: | ||
This function performs deep copy of the underlying data. That will change in | ||
future releases. | ||
This function may perform a copy of the data even if `copy==False` when JAX version is | ||
insufficient (<0.4.16) | ||
|
||
Warning: | ||
As private this API may change without notice. | ||
|
@@ -49,12 +64,12 @@ | |
jax.Array: JAX array with the same values and backing device as | ||
input DALI tensor. | ||
""" | ||
if _jax_has_old_dlpack(): | ||
if _jax_has_old_dlpack: | ||
copy = True | ||
jax_array = jax.dlpack.from_dlpack(dali_tensor.__dlpack__(stream=None)) | ||
else: | ||
jax_array = jax.dlpack.from_dlpack(dali_tensor) | ||
|
||
# For now we need this copy to make sure that underlying memory is available. | ||
# One solution is to implement full DLPack contract in DALI. | ||
# TODO(awolant): Remove this copy. | ||
return jax_array.copy() | ||
if copy: | ||
jax_array = jax_array.copy() | ||
return jax_array |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a test that compares docstrings and it failed before. Apparently it's not run in CI (?).