Skip to content
This repository has been archived by the owner on Nov 17, 2023. It is now read-only.

[Bug][Numpy] Very basic functionality of Gluon is broken #18193

Closed
sxjscience opened this issue Apr 29, 2020 · 10 comments
Closed

[Bug][Numpy] Very basic functionality of Gluon is broken #18193

sxjscience opened this issue Apr 29, 2020 · 10 comments

Comments

@sxjscience
Copy link
Member

sxjscience commented Apr 29, 2020

After #17283, the following very basic example of gluon is broken:

import mxnet as mx
mx.npx.set_np()
from mxnet.gluon import nn
net = nn.Dense(16, in_units=16)
net.initialize()
out = net(mx.np.ones((32, 16)))
out.asnumpy()
~/mxnet/python/mxnet/numpy/random.py in uniform(low, high, size, dtype, ctx, out)
    135     inequality condition.
    136     """
--> 137     return _mx_nd_np.random.uniform(low, high, size=size, ctx=ctx, dtype=dtype, out=out)
    138 
    139 

~/mxnet/python/mxnet/ndarray/numpy/random.py in uniform(low, high, size, dtype, ctx, out)
    135     if size == ():
    136         size = None
--> 137     return _api_internal.uniform(low, high, size, ctx, dtype, out)
    138 
    139 

~/mxnet/python/mxnet/_ffi/_ctypes/function.py in __call__(self, *args)
    113                 self.handle, values, tcodes, ctypes.c_int(num_args),
    114                 ctypes.byref(ret_val), ctypes.byref(ret_tcode)) != 0:
--> 115             raise get_last_ffi_error()
    116         _ = temp_args
    117         _ = args

MXNetError: Traceback (most recent call last):
  File "../src/imperative/./imperative_utils.h", line 251
MXNetError: Check failed: outputs[i]->dtype() == out_types[i] (0 vs. 1) : 0-th output has invalid dtype. Expecting 1 got 0 in operator _npi_uniform
@sxjscience sxjscience changed the title [Bug][Numpy] Very basic functionality of Gluon is broken after https://github.com/apache/incubator-mxnet/pull/17283 [Bug][Numpy] Very basic functionality of Gluon is broken Apr 29, 2020
@sxjscience
Copy link
Member Author

@yzhliu @JiangZhaoh @szha

@yzhliu
Copy link
Member

yzhliu commented Apr 29, 2020

Assignee: @JiangZhaoh

@JiangZhaoh
Copy link
Contributor

Sorry for that. I'll fix it as soon as possible.

@szha
Copy link
Member

szha commented Apr 30, 2020

which tests were supposed to catch this issue?

@sxjscience
Copy link
Member Author

sxjscience commented Apr 30, 2020

@yzhliu yzhliu added the WIP label Apr 30, 2020
@JiangZhaoh
Copy link
Contributor

That's my issue. In order to not effect original tests, I didn't change the decorator "use_np", which still use return use_np_shape(use_np_array(func)). But in npx.set_np() I default flag dtype as True and then set_np_default_dtype(dtype).
Details in /mxnet/python/mxnet/util.py

@sxjscience
Copy link
Member Author

OK, so this is not related to CI.

@JiangZhaoh
Copy link
Contributor

JiangZhaoh commented Apr 30, 2020

I have found the reason. It because set_np() only change the mxnet.numpy functions to default dtype as float64, but doesn't effect mxnet.ndarray functions which still default float32.
The issue mentioned above just like this code:

>>> import mxnet
>>> from mxnet import numpy as np
>>> from mxnet import ndarray as nd
>>> from mxnet import npx
>>> npx.set_np()
>>> a = nd.zeros(5)
>>> a.dtype
<class 'numpy.float32'>
>>> np.random.uniform(low = -1, high = 1, size = (5), out = a)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/mxnet/python/mxnet/numpy/random.py", line 137, in uniform
    return _mx_nd_np.random.uniform(low, high, size=size, ctx=ctx, dtype=dtype, out=out)
  File "/home/ubuntu/mxnet/python/mxnet/ndarray/numpy/random.py", line 145, in uniform
    return _api_internal.uniform(low, high, size, ctx, dtype, out)
  File "mxnet/_ffi/_cython/./function.pxi", line 188, in mxnet._ffi._cy3.core.FunctionBase.__call__
  File "mxnet/_ffi/_cython/./function.pxi", line 133, in mxnet._ffi._cy3.core.FuncCall
  File "mxnet/_ffi/_cython/./base.pxi", line 90, in mxnet._ffi._cy3.core.CALL
mxnet.base.MXNetError: Traceback (most recent call last):
  File "../src/imperative/./imperative_utils.h", line 251
MXNetError: Check failed: outputs[i]->dtype() == out_types[i] (0 vs. 1) : 0-th output has invalid dtype. Expecting 1 got 0 in operator _npi_uniform
>>> npx.set_np(dtype = False)
>>> np.random.uniform(low = -1, high = 1, size = (5), out = a)
[0.09762704 0.18568921 0.43037868 0.6885315  0.20552671]
<NDArray 5 @cpu(0)>

So, I think I'd better not to set dtype=True flag in npx.set_np(), seperate this flag along in a single function npx.set_numpy_default_dtype() instead. Or if there any other better solutions, please tell me.

@sxjscience
Copy link
Member Author

sxjscience commented Apr 30, 2020 via email

This was referenced Apr 30, 2020
@yzhliu
Copy link
Member

yzhliu commented May 20, 2020

should be resolved by #18200

@yzhliu yzhliu closed this as completed May 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants