Skip to main content

QLstmCell

Struct QLstmCell 

Source
pub struct QLstmCell<'a, const IN: usize, const HID: usize, C: CellStorage = i8> {
Show 22 fields pub input_weights: &'a [[[i8; IN]; HID]; 4], pub recurrent_weights: &'a [[[i8; HID]; HID]; 4], pub biases: &'a [[i32; HID]; 4], pub input_zero_point: i8, pub hidden_zero_point: i8, pub cell_zero_point: C, pub input_to_lut_multiplier: [i32; 4], pub input_to_lut_shift: [i32; 4], pub recurrent_to_lut_multiplier: [i32; 4], pub recurrent_to_lut_shift: [i32; 4], pub sigmoid_lut: &'a [i8; 256], pub tanh_lut: &'a [i8; 256], pub f_times_c_multiplier: i32, pub f_times_c_shift: i32, pub i_times_g_multiplier: i32, pub i_times_g_shift: i32, pub cell_qmin: C, pub cell_qmax: C, pub cell_to_tanh_multiplier: i32, pub cell_to_tanh_shift: i32, pub tanh_cell_lut: &'a [i8; 256], pub output_requantizer: Requantizer,
}
Expand description

One time step of a quantized LSTM cell with IN int8 inputs and HID hidden units (C++ QLSTMCell), in the TFLite / CMSIS-NN quantization model.

The cell borrows its weights, biases and lookup tables, so they can live in flash, and forward updates the caller’s hidden and cell state in place: the caller runs the time loop. Gates are in TFLite’s order: input i, forget f, cell g, output o, and the weights are gate-major, input_weights[gate][unit][input], symmetric int8.

Each step, for each gate and hidden unit, the input and recurrent sums (i32, each less its tensor’s zero point) are rescaled to the gate LUT’s input grid and added to the bias, which is in that grid too; the result, saturated to int8, indexes the gate’s LUT. The LUTs’ output grids are fixed: sigmoid scale 1/256 with zero point -128, tanh scale 1/128 with zero point 0. Then

  • c = clamp(rescale(f * (c_prev - c_zp)) + rescale(i * g) + c_zp, cell_qmin, cell_qmax)
  • h = output_requantizer(o * tanh(c)), with tanh(c) from tanh_cell_lut after rescaling c - c_zp to its input grid.

Build the multipliers and shifts from float scales with calibration::build_lstm_params, the biases with calibration::quantize_gate_biases and the LUTs with calibration::build_sigmoid_lut and build_tanh_lut (std feature), or write the integers out as constants.

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

// With zero weights and biases every gate sees 0: with these LUTs each
// sigmoid gate is 0.5 (0 in its grid) and the cell input g is 0. Each step
// the cell state halves (f_times_c is 2^-8: f centred at 128, times c, over
// 256) and the hidden state, o * tanh(c), is 0 (the tanh table is all 0).
static ZERO_LUT: [i8; 256] = [0; 256];
let cell: QLstmCell<'_, 2, 3> = QLstmCell {
    input_weights: &[[[0; 2]; 3]; 4],
    recurrent_weights: &[[[0; 3]; 3]; 4],
    biases: &[[0; 3]; 4],
    input_zero_point: 0,
    hidden_zero_point: 0,
    cell_zero_point: 0,
    input_to_lut_multiplier: [1 << 30; 4],
    input_to_lut_shift: [0; 4],
    recurrent_to_lut_multiplier: [1 << 30; 4],
    recurrent_to_lut_shift: [0; 4],
    sigmoid_lut: &ZERO_LUT,
    tanh_lut: &ZERO_LUT,
    f_times_c_multiplier: 1 << 30,
    f_times_c_shift: 7,
    i_times_g_multiplier: 1 << 30,
    i_times_g_shift: 7,
    cell_qmin: -127,
    cell_qmax: 127,
    cell_to_tanh_multiplier: 1 << 30,
    cell_to_tanh_shift: 0,
    tanh_cell_lut: &ZERO_LUT,
    output_requantizer: Requantizer { multiplier: 1 << 30, shift: 7, zero_point: 0, qmin: -128, qmax: 127 },
};
let mut hidden = [5i8, -5, 0];
let mut state = [100i8, -60, 7];
cell.forward(&[10, -10], &mut hidden, &mut state);
assert_eq!(state, [50, -30, 4]); // 3.5 rounds away from zero
assert_eq!(hidden, [0, 0, 0]);

Fields§

§input_weights: &'a [[[i8; IN]; HID]; 4]

input_weights[gate][unit][input], gates in the order i, f, g, o.

§recurrent_weights: &'a [[[i8; HID]; HID]; 4]

recurrent_weights[gate][unit][previous unit].

§biases: &'a [[i32; HID]; 4]

biases[gate][unit], in the LUT input grid.

§input_zero_point: i8

The input tensor’s zero point.

§hidden_zero_point: i8

The hidden state’s zero point (also the output requantizer’s).

§cell_zero_point: C

The cell state’s zero point.

§input_to_lut_multiplier: [i32; 4]

Per gate, the Q0.31 multiplier rescaling the input sum to the LUT input grid.

§input_to_lut_shift: [i32; 4]

Per gate, the shift for input_to_lut_multiplier (positive divides, as in Requantizer).

§recurrent_to_lut_multiplier: [i32; 4]

Per gate, the multiplier rescaling the recurrent sum to the LUT input grid.

§recurrent_to_lut_shift: [i32; 4]

Per gate, the shift for recurrent_to_lut_multiplier.

§sigmoid_lut: &'a [i8; 256]

Sigmoid table for the i, f and o gates (output scale 1/256, zero point -128).

§tanh_lut: &'a [i8; 256]

Tanh table for the g gate (output scale 1/128, zero point 0).

§f_times_c_multiplier: i32

Multiplier rescaling f * (c_prev - c_zp) to the cell grid.

§f_times_c_shift: i32

Shift for f_times_c_multiplier.

§i_times_g_multiplier: i32

Multiplier rescaling i * g to the cell grid.

§i_times_g_shift: i32

Shift for i_times_g_multiplier.

§cell_qmin: C

Lowest cell state. If cell_qmin > cell_qmax the cell state is cell_qmax.

§cell_qmax: C

Highest cell state.

§cell_to_tanh_multiplier: i32

Multiplier rescaling c - c_zp to tanh_cell_lut’s input grid.

§cell_to_tanh_shift: i32

Shift for cell_to_tanh_multiplier.

§tanh_cell_lut: &'a [i8; 256]

Tanh table for the cell state (output scale 1/128, zero point 0).

§output_requantizer: Requantizer

Rescales o * tanh(c) to the hidden state’s grid, adds its zero point and saturates.

Implementations§

Source§

impl<const IN: usize, const HID: usize, C: CellStorage> QLstmCell<'_, IN, HID, C>

Source

pub fn forward( &self, input: &[i8; IN], hidden: &mut [i8; HID], cell: &mut [C; HID], )

One time step: reads input, hidden (h at t-1) and cell (c at t-1), and leaves h and c at t in hidden and cell.

Trait Implementations§

Source§

impl<'a, const IN: usize, const HID: usize, C: Clone + CellStorage> Clone for QLstmCell<'a, IN, HID, C>

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<'a, const IN: usize, const HID: usize, C: Copy + CellStorage> Copy for QLstmCell<'a, IN, HID, C>

Source§

impl<'a, const IN: usize, const HID: usize, C: Debug + CellStorage> Debug for QLstmCell<'a, IN, HID, C>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, const IN: usize, const HID: usize, C> Freeze for QLstmCell<'a, IN, HID, C>
where C: Freeze,

§

impl<'a, const IN: usize, const HID: usize, C> RefUnwindSafe for QLstmCell<'a, IN, HID, C>
where C: RefUnwindSafe,

§

impl<'a, const IN: usize, const HID: usize, C> Send for QLstmCell<'a, IN, HID, C>
where C: Send,

§

impl<'a, const IN: usize, const HID: usize, C> Sync for QLstmCell<'a, IN, HID, C>
where C: Sync,

§

impl<'a, const IN: usize, const HID: usize, C> Unpin for QLstmCell<'a, IN, HID, C>
where C: Unpin,

§

impl<'a, const IN: usize, const HID: usize, C> UnsafeUnpin for QLstmCell<'a, IN, HID, C>
where C: UnsafeUnpin,

§

impl<'a, const IN: usize, const HID: usize, C> UnwindSafe for QLstmCell<'a, IN, HID, C>
where C: UnwindSafe,

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.