-
Notifications
You must be signed in to change notification settings - Fork 35
BUG: add test that wrapping preserves view/copy semantics, fix where it doesn't #333
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c9cfc2c
TST: add a test that wrapping preserves a view/copy semantics for una…
ev-br 1facc35
BUG: make ceil,trunc,floor always respect view/copy semantics
ev-br 0ad664b
Apply suggestions from code review
ev-br 118ae2d
TST: test views vs copies on array-api-strict, too
ev-br b0eed55
Apply suggestions from code review
ev-br 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
@@ -0,0 +1,64 @@ | ||
""" | ||
A collection of tests to make sure that wrapped namespaces agree with the bare ones | ||
on whether to return a view or a copy of inputs. | ||
""" | ||
import pytest | ||
from ._helpers import import_, wrapped_libraries | ||
|
||
|
||
FUNC_INPUTS = [ | ||
# func_name, arr_input, dtype, scalar_value | ||
('abs', [1, 2], 'int8', 3), | ||
('abs', [1, 2], 'float32', 3.), | ||
('ceil', [1, 2], 'int8', 3), | ||
('clip', [1, 2], 'int8', 3), | ||
('conj', [1, 2], 'int8', 3), | ||
('floor', [1, 2], 'int8', 3), | ||
('imag', [1j, 2j], 'complex64', 3), | ||
('positive', [1, 2], 'int8', 3), | ||
('real', [1., 2.], 'float32', 3.), | ||
('round', [1, 2], 'int8', 3), | ||
('sign', [0, 0], 'float32', 3), | ||
('trunc', [1, 2], 'int8', 3), | ||
('trunc', [1, 2], 'float32', 3), | ||
] | ||
|
||
|
||
def ensure_unary(func, arr): | ||
"""Make a trivial unary function from func.""" | ||
if func.__name__ == 'clip': | ||
return lambda x: func(x, arr[0], arr[1]) | ||
return func | ||
|
||
|
||
def is_view(func, a, value): | ||
"""Apply `func`, mutate the output; does the input change?""" | ||
b = func(a) | ||
b[0] = value | ||
return a[0] == value | ||
|
||
|
||
@pytest.mark.parametrize('xp_name', wrapped_libraries + ['array_api_strict']) | ||
@pytest.mark.parametrize('inputs', FUNC_INPUTS, ids=[inp[0] for inp in FUNC_INPUTS]) | ||
def test_view_or_copy(inputs, xp_name): | ||
bare_xp = import_(xp_name, wrapper=False) | ||
wrapped_xp = import_(xp_name, wrapper=True) | ||
|
||
func_name, arr_input, dtype_str, value = inputs | ||
dtype = getattr(bare_xp, dtype_str) | ||
|
||
bare_func = getattr(bare_xp, func_name) | ||
bare_func = ensure_unary(bare_func, arr_input) | ||
|
||
wrapped_func = getattr(wrapped_xp, func_name) | ||
wrapped_func = ensure_unary(wrapped_func, arr_input) | ||
|
||
# bare namespace: mutate the output, does the input change? | ||
a = bare_xp.asarray(arr_input, dtype=dtype) | ||
is_view_bare = is_view(bare_func, a, value) | ||
|
||
# wrapped namespace: mutate the output, does the input change? | ||
a1 = wrapped_xp.asarray(arr_input, dtype=dtype) | ||
is_view_wrapped = is_view(wrapped_func, a1, value) | ||
|
||
assert is_view_bare == is_view_wrapped |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
int arguments are outside of the scope of the Array API.
So I'm not sure this should be here at all?
For numpy is a bit more nuanced - on one hand it makes sense to unify numpy 1.x behaviour to match 2.x.
On the other hand it's a array-api-compat tweak specifically to cover a change in behaviour that is already outside of the Array API.
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 think it's just backwards compat. On main, all backends try to return integers for integer inputs (via
common/_aliases.py
), so this PR tries to preserve that, while fixing the views/copies issue.