Skip to main content

QConv2D

Struct QConv2D 

Source
pub struct QConv2D<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize = 1, const SW: usize = 1, const PT: usize = 0, const PB: usize = 0, const PL: usize = 0, const PR: usize = 0> {
    pub weights: &'a [[[[i8; C]; KW]; KH]; F],
    pub biases: Option<&'a [i32; F]>,
    pub input_zero_point: i8,
    pub requantizer: Requantizer,
}
Expand description

A quantized 2D convolution with one requantizer for the whole output (C++ QConv2D<int8_t, int8_t, int32_t, int8_t, H, W, C, KH, KW, SH, SW, F, PT, PB, PL, PR>).

C input channels, a KH x KW kernel, F filters, strides SH and SW (default 1) and padding PT, PB, PL, PR rows and columns (default 0, VALID). Weights are weights[filter][row][column][channel] (OHWI, as TFLite stores them), symmetric int8; biases are in the accumulator’s scale. The layer borrows them, so they can live in flash.

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

// Two 2x2 filters over a 3x3 single-channel input: a 2x2x2 output.
static WEIGHTS: [[[[i8; 1]; 2]; 2]; 2] = [[[[1], [1]], [[1], [1]]], [[[1], [0]], [[0], [-1]]]];
let conv: QConv2D<'_, 1, 2, 2, 2> = QConv2D {
    weights: &WEIGHTS,
    biases: None,
    input_zero_point: 0,
    requantizer: Requantizer { multiplier: 1 << 30, shift: -1, zero_point: 0, qmin: -128, qmax: 127 },
};
let input = [[[1], [2], [3]], [[4], [5], [6]], [[7], [8], [9]]];
let mut output = [[[0i8; 2]; 2]; 2];
conv.forward(&input, &mut output);
// The sum of each window, and its top-left minus bottom-right.
assert_eq!(output, [[[12, -4], [16, -4]], [[24, -4], [28, -4]]]);

Fields§

§weights: &'a [[[[i8; C]; KW]; KH]; F]

weights[f][kh][kw][c].

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

One i32 bias per filter, or None.

§input_zero_point: i8

The input tensor’s zero point, which also fills the padding.

§requantizer: Requantizer

Rescales every accumulator to the output tensor’s grid.

Implementations§

Source§

impl<const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> QConv2D<'_, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

Source

pub fn forward<const H: usize, const W: usize, const OH: usize, const OW: usize>( &self, input: &[[[i8; C]; W]; H], output: &mut [[[i8; F]; OW]; OH], )

One forward pass over an H x W x C input into an OH x OW x F output. OH and OW must be what the kernel, strides and padding produce (a compile error otherwise).

Trait Implementations§

Source§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Clone for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

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 C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Copy for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

Source§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Debug for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Freeze for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> RefUnwindSafe for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Send for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Sync for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> Unpin for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> UnsafeUnpin for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

§

impl<'a, const C: usize, const KH: usize, const KW: usize, const F: usize, const SH: usize, const SW: usize, const PT: usize, const PB: usize, const PL: usize, const PR: usize> UnwindSafe for QConv2D<'a, C, KH, KW, F, SH, SW, PT, PB, PL, PR>

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.