Nonuniform-to-Uniform Quantization

April 28, 2022 ยท View on GitHub

This repository contains the training code of N2UQ introduced in our CVPR 2022 paper: "Nonuniform-to-Uniform Quantization: Towards Accurate Quantization via Generalized Straight-Through Estimation"

In this study, we propose a quantization method that can learn the non-uniform input thresholds to maintain the strong representation ability of nonuniform methods, while output uniform quantized levels to be hardware-friendly and efficient as the uniform quantization for model inference.

To train the quantized network with learnable input thresholds, we introduce a generalized straight-through estimator (G-STE) for intractable backward derivative calculation w.r.t. threshold parameters.

The formula for N2UQ is simply as follows,

Forward pass:

Backward pass:

Moreover, we proposed L1 norm based entropy preserving weight regularization for weight quantization.

Citation

If you find our code useful for your research, please consider citing:

@inproceedings{liu2022nonuniform,
  title={Nonuniform-to-Uniform Quantization: Towards Accurate Quantization via Generalized Straight-Through Estimation},
  author={Liu, Zechun and Cheng, Kwang-Ting and Huang, Dong and Xing, Eric and Shen, Zhiqiang},
  journal={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition (CVPR)},
  year={2022}
}

Run

1. Requirements:

  • python 3.6, pytorch 1.7.1, torchvision 0.8.2
  • gdown

2. Data:

  • Download ImageNet dataset

3. Pretrained Models:

  • pip install gdown # gdown will automatically download the models
  • If gdown doesn't work, you may need to manually download the pretrained models and put them in the correponding ./models/ folder.

4. Steps to run:

(1) For ResNet architectures:

  • Change directory to ./resnet/
  • Run bash run.sh architecture n_bits quantize_downsampling
  • E.g., bash run.sh resnet18 2 0 for quantize resnet18 to 2-bit without quantizing downsampling layers

(2) For MobileNet architectures:

  • Change directory to ./mobilenetv2/
  • Run bash run.sh

Models

1. ResNet

NetworkMethodsW2/A2W3/A3W4/A4
ResNet-18
PACT64.468.169.2
DoReFa-Net64.767.568.1
LSQ67.670.271.1
N2UQ69.4 Model-Res18-2bit71.9 Model-Res18-3bit72.9 Model-Res18-4bit
N2UQ *69.7 Model-Res18-2bit72.1 Model-Res18-3bit73.1 Model-Res18-4bit
ResNet-34
LSQ71.673.474.1
N2UQ73.3 Model-Res34-2bit75.2 Model-Res34-3bit76.0 Model-Res34-4bit
N2UQ *73.4 Model-Res34-2bit75.3 Model-Res34-3bit76.1 Model-Res34-4bit
ResNet-50
PACT64.468.169.2
LSQ67.670.271.1
N2UQ75.8 Model-Res50-2bit77.5 Model-Res50-3bit78.0 Model-Res50-4bit
N2UQ *76.4 Model-Res50-2bit77.6 Model-Res50-3bit78.0 Model-Res50-4bit

Note that N2UQ without * denotes quantizing all the convolutional layers except the first input convolutional layer.

N2UQ with * denotes quantizing all the convolutional layers except the first input convolutional layer and three downsampling layers.

W2/A2, W3/A3, W4/A4 denote the cases where the weights and activations are both quantized to 2 bits, 3 bits, and 4 bits, respectively.

2. MobileNet

NetworkMethodsW4/A4
MobileNet-V2N2UQ72.1 Model-MBV2-4bit

Contact

Zechun Liu, HKUST (zliubq at connect.ust.hk)