pub enum QuantizationDescriptor {
PerTensorAffine(PerTensorAffine),
PerChannelAffine(PerChannelAffine),
PerBlockAffine(PerBlockAffine),
Nf4(Nf4),
Mxfp(Mxfp),
}Expand description
A typed quantization descriptor carrying the parameters for one of the supported quantization schemes.
§Equality and hashing
Only PartialEq is derived — not Eq or Hash — because the
PerTensorAffine variant contains f32 fields. IEEE 754 NaN != NaN, which
would make Eq semantically unsound (design decision #2).
§Examples
use hurray_core::{PerTensorAffine, QuantizationDescriptor, QuantizationSchemeTag};
let desc = QuantizationDescriptor::PerTensorAffine(
PerTensorAffine::new(0.5, 128).unwrap(),
);
assert_eq!(desc.scheme_tag(), QuantizationSchemeTag::PerTensorAffine);
assert_eq!(desc.encoded_len(), 16);Variants§
PerTensorAffine(PerTensorAffine)
Per-tensor affine quantization (scheme tag 0x01).
PerChannelAffine(PerChannelAffine)
Per-channel affine quantization (scheme tag 0x02).
PerBlockAffine(PerBlockAffine)
Per-block affine quantization (scheme tag 0x03).
Nf4(Nf4)
NF4 (NormalFloat4) block quantization (scheme tag 0x04).
Mxfp(Mxfp)
MXFP (OCP Microscaling) block quantization (scheme tag 0x05).
Implementations§
Source§impl QuantizationDescriptor
impl QuantizationDescriptor
Sourcepub fn scheme_tag(&self) -> QuantizationSchemeTag
pub fn scheme_tag(&self) -> QuantizationSchemeTag
Returns the QuantizationSchemeTag for this descriptor.
§Examples
use hurray_core::{Nf4, QuantizationDescriptor, QuantizationSchemeTag};
let desc = QuantizationDescriptor::Nf4(Nf4::new(0, 64, 1).unwrap());
assert_eq!(desc.scheme_tag(), QuantizationSchemeTag::Nf4);Sourcepub fn encoded_len(&self) -> usize
pub fn encoded_len(&self) -> usize
Returns the total encoded length of this descriptor in bytes, including the 4-byte header.
§Examples
use hurray_core::{PerTensorAffine, PerBlockAffine, ElementType, QuantizationDescriptor};
let pt = QuantizationDescriptor::PerTensorAffine(
PerTensorAffine::new(1.0, 0).unwrap()
);
assert_eq!(pt.encoded_len(), 16);
let pb = QuantizationDescriptor::PerBlockAffine(
PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float32).unwrap()
);
assert_eq!(pb.encoded_len(), 24);Sourcepub fn encode_into(&self, out: &mut [u8]) -> Result<usize>
pub fn encode_into(&self, out: &mut [u8]) -> Result<usize>
Encodes this descriptor into out without any allocation.
This is the zero-alloc canonical encoding path required by Layer 5
streaming writers. out must be at least Self::encoded_len() bytes.
Returns the number of bytes written.
§Errors
Error::InvalidQuantization—outis shorter thanencoded_len().
§Examples
use hurray_core::{PerTensorAffine, QuantizationDescriptor};
let desc = QuantizationDescriptor::PerTensorAffine(
PerTensorAffine::new(0.5, 0).unwrap()
);
let mut buf = vec![0u8; desc.encoded_len()];
let written = desc.encode_into(&mut buf).unwrap();
assert_eq!(written, 16);
// Round-trip.
let (decoded, consumed) = QuantizationDescriptor::decode(&buf).unwrap();
assert_eq!(consumed, 16);
assert_eq!(decoded, desc);Sourcepub fn encode_to_vec(&self) -> Vec<u8> ⓘ
pub fn encode_to_vec(&self) -> Vec<u8> ⓘ
Encodes this descriptor into a freshly allocated Vec<u8>.
This is a convenience wrapper around Self::encode_into. Prefer
Self::encode_into in hot paths to avoid allocation.
§Examples
use hurray_core::{Nf4, QuantizationDescriptor};
let desc = QuantizationDescriptor::Nf4(Nf4::new(0, 64, 1).unwrap());
let bytes = desc.encode_to_vec();
assert_eq!(bytes.len(), 16);
let (decoded, _) = QuantizationDescriptor::decode(&bytes).unwrap();
assert_eq!(decoded, desc);Sourcepub fn decode(bytes: &[u8]) -> Result<(Self, usize)>
pub fn decode(bytes: &[u8]) -> Result<(Self, usize)>
Decodes a QuantizationDescriptor from the beginning of bytes.
Returns the decoded descriptor and the number of bytes consumed. This lets streaming readers advance their cursor without re-scanning (design decision: returning consumed count avoids a second length query).
§Errors
Error::QuantizationDescriptorTooShort— not enough bytes.Error::InvalidQuantizationSchemeTag— tag is0x00or0xFF.Error::ReservedQuantizationSchemeTag— tag is in0x60–0xEF.Error::PrivateQuantizationSchemeTag— tag is in0xF0–0xFE.Error::UnknownQuantizationSchemeTag— tag is unallocated.Error::UnsupportedSchemeVersion— version exceeds supported maximum.Error::ReservedQuantizationFlagsBits— reserved flags bits are set.Error::InvalidBlockSize— block_size is out of range or not a power of two.Error::InvalidQuantization— other payload validation failure.
§Examples
use hurray_core::{PerTensorAffine, QuantizationDescriptor};
let desc = QuantizationDescriptor::PerTensorAffine(
PerTensorAffine::new(1.0, 0).unwrap()
);
let bytes = desc.encode_to_vec();
let (decoded, consumed) = QuantizationDescriptor::decode(&bytes).unwrap();
assert_eq!(consumed, bytes.len());
assert_eq!(decoded, desc);Sourcepub fn valid_storage_types(&self) -> &'static [ElementType]
pub fn valid_storage_types(&self) -> &'static [ElementType]
Returns the set of storage ElementTypes that are valid for this descriptor.
§Examples
use hurray_core::{ElementType, Mxfp, QuantizationDescriptor};
let desc = QuantizationDescriptor::Mxfp(Mxfp::new(0, 32, 1).unwrap());
assert!(desc.valid_storage_types().contains(&ElementType::Float8E4M3));
assert!(!desc.valid_storage_types().contains(&ElementType::Float32));Sourcepub fn is_valid_storage_type(&self, ty: ElementType) -> bool
pub fn is_valid_storage_type(&self, ty: ElementType) -> bool
Returns true if ty is a valid storage type for this descriptor.
§Examples
use hurray_core::{ElementType, PerTensorAffine, QuantizationDescriptor};
let desc = QuantizationDescriptor::PerTensorAffine(
PerTensorAffine::new(1.0, 0).unwrap()
);
assert!(desc.is_valid_storage_type(ElementType::Int8));
assert!(!desc.is_valid_storage_type(ElementType::Float32));Trait Implementations§
Source§impl Clone for QuantizationDescriptor
impl Clone for QuantizationDescriptor
Source§fn clone(&self) -> QuantizationDescriptor
fn clone(&self) -> QuantizationDescriptor
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for QuantizationDescriptor
impl Debug for QuantizationDescriptor
Source§impl PartialEq for QuantizationDescriptor
impl PartialEq for QuantizationDescriptor
Source§fn eq(&self, other: &QuantizationDescriptor) -> bool
fn eq(&self, other: &QuantizationDescriptor) -> bool
self and other values to be equal, and is used by ==.