Skip to main content

QuantizationDescriptor

Enum QuantizationDescriptor 

Source
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

Source

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);
Source

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);
Source

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
§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);
Source

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);
Source

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
§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);
Source

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));
Source

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

Source§

fn clone(&self) -> QuantizationDescriptor

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 Debug for QuantizationDescriptor

Source§

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

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

impl PartialEq for QuantizationDescriptor

Source§

fn eq(&self, other: &QuantizationDescriptor) -> 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.
Source§

impl StructuralPartialEq for QuantizationDescriptor

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