Skip to main content

Quantizers

PyPI version License Tests Coverage

Hardware-oriented numerical quantizers for deep learning models, implemented in Keras v3 and NumPy. Provides bit-accurate precision matching with Vivado/Vitis HLS implementations.

Features

Supported Quantizers

Fixed-Point Quantizer

Parameters:

  • k (keep_negative): Enable negative numbers
  • i (integer_bits): Number of bits before decimal point (excludes sign bit)
  • f (fractional_bits): Number of bits after decimal point
  • For C++: W = k + i + f, I = k + i, S = k

Supported modes:

  • Rounding: TRN, RND, RND_CONV, TRN_ZERO, RND_ZERO, RND_MIN_INF, RND_INF
    • S_RND and S_RND_CONV for stochastic rounding; Not available in NumPy implementation as it is for training only
  • Overflow: WRAP, SAT, SAT_SYM, WRAP_SM

Limitations:

  • WRAP_SM only works with RND or RND_CONV rounding
  • WRAP* modes don't provide surrogate gradients for integer bits
  • Saturation bit forced to zero for WRAP and WRAP_SM

Minifloat Quantizer

Parameters:

  • m (mantissa_bits): Mantissa width
  • e (exponent_bits): Exponent width
  • e0 (exponent_zero): Exponent bias (default: 0)
  • Range: [-2^(e-1) + e0, 2^(e-1) - 1 + e0]

Features:

  • Supports subnormal numbers
  • Uses RND_CONV rounding and SAT overflow
  • HLS-synthesizable implementation in test/cpp_source/ap_types/ap_float.h

Simplified Quantizers

  • Binary: Maps to {-1,1} with 0 to -1. (preliminary implementation)
  • Ternary: Shorthand for fixed-point fixed<2, 1, RND_CONV, SAT_SYM>

Installation

requires python>=3.10

pip install quantizers

keras>=3.0 and at least one compatible backend (pytorch, jax, or tensorflow) is required for training.

Usage

Stateless Quantizers

from quantizers import (
  float_quantize(_np), # add _np for NumPy implementation
  get_fixed_quantizer(_np),
  binary_quantize(_np),
  ternary_quantize(_np),
)

# Fixed-point quantizer
fixed_quantizer = get_fixed_quantizer(round_mode, overflow_mode)
fixedp_qtensor = fixed_quantizer(
    x,
    integer_bits,
    fractional_bits,
    keep_negative,
    training, # For stochastic rounding, and WRAP does not happen during training
    seed, # For stochastic rounding only
)

# Minifloat quantizer
floatp_qtensor = float_quantize(x, mantissa_bits, exponent_bits, exponent_zero)

# Simplified quantizers
binary_qtensor = binary_quantize(x)
ternary_qtensor = ternary_quantize(x)

Stateful Quantizers

# Can be used for, but not intended for training
fixed_q = FixedQ(
    width,
    integer_bits, # including the sign bit)
    keep_negative,
    fixed_round_mode, # No stochastic rounding
    fixed_overflow_mode
)
quantized = fixed_q(x)

mfloat_q = MinifloatQ(mantissa_bits, exponent_bits, exponent_zero)
quantized = mfloat_q(x)

Metadata

Release files for quantizers 1.2.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for quantizers 1.2.2
File Size Uploaded Provenance
quantizers-1.2.2.tar.gz 118.0 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for quantizers 1.2.2
File Interpreter ABI Platform
quantizers-1.2.2-py3-none-any.whl Python 3 none any Details

Total release size: 133.7 kB

Release files / quantizers-1.2.2.tar.gz

Download URL quantizers-1.2.2.tar.gz
Size 118.0 kB
Tags Source
SHA-256 checksum
How to use checksums
cb76c4e273a96dd6735c33c9a3b0f7f688206aad4774cd7b9ee5effb9d9d6fab
BLAKE2b-256 checksum
How to use checksums
73565a240db3b16101dce9c3fca296f6806aa9b523d5bf43388293bf69fbe537
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Release files / quantizers-1.2.2-py3-none-any.whl

Download URL quantizers-1.2.2-py3-none-any.whl
Size 15.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
de6b6f9033fcd71593afd203dff68d5ee72563997b5426c6ecb2236fd11c9ef5
BLAKE2b-256 checksum
How to use checksums
0798f1db1cf7ffea7e73767d5a816ca36dfd4cd9761127cb0545c945dc680272
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.22

Security for quantizers 1.2.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page