Skip to main content

qnq's not quantization

Project description

QNQ -- QNQ's not quantization

version 1.1.0 2021.2.5

Description

The toolkit is for Techart algorithm team to quantize their custom neural network's pretrained model. The toolkit is beta now, you can contact me with email(dongz.cn@outlook.com) for adding ops and fixing bugs.

How to install

pip install qnq

How to use

This README.MD is in very early stages, and will be updated soon. you can visit https://git.zwdong.com/zhiwei.dong/qnq_tutorial for more examples for QNQ.

  1. Prepare your model.

    1. Check if your model contains non-class operator, like torch.matmul.
    2. If True, add from qnq.operators.torchfunc_ops import * to your code.
    3. Then use class replace non-class operator, you can refer fellow #! add by dongz
    class BasicBlock(nn.Module):
        expansion = 1
    
        def __init__(self, inplanes, planes, stride=1, downsample=None):
            super(BasicBlock, self).__init__()
            self.conv1 = conv3x3(inplanes, planes, stride)
            self.bn1 = nn.BatchNorm2d(planes)
            self.relu1 = nn.ReLU(inplace=True)
            self.relu2 = nn.ReLU(inplace=True)
            self.conv2 = conv3x3(planes, planes)
            self.bn2 = nn.BatchNorm2d(planes)
            self.downsample = downsample
            self.stride = stride
    
            #! add by dongz
            self.torch_add = TorchAdd()
    
        def forward(self, x):
            identity = x
    
            out = self.conv1(x)
            out = self.bn1(out)
            out = self.relu1(out)
    
            out = self.conv2(out)
            out = self.bn2(out)
    
            if self.downsample is not None:
                identity = self.downsample(x)
    
            #! add by dongz
            out = self.torch_add(out, identity)
            # out += identity
            out = self.relu2(out)
    
            return out
    
  2. Prepare 'metrics', 'metrics_light'(optional) and 'steper'.

    1. Choose at least 1k data to calibration your quantized model.
    2. 'metrics' inference without input params, return metrics value(a float number).
    3. 'metrics_light' inference without input params, return metrics value(a float number), you can choose 1/10 testsets to test.
    4. 'steper' done inference and without input params too, but add quant.step(), and no return.
    5. Check qnq_tutorial for details.
  3. Prepare pretrained checkpoints.

    1. Train your model and use torch.save() to save your checkpoints.
    2. Use checkpoints = torch.load(checkpoints_path) and model.load_state_dict(checkpoints) to load your checkpoints.
  4. Quantize

    1. For code
      1. Add from qnq import QNQ
      2. Add quant = QNQ(model, save_path, config_path, metrics, metrics_light, steper).
      3. Add quant.search()
    2. First run the program will exit, but the config_path will show a yaml file.
    3. Edit config.yaml and rerun for quantization.

Operators supported

  • Convolution Layers
    • Conv
    • ConvTranspose
  • Pooling Layers
    • MaxPool
    • AveragePool
    • AdaptiveAvgPool
  • Activation
    • Relu、Relu6
    • PRelu、LeakyRelu
    • LogSoftmax
  • Normalization Layers
    • BatchNorm
    • LayerNorm
  • Recurrent
    • LSTM
  • Linear Layers
    • Linear
  • Vision Layers
    • Upsample
    • Embedding
  • Torch Function
    • Add, Sum, Minus, DotMul, MatMul, Div,
    • Sqrt, Exp
    • Sin, Cos
    • SoftMax, Sigmoid, Tanh
    • TorchTemplate, TorchDummy

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qnq-1.1.1.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

qnq-1.1.1-py2.py3-none-any.whl (45.8 kB view details)

Uploaded Python 2Python 3

File details

Details for the file qnq-1.1.1.tar.gz.

File metadata

  • Download URL: qnq-1.1.1.tar.gz
  • Upload date:
  • Size: 27.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.9

File hashes

Hashes for qnq-1.1.1.tar.gz
Algorithm Hash digest
SHA256 9cab2b1d457d372a39ba40ec42bf3360e32ac347502f50fa5cba5685c176cb60
MD5 1739cb518fd179a53a456b045adc4cc2
BLAKE2b-256 030188816e61ecce897ba23e3ffdfc6e96a01a1b2b1e55c3d5d716a38f6500dd

See more details on using hashes here.

File details

Details for the file qnq-1.1.1-py2.py3-none-any.whl.

File metadata

  • Download URL: qnq-1.1.1-py2.py3-none-any.whl
  • Upload date:
  • Size: 45.8 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.6.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.7.9

File hashes

Hashes for qnq-1.1.1-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 afe9d66c6a3fa36b3e9cd57035aad6f220821cf451fc2d3b76a3d098f138b3c4
MD5 08b026e61c590b6402ab679489495cc0
BLAKE2b-256 33c39c9584701cd59dd11ee4cbf2e36de864f901467a2949e442ac635ee7f567

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page