Skip to main content

QAdd

Struct QAdd 

Source
pub struct QAdd {
    pub input_a_zero_point: i8,
    pub input_b_zero_point: i8,
    pub left_shift: i32,
    pub input_a_multiplier: i32,
    pub input_a_shift: i32,
    pub input_b_multiplier: i32,
    pub input_b_shift: i32,
    pub output_requantizer: Requantizer,
}
Expand description

Elementwise addition of two int8 tensors with their own scales and zero points, into a third (C++ QAdd, TFLite ADD): the skip connections of ResNet and MobileNet-V2.

Each input, less its zero point, is shifted left by left_shift (20 in TFLite) and rescaled by its own multiplier to a common scale, twice the larger input scale; the i32 sum is then requantized to the output. A fused ReLU raises output_requantizer.qmin to the output zero point. Build one from float scales with calibration::build_add (std feature), or write the integers out as constants.

use tinymind::int8::{QAdd, Requantizer};

// Both inputs and the output share one scale (left shift 20, then 1/2, then
// 2^-19 back): the sum of the real values, on the same grid.
const ADD: QAdd = QAdd {
    input_a_zero_point: 0,
    input_b_zero_point: 0,
    left_shift: 20,
    input_a_multiplier: 1 << 30,
    input_a_shift: 0,
    input_b_multiplier: 1 << 30,
    input_b_shift: 0,
    output_requantizer: Requantizer {
        multiplier: 1 << 30,
        shift: 18,
        zero_point: 0,
        qmin: -128,
        qmax: 127,
    },
};
let a = [[[10i8, -20]; 3]; 2];
let b = [[[5i8, 100]; 3]; 2];
let mut sum = [[[0i8; 2]; 3]; 2];
ADD.forward(&a, &b, &mut sum);
assert_eq!(sum[1][2], [15, 80]);

Tensors of different shapes do not compile:

ⓘ
let a = [[[0i8; 2]; 3]; 2];
let b = [[[0i8; 2]; 2]; 3]; // 3 x 2, not 2 x 3
let mut sum = [[[0i8; 2]; 3]; 2];
add.forward(&a, &b, &mut sum);

Fields§

§input_a_zero_point: i8

Zero point of the first input.

§input_b_zero_point: i8

Zero point of the second input.

§left_shift: i32

Left shift applied to each input less its zero point, before rescaling: head room for the multipliers. tinymind (C++)’s contract is 0..=23 (the shifted value fits an i32); a negative one is treated as 0, and a larger one saturates.

§input_a_multiplier: i32

Q0.31 multiplier rescaling the first input to the common scale.

§input_a_shift: i32

Shift for input_a_multiplier (positive divides, as in Requantizer).

§input_b_multiplier: i32

Q0.31 multiplier rescaling the second input to the common scale.

§input_b_shift: i32

Shift for input_b_multiplier.

§output_requantizer: Requantizer

Rescales the i32 sum to the output: its multiplier and shift, the output zero point and the saturation range.

Implementations§

Source§

impl QAdd

Source

pub fn forward<T: Tensor>(&self, a: &T, b: &T, out: &mut T)

out = a + b element by element. The three tensors have the same type, so the same shape: a flat [i8; N], a row [[i8; C]; W] or an NHWC tensor [[[i8; C]; W]; H].

From 256 elements, each input’s rescaling is tabulated first for all 256 int8 values (2 KB of stack), so an element costs two table reads, an add and the requantizer: about a ninth of the cycles on a Cortex-M4. The table holds the same values the element-wise form computes.

Trait Implementations§

Source§

impl Clone for QAdd

Source§

fn clone(&self) -> Self

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 QAdd

Source§

impl Debug for QAdd

Source§

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

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

impl Eq for QAdd

Source§

impl PartialEq for QAdd

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for QAdd

Auto Trait Implementations§

§

impl Freeze for QAdd

§

impl RefUnwindSafe for QAdd

§

impl Send for QAdd

§

impl Sync for QAdd

§

impl Unpin for QAdd

§

impl UnsafeUnpin for QAdd

§

impl UnwindSafe for QAdd

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.