Skip to main content

Quantization algorithms to compress aidge networks.

Project description

Aidge Quantization Module

You can find in this folder the library that implements the quantization algorithms. For the moment only Post Training Quantization (PTQ) is available. Its implementation does support multiple branch architectures.

[TOC]

Installation

Dependencies

  • GCC
  • Make/Ninja
  • CMake
  • Python (optional, if you have no intend to use this library in python with pybind)

Aidge dependencies

  • aidge_core The requirements for installing the library are the followings:

    • GCC, Make and CMake for the compilation pipeline
    • The AIDGE modules aidge_core, aidge_onnx and aidge_backend_cpu
    • Python (> 3.7) if you intend to use the pybind wrapper

Configuration of environment variables

Variable Default value Description
AIDGE_INSTALL / lib / libAidge Path to the installation folder of Aidge, must be the same used for all aidge dependencies
AIDGE_C_COMPILER gcc C Compiler to use
AIDGE_CXX_COMPILER g++ CXX Compiler to use
AIDGE_BUILD_TYPE Release Can either be Release or Debug
AIDGE_ASAN OFF Compile with ASAN for debug
AIDGE_WITH_CUDA ON Compile CUDA kernel for quantization, requires nvcc and aidge_bakcend_cuda
AIDGE_CMAKE_ARCH "" Append architecture-specific arguments if provided
AIDGE_BUILD_GEN "" To specify a CMake generator (for example Ninja)

Pip installation

pip install . -v

TIPS : Use environment variables to change compilation options :

  • AIDGE_INSTALL : to set the installation folder. Defaults to /usr/local/lib. :warning: This path must be identical to aidge_core install path.
  • AIDGE_BUILD_TYPE : to set the compilation mode to Debug or Release
  • AIDGE_WITH_CUDA : if your computer hasn't graphical card, don't forget to set if OFF
  • AIDGE_BUILD_GEN : to set the build backend with

User guide

In order to perform a quantization, you will need an AIDGE model (that can be loaded from an ONNX). Then, you will have to provide a calibration dataset consisting of AIDGE tensors (that can be loaded from some numpy arrays). And finally, you will have to specify the quantization number of bits.

Performing the PTQ on your model will then be a one liner:

aidge_quantization.quantize_network(aidge_model, nb_of_bits, calibration_set)

Technical insights

The PTQ algorithm consists of 3 main steps:

- Normalization of the parameters, so that each node set of weights fits in the [-1:1] range.
- Normalization of the activations, so that each node output value fits in the [-1:1] range.
- Quantization of the scaling nodes previously inserted

To achieve those steps, one must propagate the scaling factors inside the network. One should also balance the different branches when they are merging. A particular care is needed for the biases rescaling at each step.

Doing quantization step by step

It is possible to perform the PTQ step by step, thanks to the exposed functions of the API. In that case, here is the standard pipeline:

- Prepare the network for the PTQ (remove the flatten nodes, fuse the BatchNorms ...)
- Insert the scaling nodes that will allow the model calibration
- Perform the Cross Layer Equalization if possible
- Perform the parameter normalization
- Compute the node output ranges over an input calibration dataset
- Adjust the output ranges using a specified error metric (MSE, KL, ...)
- Perform the activation normalization
- Quantize the normalized network
- Convert the scaling factors to bit-shifting operations if needed

Further work

  • add Quantization Aware Training (QAT)

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

aidge_quantization-0.4.1-cp313-cp313-manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.28+ x86-64

aidge_quantization-0.4.1-cp312-cp312-manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

aidge_quantization-0.4.1-cp311-cp311-manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

aidge_quantization-0.4.1-cp310-cp310-manylinux_2_28_x86_64.whl (1.6 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.28+ x86-64

File details

Details for the file aidge_quantization-0.4.1-cp313-cp313-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for aidge_quantization-0.4.1-cp313-cp313-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 d663d16aef5af47245fa53d4e5487d30a99e71403b8653775829f74e461b3e6e
MD5 ca301679806cb52345446dbb8e1c4a6a
BLAKE2b-256 42f921f86aa8d40e0d5a3e758b2a1955e6fefc0a1f0a7be67fcbee463d7ddb2f

See more details on using hashes here.

File details

Details for the file aidge_quantization-0.4.1-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for aidge_quantization-0.4.1-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ef08dc9e94c45a9cdc3f22762af225aa9e6b4cfb55f452f52696d3a5045b75a1
MD5 22cbbd86e87f403fbeba14ab051f084c
BLAKE2b-256 63bb56719871e75e589e8472c1552a3f35ae8eb7308740b3a8b9e588cbb4b42a

See more details on using hashes here.

File details

Details for the file aidge_quantization-0.4.1-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for aidge_quantization-0.4.1-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 91e5697398f11ed7694db57bfd676845b2d1cb2ef8fda1ea0c877f871216215a
MD5 6ebbcc493a336b8eaefd8ba613badfcb
BLAKE2b-256 6c128b3dcf8236b3f9da346859cb65cd16a79aade9d8ba2212b27ad88e9466f7

See more details on using hashes here.

File details

Details for the file aidge_quantization-0.4.1-cp310-cp310-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for aidge_quantization-0.4.1-cp310-cp310-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 589c1000329686990add1cc604c56df1a4f47e89074c74038f7f038dce1349a4
MD5 6a88ab2c278b7a90caa0ce4570fe63eb
BLAKE2b-256 bebbe890b802edb6fd57cd4391c889fc161cb2d8adc08f163e96609173260e86

See more details on using hashes here.

Supported by

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