-
Notifications
You must be signed in to change notification settings - Fork 6.8k
mxnet.numpy and numpy differ regarding binary op result type with broadcast 0-dim array input #20898
Comments
In general we want to conform to the array API standard. While fp16 is not in the array standard yet, I tried the same with ints and the same discrepancy exists.
According to the type promotion rules in array API standard, mxnet's current behavior is desired.
@rgommers do you expect this to become a problem for API standard adoption on the numpy side? |
Type promotion in NumPy has a lot of weird corner cases. Right now there's an effort to get rid of those, including value-based casting. A draft NEP (Numpy Enhancement Proposal) was just posted: https://mail.python.org/archives/list/numpy-discussion@python.org/thread/RWOZYX2LN66T243C6II2BCXRWCPM3CV2/. Type casting issues are the most important reason for why we added array API support as a separate namespace ( Many of the issues like the 0-D array special-casing brought up here continue to yield bug reports in NumPy itself and downstream libraries, and are design problems that shouldn't be emulated but needed fixing already before we designed the array API standard. |
To clarify, let me also say: For MXNet I think it makes sense to not emulate the separate |
@DickJC123 to sum up this should be a bug in numpy and they need to fix the type promotion. We can use the array API namespace for comparison in tests. |
Description
I discovered this issue when troubleshooting a unittest failure of ./tests/python/unittest/test_numpy_op.py::test_np_standard_binary_funcs. The command-line repro is given in the
steps to reproduce
section below. The test involves adding 2 operands, one with dtype float16, the other of dtype float64.The standard numpy (
onp
below) has a somewhat strange behavior when one of the inputs is broadcast: if the broadcast input is a 0-dim array comprising a single scalar, then the output dtype will be the dtype of the other input, and the scalar is cast to that other input's precision. However, if the broadcast input is a 1-element array of shape (1,), then the output has the dtype of the most precise input, and the least precise input is cast-up to that precision.MXNet's numpy does not match this behavior: in both cases the output is cast-up to the highest precision.
See the sequence below. The lines
a_onp + b_onp
anda_np + b_np
match, showing the same behavior when broadcasting a 1-element array. However, the linesa2_onp + b_onp
anda2_np + b_np
do not match, showing a different behavior when broadcasting a 0-dim array:Steps to reproduce
Error Message
Partial output:
Environment
Ubuntu 20.04. Recent master build. Failure of cpu test, so GPU config not relevant.
The text was updated successfully, but these errors were encountered: