-
Notifications
You must be signed in to change notification settings - Fork 3.1k
Add array-api copy semantics to DLPackManagedTensorToBuffer #29963
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
base: main
Are you sure you want to change the base?
Conversation
604aea5
to
51573a4
Compare
e226d31
to
d2c005f
Compare
d2c005f
to
11ed1a3
Compare
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.
Thanks!
se = str(e) | ||
i = se.index("is not aligned to") | ||
if i > 0: | ||
raise ValueError( |
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.
I assume we cannot raise this error directly in C++ because it talks about Python-level arguments?
_arr = jnp.asarray(_buf) | ||
if copy and jaxlib_version >= (0, 6, 3): | ||
# dlpack_managed_tensor_to_buffer implements array-api copy | ||
# semantics, so resetting copy to avoid an unnecessary copy |
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.
It might be more clear to say that copy
was already handled by dlpack_managed_tensor_to_buffer
, I think.
Also, nit: add a "." :)
// require a view (copy == False) or try create a view (copy == None) | ||
auto result = device.client()->CreateViewOfDeviceBuffer( | ||
data, shape, *device.default_memory_space(), on_delete_callback, stream); | ||
if ((result.status().code() != absl::StatusCode::kInvalidArgument) |
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.
Nit: drop redundant parens?
TF_ASSIGN_OR_RETURN(auto* memory_space, device.default_memory_space()); | ||
|
||
// Create a copy. | ||
auto result = device.client()->BufferFromHostBuffer( |
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.
Nit: directly return?
bool fallback_to_copy = !copy.has_value() && dlmt->dl_tensor.device.device_type == kDLCPU; | ||
|
||
if (!copy.value_or(false)) { | ||
// require a view (copy == False) or try create a view (copy == None) |
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.
Nit: I'd nuke this and other similar comments personally, and instead try to change the code to make the conditions more obvious (e.g. on L233).
@@ -738,7 +738,7 @@ def buffer_to_dlpack_managed_tensor( | |||
) -> Any: ... | |||
@overload | |||
def dlpack_managed_tensor_to_buffer( | |||
tensor: Any, device: Device, stream: int | None | |||
tensor: Any, device: Device, stream: int | None, copy: bool | None |
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.
Add = None
, since copy
has a default.
As in the title.
Fixes #29810