Skip to main content

PerChannelAffine

Struct PerChannelAffine 

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

Quantization parameters for per-channel affine quantization.

Each slice along axis has its own scale (and optionally zero_point). The parameter arrays are stored in separate buffers referenced by index in the tensor descriptor’s buffer table.

The dequantization formula for element q at logical index [i_0, …, i_{rank-1}] is:

c      = i_axis
x_real = scale[c] * (q - zero_point[c])

When the SYMMETRIC flag is set, zero_point[c] is treated as 0.

§Wire format

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

OffsetFieldType
4axisuint32 LE
8scale_buffer_indexuint32 LE
12zero_point_buffer_indexuint32 LE (0xFFFFFFFF if symmetric)
16scale_type_taguint8 (must be 0x03 in v1)
17_reserveduint8[3] (must be 0x00)

§Design notes

PartialEq, Eq, and Hash are all derived because this struct contains no floating-point fields — all fields are integers or Option<u32>.

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

§Examples

use hurray_core::PerChannelAffine;

// Asymmetric: separate scale (buffer 1) and zero-point (buffer 2) arrays.
let q = PerChannelAffine::new_asymmetric(0, 1, 2).unwrap();
assert!(!q.is_symmetric());
assert_eq!(q.zero_point_buffer_index(), Some(2));

// Symmetric: only a scale buffer, zero-point is implicitly zero.
let s = PerChannelAffine::new_symmetric(0, 1).unwrap();
assert!(s.is_symmetric());
assert_eq!(s.zero_point_buffer_index(), None);

Implementations§

Source§

impl PerChannelAffine

Source

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

Creates an asymmetric PerChannelAffine descriptor.

§Errors
  • Error::InvalidQuantization — scale_buf equals 0xFFFFFFFF (the zero-point sentinel value, which is not a valid scale buffer index).
§Examples
use hurray_core::PerChannelAffine;

let q = PerChannelAffine::new_asymmetric(0, 1, 2).unwrap();
assert!(!q.is_symmetric());
assert_eq!(q.scale_buffer_index(), 1);
assert_eq!(q.zero_point_buffer_index(), Some(2));
Source

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

Creates a symmetric PerChannelAffine descriptor.

In symmetric mode the zero-point array is implicit (all zeros); no zero-point buffer entry is required in the tensor descriptor’s buffer table.

§Errors
§Examples
use hurray_core::PerChannelAffine;

let q = PerChannelAffine::new_symmetric(1, 2).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::PerChannelAffine;

assert!(PerChannelAffine::new_symmetric(0, 1).unwrap().is_symmetric());
assert!(!PerChannelAffine::new_asymmetric(0, 1, 2).unwrap().is_symmetric());
Source

pub fn axis(&self) -> u32

Returns the quantization axis index.

§Examples
use hurray_core::PerChannelAffine;

let q = PerChannelAffine::new_symmetric(3, 1).unwrap();
assert_eq!(q.axis(), 3);
Source

pub fn scale_buffer_index(&self) -> u32

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

§Examples
use hurray_core::PerChannelAffine;

let q = PerChannelAffine::new_symmetric(0, 5).unwrap();
assert_eq!(q.scale_buffer_index(), 5);
Source

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

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

None maps to the wire sentinel 0xFFFFFFFF.

§Examples
use hurray_core::PerChannelAffine;

let asym = PerChannelAffine::new_asymmetric(0, 1, 2).unwrap();
assert_eq!(asym.zero_point_buffer_index(), Some(2));

let sym = PerChannelAffine::new_symmetric(0, 1).unwrap();
assert_eq!(sym.zero_point_buffer_index(), None);
Source

pub fn scale_type(&self) -> ElementType

Returns the element type used for scale values (always Float32 in v1).

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

let q = PerChannelAffine::new_symmetric(0, 1).unwrap();
assert_eq!(q.scale_type(), ElementType::Float32);
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-channel-affine.md § Valid Storage Types.

WHY &'static [ElementType]: no allocation per call; slice::contains over ≤10 items beats any hash structure (design decision #5).

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

assert!(PerChannelAffine::valid_storage_types().contains(&ElementType::Int8));
assert!(!PerChannelAffine::valid_storage_types().contains(&ElementType::Float32));

Trait Implementations§

Source§

impl Clone for PerChannelAffine

Source§

fn clone(&self) -> PerChannelAffine

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 PerChannelAffine

Source§

impl Debug for PerChannelAffine

Source§

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

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

impl Eq for PerChannelAffine

Source§

impl Hash for PerChannelAffine

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 PerChannelAffine

Source§

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

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.