#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Error {
ArgumentError,
NoImplementation,
Failure,
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::ArgumentError => write!(f, "Invalid or incompatible arguments"),
Error::NoImplementation => write!(f, "No implementation available"),
Error::Failure => write!(f, "Operation failure"),
}
}
}
pub type Result<T> = core::result::Result<T, Error>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Dims {
pub n: i32,
pub h: i32,
pub w: i32,
pub c: i32,
}
impl Dims {
pub const fn new(n: i32, h: i32, w: i32, c: i32) -> Self {
Self { n, h, w, c }
}
pub const fn total_size(&self) -> usize {
(self.n * self.h * self.w * self.c) as usize
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Tile {
pub w: i32,
pub h: i32,
}
impl Tile {
pub const fn new(w: i32, h: i32) -> Self {
Self { w, h }
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Activation {
pub min: i32,
pub max: i32,
}
impl Activation {
pub const fn new(min: i32, max: i32) -> Self {
Self { min, max }
}
pub const fn int8_unconstrained() -> Self {
Self {
min: i8::MIN as i32,
max: i8::MAX as i32,
}
}
pub const fn int16_unconstrained() -> Self {
Self {
min: i16::MIN as i32,
max: i16::MAX as i32,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PerTensorQuantParams {
pub multiplier: i32,
pub shift: i32,
}
impl PerTensorQuantParams {
pub const fn new(multiplier: i32, shift: i32) -> Self {
Self { multiplier, shift }
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PerChannelQuantParams<'a> {
pub multiplier: &'a [i32],
pub shift: &'a [i32],
}
impl<'a> PerChannelQuantParams<'a> {
pub const fn new(multiplier: &'a [i32], shift: &'a [i32]) -> Self {
Self { multiplier, shift }
}
}
#[derive(Debug, Clone)]
pub enum QuantParams<'a> {
PerTensor(PerTensorQuantParams),
PerChannel(PerChannelQuantParams<'a>),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConvParams {
pub input_offset: i32,
pub output_offset: i32,
pub stride: Tile,
pub padding: Tile,
pub dilation: Tile,
pub activation: Activation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DwConvParams {
pub input_offset: i32,
pub output_offset: i32,
pub ch_mult: i32,
pub stride: Tile,
pub padding: Tile,
pub dilation: Tile,
pub activation: Activation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FcParams {
pub input_offset: i32,
pub filter_offset: i32,
pub output_offset: i32,
pub activation: Activation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PoolParams {
pub stride: Tile,
pub padding: Tile,
pub activation: Activation,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct SoftmaxParams {
pub input_scale: i32,
pub diff_min: i32,
}
#[derive(Debug)]
pub struct Context<'a> {
pub buf: Option<&'a mut [u8]>,
}
impl<'a> Context<'a> {
pub const fn empty() -> Self {
Self { buf: None }
}
pub fn new(buf: &'a mut [u8]) -> Self {
Self { buf: Some(buf) }
}
}