You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mxnet.apache.org by GitBox <gi...@apache.org> on 2018/08/06 09:00:59 UTC

[GitHub] khaotik opened a new issue #12045: create a custom operator with scalar parameters in python

khaotik opened a new issue #12045: create a custom operator with scalar parameters in python
URL: https://github.com/apache/incubator-mxnet/issues/12045
 
 
   ## Description
   I tried to create a custom Op in python (e.g. `k` in `topk`) with a scalar parameter. Mostly followed #5580 but still got error.
   
   ## Environment info (Required)
   
   ```
   ----------Python Info----------
   Version      : 3.6.6
   Compiler     : GCC 7.2.0
   Build        : ('default', 'Jun 28 2018 17:14:51')
   Arch         : ('64bit', '')
   ------------Pip Info-----------
   Version      : 18.0
   Directory    : /home/khaotik/anaconda3/lib/python3.6/site-packages/pip
   ----------MXNet Info-----------
   Version      : 1.2.1
   Directory    : /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet
   Commit Hash   : 106391a1f0ee012b1ea38764d711e76774ce77e1
   ----------System Info----------
   Platform     : Linux-4.15.0-29-generic-x86_64-with-debian-buster-sid
   system       : Linux
   node         : KKST
   release      : 4.15.0-29-generic
   version      : #31-Ubuntu SMP Tue Jul 17 15:39:52 UTC 2018
   ----------Hardware Info----------
   machine      : x86_64
   processor    : x86_64
   Architecture:        x86_64
   CPU op-mode(s):      32-bit, 64-bit
   Byte Order:          Little Endian
   CPU(s):              4
   On-line CPU(s) list: 0-3
   Thread(s) per core:  1
   Core(s) per socket:  4
   Socket(s):           1
   NUMA node(s):        1
   Vendor ID:           GenuineIntel
   CPU family:          6
   Model:               42
   Model name:          Intel(R) Core(TM) i5-2400 CPU @ 3.10GHz
   Stepping:            7
   CPU MHz:             2027.394
   CPU max MHz:         3400.0000
   CPU min MHz:         1600.0000
   BogoMIPS:            6186.07
   Virtualization:      VT-x
   L1d cache:           32K
   L1i cache:           32K
   L2 cache:            256K
   L3 cache:            6144K
   NUMA node0 CPU(s):   0-3
   Flags:               fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic popcnt tsc_deadline_timer aes xsave avx lahf_lm epb pti ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid xsaveopt dtherm ida arat pln pts
   ----------Network Test----------
   Setting timeout: 10
   Timing for MXNet: https://github.com/apache/incubator-mxnet, DNS: 0.0875 sec, LOAD: 2.6200 sec.
   Timing for Gluon Tutorial(en): http://gluon.mxnet.io, DNS: 0.0636 sec, LOAD: 5.9708 sec.
   Timing for Gluon Tutorial(cn): https://zh.gluon.ai, DNS: 1.7244 sec, LOAD: 3.9858 sec.
   Timing for FashionMNIST: https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/gluon/dataset/fashion-mnist/train-labels-idx1-ubyte.gz, DNS: 0.3269 sec, LOAD: 1.2278 sec.
   Timing for PYPI: https://pypi.python.org/pypi/pip, DNS: 0.2559 sec, LOAD: 11.0024 sec.
   Timing for Conda: https://repo.continuum.io/pkgs/free/, DNS: 0.1979 sec, LOAD: 1.7397 sec.
   ```
   
   Package used (Python/R/Scala/Julia):
   
   Installed via `pip install mxnet-cu92mkl`, python version `3.6.6 (anaconda)`
   
   ## Error Message:
   
   ```
   Error in softmax.infer_shape: Traceback (most recent call last):
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/operator.py", line 744, in infer_shape_entry
       "shapes, got %d."%(n_in, len(ishape))
   AssertionError: InferShape Error: expecting 4 entries in returned input shapes, got 2.
   
   Traceback (most recent call last):
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1513, in simple_bind
       ctypes.byref(exe_handle)))
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/base.py", line 149, in check_call
       raise MXNetError(py_str(_LIB.MXGetLastError()))
   mxnet.base.MXNetError: Error in operator softmax: [08:30:06] src/operator/custom/custom.cc:153: Check failed: reinterpret_cast<CustomOpInferShapeFunc>( params.info->callbacks[kCustomOpPropInferShape])( shapes.size(), ndims.data(), shapes.data(), params.info->contexts[kCustomOpPropInferShape]) 
   
   Stack trace returned 10 entries:
   [bt] (0) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d70c2) [0x7fd0168d50c2]
   [bt] (1) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d7688) [0x7fd0168d5688]
   [bt] (2) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x4912ef) [0x7fd016a8f2ef]
   [bt] (3) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x295caff) [0x7fd018f5aaff]
   [bt] (4) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x295f5ef) [0x7fd018f5d5ef]
   [bt] (5) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294b3d9) [0x7fd018f493d9]
   [bt] (6) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294be94) [0x7fd018f49e94]
   [bt] (7) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(MXExecutorSimpleBind+0x2260) [0x7fd018eae0e0]
   [bt] (8) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call_unix64+0x4c) [0x7fd04ad73ec0]
   [bt] (9) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call+0x22d) [0x7fd04ad7387d]
   
   
   
   During handling of the above exception, another exception occurred:
   
   Traceback (most recent call last):
     File "mxtest.py", line 59, in <module>
       for_training=False
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/module.py", line 430, in bind
       state_names=self._state_names)
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/executor_group.py", line 265, in __init__
       self.bind_exec(data_shapes, label_shapes, shared_group)
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/executor_group.py", line 361, in bind_exec
       shared_group))
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/executor_group.py", line 639, in _bind_ith_exec
       shared_buffer=shared_data_arrays, **input_shapes)
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1519, in simple_bind
       raise RuntimeError(error_msg)
   RuntimeError: simple_bind error. Arguments:
   x: (4, 4)
   y: (4,)
   Error in operator softmax: [08:30:06] src/operator/custom/custom.cc:153: Check failed: reinterpret_cast<CustomOpInferShapeFunc>( params.info->callbacks[kCustomOpPropInferShape])( shapes.size(), ndims.data(), shapes.data(), params.info->contexts[kCustomOpPropInferShape]) 
   
   Stack trace returned 10 entries:
   [bt] (0) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d70c2) [0x7fd0168d50c2]
   [bt] (1) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d7688) [0x7fd0168d5688]
   [bt] (2) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x4912ef) [0x7fd016a8f2ef]
   [bt] (3) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x295caff) [0x7fd018f5aaff]
   [bt] (4) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x295f5ef) [0x7fd018f5d5ef]
   [bt] (5) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294b3d9) [0x7fd018f493d9]
   [bt] (6) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294be94) [0x7fd018f49e94]
   [bt] (7) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(MXExecutorSimpleBind+0x2260) [0x7fd018eae0e0]
   [bt] (8) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call_unix64+0x4c) [0x7fd04ad73ec0]
   [bt] (9) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call+0x22d) [0x7fd04ad7387d]
   
   ```
   
   ## Minimum reproducible example
   
   Mostly copied code by @saicoco in #5580
   
   ```python
   import numpy as np
   import mxnet as mx
   import mxnet.symbol as ms
   
   class Softmax(mx.operator.CustomOp):
   
       def __init__(self, xxx, yyy):
           self.xxx = xxx
           self.yyy = yyy
       def forward(self, is_train, req, in_data, out_data, aux):
           x = in_data[0].asnumpy()
           y = np.exp(x - x.max(axis=1).reshape((x.shape[0], 1)))
           y /= y.sum(axis=1).reshape((x.shape[0], 1))
           print(self.xxx, self.yyy)
           self.assign(out_data[0], req[0], mx.nd.array(y))
   
       def backward(self, req, out_grad, in_data, out_data, in_grad, aux):
           l = in_data[1].asnumpy().ravel().astype(np.int)
           y = out_data[0].asnumpy()
           y[np.arange(l.shape[0]), l] -= 1.0
           self.assign(in_grad[0], req[0], mx.nd.array(y))
   
   @mx.operator.register("softmax")
   class SoftmaxProp(mx.operator.CustomOpProp):
       def __init__(self, xxx, yyy):
           super(SoftmaxProp, self).__init__(need_top_grad=False)
   
           # add parameter
           self.xxx = xxx
           self.yyy = yyy
       def list_arguments(self):
           return ['data', 'label', 'xxx', 'yyy']
   
       def list_outputs(self):
           return ['output']
   
       def infer_shape(self, in_shape):
           data_shape = in_shape[0]
           label_shape = (in_shape[0][0],)
           output_shape = in_shape[0]
           return [data_shape, label_shape], [output_shape], []
   
       def create_operator(self, ctx, shapes, dtypes):
           return Softmax(xxx=self.xxx, yyy=self.yyy)
   
   s_x = ms.Variable('x', shape=(4,4))
   s_y = ms.Variable('y', shape=(4,))
   s_loss = ms.Custom(data=s_x, label=s_y, xxx=1, yyy=2, name='softmax', op_type='softmax')
   
   top_module = mx.mod.Module(
       symbol=s_loss,
       context=mx.cpu(),
       data_names=['x'],
       label_names=['y']
   )
   top_module.bind(
       data_shapes=list(dict(x=(4,4)).items()),
       label_shapes=list(dict(y=(4,)).items()),
       for_training=False
   )
   ```
   
   ## What have you tried to solve it?
   
   The code above ignores shapes of `xxx, yyy` in `infer_shape`. If I do provide complete shape info in `infer_shape` (note they are scalars):
   
   ```python
   ...
       def infer_shape(self, in_shape):
           data_shape = in_shape[0]
           label_shape = (in_shape[0][0],)
           output_shape = in_shape[0]
           return [data_shape, label_shape, (), ()], [output_shape], []
   ...
   ```
   
   Now I have another error:
   
   ```
   Traceback (most recent call last):
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1513, in simple_bind
       ctypes.byref(exe_handle)))
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/base.py", line 149, in check_call
       raise MXNetError(py_str(_LIB.MXGetLastError()))
   mxnet.base.MXNetError: [08:37:25] src/executor/graph_executor.cc:456: InferShape pass cannot decide shapes for the following arguments (0s means unknown dimensions). Please consider providing them as inputs:
   softmax_xxx: [], softmax_yyy: [], 
   
   Stack trace returned 10 entries:
   [bt] (0) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d70c2) [0x7f66e57ea0c2]
   [bt] (1) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d7688) [0x7f66e57ea688]
   [bt] (2) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x292aaa1) [0x7f66e7e3daa1]
   [bt] (3) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294b49c) [0x7f66e7e5e49c]
   [bt] (4) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294be94) [0x7f66e7e5ee94]
   [bt] (5) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(MXExecutorSimpleBind+0x2260) [0x7f66e7dc30e0]
   [bt] (6) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call_unix64+0x4c) [0x7f6719c88ec0]
   [bt] (7) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call+0x22d) [0x7f6719c8887d]
   [bt] (8) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(_ctypes_callproc+0x2ce) [0x7f6719e9ddae]
   [bt] (9) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(+0x127e5) [0x7f6719e9e7e5]
   
   
   
   During handling of the above exception, another exception occurred:
   
   Traceback (most recent call last):
     File "mxtest.py", line 59, in <module>
       for_training=False
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/module.py", line 430, in bind
       state_names=self._state_names)
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/executor_group.py", line 265, in __init__
       self.bind_exec(data_shapes, label_shapes, shared_group)
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/executor_group.py", line 361, in bind_exec
       shared_group))
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/module/executor_group.py", line 639, in _bind_ith_exec
       shared_buffer=shared_data_arrays, **input_shapes)
     File "/home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1519, in simple_bind
       raise RuntimeError(error_msg)
   RuntimeError: simple_bind error. Arguments:
   x: (4, 4)
   y: (4,)
   [08:37:25] src/executor/graph_executor.cc:456: InferShape pass cannot decide shapes for the following arguments (0s means unknown dimensions). Please consider providing them as inputs:
   softmax_xxx: [], softmax_yyy: [], 
   
   Stack trace returned 10 entries:
   [bt] (0) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d70c2) [0x7f66e57ea0c2]
   [bt] (1) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x2d7688) [0x7f66e57ea688]
   [bt] (2) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x292aaa1) [0x7f66e7e3daa1]
   [bt] (3) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294b49c) [0x7f66e7e5e49c]
   [bt] (4) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x294be94) [0x7f66e7e5ee94]
   [bt] (5) /home/khaotik/anaconda3/lib/python3.6/site-packages/mxnet/libmxnet.so(MXExecutorSimpleBind+0x2260) [0x7f66e7dc30e0]
   [bt] (6) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call_unix64+0x4c) [0x7f6719c88ec0]
   [bt] (7) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/../../libffi.so.6(ffi_call+0x22d) [0x7f6719c8887d]
   [bt] (8) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(_ctypes_callproc+0x2ce) [0x7f6719e9ddae]
   [bt] (9) /home/khaotik/anaconda3/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(+0x127e5) [0x7f6719e9e7e5]
   
   ```
   
   What's the right way to pass a "template parameter" to custom Operator without having to put them inside compute graph? (hopefully I don't have dig into C++/CUDA code)

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on 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


With regards,
Apache Git Services