-
Notifications
You must be signed in to change notification settings - Fork 29
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
ENH: is_lazy_array() #228
Merged
+107
−6
Merged
ENH: is_lazy_array() #228
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -824,6 +824,63 @@ def is_writeable_array(x) -> bool: | |
return True | ||
|
||
|
||
def is_lazy_array(x) -> bool: | ||
"""Return True if x is potentially a future or it may be otherwise impossible or | ||
expensive to eagerly read its contents, regardless of their size, e.g. by | ||
calling ``bool(x)`` or ``float(x)``. | ||
|
||
Return False otherwise; e.g. ``bool(x)`` etc. is guaranteed to succeed and to be | ||
cheap as long as the array has the right dtype and size. | ||
|
||
Note | ||
---- | ||
This function errs on the side of caution for array types that may or may not be | ||
lazy, e.g. JAX arrays, by always returning True for them. | ||
""" | ||
if ( | ||
is_numpy_array(x) | ||
or is_cupy_array(x) | ||
or is_torch_array(x) | ||
or is_pydata_sparse_array(x) | ||
): | ||
return False | ||
|
||
# **JAX note:** while it is possible to determine if you're inside or outside | ||
# jax.jit by testing the subclass of a jax.Array object, as well as testing bool() | ||
# as we do below for unknown arrays, this is not recommended by JAX best practices. | ||
|
||
# **Dask note:** Dask eagerly computes the graph on __bool__, __float__, and so on. | ||
# This behaviour, while impossible to change without breaking backwards | ||
# compatibility, is highly detrimental to performance as the whole graph will end | ||
# up being computed multiple times. | ||
|
||
if is_jax_array(x) or is_dask_array(x) or is_ndonnx_array(x): | ||
return True | ||
|
||
# Unknown Array API compatible object. Note that this test may have dire consequences | ||
# in terms of performance, e.g. for a lazy object that eagerly computes the graph | ||
# on __bool__ (dask is one such example, which however is special-cased above). | ||
|
||
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. array_api_strict reaches this point |
||
# Select a single point of the array | ||
s = size(x) | ||
if s is None: | ||
return True | ||
xp = array_namespace(x) | ||
if s > 1: | ||
x = xp.reshape(x, (-1,))[0] | ||
# Cast to dtype=bool and deal with size 0 arrays | ||
x = xp.any(x) | ||
|
||
try: | ||
bool(x) | ||
return False | ||
# The Array API standard dictactes that __bool__ should raise TypeError if the | ||
# output cannot be defined. | ||
# Here we allow for it to raise arbitrary exceptions, e.g. like Dask does. | ||
except Exception: | ||
return True | ||
|
||
|
||
__all__ = [ | ||
"array_namespace", | ||
"device", | ||
|
@@ -845,6 +902,7 @@ def is_writeable_array(x) -> bool: | |
"is_pydata_sparse_array", | ||
"is_pydata_sparse_namespace", | ||
"is_writeable_array", | ||
"is_lazy_array", | ||
"size", | ||
"to_device", | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a good argument for replacing everything below this point with a blind
return False
. Please discuss if you'd prefer it that way.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.
This seems fine as is to me, since it's more correct. In case it's a problem for a library that's not explicitly handled, then support for it can be added, or a discussion can happen then - at least we'll learn something.