Skip to main content

QGruCell

Struct QGruCell 

Source
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 zero

Fields§

§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: i8

The input tensor’s zero point.

§hidden_zero_point: i8

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: i32

Multiplier rescaling r * (h - h_zp) to the hidden grid.

§r_times_h_shift: i32

Shift for r_times_h_multiplier.

§one_minus_z_times_n_multiplier: i32

Multiplier rescaling (1 - z) * n to the hidden grid.

§one_minus_z_times_n_shift: i32

Shift for one_minus_z_times_n_multiplier.

§z_times_h_multiplier: i32

Multiplier rescaling z * (h - h_zp) to the hidden grid.

§z_times_h_shift: i32

Shift for z_times_h_multiplier.

§output_qmin: i8

Lowest hidden state. If output_qmin > output_qmax the hidden state is output_qmax.

§output_qmax: i8

Highest hidden state.

Implementations§

Source§

impl<const IN: usize, const HID: usize> QGruCell<'_, IN, HID>

Source

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

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

Trait Implementations§

Source§

impl<'a, const IN: usize, const HID: usize> Clone for QGruCell<'a, IN, HID>

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> Copy for QGruCell<'a, IN, HID>

Source§

impl<'a, const IN: usize, const HID: usize> Debug for QGruCell<'a, IN, HID>

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> Freeze for QGruCell<'a, IN, HID>

§

impl<'a, const IN: usize, const HID: usize> RefUnwindSafe for QGruCell<'a, IN, HID>

§

impl<'a, const IN: usize, const HID: usize> Send for QGruCell<'a, IN, HID>

§

impl<'a, const IN: usize, const HID: usize> Sync for QGruCell<'a, IN, HID>

§

impl<'a, const IN: usize, const HID: usize> Unpin for QGruCell<'a, IN, HID>

§

impl<'a, const IN: usize, const HID: usize> UnsafeUnpin for QGruCell<'a, IN, HID>

§

impl<'a, const IN: usize, const HID: usize> UnwindSafe for QGruCell<'a, IN, HID>

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.