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

nd.stack causes abort for differently sized arrays #9380

Closed
aodhan-domhnaill opened this issue Jan 11, 2018 · 4 comments · Fixed by #9681
Closed

nd.stack causes abort for differently sized arrays #9380

aodhan-domhnaill opened this issue Jan 11, 2018 · 4 comments · Fixed by #9681

Comments

@aodhan-domhnaill
Copy link

$ pip list | grep mxnet
mxnet (1.0.0.post1)
dca90487275d:~ macaidan$ python
Python 3.6.3 (default, Oct  4 2017, 06:09:15) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from mxnet import nd
>>> a = nd.arange(20).reshape((10, 2))
>>> b = nd.arange(10).reshape((10, 1))
>>> nd.stack(a, b)
[12:09:05] /Users/travis/build/dmlc/mxnet-distro/mxnet-build/dmlc-core/include/dmlc/logging.h:308: [12:09:05] include/mxnet/./tensor_blob.h:276: Check failed: this->shape_.Size() == shape.Size() (10 vs. 20) TBlob.get_with_shape: new and old shape do not match total elements

full output

This causes the Python environment to exit. A simple non-fatal error message would suffice in this case.

@eric-haibin-lin
Copy link
Member

@piiswrong in this case is the expected result with shape (10,3)?

@aodhan-domhnaill
Copy link
Author

aodhan-domhnaill commented Jan 18, 2018

@eric-haibin-lin So this wasn't so much about it doing the right thing as simply not killing Python. This error shouldn't be fatal. Note Numpy's behavior,

$ python
Python 3.6.3 (default, Oct  4 2017, 06:09:15) 
[GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.37)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy as np
>>> a = np.arange(20).reshape((20, 2))
>>> a = np.arange(20).reshape((10, 2))
>>> b = np.arange(10).reshape((10, 1))
>>> np.stack([a, b])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.6/site-packages/numpy/core/shape_base.py", line 354, in stack
    raise ValueError('all input arrays must have the same shape')
ValueError: all input arrays must have the same shape
>>> np.hstack([a, b])
array([[ 0,  1,  0],
       [ 2,  3,  1],
       [ 4,  5,  2],
       [ 6,  7,  3],
       [ 8,  9,  4],
       [10, 11,  5],
       [12, 13,  6],
       [14, 15,  7],
       [16, 17,  8],
       [18, 19,  9]])

Numpy doesn't kill the session when it throws the error. But beyond that, MXNet lacks an nd.hstack function, so it would be nice if nd.stack could handle stacking of different shapes.

For example, pseudocode outlining the shape assertions.

def stack(A, B, axis=0):
    for i, (s0, s1) in enumerate(zip(A.shape, B.shape)):
        if axis != i:
            assert A.shape[i] == B.shape[i]
    # Actually do the stacking
    C = actual_stacking(A, B, axis=axis)
    for i, (s0, s1) in enumerate(zip(A.shape, B.shape, C.shape)):
        if axis != i:
            assert A.shape[i] == B.shape[i]
            assert A.shape[i] == C.shape[i]
       else:
            assert C.shape[i] == A.shape[i] + B.shape[i]
    return C

@eric-haibin-lin
Copy link
Member

I see your point. @anirudh2290 is actively working on better support for exception handling in MXNet. He will probably post a design doc for this soon.

@anirudh2290
Copy link
Member

Related to: #7335

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants