8000 BUG: Ensure out is returned in einsum. by mattip · Pull Request #11406 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

BUG: Ensure out is returned in einsum. #11406

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 3 commits into from
Jun 29, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion numpy/core/src/multiarray/einsum.c.src
Original file line number Diff line number Diff line change
Expand Up @@ -2769,8 +2769,13 @@ PyArray_EinsteinSum(char *subscripts, npy_intp nop,

/* Initialize the output to all zeros and reset the iterator */
ret = NpyIter_GetOperandArray(iter)[nop];
if (PyArray_AssignZero(ret, NULL) < 0) {
goto fail;
}
if (out != NULL) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks fishy, why do anything to ret if it is simply set to out?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the output operand has writeback semantics, NpyIter_GetOperandArray(iter)[nop] is the temporary ndarray created with out as it's base ndarray. Then we zero the temporary data rather than changing out itself. See issue #10956 for the python-level equivalent discussion about what is meant by iter.operand[i] - the temporary ndarray with writeback semantics or the underlying user-created input-ouput ndarray.

Lines 2775-2777 ensure that ret and out point to the same ndarray, as required when using an out kwarg.

ret = out;
}
Py_INCREF(ret);
PyArray_AssignZero(ret, NULL);


/***************************/
Expand Down
5 changes: 5 additions & 0 deletions numpy/core/tests/test_einsum.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,11 @@ def test_small_boolean_arrays(self):
res = np.einsum('...ij,...jk->...ik', a, a, out=out)
assert_equal(res, tgt)

def test_out_is_res(self):
a = np.arange(9).reshape(3, 3)
res = np.einsum('...ij,...jk->...ik', a, a, out=a)
assert res is a

def optimize_compare(self, string):
# Tests all paths of the optimization function against
# conventional einsum
Expand Down
0