Skip to main content

QuantScheme

Struct QuantScheme 

Source
pub struct QuantScheme {
    pub value: QuantValue,
    pub store: QuantStore,
    pub mode: QuantMode,
    /* private fields */
}
Expand description

Describes a quantization scheme/configuration.

Scales come at up to two levels, each an optional field set through per_tensor and per_block in any order:

// One scale for the whole tensor, stored as f32. Also what a scheme with no level resolves to.
QuantScheme::default().per_tensor(ScaleDtype::F32);

// One scale per block of 32 values.
QuantScheme::default().per_block([32], ScaleDtype::F32);

// Two levels: ue4m3 block scales, normalized by a single per-tensor f32 scale.
QuantScheme::default()
    .per_block([16], ScaleDtype::UE4M3)
    .per_tensor(ScaleDtype::F32);

A two-level scheme exists so block scales can live in a narrow type: the global per-tensor scale absorbs the tensor’s dynamic range, and the block dtype only covers the spread between blocks. That spread is still bounded: a block whose scale falls further below the largest one than the block dtype can express is stored at that dtype’s smallest value, far too coarse for it, and every value in the block quantizes to zero. ScaleDtype::UE4M3 spans about 2^18 this way, so a tensor holding a genuine outlier can lose its ordinary values.

Fields§

§value: QuantValue

The logical data type of quantized input values (e.g., QuantValue::Q8F).

This defines how values are interpreted during computation, independent of how they’re stored.

§store: QuantStore

Data type used for storing quantized values.

§mode: QuantMode

Quantization mode (e.g., symmetric).

Implementations§

Source§

impl QuantScheme

Source

pub fn nvfp4() -> QuantScheme

The NVFP4 format: fp4 (e2m1) values in blocks of 16, with ue4m3 block scales normalized by one per-tensor f32 scale.

Source§

impl QuantScheme

Source

pub fn with_mode(self, mode: QuantMode) -> QuantScheme

Set the quantization mode.

Source

pub fn with_value(self, value: QuantValue) -> QuantScheme

Set the data type used for quantized values.

Source

pub fn with_store(self, store: QuantStore) -> QuantScheme

Set the data type used to store quantized values.

Source

pub fn per_tensor(self, dtype: ScaleDtype) -> QuantScheme

Set the per-tensor scale level, stored as dtype.

Source

pub fn per_block( self, block: impl AsRef<[u8]>, dtype: ScaleDtype, ) -> QuantScheme

Set the per-block scale level: one scale per block of block values, stored as dtype.

Source

pub fn tensor_scale(&self) -> Option<ScaleDtype>

The per-tensor scale level, the global level when a block level is present.

A scheme storing no level at all resolves here to a per-tensor f32 scale; the resolution is not stored, so such a scheme compares equal to Default, not to an explicit per_tensor(F32).

Source

pub fn block_scale(&self) -> Option<BlockScale>

The per-block scale level, the innermost when both levels are present.

Source

pub fn num_levels(&self) -> usize

The number of scale levels: as many scale tensors ride along with the values.

Source

pub fn scale_dtype(&self) -> ScaleDtype

The innermost level’s scale dtype, the type the per-position scales are stored in.

Source

pub fn block_size(&self) -> Option<BlockSize>

The block level’s size, or None for per-tensor quantization.

Source

pub fn swap_block_dims(&mut self, rank: usize, dim0: usize, dim1: usize)

Swap two tensor dimensions in the block level, mirroring shape.swap(dim0, dim1). The per-tensor level is unaffected.

dim0/dim1 are bare indices on purpose, mirroring [T]::swap’s own signature.

Source

pub fn permute_block_dims(&mut self, rank: usize, axes: &[usize])

Permute the block level, mirroring a permutation of the tensor’s axes. The per-tensor level is unaffected.

Source

pub fn size_bits_stored(&self) -> usize

Returns the size of the quantization storage type in bits.

Source

pub fn size_bits_value(&self) -> usize

Returns the size of the quantization storage type in bits.

Source

pub fn num_quants(&self) -> usize

Returns the number of quantized values stored in a single element.

Source

pub fn native_packing(&self) -> usize

Returns the native packing factor for the values. When native packing > 1, the packed representation stores num_quants elements grouped into packs of native_packing size.

Source

pub fn packing_dim(&self) -> Option<usize>

Returns the packing dim for the store.

Source

pub fn swap_packing_dim(&mut self, dim0: usize, dim1: usize)

Swaps the packing dim if it’s either of dim0 or dim1. Executes the corresponding update to shape.swap(dim0, dim1).

Trait Implementations§

Source§

impl Clone for QuantScheme

Source§

fn clone(&self) -> QuantScheme

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Copy for QuantScheme

Source§

impl Debug for QuantScheme

Source§

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

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

impl Default for QuantScheme

Source§

fn default() -> QuantScheme

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for QuantScheme

Source§

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

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

impl Eq for QuantScheme

Source§

impl Hash for QuantScheme

Source§

fn hash<__H>(&self, state: &mut __H)
where __H: Hasher,

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Ord for QuantScheme

Source§

fn cmp(&self, other: &QuantScheme) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for QuantScheme

Source§

fn eq(&self, other: &QuantScheme) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for QuantScheme

Source§

fn partial_cmp(&self, other: &QuantScheme) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for QuantScheme

Source§

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

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for QuantScheme

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

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

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> StoreKey for T

Source§

impl<T> StoreValue for T

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

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.