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

Unknown shape in flatten op. #18165

Open
roywei opened this issue Apr 25, 2020 · 2 comments
Open

Unknown shape in flatten op. #18165

roywei opened this issue Apr 25, 2020 · 2 comments
Labels

Comments

@roywei
Copy link
Member

roywei commented Apr 25, 2020

Right now there is no way to represent unknown shape in MXNet

According to #14253, now it use -1 instead of 0 as unknown dim. This should only take effect if mx.npx.set_np() is used.

I have the following use case when I want to bind a shape with unknown batch size, and feed different batch size data during forward.

import mxnet as mx
a = mx.sym.Variable("a")
b = mx.sym.Flatten(a)
exec = b.simple_bind(ctx=mx.cpu(), a=(0,10,2))

if give the following error:

Traceback (most recent call last):
  File "//anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py", line 1780, in simple_bind
    ctypes.byref(exe_handle)))
  File "//anaconda3/lib/python3.7/site-packages/mxnet/base.py", line 246, in check_call
    raise get_last_ffi_error()
mxnet.base.MXNetError: Traceback (most recent call last):
  File "src/executor/../common/exec_utils.h", line 391
MXNetError: InferShape pass cannot decide shapes for the following arguments (-1 means unknown dimensions). Please consider providing them as inputs:
a: [-1,10,2],

But if I use -1, it also fails no matter if set_np() is used.

>>> exec = b.simple_bind(ctx=mx.cpu(), a=(-1, 10,2))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "//anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py", line 1758, in simple_bind
    array('I', provided_arg_shape_data)),
OverflowError: can't convert negative value to unsigned int
@roywei roywei added the Bug label Apr 25, 2020
@apeforest
Copy link
Contributor

apeforest commented May 31, 2020

It works with the np_shape API. @reminisce @haojin2 Is this by design that requires the np_shape API for backward compatibility? Thanks.

import mxnet as mx
with mx.np_shape(active=True):
    a = mx.sym.Variable("a")
    b = mx.sym.Flatten(a)
    exec = b.simple_bind(ctx=mx.cpu(), a=(0,10,2))

@roywei
Copy link
Member Author

roywei commented Jul 2, 2020

I think it still fails for unknow shape case. The above code works fine for 0 dim, but use -1 to represent unknown shape still fails:

>>> with mx.np_shape(active=True):
...     a = mx.sym.Variable("a")
...     b = mx.sym.Flatten(a)
...     exec = b.simple_bind(ctx=mx.cpu(), a=(-1,10,2))
...     exec.forward(a=mx.nd.ones((32,10,2)))
...
Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "//anaconda3/lib/python3.7/site-packages/mxnet/symbol/symbol.py", line 1754, in simple_bind
    array('I', provided_arg_shape_data)),
OverflowError: can't convert negative value to unsigned int

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

No branches or pull requests

2 participants