You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@mxnet.apache.org by GitBox <gi...@apache.org> on 2020/10/03 20:48:28 UTC

[GitHub] [incubator-mxnet] szha opened a new issue #19286: infer shape error in hybridized block for zero-size input

szha opened a new issue #19286:
URL: https://github.com/apache/incubator-mxnet/issues/19286


   ## Description
   For a hybridized HybridBlock, the new forward interface throws an error for zero-size input
   
   ### Error Message
   ```
   MXNetError: MXNetError: Shape inconsistent, Provided = [2,4,0,128], inferred shape=[2,4,4,128]
   ```
   <details>
   <summary>Complete error</summary>
   ---> 17 model(np.zeros((2, 2, 4, 0, 128)))
   
   ~/mxnet/python/mxnet/util.py in _with_np_shape(*args, **kwargs)
       296         def _with_np_shape(*args, **kwargs):
       297             with np_shape(active=True):
   --> 298                 return func(*args, **kwargs)
       299         return _with_np_shape
       300     else:
   
   ~/mxnet/python/mxnet/util.py in _with_np_array(*args, **kwargs)
       480         def _with_np_array(*args, **kwargs):
       481             with np_array(active=True):
   --> 482                 return func(*args, **kwargs)
       483         return _with_np_array
       484     else:
   
   ~/mxnet/python/mxnet/gluon/block.py in __call__(self, x, *args)
      1427
      1428             with x.ctx:
   -> 1429                 return self._call_cached_op(x, *args)
      1430
      1431     def forward(self, x, *args):
   
   ~/mxnet/python/mxnet/util.py in _with_np_shape(*args, **kwargs)
       296         def _with_np_shape(*args, **kwargs):
       297             with np_shape(active=True):
   --> 298                 return func(*args, **kwargs)
       299         return _with_np_shape
       300     else:
   
   ~/mxnet/python/mxnet/util.py in _with_np_array(*args, **kwargs)
       480         def _with_np_array(*args, **kwargs):
       481             with np_array(active=True):
   --> 482                 return func(*args, **kwargs)
       483         return _with_np_array
       484     else:
   
   ~/mxnet/python/mxnet/gluon/block.py in _call_cached_op(self, *args)
      1096     def _call_cached_op(self, *args):
      1097         if self._cached_op is None:
   -> 1098             self._build_cache(*args)
      1099         assert self._cached_op, "Gluon failed to build the cache. " \
      1100                                 "This should never happen. " \
   
   ~/mxnet/python/mxnet/util.py in _with_np_shape(*args, **kwargs)
       296         def _with_np_shape(*args, **kwargs):
       297             with np_shape(active=True):
   --> 298                 return func(*args, **kwargs)
       299         return _with_np_shape
       300     else:
   
   ~/mxnet/python/mxnet/util.py in _with_np_array(*args, **kwargs)
       480         def _with_np_array(*args, **kwargs):
       481             with np_array(active=True):
   --> 482                 return func(*args, **kwargs)
       483         return _with_np_array
       484     else:
   
   ~/mxnet/python/mxnet/gluon/block.py in _build_cache(self, *args)
       993
       994     def _build_cache(self, *args):
   --> 995         data, out = self._get_graph(*args)
       996         data_names = {data.name: i for i, data in enumerate(data)}
       997         params = {p.var().name: p for p in self.collect_params().values()}
   
   ~/mxnet/python/mxnet/util.py in _with_np_shape(*args, **kwargs)
       296         def _with_np_shape(*args, **kwargs):
       297             with np_shape(active=True):
   --> 298                 return func(*args, **kwargs)
       299         return _with_np_shape
       300     else:
   
   ~/mxnet/python/mxnet/util.py in _with_np_array(*args, **kwargs)
       480         def _with_np_array(*args, **kwargs):
       481             with np_array(active=True):
   --> 482                 return func(*args, **kwargs)
       483         return _with_np_array
       484     else:
   
   ~/mxnet/python/mxnet/gluon/block.py in _get_graph(self, *args)
       989                 return self._get_graph_v1(*args)
       990             else:  # Gluon 2 based on deferred compute mode
   --> 991                 return self._get_graph_v2(*args)
       992         return self._cached_graph
       993
   
   ~/mxnet/python/mxnet/util.py in _with_np_shape(*args, **kwargs)
       296         def _with_np_shape(*args, **kwargs):
       297             with np_shape(active=True):
   --> 298                 return func(*args, **kwargs)
       299         return _with_np_shape
       300     else:
   
   ~/mxnet/python/mxnet/util.py in _with_np_array(*args, **kwargs)
       480         def _with_np_array(*args, **kwargs):
       481             with np_array(active=True):
   --> 482                 return func(*args, **kwargs)
       483         return _with_np_array
       484     else:
   
   ~/mxnet/python/mxnet/gluon/block.py in _get_graph_v2(self, *args)
       978             args = _regroup(flatten_args, self._in_format)
       979             with autograd.pause(), dc.context():
   --> 980                 out = super().__call__(*args)
       981             flatten_out, self._out_format = _flatten(out, "output")
       982             symbol_outputs = dc.get_symbol(flatten_out, sym_cls=type(symbol_inputs[0]))
   
   ~/mxnet/python/mxnet/gluon/block.py in __call__(self, *args)
       709             hook(self, args)
       710
   --> 711         out = self.forward(*args)
       712
       713         for hook in self._forward_hooks.values():
   
   ~/mxnet/python/mxnet/util.py in _with_np_shape(*args, **kwargs)
       296         def _with_np_shape(*args, **kwargs):
       297             with np_shape(active=True):
   --> 298                 return func(*args, **kwargs)
       299         return _with_np_shape
       300     else:
   
   ~/mxnet/python/mxnet/util.py in _with_np_array(*args, **kwargs)
       480         def _with_np_array(*args, **kwargs):
       481             with np_array(active=True):
   --> 482                 return func(*args, **kwargs)
       483         return _with_np_array
       484     else:
   
   <ipython-input-1-164fad6d3e22> in forward(self, x)
         8         super().__init__()
         9     def forward(self, x):
   ---> 10         return x[0]
        11
        12
   
   ~/mxnet/python/mxnet/numpy/multiarray.py in __getitem__(self, key)
       743                     'index {} is out of bounds for axis 0 with size {}'.format(
       744                         key, shape[0]))
   --> 745             return self._at(key)
       746         elif isinstance(key, py_slice):
       747             # Unlike numpy/_symbol.py, calls MXNDArraySlice64 writable memory
   
   ~/mxnet/python/mxnet/ndarray/ndarray.py in _at(self, idx)
      1423         else:
      1424             check_call(_LIB.MXNDArrayAt(
   -> 1425                 self.handle, ctypes.c_uint32(idx), ctypes.byref(handle)))
      1426         return self.__class__(handle=handle, writable=self.writable)
      1427
   
   ~/mxnet/python/mxnet/base.py in check_call(ret)
       244     """
       245     if ret != 0:
   --> 246         raise get_last_ffi_error()
       247
       248
   
   MXNetError: MXNetError: Shape inconsistent, Provided = [2,4,0,128], inferred shape=[2,4,4,128]
   </details>
   
   ## To Reproduce
   
   Run the following script
   
   ```
   from mxnet import np, npx, gluon, use_np
   
   npx.set_np()
   
   @use_np
   class TestModel(gluon.HybridBlock):
       def __init__(self):
           super().__init__()
       def forward(self, x):
           return x[0]
   
   
   model = TestModel()
   model.initialize()
   model.hybridize()
   
   model(np.zeros((2, 2, 4, 0, 128)))
   ```
   
   ## Environment
   
   <details>
   <summary>Environment Information</summary>
   
   ```
   ----------Python Info----------
   ('Version      :', '3.7.8')
   ('Compiler     :', 'GCC 4.2.1 Compatible Apple LLVM 11.0.3 (clang-1103.0.29.20) (-macos10.15-objc-')
   ('Build        :', ('default', 'Jun  5 2020 22:59:21'))
   ('Arch         :', ('64bit', ''))
   ------------Pip Info-----------
   No corresponding pip install for current python.
   ----------MXNet Info-----------
   No MXNet installed.
   ----------System Info----------
   ('Platform     :', 'Darwin-19.6.0-x86_64-i386-64bit')
   ('system       :', 'Darwin')
   ('node         :', 'a483e79ab3ab')
   ('release      :', '19.6.0')
   ('version      :', 'Darwin Kernel Version 19.6.0: Mon Aug 31 22:12:52 PDT 2020; root:xnu-6153.141.2~1/RELEASE_X86_64')
   ----------Hardware Info----------
   ('machine      :', 'x86_64')
   ('processor    :', 'i386')
   machdep.cpu.brand_string: Intel(R) Core(TM) i7-8569U CPU @ 2.80GHz
   machdep.cpu.features: FPU VME DE PSE TSC MSR PAE MCE CX8 APIC SEP MTRR PGE MCA CMOV PAT PSE36 CLFSH DS ACPI MMX FXSR SSE SSE2 SS HTT TM PBE SSE3 PCLMULQDQ DTES64 MON DSCPL VMX EST TM2 SSSE3 FMA CX16 TPR PDCM SSE4.1 SSE4.2 x2APIC MOVBE POPCNT AES PCID XSAVE OSXSAVE SEGLIM64 TSCTMR AVX1.0 RDRAND F16C
   machdep.cpu.leaf7_features: RDWRFSGS TSC_THREAD_OFFSET SGX BMI1 AVX2 SMEP BMI2 ERMS INVPCID FPU_CSDS MPX RDSEED ADX SMAP CLFSOPT IPT MDCLEAR TSXFA IBRS STIBP L1DF SSBD
   machdep.cpu.extfeatures: SYSCALL XD 1GBPAGE EM64T LAHF LZCNT PREFETCHW RDTSCP TSCI
   ----------Network Test----------
   Setting timeout: 10
   Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0040 sec, LOAD: 2.5430 sec.
   Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.6464 sec, LOAD: 9.0609 sec.
   Timing for FashionMNIST: https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz, DNS: 0.0006 sec, LOAD: 2.1972 sec.
   Error open Conda: https://repo.continuum.io/pkgs/free/, HTTP Error 403: Forbidden, DNS finished in 0.000483989715576 sec.
   Timing for Gluon Tutorial(en): http://gluon.mxnet.io, DNS: 0.0009 sec, LOAD: 2.8254 sec.
   Error open Gluon Tutorial(cn): https://zh.gluon.ai, <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:727)>, DNS finished in 0.00119805335999 sec.
   ----------Environment----------
   CC="/usr/local/opt/llvm/bin/clang"
   CXX="/usr/local/opt/llvm/bin/clang++"
   ```
   
   </details>


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@mxnet.apache.org
For additional commands, e-mail: issues-help@mxnet.apache.org


[GitHub] [incubator-mxnet] szha closed issue #19286: infer shape error in hybridized block for zero-size input

Posted by GitBox <gi...@apache.org>.
szha closed issue #19286:
URL: https://github.com/apache/incubator-mxnet/issues/19286


   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@mxnet.apache.org
For additional commands, e-mail: issues-help@mxnet.apache.org


[GitHub] [incubator-mxnet] leezu commented on issue #19286: infer shape error in hybridized block for zero-size input

Posted by GitBox <gi...@apache.org>.
leezu commented on issue #19286:
URL: https://github.com/apache/incubator-mxnet/issues/19286#issuecomment-703816274


   This bug is related to legacy (non-numpy) reshape operator. Reshape with `0` implies "copy this dimension from input".
   
   We can see that all shapes are correct prior to infer_shape pass:
   
   ```
   frame #6: 0x00007fff3bd1cb8b libmxnet.so`mxnet::imperative::SetShapeType(ctx=0x00007fffffff8f18, attrs=0x00007fffffff97a8, inputs=size=1, outputs=size=1, dispatch_mode=0x00007fffffff8f24) at imperative_utils.h:208:26
      205        common::ConvertToNumpyShape(&in_shapes);
      206        common::ConvertToNumpyShape(&out_shapes);
      207      }
   -> 208      const bool success = infershape[attrs.op](attrs, &in_shapes, &out_shapes);
      209      if (!success) {
      210        std::stringstream os;
      211        os << "Operator " << attrs.op->name << " inferring shapes failed.\n";
   (lldb) parray 5 in_shapes[0].data_heap_
   (long *) $15 = 0x0000555555f45700 {
     (long) [0] = 1
     (long) [1] = 2
     (long) [2] = 4
     (long) [3] = 0
     (long) [4] = 128
   }
   (lldb) p out_shapes                                                                                                                                                                    (mxnet::ShapeVector) $16 = size=1 {
     [0] = {
       mxnet::Tuple<long> = {
         ndim_ = 4
         num_heap_allocated_ = 0
         data_stack_ = ([0] = 2, [1] = 4, [2] = 0, [3] = 128)
         data_heap_ = 0x0000000000000000
       }
     }
   }
   ```
   
   But after infer_shape, `0` is replaced by `4`.
   
   ```
   frame #1: 0x00007fff4968c1be libmxnet.so`mxnet::op::ReshapeShape(attrs=0x00007fffffff97a8, in_attrs=0x00005555569cbb68, out_attrs=0x00005555569cbb80) at matrix_op-inl.h:235:3
      232      << "Target: " << oshape
      233      << "\nSource: " << dshape;
      234  #endif
   -> 235    SHAPE_ASSIGN_CHECK(*out_attrs, 0, oshape);
      236    return ReverseReshapeInferShape(&(*in_attrs)[0], (*out_attrs)[0]);
      237  }
      238
   (lldb) p oshape
   (mxnet::TShape) $17 = {
     mxnet::Tuple<long> = {
       ndim_ = 4
       num_heap_allocated_ = 0
       data_stack_ = ([0] = 2, [1] = 4, [2] = 4, [3] = 128)
       data_heap_ = 0x0000000000000000
     }
   }
   ```
   
   https://github.com/apache/incubator-mxnet/blob/f732530c8e1f8dcc11134e935430e3793ddbf4c8/src/operator/tensor/matrix_op-inl.h#L194-L237
   
   The root cause is that `MXNDArrayAt`, `MXNDArrayReshape` and `MXNDArraySlice` do not sufficiently distinguish between the numpy and non-numpy mode and always record legacy operators.
   
   We need to update the recording step to record numpy / legacy operator based on if numpy / legacy mode is enabled:
   
    https://github.com/apache/incubator-mxnet/blob/72eff9b66ecc683c3e7f9ad2c0ba69efa8dd423b/src/ndarray/ndarray.cc#L302-L308 


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@mxnet.apache.org
For additional commands, e-mail: issues-help@mxnet.apache.org