eleanor.jax.weight_quantization#
- class eleanor.jax.weight_quantization.QuantizedLinear(in_features, out_features, use_bias=True, dtype=None, quant_bits=8, stochastic=True, *, key)[source]#
Bases:
LinearAdapted from nn.Linear. Performs a linear transformation, and produces quantized weights when called.
- Parameters:
- __call__(x, *, key=None)[source]#
- Parameters:
x (Array) – The input. Should be a JAX array of shape (in_features,). (Or shape () if in_features=”scalar”.)
key (PRNGKey) – Ignored; provided for compatibility with the rest of the Equinox API. (Keyword only argument.)
- Return type:
Notes
If you want to use higher order tensors as inputs (for example featuring ” “batch dimensions) then use jax.vmap. For example, for an input x of ” “shape (batch, in_features), using
`python linear = eleanor.weight_quantization.QuantizedLinear(...) jax.vmap(linear)(x) `will produce the appropriate output of shape (batch, out_features).
- eleanor.jax.weight_quantization.quantize_weights(weights, num_bits=8, preserve_zero=True, preserve_max_val=False, stochastic=True, key=None)[source]#
Quantize weights to a specified bit precision. It can apply stochastic rounding using the weight value as the probability for rounding.
- Parameters:
weights (Array) – The weights to quantize.
num_bits (int) – Number of bits for quantization (e.g., 8 for int8).
preserve_zero (bool) – Preserve zero value in the quantization.
preserve_max_val (bool) – preserve maximum weight value in the quantization.
stochastic (bool) – Apply stochastic rounding based on the weight value.
key (PRNGKey) – A random key is used to implement stochastic rounding. Optional in case not using stochastic rounding.
- Return type:
Quantized weights.