BUG: `where=` in gufuncs is slightly broken with `out=` and casts
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 32.8k
- Forks
- 12.8k
- Avg merge
- 1d 7h
- Merged PRs (30d)
- 197
Description
EDIT (seberg): The original behavior change described here seems OK. However, another issue was identified: https://github.com/numpy/numpy/issues/18700#issuecomment-810618183
When the out parameter on ufunc is given, numpy 1.19 (and older versions) would always pass the values of the out array to the kernel. In numpy 1.20, that changed and a fresh uninitialized array is given when the type does not match exactly.
In other words... given a no-op ufunc (kernel does nothing) that takes 1 input and 1 output. And, it is called as:
out = np.arange(10)
no_op_ufunc(inp, out=out)
Is the output guaranteed to retain the original values?
Reproducing code example:
Reproducer from https://github.com/numba/numba/issues/6864
import numba
import numpy as np
@numba.guvectorize(["void(float64[:], uint8[:])"], "(n)->(n)", nopython=True)
def func(x, out):
for i in range(x.size):
# set every fourth element to 1
if i % 4 == 0:
out[i] = 1
x = np.random.rand(150,150)
out = np.zeros_like(x, dtype=np.int8) # dtype does not match expected
func(x, out)
with Numba 0.53 and Numpy 1.20.1, the result is:
array([[ 1, 49, 29, ..., 96, 1, 2],
[ 1, 0, -80, ..., -38, 1, 97],
[ 1, 2, 0, ..., 0, 1, 0],
...,
[ 1, -1, -1, ..., 0, 1, 0],
[ 1, 0, 0, ..., 0, 1, 0],
[ 1, 0, 0, ..., -34, 1, 97]], dtype=int8)
In np1.20, the skipped slots are containing random values.
with Numba 0.53 and Numpy 1.19.5, the result is:
array([[1, 0, 0, ..., 0, 1, 0],
[1, 0, 0, ..., 0, 1, 0],
[1, 0, 0, ..., 0, 1, 0],
...,
[1, 0, 0, ..., 0, 1, 0],
[1, 0, 0, ..., 0, 1, 0],
[1, 0, 0, ..., 0, 1, 0]], dtype=int8)
In np1.19, the skipped slots are retaining the original zero values.
Error message:
No error message. The problem is a change in behavior.
NumPy/Python version information:
>>> import sys, numpy; print(numpy.__version__, sys.version)
1.20.1 3.9.2 (default, Mar 3 2021, 11:58:52)
[Clang 10.0.0 ]
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the NumPy/Numba reproducer using a gufunc with out= whose dtype does not match exactly, then trace gufunc handling of out= and casts. Resolve the behavior identified in the linked follow-up comment and add coverage demonstrating the expected result for skipped output elements.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- data
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100