Skip to main content

QuantizationParams

Struct QuantizationParams 

Source
pub struct QuantizationParams {
    pub scale: f32,
    pub zero_point: i32,
    pub qmin: i8,
    pub qmax: i8,
}
Expand description

Quantization parameters for a tensor or tensor slice.

Defines the mapping between floating-point values and quantized integers:

  • Symmetric: x_q = round(x / scale)
  • Asymmetric: x_q = round(x / scale) + zero_point

§Invariants

  • scale > 0.0 (enforced at construction)
  • qmin <= zero_point <= qmax
  • For symmetric mode: zero_point == 0

Fields§

§scale: f32

Scale factor for quantization. Maps FP32 range to INT8 range.

§zero_point: i32

Zero point for asymmetric quantization. Always 0 for symmetric quantization.

§qmin: i8

Minimum quantized value (typically -128 for i8).

§qmax: i8

Maximum quantized value (typically 127 for i8).

Implementations§

Source§

impl QuantizationParams

Source

pub fn from_minmax( min_val: f32, max_val: f32, mode: QuantizationMode, ) -> CnnResult<Self>

Create symmetric quantization parameters from min/max values.

Symmetric quantization uses zero_point = 0 and maps the range [-max_abs, max_abs] to [-127, 127].

§Arguments
  • min_val - Minimum value in the FP32 tensor
  • max_val - Maximum value in the FP32 tensor
§Returns

Quantization parameters with zero_point = 0.

§Example
let params = QuantizationParams::from_minmax(-10.0, 10.0, QuantizationMode::Symmetric);
assert_eq!(params.zero_point, 0);
assert!(params.scale > 0.0);
Source

pub fn from_percentile( percentile_min: f32, percentile_max: f32, mode: QuantizationMode, ) -> CnnResult<Self>

Create quantization parameters from percentile values.

Used during calibration to exclude outliers that would skew the quantization range. Typically uses 0.001 and 0.999 percentiles.

§Arguments
  • percentile_min - Lower percentile value (e.g., -10.0)
  • percentile_max - Upper percentile value (e.g., 10.0)
  • mode - Quantization mode (symmetric or asymmetric)
§Example
// Use 99.8% of the data range, excluding outliers
let params = QuantizationParams::from_percentile(
    -9.5, 9.5, QuantizationMode::Symmetric
);
Source

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

Validate that the parameters satisfy invariants.

§Invariants
  • scale > 0.0
  • qmin <= qmax
  • qmin <= zero_point <= qmax
  • For symmetric mode: zero_point == 0
Source

pub fn quantize_value(&self, value: f32) -> i8

Quantize a single FP32 value to INT8.

Formula:

  • Symmetric: x_q = round(x / scale)
  • Asymmetric: x_q = round(x / scale) + zero_point

Result is clamped to [qmin, qmax].

Source

pub fn dequantize_value(&self, value: i8) -> f32

Dequantize a single INT8 value to FP32.

Formula:

  • Symmetric: x = x_q * scale
  • Asymmetric: x = (x_q - zero_point) * scale

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<'de> Deserialize<'de> for QuantizationParams

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Serialize for QuantizationParams

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

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> 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

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,