Skip to content
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

numba doesn't raise exception for broadcast error with += #4542

Open
asmeurer opened this issue Sep 9, 2019 · 2 comments
Open

numba doesn't raise exception for broadcast error with += #4542

asmeurer opened this issue Sep 9, 2019 · 2 comments
Labels
bug - incorrect behavior Bugs: incorrect behavior

Comments

@asmeurer
Copy link
Contributor

asmeurer commented Sep 9, 2019

Originally from #4432 (comment)

>>> def test1(a, b):
...     a += b
>>> def test2(a, b):
...     return a + b
>>> @njit
... def test1_jit(a, b):
...     a += b
>>> @njit
... def test2_jit(a, b):
...     return a + b
>>> test1(np.empty((3, 4)), np.empty((3, 5)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test1
ValueError: operands could not be broadcast together with shapes (3,4) (3,5) (3,4)
>>> test2(np.empty((3, 4)), np.empty((3, 5)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<stdin>", line 2, in test2
ValueError: operands could not be broadcast together with shapes (3,4) (3,5)
>>> test1_jit(np.empty((3, 4)), np.empty((3, 5)))
>>> test2_jit(np.empty((3, 4)), np.empty((3, 5)))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: unable to broadcast argument 1 to output array
File "<stdin>", line 3,

The issue is test1_jit, which should raise an exception.

As an aside, the error line for test2_jit is messed up somehow.

@esc
Copy link
Member

esc commented Sep 10, 2019

@asmeurer thanks for reporting this. I can reproduce this myself and will now label this as 'bug'.

Also, @stuartarchibald here is the minimal reproducer for your convenience:

import numpy as np
from numba import njit


@njit
def test1(a, b):
    a += b


@njit
def test2(a, b):
    return a + b


c = np.empty((3, 4))
d = np.empty((3, 5))

try:
    test1.py_func(c, d)
except Exception as e:
    print(e)
else:
    print("NO EXCEPTION")

try:
    test2.py_func(c, d)
except Exception as e:
    print(e)
else:
    print("NO EXCEPTION")


try:
    test1(c, d)
except Exception as e:
    print(e)
else:
    print("NO EXCEPTION")


try:
    test2(c, d)
except Exception as e:
    print(e)
else:
    print("NO EXCEPTION")

@esc esc added the bug label Sep 10, 2019
@stuartarchibald
Copy link
Contributor

I think that this is a variant of the actual problem which is that an explicitly supplied output array to a ufunc is not checked for suitability (infix operators are rewritten to look like function calls with explicit output):

import numpy as np
from numba import njit

@njit
def test1(a, b, c):
    np.add(a, b, c)

c = np.ones((3, 4))
d = np.ones((3, 4))
f = np.zeros((3, 5))

try:
    test1(c, d, f)
except Exception as e:
    print(e)
else:
    print("NO EXCEPTION")
    print(f)

gives:

NO EXCEPTION
[[2.00000000e+000 2.00000000e+000 2.00000000e+000 2.00000000e+000
  2.00000000e+000]
 [2.00000000e+000 2.00000000e+000 2.00000000e+000 2.00000000e+000
  2.00000000e+000]
 [2.00000000e+000 2.00000000e+000 2.00000000e+000 2.00000000e+000
  1.38348601e-309]]

@stuartarchibald stuartarchibald added bug - incorrect behavior Bugs: incorrect behavior and removed bug labels Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug - incorrect behavior Bugs: incorrect behavior
Projects
None yet
Development

No branches or pull requests

3 participants