1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
5pub enum Error {
6 ArgumentError,
8 NoImplementation,
10 Failure,
12}
13
14impl core::fmt::Display for Error {
15 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
16 match self {
17 Error::ArgumentError => write!(f, "Invalid or incompatible arguments"),
18 Error::NoImplementation => write!(f, "No implementation available"),
19 Error::Failure => write!(f, "Operation failure"),
20 }
21 }
22}
23
24pub type Result<T> = core::result::Result<T, Error>;
26
27#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
29pub struct Dims {
30 pub n: i32,
32 pub h: i32,
34 pub w: i32,
36 pub c: i32,
38}
39
40impl Dims {
41 pub const fn new(n: i32, h: i32, w: i32, c: i32) -> Self {
43 Self { n, h, w, c }
44 }
45
46 pub const fn total_size(&self) -> usize {
48 (self.n * self.h * self.w * self.c) as usize
49 }
50}
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
54pub struct Tile {
55 pub w: i32,
57 pub h: i32,
59}
60
61impl Tile {
62 pub const fn new(w: i32, h: i32) -> Self {
64 Self { w, h }
65 }
66}
67
68#[derive(Debug, Clone, Copy, PartialEq, Eq)]
70pub struct Activation {
71 pub min: i32,
73 pub max: i32,
75}
76
77impl Activation {
78 pub const fn new(min: i32, max: i32) -> Self {
80 Self { min, max }
81 }
82
83 pub const fn int8_unconstrained() -> Self {
85 Self {
86 min: i8::MIN as i32,
87 max: i8::MAX as i32,
88 }
89 }
90
91 pub const fn int16_unconstrained() -> Self {
93 Self {
94 min: i16::MIN as i32,
95 max: i16::MAX as i32,
96 }
97 }
98}
99
100#[derive(Debug, Clone, Copy, PartialEq, Eq)]
102pub struct PerTensorQuantParams {
103 pub multiplier: i32,
105 pub shift: i32,
107}
108
109impl PerTensorQuantParams {
110 pub const fn new(multiplier: i32, shift: i32) -> Self {
112 Self { multiplier, shift }
113 }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq)]
118pub struct PerChannelQuantParams<'a> {
119 pub multiplier: &'a [i32],
121 pub shift: &'a [i32],
123}
124
125impl<'a> PerChannelQuantParams<'a> {
126 pub const fn new(multiplier: &'a [i32], shift: &'a [i32]) -> Self {
128 Self { multiplier, shift }
129 }
130}
131
132#[derive(Debug, Clone)]
134pub enum QuantParams<'a> {
135 PerTensor(PerTensorQuantParams),
137 PerChannel(PerChannelQuantParams<'a>),
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Eq)]
143pub struct ConvParams {
144 pub input_offset: i32,
146 pub output_offset: i32,
148 pub stride: Tile,
150 pub padding: Tile,
152 pub dilation: Tile,
154 pub activation: Activation,
156}
157
158#[derive(Debug, Clone, Copy, PartialEq, Eq)]
160pub struct DwConvParams {
161 pub input_offset: i32,
163 pub output_offset: i32,
165 pub ch_mult: i32,
167 pub stride: Tile,
169 pub padding: Tile,
171 pub dilation: Tile,
173 pub activation: Activation,
175}
176
177#[derive(Debug, Clone, Copy, PartialEq, Eq)]
179pub struct FcParams {
180 pub input_offset: i32,
182 pub filter_offset: i32,
184 pub output_offset: i32,
186 pub activation: Activation,
188}
189
190#[derive(Debug, Clone, Copy, PartialEq, Eq)]
192pub struct PoolParams {
193 pub stride: Tile,
195 pub padding: Tile,
197 pub activation: Activation,
199}
200
201#[derive(Debug, Clone, Copy, PartialEq, Eq)]
203pub struct SoftmaxParams {
204 pub input_scale: i32,
206 pub diff_min: i32,
208}
209
210#[derive(Debug)]
212pub struct Context<'a> {
213 pub buf: Option<&'a mut [u8]>,
215}
216
217impl<'a> Context<'a> {
218 pub const fn empty() -> Self {
220 Self { buf: None }
221 }
222
223 pub fn new(buf: &'a mut [u8]) -> Self {
225 Self { buf: Some(buf) }
226 }
227}