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: i8Zero point of the first input.
input_b_zero_point: i8Zero point of the second input.
left_shift: i32Left 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: i32Q0.31 multiplier rescaling the first input to the common scale.
input_a_shift: i32Shift for input_a_multiplier (positive divides, as in Requantizer).
input_b_multiplier: i32Q0.31 multiplier rescaling the second input to the common scale.
input_b_shift: i32Shift for input_b_multiplier.
output_requantizer: RequantizerRescales the i32 sum to the output: its multiplier and shift, the
output zero point and the saturation range.
Implementations§
Source§impl QAdd
impl QAdd
Sourcepub fn forward<T: Tensor>(&self, a: &T, b: &T, out: &mut T)
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.