pub struct QuantParams {
pub scale: f64,
pub zero_point: i32,
pub qmin: i32,
pub qmax: i32,
}Expand description
Affine/symmetric integer quantization parameters.
Holds the grid step (scale), the integer zero_point (always 0 for the
symmetric scheme) and the integer clamp range [qmin, qmax].
Fields§
§scale: f64Grid step: the real spacing between adjacent integer codes.
zero_point: i32Integer code onto which the real value 0 maps.
qmin: i32Lowest representable integer code.
qmax: i32Highest representable integer code.
Implementations§
Source§impl QuantParams
impl QuantParams
Sourcepub fn from_minmax(
min_v: f64,
max_v: f64,
dtype: IntDtype,
scheme: QuantScheme,
) -> Result<Self, GpuOptimError>
pub fn from_minmax( min_v: f64, max_v: f64, dtype: IntDtype, scheme: QuantScheme, ) -> Result<Self, GpuOptimError>
Build parameters directly from a real [min, max] interval.
For QuantScheme::Symmetric this reduces to Self::from_absmax over
max(|min|, |max|). For QuantScheme::Affine the interval is nudged to
include the real 0, scale = (max - min) / (qmax - qmin) and the
zero_point is chosen so the real 0 maps onto an exact integer code.
§Errors
Returns GpuOptimError::InvalidState if the inputs are non-finite,
inverted (max < min), or describe a degenerate zero-width interval.
Sourcepub fn from_absmax(absmax: f64, dtype: IntDtype) -> Result<Self, GpuOptimError>
pub fn from_absmax(absmax: f64, dtype: IntDtype) -> Result<Self, GpuOptimError>
Build symmetric parameters from an absolute maximum.
scale = absmax / qmax, zero_point = 0.
§Errors
Returns GpuOptimError::InvalidState when absmax is non-finite or not
strictly positive (a degenerate, all-zero tensor cannot be calibrated).
Sourcepub fn per_tensor_minmax(
x: &Array1<f64>,
dtype: IntDtype,
scheme: QuantScheme,
) -> Result<Self, GpuOptimError>
pub fn per_tensor_minmax( x: &Array1<f64>, dtype: IntDtype, scheme: QuantScheme, ) -> Result<Self, GpuOptimError>
Calibrate per-tensor parameters from the exact [min, max] of x.
§Errors
Returns GpuOptimError::InvalidState for an empty tensor or a
degenerate (zero-range) calibration.
Sourcepub fn symmetric_from_absmax(
x: &Array1<f64>,
dtype: IntDtype,
) -> Result<Self, GpuOptimError>
pub fn symmetric_from_absmax( x: &Array1<f64>, dtype: IntDtype, ) -> Result<Self, GpuOptimError>
Calibrate symmetric parameters from max(|x|).
§Errors
Returns GpuOptimError::InvalidState for an empty or all-zero tensor.
Sourcepub fn per_tensor_percentile(
x: &Array1<f64>,
dtype: IntDtype,
scheme: QuantScheme,
clip_fraction: f64,
) -> Result<Self, GpuOptimError>
pub fn per_tensor_percentile( x: &Array1<f64>, dtype: IntDtype, scheme: QuantScheme, clip_fraction: f64, ) -> Result<Self, GpuOptimError>
Calibrate per-tensor parameters after clipping the tails.
The interval is taken between the clip_fraction and 1 - clip_fraction
quantiles (nearest-rank) of x, which suppresses outliers before the
scale is derived.
§Errors
Returns GpuOptimError::InvalidState for an empty tensor, a
clip_fraction outside [0, 0.5), or a degenerate calibration.
Sourcepub fn quantize(&self, x: f64, mode: RoundingMode, rng: &mut impl Rng) -> i32
pub fn quantize(&self, x: f64, mode: RoundingMode, rng: &mut impl Rng) -> i32
Quantize a single real value to an integer code (with clamping).
Sourcepub fn dequantize(&self, q: i32) -> f64
pub fn dequantize(&self, q: i32) -> f64
Dequantize an integer code back to a real value.
Sourcepub fn fake_quant_scalar(
&self,
x: f64,
mode: RoundingMode,
rng: &mut impl Rng,
) -> f64
pub fn fake_quant_scalar( &self, x: f64, mode: RoundingMode, rng: &mut impl Rng, ) -> f64
Full fake-quant round trip for a single value, dequant(quant(x)).
Trait Implementations§
Source§impl Clone for QuantParams
impl Clone for QuantParams
Source§fn clone(&self) -> QuantParams
fn clone(&self) -> QuantParams
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreimpl Copy for QuantParams
Source§impl Debug for QuantParams
impl Debug for QuantParams
Source§impl PartialEq for QuantParams
impl PartialEq for QuantParams
impl StructuralPartialEq for QuantParams
Auto Trait Implementations§
impl Freeze for QuantParams
impl RefUnwindSafe for QuantParams
impl Send for QuantParams
impl Sync for QuantParams
impl Unpin for QuantParams
impl UnsafeUnpin for QuantParams
impl UnwindSafe for QuantParams
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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