Quantizer.md 3.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
Quantizer on NNI Compressor
===

## Naive Quantizer

We provide Naive Quantizer to quantizer weight to default 8 bits, you can use it to test quantize algorithm without any configure.

### Usage
tensorflow
```python
QuanluZhang's avatar
QuanluZhang committed
11
nni.compressors.tensorflow.NaiveQuantizer(model_graph).compress()
12
13
14
```
pytorch
```python
QuanluZhang's avatar
QuanluZhang committed
15
nni.compressors.torch.NaiveQuantizer(model).compress()
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
```

***

## QAT Quantizer
In [Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference](http://openaccess.thecvf.com/content_cvpr_2018/papers/Jacob_Quantization_and_Training_CVPR_2018_paper.pdf), authors Benoit Jacob and Skirmantas Kligys provide an algorithm to quantize the model with training.

>We propose an approach that simulates quantization effects in the forward pass of training. Backpropagation still happens as usual, and all weights and biases are stored in floating point so that they can be easily nudged by small amounts. The forward propagation pass however simulates quantized inference as it will happen in the inference engine, by implementing in floating-point arithmetic the rounding behavior of the quantization scheme
>* Weights are quantized before they are convolved with the input. If batch normalization (see [17]) is used for the layer, the batch normalization parameters are “folded into” the weights before quantization.
>* Activations are quantized at points where they would be during inference, e.g. after the activation function is applied to a convolutional or fully connected layer’s output, or after a bypass connection adds or concatenates the outputs of several layers together such as in ResNets.


### Usage
You can quantize your model to 8 bits with the code below before your training code.

Tensorflow code
```python
from nni.compressors.tensorflow import QAT_Quantizer
chicm-ms's avatar
chicm-ms committed
34
config_list = [{ 'q_bits': 8, 'op_types': ['default'] }]
QuanluZhang's avatar
QuanluZhang committed
35
36
quantizer = QAT_Quantizer(tf.get_default_graph(), config_list)
quantizer.compress()
37
38
39
40
```
PyTorch code
```python
from nni.compressors.torch import QAT_Quantizer
chicm-ms's avatar
chicm-ms committed
41
config_list = [{ 'q_bits': 8, 'op_types': ['default'] }]
QuanluZhang's avatar
QuanluZhang committed
42
43
quantizer = QAT_Quantizer(model, config_list)
quantizer.compress()
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
```

You can view example for more information

#### User configuration for QAT Quantizer
* **q_bits:** This is to specify the q_bits operations to be quantized to


***

## DoReFa Quantizer
In [DoReFa-Net: Training Low Bitwidth Convolutional Neural Networks with Low Bitwidth Gradients](https://arxiv.org/abs/1606.06160), authors Shuchang Zhou and Yuxin Wu provide an algorithm named DoReFa to quantize the weight, activation and gradients with training.

### Usage
To implement DoReFa Quantizer, you can add code below before your training code

Tensorflow code
```python
from nni.compressors.tensorflow import DoReFaQuantizer
config_list = [{ 'q_bits': 8, 'op_types': 'default' }]
QuanluZhang's avatar
QuanluZhang committed
64
65
quantizer = DoReFaQuantizer(tf.get_default_graph(), config_list)
quantizer.compress()
66
67
68
69
70
```
PyTorch code
```python
from nni.compressors.torch import DoReFaQuantizer
config_list = [{ 'q_bits': 8, 'op_types': 'default' }]
QuanluZhang's avatar
QuanluZhang committed
71
72
quantizer = DoReFaQuantizer(model, config_list)
quantizer.compress()
73
74
75
76
```

You can view example for more information

Chi Song's avatar
Chi Song committed
77
#### User configuration for DoReFa Quantizer
78
* **q_bits:** This is to specify the q_bits operations to be quantized to