Skip to main content

QuantScheme

Struct QuantScheme 

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§

§

impl QuantScheme

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.

§

impl QuantScheme

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

Set the quantization mode.

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

Set the data type used for quantized values.

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

Set the data type used to store quantized values.

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

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

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.

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

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

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

pub fn num_levels(&self) -> usize

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

pub fn scale_dtype(&self) -> ScaleDtype

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

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

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

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.

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.

pub fn size_bits_stored(&self) -> usize

Returns the size of the quantization storage type in bits.

pub fn size_bits_value(&self) -> usize

Returns the size of the quantization storage type in bits.

pub fn num_quants(&self) -> usize

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

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.

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

Returns the packing dim for the store.

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§

§

impl Clone for QuantScheme

§

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
§

impl Copy for QuantScheme

§

impl Debug for QuantScheme

§

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

Formats the value using the given formatter. Read more
§

impl Default for QuantScheme

§

fn default() -> QuantScheme

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

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

§

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
§

impl Eq for QuantScheme

§

impl Hash for QuantScheme

§

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
§

impl Ord for QuantScheme

§

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
§

impl PartialEq for QuantScheme

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl PartialOrd for QuantScheme

§

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
§

impl Serialize for QuantScheme

§

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
§

impl StructuralPartialEq for QuantScheme

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Adaptor<()> for T

§

fn adapt(&self)

Adapt the type to be passed to a metric.
Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Boilerplate for T
where T: Copy + Send + Sync + Debug + PartialEq + 'static,

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
§

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

§

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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

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

Compare self to key and return their ordering.
§

impl<K, Q> Comparable<Q> for K
where K: Borrow<Q> + ?Sized, Q: Ord + ?Sized,

§

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

Compare self to key and return their ordering.
Source§

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

§

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

§

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

Compare self to key and return true if they are equal.
§

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

§

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

Checks if this value is equivalent to the given key. Read more
§

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

§

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

Compare self to key and return true if they are equal.
§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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
§

impl<T> Pointable for T

§

const ALIGN: usize

The alignment of pointer.
§

type Init = T

The type for initializers.
§

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

Initializes a with the given initializer. Read more
§

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

Dereferences the given pointer. Read more
§

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

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

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

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

§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] only if self and other return Action::Follow. Read more
§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns [Action::Follow] if either self or other returns Action::Follow. Read more
§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<R, P> ReadPrimitive<R> for P
where R: Read + ReadEndian<P>, P: Default,

Source§

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
Source§

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
Source§

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

impl<T> StoreKey for T

§

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 = 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.
§

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

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more