Skip to main content

No project description provided

Project description

Quantizers

PyPI version License

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)

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

quantizers-1.0.0.tar.gz (117.1 kB view details)

Uploaded Source

Built Distribution

quantizers-1.0.0-py3-none-any.whl (14.9 kB view details)

Uploaded Python 3

File details

Details for the file quantizers-1.0.0.tar.gz.

File metadata

  • Download URL: quantizers-1.0.0.tar.gz
  • Upload date:
  • Size: 117.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for quantizers-1.0.0.tar.gz
Algorithm Hash digest
SHA256 7e3f7227f7fe7f536c7b7fad3f14dc165f392002b3283871c48956773fac7e59
MD5 4a0d916689411c4ec6856f093ed8c6c9
BLAKE2b-256 72e0becd6eee3e68c691aacce0a651244259fb4f8e3e926b953bc1d3a7aa5b19

See more details on using hashes here.

File details

Details for the file quantizers-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: quantizers-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.9

File hashes

Hashes for quantizers-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 28c34c632eddf12ec98476facecb06717fce83a5173f3449b27dc0648f0880dc
MD5 853b700e336f0429c94d2e63813ac2fd
BLAKE2b-256 5a898281bca7ac6d6dc56e035fd4208f7a936d5c74d149a3dde132e069becd8c

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