Skip to main content

Scalar

Trait Scalar 

Source
pub trait Scalar:
    Copy
    + Default
    + PartialOrd
    + Add<Output = Self>
    + AddAssign
    + Sub<Output = Self>
    + Mul<Output = Self>
    + Div<Output = Self> {
    const ZERO: Self;
    const ONE: Self;

    // Required methods
    fn pow2_neg(n: u32) -> Self;
    fn div_int(self, n: i32) -> Self;

    // Provided methods
    fn mul_round_toward_zero(self, rhs: Self) -> Self { ... }
    fn mul_round_nearest(self, rhs: Self) -> Self { ... }
}
Expand description

A network’s value type: the arithmetic a network needs from the numbers it computes with. This is the Rust counterpart of the implicit “ValueType concept” that the C++ templates rely on: in C++ a missing operator is a template instantiation error, here it is a trait-bound error at the call site.

Implemented for the QValue formats and for f32 and f64: the networks run in fixed point or in floating point, as tinymind (C++)’s do when instantiated with a Q format, float or double. Called Fixed up to 0.1.5; that name remains as a deprecated alias (Fixed).

Required Associated Constants§

Source

const ZERO: Self

0.0

Source

const ONE: Self

1.0

Required Methods§

Source

fn pow2_neg(n: u32) -> Self

2^-n

Source

fn div_int(self, n: i32) -> Self

Divide by a whole number (C++ operator/=(int)).

Provided Methods§

Source

fn mul_round_toward_zero(self, rhs: Self) -> Self

self * rhs rounded toward zero (C++ multiplyRoundTowardZero), used for SGD’s momentum and acceleration terms so that a delta weight no gradient refreshes decays to exactly 0. The default is self * rhs, right for floating point; the Q formats round their product toward zero instead of flooring it.

Source

fn mul_round_nearest(self, rhs: Self) -> Self

self * rhs rounded to nearest, ties away from zero (C++ multiplyRoundNearest), used for SGD’s step learning rate * gradient, so that steps smaller than the format’s resolution do not all drift one way. The default is self * rhs, right for floating point; the Q formats round their product to nearest instead of flooring it.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety".

Implementations on Foreign Types§

Source§

impl Scalar for f32

Source§

const ZERO: Self = 0.0

Source§

const ONE: Self = 1.0

Source§

fn pow2_neg(n: u32) -> Self

Source§

fn div_int(self, n: i32) -> Self

Source§

impl Scalar for f64

Source§

const ZERO: Self = 0.0

Source§

const ONE: Self = 1.0

Source§

fn pow2_neg(n: u32) -> Self

Source§

fn div_int(self, n: i32) -> Self

Implementors§

Source§

impl<const FRAC: u32> Scalar for QValue<i16, FRAC>

Source§

const ZERO: Self = Self::ZERO

Source§

const ONE: Self = Self::ONE

Source§

impl<const FRAC: u32> Scalar for QValue<i32, FRAC>

Source§

const ZERO: Self = Self::ZERO

Source§

const ONE: Self = Self::ONE