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)), withtanh(c)fromtanh_cell_lutafter rescalingc - c_zpto 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: i8The input tensor’s zero point.
The hidden state’s zero point (also the output requantizer’s).
cell_zero_point: CThe 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: i32Multiplier rescaling f * (c_prev - c_zp) to the cell grid.
f_times_c_shift: i32Shift for f_times_c_multiplier.
i_times_g_multiplier: i32Multiplier rescaling i * g to the cell grid.
i_times_g_shift: i32Shift for i_times_g_multiplier.
cell_qmin: CLowest cell state. If cell_qmin > cell_qmax the cell state is cell_qmax.
cell_qmax: CHighest cell state.
cell_to_tanh_multiplier: i32Multiplier rescaling c - c_zp to tanh_cell_lut’s input grid.
cell_to_tanh_shift: i32Shift 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: RequantizerRescales o * tanh(c) to the hidden state’s grid, adds its zero point
and saturates.