numpy icon indicating copy to clipboard operation
numpy copied to clipboard

Confusing behavior of where in ufuncs

Open haojin2 opened this issue 7 years ago • 8 comments

Reproducing code example:

>>> import numpy as np
>>> a = np.random.uniform(size=(3, 4))
>>> where = np.random.uniform(size=(3, 4)) >= 0.5
>>> np.sin(a)
array([[0.29821273, 0.21740487, 0.01291611, 0.1284391 ],
       [0.72737801, 0.32444491, 0.27899213, 0.73718454],
       [0.32471309, 0.15702175, 0.39378894, 0.69413136]])
>>> np.sin(a, where=where)
array([[0.29821273, 0.21740487, 0.01291611, 0.1284391 ],
       [0.72737801, 0.32444491, 0.27899213, 0.73718454],
       [0.32471309, 0.15702175, 0.39378894, 0.69413136]])
>>> out = np.zeros(a.shape)
>>> np.sin(a, out=out)
array([[0.29821273, 0.21740487, 0.01291611, 0.1284391 ],
       [0.72737801, 0.32444491, 0.27899213, 0.73718454],
       [0.32471309, 0.15702175, 0.39378894, 0.69413136]])
>>> out
array([[0.29821273, 0.21740487, 0.01291611, 0.1284391 ],
       [0.72737801, 0.32444491, 0.27899213, 0.73718454],
       [0.32471309, 0.15702175, 0.39378894, 0.69413136]])
>>> out = np.zeros(a.shape)
>>> np.sin(a, out=out, where=where)
array([[0.        , 0.21740487, 0.01291611, 0.        ],
       [0.72737801, 0.32444491, 0.        , 0.        ],
       [0.32471309, 0.15702175, 0.39378894, 0.        ]])
>>> out
array([[0.        , 0.21740487, 0.01291611, 0.        ],
       [0.72737801, 0.32444491, 0.        , 0.        ],
       [0.32471309, 0.15702175, 0.39378894, 0.        ]])
>>> np.sin(a, where=where)
array([[0.        , 0.21740487, 0.01291611, 0.        ],
       [0.72737801, 0.32444491, 0.        , 0.        ],
       [0.32471309, 0.15702175, 0.39378894, 0.        ]])
>>> np.sin(a)
array([[0.29821273, 0.21740487, 0.01291611, 0.1284391 ],
       [0.72737801, 0.32444491, 0.27899213, 0.73718454],
       [0.32471309, 0.15702175, 0.39378894, 0.69413136]])

Numpy/Python version information:

1.17.2 3.6.7 (default, Oct 22 2018, 11:32:17) 
[GCC 8.2.0]

haojin2 avatar Oct 08 '19 00:10 haojin2

Notice that before a call with out specified was performed, where was not playing a part at all. However, after a call with out was performed suddenly the where started to have some effects on the output.

haojin2 avatar Oct 08 '19 01:10 haojin2

I am not sure where this logic is actually hidden, but the documentation states that the behaviour on masked fields is undefined. So, this must be a matter of optimization since handling the mask correctly is typically more difficult than just calculating everything. In fact, I would be a bit surprised, but won't guarantee that it does not calculate everything in the out case as well, but simply only writes out the desired ones.

seberg avatar Oct 08 '19 05:10 seberg

@seberg I just ran again with the where mask printed out and result was like this:

>>> import numpy as np
>>> a = np.random.uniform(size=(3, 4))
>>> where = np.random.uniform(size=(3, 4)) >= 0.5
>>> where
array([[False, False, False, False],
       [ True,  True,  True,  True],
       [ True, False,  True,  True]])
>>> np.sin(a)
array([[0.59597809, 0.48786673, 0.3121218 , 0.06527679],
       [0.35881205, 0.04690551, 0.59981618, 0.51865972],
       [0.59330093, 0.10031214, 0.6879784 , 0.71111629]])
>>> np.sin(a, where=where)
array([[0.59597809, 0.48786673, 0.3121218 , 0.06527679],
       [0.35881205, 0.04690551, 0.59981618, 0.51865972],
       [0.59330093, 0.10031214, 0.6879784 , 0.71111629]])
>>> out = np.zeros(a.shape)
>>> np.sin(a, out=out, where=where)
array([[0.        , 0.        , 0.        , 0.        ],
       [0.35881205, 0.04690551, 0.59981618, 0.51865972],
       [0.59330093, 0.        , 0.6879784 , 0.71111629]])
>>> np.sin(a, where=where)
array([[0.        , 0.        , 0.        , 0.        ],
       [0.35881205, 0.04690551, 0.59981618, 0.51865972],
       [0.59330093, 0.        , 0.6879784 , 0.71111629]])
>>> where1 = np.random.uniform(size=(3, 4)) >= 0.5
>>> where1
array([[ True,  True, False,  True],
       [ True, False, False,  True],
       [ True,  True,  True, False]])
>>> np.sin(a, where=where1)
array([[0.59597809, 0.48786673, 0.        , 0.06527679],
       [0.35881205, 0.04690551, 0.59981618, 0.51865972],
       [0.59330093, 0.10031214, 0.6879784 , 0.71111629]])
>>> where1 + where
array([[ True,  True, False,  True],
       [ True,  True,  True,  True],
       [ True,  True,  True,  True]])

You can see that the results of the 2 np.sin(a, where=where) calls behave differently simply with an extra call with out specified. Obviously the function is writing to the out buffer even when that buffer is not given to the later function calls. Don't you think this is a bug?

haojin2 avatar Oct 08 '19 08:10 haojin2

Don't you think this is a bug?

No, what's happening is the memory is being reused since it has been freed. It would only be a bug if the value of out changed after a call that does not use it, which I highly doubt happens

eric-wieser avatar Oct 08 '19 09:10 eric-wieser

From the docs (emphasis mine):

out

If ‘out’ is None (the default), a uninitialized return array is created.

where

Accepts a boolean array which is broadcast together with the operands. Values of True indicate to calculate the ufunc at that position, values of False indicate to leave the value in the output alone. This argument cannot be used for generalized ufuncs as those take non-scalar input.

Note that if an uninitialized return array is created, values of False will leave those values uninitialized

.

eric-wieser avatar Oct 08 '19 09:10 eric-wieser

@eric-wieser

>>> import numpy as np
>>> a = np.random.uniform(size=(3, 4))
>>> where = np.random.uniform(size=(3, 4)) >= 0.5
>>> where
array([[ True, False,  True,  True],
       [ True,  True,  True, False],
       [ True, False, False,  True]])
>>> np.sin(a, where=where)
array([[0.05510498, 0.3862249 , 0.82762227, 0.31490047],
       [0.28448463, 0.18120873, 0.74460137, 0.21963396],
       [0.2456275 , 0.30610894, 0.00541544, 0.26743686]])

So whose uninitialized memory (which happens to contain perfect results to the sine function) is the output reusing in this case?

haojin2 avatar Oct 08 '19 09:10 haojin2

Yes, it's output reuse - in a fresh shell, I get clearly non-perfect results in the uninitialized entries:

In [2]: np.sin(a, where=where)
Out[2]:
array([[0.45490664, 0.39777741, 0.35792116, 0.29698393],
       [0.16088846, 0.2830777 , 0.4131211 , 0.48153693],
       [0.69889685, 0.38076401, 0.3028684 , 0.41491555]])

In [3]: np.sin(a)
Out[3]:
array([[0.29971976, 0.39777741, 0.35792116, 0.38027649],
       [0.16088846, 0.00961433, 0.12750123, 0.48153693],
       [0.69889685, 0.72903783, 0.3028684 , 0.41491555]])

eric-wieser avatar Oct 08 '19 09:10 eric-wieser

@eric-wieser Fair enough

haojin2 avatar Oct 08 '19 18:10 haojin2