Skip to main content

PerBlockAffine

Struct PerBlockAffine 

Source
pub struct PerBlockAffine { /* private fields */ }
Expand description

Quantization parameters for per-block affine quantization.

The tensor is divided into fixed-size contiguous blocks along axis. Each block carries its own scale (and optionally zero_point). Partial trailing blocks are permitted; see the spec for padding rules.

The dequantization formula for element q with block index b is:

s      = scale[b]           (widened to float32 if float16/bfloat16)
z      = zero_point[b]      (0 if symmetric)
x_real = s * (q - z)

§Wire format

Total descriptor length: 24 bytes (including the 4-byte header).

OffsetFieldType
4axisuint32 LE
8block_sizeuint32 LE
12scale_buffer_indexuint32 LE
16zero_point_buffer_indexuint32 LE (0xFFFFFFFF if symmetric)
20scale_type_taguint8 (0x01, 0x02, or 0x03)
21_reserveduint8[3] (must be 0x00)

§Design notes

PartialEq, Eq, and Hash are all derived because this struct contains no floating-point fields.

Copy because the struct is ≤ 24 bytes with no Drop glue.

§Examples

use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float32).unwrap();
assert!(q.is_symmetric());
assert_eq!(q.block_size(), 32);
assert_eq!(q.scale_type(), ElementType::Float32);

Implementations§

Source§

impl PerBlockAffine

Source

pub fn new_asymmetric( axis: u32, block_size: u32, scale_buffer_index: u32, zero_point_buffer_index: u32, scale_type: ElementType, ) -> Result<Self>

Creates an asymmetric PerBlockAffine descriptor.

§Errors
§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_asymmetric(0, 64, 1, 2, ElementType::Float16).unwrap();
assert!(!q.is_symmetric());
assert_eq!(q.zero_point_buffer_index(), Some(2));
Source

pub fn new_symmetric( axis: u32, block_size: u32, scale_buffer_index: u32, scale_type: ElementType, ) -> Result<Self>

Creates a symmetric PerBlockAffine descriptor.

In symmetric mode the zero-point array is implicit (all zeros); no zero-point buffer entry is required.

§Errors
§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(0, 128, 1, ElementType::BFloat16).unwrap();
assert!(q.is_symmetric());
assert_eq!(q.zero_point_buffer_index(), None);
Source

pub fn is_symmetric(&self) -> bool

Returns true if this descriptor uses symmetric quantization (no zero point).

§Examples
use hurray_core::{ElementType, PerBlockAffine};

assert!(PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float32)
    .unwrap()
    .is_symmetric());
Source

pub fn axis(&self) -> u32

Returns the quantization axis index.

§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(2, 32, 1, ElementType::Float32).unwrap();
assert_eq!(q.axis(), 2);
Source

pub fn block_size(&self) -> u32

Returns the number of logical elements per block along axis.

§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(0, 64, 1, ElementType::Float32).unwrap();
assert_eq!(q.block_size(), 64);
Source

pub fn scale_buffer_index(&self) -> u32

Returns the buffer table index of the per-block scale array.

§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(0, 32, 3, ElementType::Float32).unwrap();
assert_eq!(q.scale_buffer_index(), 3);
Source

pub fn zero_point_buffer_index(&self) -> Option<u32>

Returns the buffer table index of the per-block zero-point array, or None if this descriptor is symmetric.

None maps to the wire sentinel 0xFFFFFFFF.

§Examples
use hurray_core::{ElementType, PerBlockAffine};

let asym = PerBlockAffine::new_asymmetric(0, 32, 1, 2, ElementType::Float32).unwrap();
assert_eq!(asym.zero_point_buffer_index(), Some(2));

let sym = PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float32).unwrap();
assert_eq!(sym.zero_point_buffer_index(), None);
Source

pub fn scale_type(&self) -> ElementType

Returns the element type used for scale values.

§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float16).unwrap();
assert_eq!(q.scale_type(), ElementType::Float16);
Source

pub fn num_blocks_per_axis(&self, shape_axis: u64) -> u64

Computes the number of blocks along axis for a given shape_axis size.

Uses ceil(shape_axis / block_size).

Returns 0 when shape_axis == 0 per the ADR-007 empty-axis carve-out: an empty quantization axis produces zero blocks and zero-byte parameter buffers.

§Examples
use hurray_core::{ElementType, PerBlockAffine};

let q = PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float32).unwrap();
assert_eq!(q.num_blocks_per_axis(64), 2);
assert_eq!(q.num_blocks_per_axis(65), 3); // partial trailing block
assert_eq!(q.num_blocks_per_axis(0), 0);  // ADR-007 empty-axis carve-out
Source

pub fn validate_against_shape_axis(&self, shape_axis: u64) -> Result<()>

Validates this descriptor against the resolved shape_axis size.

Rejects if shape_axis is the DYNAMIC sentinel (u64::MAX), or if shape_axis > 0 and block_size > shape_axis.

Does not check shape_axis == 0 (ADR-007 carve-out: the upper-bound check is waived for empty axes).

§Errors
§Examples
use hurray_core::{ElementType, PerBlockAffine, DYNAMIC};

let q = PerBlockAffine::new_symmetric(0, 32, 1, ElementType::Float32).unwrap();
assert!(q.validate_against_shape_axis(64).is_ok());
assert!(q.validate_against_shape_axis(32).is_ok());
assert!(q.validate_against_shape_axis(0).is_ok()); // ADR-007: waived
assert!(q.validate_against_shape_axis(16).is_err()); // block_size > shape_axis
assert!(q.validate_against_shape_axis(DYNAMIC).is_err()); // dynamic dimension
Source

pub fn valid_storage_types() -> &'static [ElementType]

Returns the set of storage ElementTypes that are valid for this scheme.

Per docs/spec/quantization/per-block-affine.md § Valid Storage Types.

WHY &'static [ElementType]: no allocation per call (design decision #5).

§Examples
use hurray_core::{ElementType, PerBlockAffine};

assert!(PerBlockAffine::valid_storage_types().contains(&ElementType::Int8));
assert!(PerBlockAffine::valid_storage_types().contains(&ElementType::Int4));
assert!(!PerBlockAffine::valid_storage_types().contains(&ElementType::Int16));

Trait Implementations§

Source§

impl Clone for PerBlockAffine

Source§

fn clone(&self) -> PerBlockAffine

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 PerBlockAffine

Source§

impl Debug for PerBlockAffine

Source§

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

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

impl Eq for PerBlockAffine

Source§

impl Hash for PerBlockAffine

Source§

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

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 PartialEq for PerBlockAffine

Source§

fn eq(&self, other: &PerBlockAffine) -> 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 PerBlockAffine

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.