Skip to main content

XSlim is an offline quantization toolkit based on PPQ for ONNX models.

Project description

XSlim

中文版 | English

Version License Python

XSlim is a Post-Training Quantization (PTQ) tool developed by SpacemiT. It integrates chip-optimized quantization strategies and provides a unified interface for ONNX model quantization via JSON configuration files.


Features

  • INT8 / FP16 / Dynamic Quantization – multiple precision levels for different deployment scenarios
  • JSON-driven configuration – simple, declarative quantization setup
  • Python API & CLI – use as a library or from the command line
  • Custom preprocessing – plug in your own preprocessing functions
  • Expanded ONNX operator coverage – run Graphwise Analysis and quantization on models that use common arithmetic, activation, comparison, reduction, dropout, and opset-24 Pad patterns
  • Automatic YOLO decode fusion – fuse supported YOLO decode subgraphs into a single spacemit_functions.YoloDecode node
  • ONNX Function-aware export – preserve embedded FunctionProto definitions and emit required custom-domain imports automatically
  • ONNX-based workflow – built on the ONNX ecosystem

Installation

python -m pip install xslim

Or install from source:

git clone https://github.com/spacemit-com/xslim.git
cd xslim
python -m pip install .

For local development, use an editable install:

python -m pip install -e .

Build metadata is defined in pyproject.toml, and the import package lives under the standard src/ layout. To build source and wheel distributions locally:

python -m pip install --upgrade build
python -m build

Quick Start

Python API

import xslim

# Using a JSON config file
xslim.quantize_onnx_model("config.json")

# Using a dict
config = {
    "model_parameters": {
        "onnx_model": "model.onnx",
        "working_dir": "./output"
    },
    "calibration_parameters": {
        "input_parameters": [{
            "mean_value": [123.675, 116.28, 103.53],
            "std_value": [58.395, 57.12, 57.375],
            "color_format": "rgb",
            "preprocess_file": "PT_IMAGENET",
            "data_list_path": "./calib_img_list.txt"
        }]
    }
}
xslim.quantize_onnx_model(config)

# You can also pass the model path and output path directly
xslim.quantize_onnx_model("config.json", "input.onnx", "output.onnx")

Command Line

# Installed CLI entry point
xslim --config config.json

# Module entry point also remains available
python -m xslim --config config.json

# Specify input and output model paths
xslim -c config.json -i input.onnx -o output.onnx

# Dynamic quantization (no config file needed)
xslim -i input.onnx -o output.onnx --dynq

# FP16 conversion (no config file needed)
xslim -i input.onnx -o output.onnx --fp16

# Convert the default ai.onnx opset to a target version
xslim -i input.onnx -o output.onnx --opset 20

# ONNX simplification only (no config file needed)
xslim -i input.onnx -o output.onnx

For config-free dynamic quantization and FP16 conversion, you can exclude operators with comma-separated names or types:

xslim -i input.onnx -o output.onnx --dynq --ignore_op_types Softmax,LayerNormalization
xslim -i input.onnx -o output.onnx --fp16 --ignore_op_names /model/head/MatMul

Static INT8 quantization expects a floating-point input model. If the model already contains QuantizeLinear or DequantizeLinear, XSlim stops with a clear error instead of quantizing an already-quantized graph again.

For supported YOLO exports, no extra switch is required: XSlim will try to fuse decode-heavy post-processing into spacemit_functions.YoloDecode during simplification and keep the corresponding ONNX FunctionProto in the exported model.

Documentation

Samples

See the samples directory for ready-to-run examples covering ResNet-18, MobileNet V3, BERT, and more. YOLO-specific usage notes are documented in the examples and accuracy-tuning guides.

Changelog

For a full list of published versions, see the Releases page. The summary below is synchronized with that release history; 2.1.0 is the current in-tree development version and has not been published yet.

Version Highlights
2.1.0 Current in-tree development version; add automatic spacemit_functions.YoloDecode fusion for supported YOLO exports, preserve custom ONNX FunctionProto definitions during quantization/export, improve opset-24/custom-domain handling coverage, expand ONNX operator execution/socket coverage, support scalar and axes-input reduce kernels, and reject static re-quantization of models that already contain QuantizeLinear / DequantizeLinear
2.0.14 Latest published release; add configurable default ai.onnx opset conversion for quantization and conversion workflows
2.0.13 Upgrade the default ONNX opset to 24, standardize operator domains, and align version metadata with the 2.0.12 release
2.0.12 Complete README changelog/release metadata, add accuracy-tuning docs and README links, introduce the xslim-accuracy-tuning GitHub skill, add YOLO truncation guidance, and rename input parameters for consistency
2.0.11 Fix Pad/missing-input handling, add Or/Einsum/Selu support, normalize Conv/ConvTranspose kernel shapes, and raise minimum Python to 3.9
2.0.10 Align release metadata, improve CI/test coverage, normalize missing default ONNX opset before dynamic quantization, and refine shape inference handling
2.0.9 Add documentation, preserve tensor dtype metadata during FP16 conversion, and restore compatibility with onnxslim 0.1.87
2.0.8 Improve packaging/CI, add torch executor operator coverage, add PyPI publish workflow, and centralize version metadata
2.0.7 Fix FP16 conversion bug on complex models
2.0.6 Fix metadata props deletion; default CLI behavior changed to model simplification (use --dynq for dynamic quantization)

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

License

This project is licensed under the Apache License 2.0.

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

xslim-2.1.0.tar.gz (320.6 kB view details)

Uploaded Source

Built Distribution

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

xslim-2.1.0-py3-none-any.whl (311.2 kB view details)

Uploaded Python 3

File details

Details for the file xslim-2.1.0.tar.gz.

File metadata

  • Download URL: xslim-2.1.0.tar.gz
  • Upload date:
  • Size: 320.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xslim-2.1.0.tar.gz
Algorithm Hash digest
SHA256 c03be8f5149076adc368fcfc496bbe412552e2287989ef1647daea531abcf1a9
MD5 81a79eb6358d559514f8134dcf896c74
BLAKE2b-256 ace1a74b4317a8e328f738caa1ea3483a3c4a7bb1e9246da03cb17526311b4c4

See more details on using hashes here.

Provenance

The following attestation bundles were made for xslim-2.1.0.tar.gz:

Publisher: publish.yml on spacemit-com/xslim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file xslim-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: xslim-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 311.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for xslim-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0e5d9a19d6acd838b24793d42cd3910205a8f223eb75bd490593402b630fe65d
MD5 83997047d4f85e5154a7d56cd1416072
BLAKE2b-256 a8e9270265297f24cad867d646a2ff37d7047c43ca51b81a356f5b676121c3d5

See more details on using hashes here.

Provenance

The following attestation bundles were made for xslim-2.1.0-py3-none-any.whl:

Publisher: publish.yml on spacemit-com/xslim

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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