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: QuantValueThe 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: QuantStoreData type used for storing quantized values.
mode: QuantModeQuantization mode (e.g., symmetric).
Implementations§
Source§impl QuantScheme
impl QuantScheme
Source§impl QuantScheme
impl QuantScheme
Sourcepub fn with_value(self, value: QuantValue) -> Self
pub fn with_value(self, value: QuantValue) -> Self
Set the data type used for quantized values.
Sourcepub fn with_store(self, store: QuantStore) -> Self
pub fn with_store(self, store: QuantStore) -> Self
Set the data type used to store quantized values.
Sourcepub fn per_tensor(self, dtype: ScaleDtype) -> Self
pub fn per_tensor(self, dtype: ScaleDtype) -> Self
Set the per-tensor scale level, stored as dtype.
Sourcepub fn per_block(self, block: impl AsRef<[u8]>, dtype: ScaleDtype) -> Self
pub fn per_block(self, block: impl AsRef<[u8]>, dtype: ScaleDtype) -> Self
Set the per-block scale level: one scale per block of block values, stored as dtype.
Sourcepub fn tensor_scale(&self) -> Option<ScaleDtype>
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).
Sourcepub fn block_scale(&self) -> Option<BlockScale>
pub fn block_scale(&self) -> Option<BlockScale>
The per-block scale level, the innermost when both levels are present.
Sourcepub fn num_levels(&self) -> usize
pub fn num_levels(&self) -> usize
The number of scale levels: as many scale tensors ride along with the values.
Sourcepub fn scale_dtype(&self) -> ScaleDtype
pub fn scale_dtype(&self) -> ScaleDtype
The innermost level’s scale dtype, the type the per-position scales are stored in.
Sourcepub fn block_size(&self) -> Option<BlockSize>
pub fn block_size(&self) -> Option<BlockSize>
The block level’s size, or None for per-tensor quantization.
Sourcepub fn swap_block_dims(&mut self, rank: usize, dim0: usize, dim1: usize)
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.
Sourcepub fn permute_block_dims(&mut self, rank: usize, axes: &[usize])
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.
Sourcepub fn size_bits_stored(&self) -> usize
pub fn size_bits_stored(&self) -> usize
Returns the size of the quantization storage type in bits.
Sourcepub fn size_bits_value(&self) -> usize
pub fn size_bits_value(&self) -> usize
Returns the size of the quantization storage type in bits.
Sourcepub fn num_quants(&self) -> usize
pub fn num_quants(&self) -> usize
Returns the number of quantized values stored in a single element.
Sourcepub fn native_packing(&self) -> usize
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.
Sourcepub fn packing_dim(&self) -> Option<usize>
pub fn packing_dim(&self) -> Option<usize>
Returns the packing dim for the store.
Sourcepub fn swap_packing_dim(&mut self, dim0: usize, dim1: usize)
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
impl Clone for QuantScheme
Source§fn clone(&self) -> QuantScheme
fn clone(&self) -> QuantScheme
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 QuantScheme
Source§impl Debug for QuantScheme
impl Debug for QuantScheme
Source§impl Default for QuantScheme
impl Default for QuantScheme
Source§impl<'de> Deserialize<'de> for QuantScheme
impl<'de> Deserialize<'de> for QuantScheme
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for QuantScheme
Source§impl Hash for QuantScheme
impl Hash for QuantScheme
Source§impl Ord for QuantScheme
impl Ord for QuantScheme
Source§fn cmp(&self, other: &QuantScheme) -> Ordering
fn cmp(&self, other: &QuantScheme) -> Ordering
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialEq for QuantScheme
impl PartialEq for QuantScheme
Source§impl PartialOrd for QuantScheme
impl PartialOrd for QuantScheme
Source§impl Serialize for QuantScheme
impl Serialize for QuantScheme
impl StructuralPartialEq for QuantScheme
Auto Trait Implementations§
impl Freeze for QuantScheme
impl RefUnwindSafe for QuantScheme
impl Send for QuantScheme
impl Sync for QuantScheme
impl Unpin for QuantScheme
impl UnsafeUnpin for QuantScheme
impl UnwindSafe for QuantScheme
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<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.