This release is a pre-release and may not be stable for production use.
nnieqat-pytorch
This is a quantize aware training package for Neural Network Inference Engine(NNIE) on pytorch, it uses hisilicon quantization library to quantize module's weight and input data as fake fp32 format. To train model which is more friendly to NNIE, just import nnieqat and replace torch.nn default modules with corresponding one.
Table of Contents
Installation
- Supported Platforms: Linux
- Accelerators and GPUs: NVIDIA GPUs via CUDA driver 10.
- Dependencies:
- python >= 3.5, < 4
- llvmlite >= 0.31.0
- pytorch >= 1.0
- numba >= 0.42.0
- numpy >= 1.18.1
- Install nnieqat via pypi: $ pip install nnieqat
Usage
-
Replace default module with NNIE quantization optimized one. include:
- torch.nn.modules.conv -> nnieqat.modules.conv
- torch.nn.modules.linear -> nnieqat.modules.linear
- torch.nn.modules.pooling -> nnieqat.modules.pooling
from nnieqat.modules import convert_layers ... ... model = convert_layers(model) print(model) # Quantized layers have "Quantized" prefix. ...
-
Freeze bn after a few epochs of training
from nnieqat.gpu.quantize import freeze_bn ... ... if epoch > 2: net.apply(freeze_bn) ...
-
Unquantize weight before update it
from nnieqat.gpu.quantize import unquant_weight ... ... net.apply(unquant_weight) optimizer.step() ...
-
Dump weight quantized model
from nnieqat.gpu.quantize import quant_weight, unquant_weight ... ... net.apply(quant_weight) save_checkpoint(...) net.apply(unquant_weight) ...
Code Examples
-
Cifar10 quantization aware training example (add nnieqat into pytorch_cifar10_tutorial)
python test/test_cifar10.py -
ImageNet quantization finetuning example (add nnieqat into pytorh_imagenet_main.py)
python test/test_imagenet.py --pretrained path_to_imagenet_dataset
Results
-
ImageNet
python test/test_imagenet.py /data/imgnet/ --arch squeezenet1_1 --lr 0.001 --pretrained --epoch 10 # nnie_lr_e-3_ft python pytorh_imagenet_main.py /data/imgnet/ --arch squeezenet1_1 --lr 0.0001 --pretrained --epoch 10 # lr_e-4_ft python test/test_imagenet.py /data/imgnet/ --arch squeezenet1_1 --lr 0.0001 --pretrained --epoch 10 # nnie_lr_e-4_ftfinetune result:
trt_fp32 trt_int8 nnie torchvision 0.56992 0.56424 0.56026 nnie_lr_e-3_ft 0.56600 0.56328 0.56612 lr_e-4_ft 0.57884 0.57502 0.57542 nnie_lr_e-4_ft 0.57834 0.57524 0.57730
Todo
- Multiple GPU training support.
- Other platforms and accelerators support.
- Generate quantized model directly.
Reference
HiSVP 量化库使用指南
Quantizing deep convolutional networks for efficient inference: A whitepaper
Metadata
Release files for nnieqat 0.1.0b0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| nnieqat-0.1.0b0.tar.gz | 812.0 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| nnieqat-0.1.0b0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 1.6 MB
Release files / nnieqat-0.1.0b0.tar.gz
| Download URL | nnieqat-0.1.0b0.tar.gz |
|---|---|
| Size | 812.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
64eed27eef6fae519e01bea098d308b98864ac63c6c169bb14e271eff24bc0c3
|
|
BLAKE2b-256 checksum How to use checksums |
70f8183569e2141a800f176d287faaf1bc4da31bb64f89874af0e0551d9b6508
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9
|
Release files / nnieqat-0.1.0b0-py3-none-any.whl
| Download URL | nnieqat-0.1.0b0-py3-none-any.whl |
|---|---|
| Size | 818.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
024229a8c8de835da2e2d73c6689a16970a732dd21529ed0a7489f4b7a337913
|
|
BLAKE2b-256 checksum How to use checksums |
494567489fefc1d9efa5ef5d6de0c7c9eafde86066e72681ed8ea3c5b2c496e0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9
|