Skip to main content

QDense

Struct QDense 

Source
pub struct QDense<'a, const IN: usize, const OUT: usize> {
    pub weights: &'a [[i8; IN]; OUT],
    pub biases: Option<&'a [i32; OUT]>,
    pub input_zero_point: i8,
    pub requantizer: Requantizer,
}
Expand description

A quantized fully connected layer with IN int8 inputs and OUT int8 outputs (C++ QDense<int8_t, int8_t, int32_t, int8_t, IN, OUT>).

The layer borrows its weights and biases, so they can live in flash as static or const data. Weights are weights[output][input] (row-major, as PyTorch and TFLite store them), symmetric int8. Biases are in the accumulator’s scale (input_scale * weight_scale); None means no bias.

Fields§

§weights: &'a [[i8; IN]; OUT]

weights[o][i]: weight from input i to output o.

§biases: Option<&'a [i32; OUT]>

One i32 bias per output, or None.

§input_zero_point: i8

The input tensor’s zero point.

§requantizer: Requantizer

Rescales each accumulator to the output tensor’s grid.

Implementations§

Source§

impl<const IN: usize, const OUT: usize> QDense<'_, IN, OUT>

Source

pub fn forward(&self, input: &[i8; IN]) -> [i8; OUT]

One forward pass.

Examples found in repository?
examples/common/imported_lstm_model.rs (line 243)
242pub fn head(input: &[i8; LSTM1_HIDDEN]) -> [i8; NUM_OUTPUTS] {
243    let x0 = FC1.forward(input);
244    PROBS.forward(&x0)
245}
More examples
Hide additional examples
examples/common/imported_onnx_mlp_model.rs (line 106)
105pub fn forward(input: &Input) -> Output {
106    let v0 = FC0.forward(input);
107    let v1 = v0.map(|x| apply_lut(x, &TANH1_LUT));
108    let v2 = FC2.forward(&v1);
109    SOFTMAX3.forward(&v2)
110}
examples/common/imported_classifier_model.rs (line 98)
97pub fn forward(input: &[i8; NUM_INPUTS]) -> [i8; NUM_OUTPUTS] {
98    let x0 = FC1.forward(input).map(|v| qrelu(v, H1_ZERO_POINT));
99    let x1 = FC2.forward(&x0);
100    PROBS.forward(&x1)
101}
examples/common/imported_lstm_i8_model.rs (line 228)
227pub fn head(input: &[i8; LSTM1_HIDDEN]) -> [i8; NUM_OUTPUTS] {
228    let x0 = FC1.forward(input).map(|v| qrelu(v, RELU1_ZERO_POINT));
229    let x1 = FC2.forward(&x0);
230    x1.map(|v| apply_lut(v, &OUT_LUT))
231}
examples/common/imported_mlp_model.rs (line 128)
127pub fn forward(input: &[i8; NUM_INPUTS]) -> [i8; NUM_OUTPUTS] {
128    let x0 = FC1.forward(input).map(|v| qrelu(v, H1_ZERO_POINT));
129    let x1 = FC2.forward(&x0).map(|v| qrelu(v, H2_ZERO_POINT));
130    let x2 = FC3.forward(&x1);
131    x2.map(|v| apply_lut(v, &OUT_LUT))
132}
examples/common/int8_xor_driver.rs (line 109)
106    fn predict(&self, input: &[i8; NUM_INPUTS]) -> (i8, i8) {
107        let hidden = self
108            .fc1
109            .forward(input)
110            .map(|h| qrelu(h, HIDDEN_ZERO_POINT as i8));
111        let logit = self.fc2.forward(&hidden)[0];
112        (logit, apply_lut(logit, &self.sigmoid))
113    }

Trait Implementations§

Source§

impl<'a, const IN: usize, const OUT: usize> Clone for QDense<'a, IN, OUT>

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 OUT: usize> Copy for QDense<'a, IN, OUT>

Source§

impl<'a, const IN: usize, const OUT: usize> Debug for QDense<'a, IN, OUT>

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 OUT: usize> Forget for QDense<'a, IN, OUT>

§

impl<'a, const IN: usize, const OUT: usize> Freeze for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: Freeze, Option<&'a [i32; OUT]>: Freeze,

§

impl<'a, const IN: usize, const OUT: usize> Move for QDense<'a, IN, OUT>

§

impl<'a, const IN: usize, const OUT: usize> RefUnwindSafe for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: RefUnwindSafe, Option<&'a [i32; OUT]>: RefUnwindSafe,

§

impl<'a, const IN: usize, const OUT: usize> Send for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: Send, Option<&'a [i32; OUT]>: Send,

§

impl<'a, const IN: usize, const OUT: usize> Sync for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: Sync, Option<&'a [i32; OUT]>: Sync,

§

impl<'a, const IN: usize, const OUT: usize> Unpin for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: Unpin, Option<&'a [i32; OUT]>: Unpin,

§

impl<'a, const IN: usize, const OUT: usize> UnsafeUnpin for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: UnsafeUnpin, Option<&'a [i32; OUT]>: UnsafeUnpin,

§

impl<'a, const IN: usize, const OUT: usize> UnwindSafe for QDense<'a, IN, OUT>
where &'a [[i8; IN]; OUT]: UnwindSafe, Option<&'a [i32; OUT]>: 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.