-
Notifications
You must be signed in to change notification settings - Fork 4
BUG: Fix matmul with out= arrays #90
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 |
---|---|---|
|
@@ -5708,7 +5708,7 @@ class MatmulCommon: | |
""" | ||
# Should work with these types. Will want to add | ||
# "O" at some point | ||
types = "?bhilqBefdFD" | ||
types = "?bhilBefdFD" | ||
|
||
def test_exceptions(self): | ||
dims = [ | ||
|
@@ -5726,7 +5726,7 @@ def test_exceptions(self): | |
for dt, (dm1, dm2) in itertools.product(self.types, dims): | ||
a = np.ones(dm1, dtype=dt) | ||
b = np.ones(dm2, dtype=dt) | ||
assert_raises(ValueError, self.matmul, a, b) | ||
assert_raises((RuntimeError, ValueError), self.matmul, a, b) | ||
|
||
def test_shapes(self): | ||
dims = [ | ||
|
@@ -5758,7 +5758,13 @@ def test_result_types(self): | |
res = self.matmul(*arg) | ||
assert_(res.dtype == dt) | ||
|
||
# vector vector returns scalars | ||
@pytest.mark.xfail(reason='no scalars') | ||
def test_result_types_2(self): | ||
# in numpy, vector vector returns scalars | ||
# we return a 0D array instead | ||
|
||
for dt in self.types: | ||
v = np.ones((1,)).astype(dt) | ||
if dt != "O": | ||
res = self.matmul(v, v) | ||
assert_(type(res) is np.dtype(dt).type) | ||
|
@@ -5919,9 +5925,10 @@ def test_matrix_matrix_values(self): | |
assert_equal(res, tgt12_21) | ||
|
||
|
||
@pytest.mark.xfail(reason='TODO: matmul (ufunc wrapping goes south?)') | ||
class TestMatmul(MatmulCommon): | ||
matmul = np.matmul | ||
|
||
def setup_method(self): | ||
self.matmul = np.matmul | ||
|
||
def test_out_arg(self): | ||
a = np.ones((5, 2), dtype=float) | ||
|
@@ -5941,17 +5948,17 @@ def test_out_arg(self): | |
assert_array_equal(out, tgt, err_msg=msg) | ||
|
||
# test out with not allowed type cast (safe casting) | ||
msg = "Cannot cast ufunc .* output" | ||
msg = "Cannot cast" | ||
out = np.zeros((5, 2), dtype=np.int32) | ||
assert_raises_regex(TypeError, msg, self.matmul, a, b, out=out) | ||
|
||
# test out with type upcast to complex | ||
out = np.zeros((5, 2), dtype=np.complex128) | ||
c = self.matmul(a, b, out=out) | ||
assert_(c is out) | ||
with suppress_warnings() as sup: | ||
sup.filter(np.ComplexWarning, '') | ||
c = c.astype(tgt.dtype) | ||
# with suppress_warnings() as sup: | ||
# sup.filter(np.ComplexWarning, '') | ||
c = c.astype(tgt.dtype) | ||
assert_array_equal(c, tgt) | ||
|
||
def test_empty_out(self): | ||
|
@@ -5961,7 +5968,7 @@ def test_empty_out(self): | |
out = np.ones((1, 1, 1)) | ||
assert self.matmul(arr, arr).shape == (0, 1, 1) | ||
|
||
with pytest.raises(ValueError, match=r"non-broadcastable"): | ||
with pytest.raises(ValueError, match="Bad size of the out array"): # match=r"non-broadcastable"): | ||
self.matmul(arr, arr, out=out) | ||
|
||
def test_out_contiguous(self): | ||
|
@@ -5974,7 +5981,7 @@ def test_out_contiguous(self): | |
# test out non-contiguous | ||
out = np.ones((5, 2, 2), dtype=float) | ||
c = self.matmul(a, b, out=out[..., 0]) | ||
assert c.base is out | ||
assert c._tensor._base is out._tensor # FIXME: self.tensor (no underscore) | ||
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. This will get cleaned up before merging, after the base of the stack gets in. |
||
assert_array_equal(c, tgt) | ||
c = self.matmul(a, v, out=out[:, 0, 0]) | ||
assert_array_equal(c, tgt_mv) | ||
|
@@ -6025,6 +6032,7 @@ def test_dot_equivalent(self, args): | |
assert_equal(r1, r3) | ||
|
||
|
||
@pytest.mark.skip(reason='object arrays') | ||
def test_matmul_exception_multiply(self): | ||
# test that matmul fails if `__mul__` is missing | ||
class add_not_multiply(): | ||
|
@@ -6034,6 +6042,7 @@ def __add__(self, other): | |
with assert_raises(TypeError): | ||
b = np.matmul(a, a) | ||
|
||
@pytest.mark.skip(reason='object arrays') | ||
def test_matmul_exception_add(self): | ||
# test that matmul fails if `__add__` is missing | ||
class multiply_not_add(): | ||
|
@@ -6043,6 +6052,7 @@ def __mul__(self, other): | |
with assert_raises(TypeError): | ||
b = np.matmul(a, a) | ||
|
||
@pytest.mark.xfail(reason="TODO: implement .view") | ||
def test_matmul_bool(self): | ||
# gh-14439 | ||
a = np.array([[1, 0],[1, 1]], dtype=bool) | ||
|
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 change is a bit curious. Storing ufuncs as class attributes seems to work ok in scripts/interactive interpreter, but breaks down in combination with pytest:
the argument processing/normalization machinery picks up the extra
self
-like argument, which leads toso there's some spooky action on a distance between meta-stuff pytest is doing and what we do here.