hpi-xnor / BMXNet

(New version is out: https://github.com/hpi-xnor/BMXNet-v2) BMXNet: An Open-Source Binary Neural Network Implementation Based on MXNet
Apache License 2.0
349 stars 95 forks source link

support for ndarray api #26

Closed analog-cbarber closed 6 years ago

analog-cbarber commented 6 years ago

Description

Support new Q* operators in mxnet.ndarray module.

Details

The new operators are visible in the mxnet.ndarray module but when you try to invoke the input argument is not handled correctly. For example:

>>> import mxnet as mx
>>> x = mx.random.normal(shape=(1,3))
>>> y = mx.ndarray.QActivation(x, act_bit=1)
Traceback (most recent call last):
  File "<input>", line 1, in <module>
  File "<string>", line 37, in QActivation
  File "/Users/cbarber/ws/bmxnet/python/mxnet/_ctypes/ndarray.py", line 92, in _imperative_invoke
    ctypes.byref(out_stypes)))
  File "/Users/cbarber/ws/bmxnet/python/mxnet/base.py", line 146, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
MXNetError: Invalid Parameter format for act_bit expect int (non-negative) but value='
[[-0.011113   -0.43647185  0.53347087]]
<NDArray 1x3 @cpu(0)>', in operator QActivation(name="", act_bit="
[[-0.011113   -0.43647185  0.53347087]]
<NDArray 1x3 @cpu(0)>")

Environment info (Required)

Built on a macbook pro with USE_CUDA and USE_OPENMP turned off. Python 2.7

Built from source as of commit e4fc9106e9ada87386407375e6297833e0cd76ad.

Note that I had to change the library type from MODULE to SHARED in the root CMakeList.txt file for the cmake build to succeed.

analog-cbarber commented 6 years ago

It appears that adding input argument to registry declaration fixes the problem:

MXNET_REGISTER_OP_PROPERTY(QActivation, QActivationProp)
.describe(R"(Quantized activation function.

The following quantized/binarized activation are supported (operations are applied elementwisely to each
scalar of the input tensor):

- `1 bit`: using deteministic sign() function to generate binary activation
- `2-32 bit`: using quantization function 

)")
.add_argument("data", "NDArray-or-Symbol", "Input data.") 
.add_arguments(QActivationParam::__FIELDS__());
yanghaojin commented 6 years ago

thanks for reporting the issue, has been fixed.