Skip to main content

QuantizationParams

Struct QuantizationParams 

Source
pub struct QuantizationParams {
    pub dtype: QuantizedDType,
    pub scheme: QuantizationScheme,
    pub scale: Vec<f32>,
    pub zero_point: Vec<i32>,
    pub block_size: Option<usize>,
    pub min_val: Option<f32>,
    pub max_val: Option<f32>,
}
Expand description

Quantization parameters

Contains all the parameters needed to quantize and dequantize tensors, including scale factors, zero points, and metadata about the quantization scheme being used.

Fields§

§dtype: QuantizedDType

Quantization data type

Specifies the target quantized data type (e.g., Int8, UInt8, Int4)

§scheme: QuantizationScheme

Quantization scheme

Defines how the quantization mapping is performed (linear, symmetric, etc.)

§scale: Vec<f32>

Scale factor(s)

Maps quantized values back to floating-point range. For per-channel quantization, contains one scale per channel. Formula: float_val = scale * (quantized_val - zero_point)

§zero_point: Vec<i32>

Zero point(s)

The quantized value that corresponds to floating-point zero. For per-channel quantization, contains one zero point per channel. For symmetric quantization, this is always 0.

§block_size: Option<usize>

Block size for block-wise quantization

When using block-wise quantization, specifies the size of each block that gets its own quantization parameters. None for other schemes.

§min_val: Option<f32>

Minimum value observed during calibration

Used for parameter calculation and validation. Set during calibration or when computing parameters from statistics.

§max_val: Option<f32>

Maximum value observed during calibration

Used for parameter calculation and validation. Set during calibration or when computing parameters from statistics.

Implementations§

Source§

impl QuantizationParams

Source

pub fn int8_symmetric() -> Self

Create parameters for INT8 symmetric quantization

INT8 symmetric quantization is commonly used for weights in neural networks due to its simplicity and good hardware support. The zero point is always 0, and the range is symmetric around zero.

§Examples
use torsh_backend::quantization::QuantizationParams;

let params = QuantizationParams::int8_symmetric();
assert_eq!(params.zero_point[0], 0);
Source

pub fn new(scale: f32, zero_point: i32) -> Self

Create basic quantization parameters with custom scale and zero point

This is a general-purpose constructor for creating quantization parameters with custom scale and zero point values. Useful for benchmarking and testing with specific parameter configurations.

§Arguments
  • scale - Scale factor for the quantization
  • zero_point - Zero point for the quantization
§Examples
use torsh_backend::quantization::QuantizationParams;

let params = QuantizationParams::new(255.0, 128);
assert_eq!(params.scale[0], 255.0);
assert_eq!(params.zero_point[0], 128);
Source

pub fn uint8_asymmetric() -> Self

Create parameters for UINT8 asymmetric quantization

UInt8 asymmetric quantization is commonly used for activations, especially after ReLU layers where values are non-negative. The zero point is typically set to 128 for balanced range utilization.

§Examples
use torsh_backend::quantization::QuantizationParams;

let params = QuantizationParams::uint8_asymmetric();
assert_eq!(params.zero_point[0], 128);
Source

pub fn int4_symmetric() -> Self

Create parameters for INT4 symmetric quantization

INT4 quantization provides extreme compression at the cost of accuracy. Symmetric INT4 is often used for weights in models where 4-bit precision is sufficient.

§Examples
use torsh_backend::quantization::QuantizationParams;

let params = QuantizationParams::int4_symmetric();
assert_eq!(params.dtype.bits(), 4);
Source

pub fn channel_wise(num_channels: usize, dtype: QuantizedDType) -> Self

Create parameters for channel-wise quantization

Channel-wise quantization applies different quantization parameters to each channel, providing better accuracy for models with varying channel sensitivities at the cost of increased parameter storage.

§Arguments
  • num_channels - Number of channels in the tensor
  • dtype - Quantization data type to use
§Examples
use torsh_backend::quantization::{QuantizationParams, QuantizedDType};

let params = QuantizationParams::channel_wise(64, QuantizedDType::Int8);
assert_eq!(params.scale.len(), 64);
assert_eq!(params.zero_point.len(), 64);
Source

pub fn block_wise(block_size: usize, dtype: QuantizedDType) -> Self

Create parameters for block-wise quantization

Block-wise quantization divides the tensor into blocks and applies different quantization parameters to each block. This can provide better accuracy than tensor-wise quantization while being more memory-efficient than channel-wise quantization.

§Arguments
  • block_size - Size of each quantization block
  • dtype - Quantization data type to use
§Examples
use torsh_backend::quantization::{QuantizationParams, QuantizedDType};

let params = QuantizationParams::block_wise(128, QuantizedDType::Int8);
assert_eq!(params.block_size, Some(128));
Source

pub fn from_statistics( &mut self, min_val: f32, max_val: f32, ) -> BackendResult<()>

Calculate quantization parameters from input statistics

Computes the optimal scale and zero point parameters based on the observed minimum and maximum values in the data. The calculation depends on the quantization scheme being used.

§Arguments
  • min_val - Minimum value observed in the data
  • max_val - Maximum value observed in the data
§Returns

Returns Ok(()) if parameters were calculated successfully, or an error if the statistics are invalid.

§Examples
use torsh_backend::quantization::QuantizationParams;

let mut params = QuantizationParams::int8_symmetric();
params.from_statistics(-2.0, 2.0).unwrap();
// Scale will be calculated to map [-2.0, 2.0] to [-128, 127]
Source

pub fn validate(&self) -> BackendResult<()>

Validate that the parameters are consistent and usable

Checks that all parameter vectors have consistent lengths, scale factors are positive, and zero points are within valid ranges.

Source

pub fn num_parameter_sets(&self) -> usize

Get the effective number of quantization parameter sets

Returns the number of independent parameter sets (scale/zero_point pairs) that this configuration represents. For tensor-wise quantization this is 1, for channel-wise it’s the number of channels.

Source

pub fn is_per_channel(&self) -> bool

Check if this configuration uses per-channel parameters

Source

pub fn quantization_error_bound(&self) -> f32

Get the quantization error bound for this configuration

Returns the maximum possible quantization error (in the original floating-point scale) for this quantization configuration.

Source

pub fn compression_ratio(&self) -> f32

Calculate the compression ratio achieved by this quantization

Returns the ratio of original size to quantized size. Assumes the original data was 32-bit floating point.

Trait Implementations§

Source§

impl Clone for QuantizationParams

Source§

fn clone(&self) -> QuantizationParams

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for QuantizationParams

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for QuantizationParams

Source§

fn default() -> Self

Default quantization parameters

Creates parameters for UInt8 linear quantization with scale=1.0 and zero_point=0, suitable for testing and initialization.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V