Skip to main content

Repository of Intel® Neural Compressor

Project description

Intel® Neural Compressor

An open-source Python library supporting popular model compression techniques on all mainstream deep learning frameworks (TensorFlow, PyTorch, and ONNX Runtime)

python version license coverage Downloads

Architecture   |   Workflow   |   LLMs Recipes   |   Results   |   Documentations


Intel® Neural Compressor aims to provide popular model compression techniques such as quantization, pruning (sparsity), distillation, and neural architecture search on mainstream frameworks such as TensorFlow, PyTorch, and ONNX Runtime, as well as Intel extensions such as Intel Extension for TensorFlow and Intel Extension for PyTorch. In particular, the tool provides the key features, typical examples, and open collaborations as below:

What's New

  • [2024/10] Transformers-like API for INT4 inference on Intel CPU and GPU.
  • [2024/07] From 3.0 release, framework extension API is recommended to be used for quantization.
  • [2024/07] Performance optimizations and usability improvements on client-side.

Installation

Install Framework

Install torch for CPU

pip install torch --index-url https://download.pytorch.org/whl/cpu

Use Docker Image with torch installed for HPU

https://docs.habana.ai/en/latest/Installation_Guide/Bare_Metal_Fresh_OS.html#bare-metal-fresh-os-single-click

Note: There is a version mapping between Intel Neural Compressor and Gaudi Software Stack, please refer to this table and make sure to use a matched combination.

Install torch/intel_extension_for_pytorch for Intel GPU

https://intel.github.io/intel-extension-for-pytorch/index.html#installation

Install torch for other platform

https://pytorch.org/get-started/locally

Install tensorflow

pip install tensorflow

Install from pypi

# Install 2.X API + Framework extension API + PyTorch dependency
pip install neural-compressor[pt]
# Install 2.X API + Framework extension API + TensorFlow dependency
pip install neural-compressor[tf]

Note: Further installation methods can be found under Installation Guide. check out our FAQ for more details.

Getting Started

Setting up the environment:

pip install "neural-compressor>=2.3" "transformers>=4.34.0" torch torchvision

After successfully installing these packages, try your first quantization program.

FP8 Quantization

Following example code demonstrates FP8 Quantization, it is supported by Intel Gaudi2 AI Accelerator.

To try on Intel Gaudi2, docker image with Gaudi Software Stack is recommended, please refer to following script for environment setup. More details can be found in Gaudi Guide.

# Run a container with an interactive shell
docker run -it --runtime=habana -e HABANA_VISIBLE_DEVICES=all -e OMPI_MCA_btl_vader_single_copy_mechanism=none --cap-add=sys_nice --net=host --ipc=host vault.habana.ai/gaudi-docker/1.17.0/ubuntu22.04/habanalabs/pytorch-installer-2.3.1:latest

Run the example:

from neural_compressor.torch.quantization import (
    FP8Config,
    prepare,
    convert,
)
import torchvision.models as models

model = models.resnet18()
qconfig = FP8Config(fp8_config="E4M3")
model = prepare(model, qconfig)
# customer defined calibration
calib_func(model)
model = convert(model)

Weight-Only Large Language Model Loading (LLMs)

Following example code demonstrates weight-only large language model loading on Intel Gaudi2 AI Accelerator.

from neural_compressor.torch.quantization import load

model_name = "TheBloke/Llama-2-7B-GPTQ"
model = load(
    model_name_or_path=model_name,
    format="huggingface",
    device="hpu",
    torch_dtype=torch.bfloat16,
)

Note:

Intel Neural Compressor will convert the model format from auto-gptq to hpu format on the first load and save hpu_model.safetensors to the local cache directory for the next load. So it may take a while to load for the first time.

Documentation

Overview
Architecture Workflow APIs LLMs Recipes Examples
PyTorch Extension APIs
Overview Dynamic Quantization Static Quantization Smooth Quantization
Weight-Only Quantization FP8 Quantization MX Quantization Mixed Precision
Tensorflow Extension APIs
Overview Static Quantization Smooth Quantization
Transformers-like APIs
Overview
Other Modules
Auto Tune Benchmark

Note: From 3.0 release, we recommend to use 3.X API. Compression techniques during training such as QAT, Pruning, Distillation only available in 2.X API currently.

Selected Publications/Events

Note: View Full Publication List.

Additional Content

Communication

  • GitHub Issues: mainly for bug reports, new feature requests, question asking, etc.
  • Email: welcome to raise any interesting research ideas on model compression techniques by email for collaborations.
  • Discord Channel: join the discord channel for more flexible technical discussion.
  • WeChat group: scan the QA code to join the technical discussion.

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

neural_compressor-3.1.tar.gz (1.3 MB view details)

Uploaded Source

Built Distribution

neural_compressor-3.1-py3-none-any.whl (1.8 MB view details)

Uploaded Python 3

File details

Details for the file neural_compressor-3.1.tar.gz.

File metadata

  • Download URL: neural_compressor-3.1.tar.gz
  • Upload date:
  • Size: 1.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.5

File hashes

Hashes for neural_compressor-3.1.tar.gz
Algorithm Hash digest
SHA256 8e1db24ee16e92e382c53ae86cb0c32e55236e891bf535fa169cccfa79f8e112
MD5 d1310b17390a6039be9c0b7d60b42f11
BLAKE2b-256 7682ca77429d0274377c5ac7f7c02efae4389dab08a96779f2b985fca13a30ca

See more details on using hashes here.

File details

Details for the file neural_compressor-3.1-py3-none-any.whl.

File metadata

File hashes

Hashes for neural_compressor-3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 a19b451910d5f4978a2ccaf8ea19a5cc9ea1e5c0d4954f1997d849d3c908414a
MD5 7108a67ae96692a5345db0c323586c35
BLAKE2b-256 8dd1084be21c66d08d4a5c6d4e134b61d9fe5ccd56d0aba9ed9e9dc7deb14aa5

See more details on using hashes here.

Supported by

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