pub struct QGruCell<'a, const IN: usize, const HID: usize> {Show 19 fields
pub input_weights: &'a [[[i8; IN]; HID]; 3],
pub recurrent_weights: &'a [[[i8; HID]; HID]; 3],
pub biases: &'a [[i32; HID]; 3],
pub input_zero_point: i8,
pub hidden_zero_point: i8,
pub input_to_lut_multiplier: [i32; 3],
pub input_to_lut_shift: [i32; 3],
pub recurrent_to_lut_multiplier: [i32; 3],
pub recurrent_to_lut_shift: [i32; 3],
pub sigmoid_lut: &'a [i8; 256],
pub tanh_lut: &'a [i8; 256],
pub r_times_h_multiplier: i32,
pub r_times_h_shift: i32,
pub one_minus_z_times_n_multiplier: i32,
pub one_minus_z_times_n_shift: i32,
pub z_times_h_multiplier: i32,
pub z_times_h_shift: i32,
pub output_qmin: i8,
pub output_qmax: i8,
}Expand description
One time step of a quantized GRU cell with IN int8 inputs and HID
hidden units (C++ QGRUCell), the reset-before-multiply variant:
r = sigmoid(W_r x + R_r h + b_r),z = sigmoid(W_z x + R_z h + b_z)n = tanh(W_n x + R_n (r * h) + b_n)h' = (1 - z) * n + z * h
This is not PyTorch’s nn.GRU (nor Keras’ default, reset_after=True),
which computes n = tanh(W_n x + b_in + r * (R_n h + b_hn)), so a model
trained that way does not run on this cell.
Gates are in the order reset r, update z, new n, and the weights are
gate-major, input_weights[gate][unit][input], symmetric int8. Each gate’s
sums are rescaled to the LUT input grid and added to the bias as in
QLstmCell, with the same fixed LUT output grids. r * h is rescaled to
the hidden state’s grid (and saturated to int8) before R_n multiplies it;
h' is rescaled to that grid too and clamped to [output_qmin, output_qmax].
The cell borrows its weights, biases and LUTs, and
forward updates the caller’s hidden state in place.
Build the multipliers and shifts from float scales with
calibration::build_gru_params (std feature), or write them out.
use tinymind::int8::QGruCell;
// With zero weights and biases, these LUTs make the update gate z 0.5 (0
// in the sigmoid grid) and the candidate n 0, so each step halves the
// hidden state: z * h, with z centred at 128 and z_times_h 2^-8.
static ZERO_LUT: [i8; 256] = [0; 256];
let cell: QGruCell<'_, 2, 3> = QGruCell {
input_weights: &[[[0; 2]; 3]; 3],
recurrent_weights: &[[[0; 3]; 3]; 3],
biases: &[[0; 3]; 3],
input_zero_point: 0,
hidden_zero_point: 0,
input_to_lut_multiplier: [1 << 30; 3],
input_to_lut_shift: [0; 3],
recurrent_to_lut_multiplier: [1 << 30; 3],
recurrent_to_lut_shift: [0; 3],
sigmoid_lut: &ZERO_LUT,
tanh_lut: &ZERO_LUT,
r_times_h_multiplier: 1 << 30,
r_times_h_shift: 7,
one_minus_z_times_n_multiplier: 1 << 30,
one_minus_z_times_n_shift: 7,
z_times_h_multiplier: 1 << 30,
z_times_h_shift: 7,
output_qmin: -128,
output_qmax: 127,
};
let mut hidden = [64i8, -40, 7];
cell.forward(&[10, -10], &mut hidden);
assert_eq!(hidden, [32, -20, 4]); // 3.5 rounds away from zeroFields§
§input_weights: &'a [[[i8; IN]; HID]; 3]input_weights[gate][unit][input], gates in the order r, z, n.
recurrent_weights: &'a [[[i8; HID]; HID]; 3]recurrent_weights[gate][unit][previous unit].
biases: &'a [[i32; HID]; 3]biases[gate][unit], in the LUT input grid.
input_zero_point: i8The input tensor’s zero point.
The hidden state’s zero point.
input_to_lut_multiplier: [i32; 3]Per gate, the Q0.31 multiplier rescaling the input sum to the LUT input grid.
input_to_lut_shift: [i32; 3]Per gate, the shift for input_to_lut_multiplier (positive divides, as
in Requantizer).
recurrent_to_lut_multiplier: [i32; 3]Per gate, the multiplier rescaling the recurrent sum to the LUT input grid.
recurrent_to_lut_shift: [i32; 3]Per gate, the shift for recurrent_to_lut_multiplier.
sigmoid_lut: &'a [i8; 256]Sigmoid table for the r and z gates (output scale 1/256, zero point -128).
tanh_lut: &'a [i8; 256]Tanh table for the n gate (output scale 1/128, zero point 0).
r_times_h_multiplier: i32Multiplier rescaling r * (h - h_zp) to the hidden grid.
r_times_h_shift: i32Shift for r_times_h_multiplier.
one_minus_z_times_n_multiplier: i32Multiplier rescaling (1 - z) * n to the hidden grid.
one_minus_z_times_n_shift: i32Shift for one_minus_z_times_n_multiplier.
z_times_h_multiplier: i32Multiplier rescaling z * (h - h_zp) to the hidden grid.
z_times_h_shift: i32Shift for z_times_h_multiplier.
output_qmin: i8Lowest hidden state. If output_qmin > output_qmax the hidden state is
output_qmax.
output_qmax: i8Highest hidden state.