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

tanh-based multi-bit weight quantization function is not idempotent #35

Closed analog-cbarber closed 5 years ago

analog-cbarber commented 6 years ago

Currently when weight_bit > 1, QFullyConnected and QConvolution quantize weights by first squashing them using tanh:

        /*
         * Quantization for dataflow.
         * bit width > 1:
         *  Forward: w_i = 2 * quantize_k-bit(tanh(x_i)/2*max(|tanh(x_i)|) + 1/2) - 1
         */

This is the quantization scheme described in the DoReFa-Net paper. However, one big problem with this function is that it is not idempotent: if you apply it repeatedly it multiple times you may get different answers than just applying it once. That is, Q(Q(W)) != Q(W). This is not typically what you want in a quantization function. Normally you would expect a quantizer to be a no-op when applied to inputs that are already set to quantization targets.

While I can see that you would want to implement the DoReFa-Net function, I think it would make sense to make this behavior optional and also implement a simpler clipping based quantizer that would not suffer from this problem.