GithubHelp home page GithubHelp logo

hustzxd / analyticalscaleforintegerquantization Goto Github PK

View Code? Open in Web Editor NEW

This project forked from submission2019/analyticalscaleforintegerquantization

0.0 2.0 0.0 1.1 MB

Example for applying Gaussian and Laplace clipping on activations of CNN.

Python 16.95% Shell 0.07% Cuda 0.33% C++ 0.07% Jupyter Notebook 82.58%

analyticalscaleforintegerquantization's Introduction

ACIQ: ANALYTICAL CLIPPING FOR INTEGER QUANTIZATION OF NEURAL NETWORKS

This is complete example for applying Laplace and Gaussian clipping on activations of CNN. ASIQ

Dependencies

Data

  • To run this code you need validation set from ILSVRC2012 data
  • Configure your dataset path by providing --data "PATH_TO_ILSVRC" or copy ILSVRC dir to ~/datasets/ILSVRC2012.
  • To get the ILSVRC2012 data, you should register on their site for access: http://www.image-net.org/

Building cuda kernels for GEMMLOWP

To improve performance GEMMLOWP quantization was implemented in cuda and requires to compile kernels.

  • Create virtual environment for python3 and activate:
virtualenv --system-site-packages -p python3 venv3
. ./venv3/bin/activate
  • build kernels
cd kernels
./build_all.sh

Prepare setup for Inference

Low precision inference requires to find scale of low precision tensors ahead of time. In order to calculate scale we need to collect statistics of activations for specific topology and dataset.

Collect statistics

python inference-sim -a resnet18 -b 512 --qtype int8 -sm collect

Statistics will be saved under ~/asiq_data/statistics folder.

Run inference experiment

Following command line will evaluate resnet18 with 4bit activations and Laplace clipping

python inference-sim -a resnet18 -b 512 --qtype int4 -sm use -th laplace

* Prec@1 65.728 Prec@5 86.706

To evaluate non clipped version just omit -th or set "-th no"

python inference-sim -a resnet18 -b 512 --qtype int4 -sm use th no

* Prec@1 53.206 Prec@5 76.860

Laplace clipping improves top1 accuracy by 12.5% w/o retraining.

Solution for optimal clipping

The best of our knowladge, differentiable equations presented in the paper doesn't have analytical solution. We solve those empirically using scipy library and find optimal alpha value for Gaussian and Laplace cases. We show linear dependency between optimal alpha and sigma for Gaussian case and optimal alpha and b for Laplace case.

optimal_alpha.ipynb

Gaussian case, linear dependency Gaussian case

Quantization with optimal clipping

In order to quantize tensor to M bit with optimal clipping we use GEMMLOWP quantization with small modification. We replace dynamic range in scale computation by 2*alpha where alpha is optimal clipping value.

Quantization code can be found here: int_quantizer.py

analyticalscaleforintegerquantization's People

Contributors

submission2019 avatar ynahshan avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.