Skip to main content

QValue

Struct QValue 

Source
pub struct QValue<S, const FRAC: u32>(/* private fields */);
Expand description

A signed fixed-point value stored in S (i16 or i32) with FRAC fractional bits.

The derived PartialOrd/Ord compare the raw integer, which is exactly what the C++ comparison operators do (they compare getValue()).

FRAC must be at least 1 and less than the storage width; anything else is a compile-time error wherever the format’s arithmetic is used:

ⓘ
// 40 fractional bits do not fit in i32 storage.
let x = tinymind::QValue::<i32, 40>::default();
let _ = x.to_f64();

Implementations§

Source§

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

Source

pub const FRACTIONAL_BITS: u32 = FRAC

Number of fractional bits (FRAC).

Source

pub const FIXED_BITS: u32

Number of integer bits, including the sign bit (storage width - FRAC).

Source

pub const ZERO: Self

0.0

Source

pub const ONE: Self

1.0. When FRAC is one less than the storage width (Q1.15, Q1.31) there are no integer bits besides the sign, 1.0 is not representable, and this wraps to -1.0 (the storage type’s minimum).

Source

pub const fn from_raw(raw: i32) -> Self

Build from raw LSBs (C++ QValue(FullWidthFieldType) / setValue(raw)).

Source

pub const fn from_int(n: i32) -> Self

Build from a whole number (C++ QValue(fixedPart, 0)). The fixed part is truncated to FIXED_BITS, as the C++ fromParts does, which is the same as shifting left and letting the high bits fall off: in Q8.8, 200 becomes -56.

Source

pub const fn pow2_neg(n: u32) -> Self

2^-n, used for tinymind (C++)’s default learning/momentum/acceleration rates.

For n > FRAC, 2^-n is below the format’s resolution and the result is ZERO: the value truncated to the format, as TruncatePolicy rounds every other result. pow2_neg(0) is ONE, with the same caveat for formats without integer bits.

Source

pub const fn raw(self) -> i32

Raw LSBs (C++ getValue()).

Source

pub fn to_f64(self) -> f64

Real-number view, computed as the C++ example does: raw / 2^FRAC in f64.

Source

pub fn from_f64_trunc(x: f64) -> Self

Truncating conversion from f64 (C++ setValue(static_cast<FullWidthValueType>(x * 2^FRAC))). Out-of-range input saturates; in C++ it is undefined behaviour.

Source

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

self * rhs with the product rounded toward zero instead of floored (C++ multiplyRoundTowardZero): the same widening and wrap to the storage width as *. SGD uses it for the momentum and acceleration terms: floored, 0.5 * (-1 LSB) stays -1 LSB, so a delta weight no gradient refreshes would never decay.

Source

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

self * rhs with the product rounded to nearest, ties away from zero, instead of floored (C++ multiplyRoundNearest): the same widening and wrap to the storage width as *. SGD uses it for the step learning rate * gradient: floored, a step smaller than one LSB is 0 when positive but a whole -1 LSB when negative, so weights with small gradients drift down.

Source

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

Divide by a whole number, truncating toward zero (C++ QValue::operator/=(int)). This is not the same as dividing by QValue::from_int(n).

Source

pub const fn is_within_zero_tolerance(self) -> bool

C++ ZeroToleranceCalculator: true when |raw| < 1 << (FRAC - 7) for formats with more than 7 fractional bits (512 LSBs for Q16.16, 2 for Q8.8), else |raw| < 2.

Source§

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

Source

pub const FRACTIONAL_BITS: u32 = FRAC

Number of fractional bits (FRAC).

Source

pub const FIXED_BITS: u32

Number of integer bits, including the sign bit (storage width - FRAC).

Source

pub const ZERO: Self

0.0

Source

pub const ONE: Self

1.0. When FRAC is one less than the storage width (Q1.15, Q1.31) there are no integer bits besides the sign, 1.0 is not representable, and this wraps to -1.0 (the storage type’s minimum).

Source

pub const fn from_raw(raw: i16) -> Self

Build from raw LSBs (C++ QValue(FullWidthFieldType) / setValue(raw)).

Source

pub const fn from_int(n: i32) -> Self

Build from a whole number (C++ QValue(fixedPart, 0)). The fixed part is truncated to FIXED_BITS, as the C++ fromParts does, which is the same as shifting left and letting the high bits fall off: in Q8.8, 200 becomes -56.

Source

pub const fn pow2_neg(n: u32) -> Self

2^-n, used for tinymind (C++)’s default learning/momentum/acceleration rates.

For n > FRAC, 2^-n is below the format’s resolution and the result is ZERO: the value truncated to the format, as TruncatePolicy rounds every other result. pow2_neg(0) is ONE, with the same caveat for formats without integer bits.

Source

pub const fn raw(self) -> i16

Raw LSBs (C++ getValue()).

Source

pub fn to_f64(self) -> f64

Real-number view, computed as the C++ example does: raw / 2^FRAC in f64.

Source

pub fn from_f64_trunc(x: f64) -> Self

Truncating conversion from f64 (C++ setValue(static_cast<FullWidthValueType>(x * 2^FRAC))). Out-of-range input saturates; in C++ it is undefined behaviour.

Source

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

self * rhs with the product rounded toward zero instead of floored (C++ multiplyRoundTowardZero): the same widening and wrap to the storage width as *. SGD uses it for the momentum and acceleration terms: floored, 0.5 * (-1 LSB) stays -1 LSB, so a delta weight no gradient refreshes would never decay.

Source

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

self * rhs with the product rounded to nearest, ties away from zero, instead of floored (C++ multiplyRoundNearest): the same widening and wrap to the storage width as *. SGD uses it for the step learning rate * gradient: floored, a step smaller than one LSB is 0 when positive but a whole -1 LSB when negative, so weights with small gradients drift down.

Source

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

Divide by a whole number, truncating toward zero (C++ QValue::operator/=(int)). This is not the same as dividing by QValue::from_int(n).

Source

pub const fn is_within_zero_tolerance(self) -> bool

C++ ZeroToleranceCalculator: true when |raw| < 1 << (FRAC - 7) for formats with more than 7 fractional bits (512 LSBs for Q16.16, 2 for Q8.8), else |raw| < 2.

Trait Implementations§

Source§

impl Activation<QValue<i16, 8>> for Tanh

Source§

fn activate(x: Q8_8) -> Q8_8

The activation of the weighted input sum x.
Source§

fn derivative(y: Q8_8) -> Q8_8

Derivative expressed in terms of the activation’s output y, the way tinymind (C++) computes it during back-propagation.
Source§

impl Activation<QValue<i16, 8>> for Sigmoid

Source§

fn activate(x: Q8_8) -> Q8_8

The activation of the weighted input sum x.
Source§

fn derivative(y: Q8_8) -> Q8_8

Derivative expressed in terms of the activation’s output y, the way tinymind (C++) computes it during back-propagation.
Source§

impl Activation<QValue<i32, 16>> for Tanh

Source§

fn activate(x: Q16_16) -> Q16_16

The activation of the weighted input sum x.
Source§

fn derivative(y: Q16_16) -> Q16_16

Derivative expressed in terms of the activation’s output y, the way tinymind (C++) computes it during back-propagation.
Source§

impl Activation<QValue<i32, 16>> for Sigmoid

Source§

fn activate(x: Q16_16) -> Q16_16

The activation of the weighted input sum x.
Source§

fn derivative(y: Q16_16) -> Q16_16

Derivative expressed in terms of the activation’s output y, the way tinymind (C++) computes it during back-propagation.
Source§

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

Source§

type Output = QValue<i32, FRAC>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

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

Source§

type Output = QValue<i16, FRAC>

The resulting type after applying the + operator.
Source§

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

Performs the + operation. Read more
Source§

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

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

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

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl<S: Clone, const FRAC: u32> Clone for QValue<S, FRAC>

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<S: Copy, const FRAC: u32> Copy for QValue<S, FRAC>

Source§

impl<S: Debug, const FRAC: u32> Debug for QValue<S, FRAC>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<S: Default, const FRAC: u32> Default for QValue<S, FRAC>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<S: Display, const FRAC: u32> Display for QValue<S, FRAC>

Prints the raw LSBs, matching the C++ operator<< (not the real value).

Source§

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

Formats the value using the given formatter. Read more
Source§

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

Source§

type Output = QValue<i32, FRAC>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

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

Source§

type Output = QValue<i16, FRAC>

The resulting type after applying the / operator.
Source§

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

Performs the / operation. Read more
Source§

impl<S: Eq, const FRAC: u32> Eq for QValue<S, FRAC>

Source§

impl<S: Hash, const FRAC: u32> Hash for QValue<S, FRAC>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

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

Source§

type Output = QValue<i32, FRAC>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

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

Source§

type Output = QValue<i16, FRAC>

The resulting type after applying the * operator.
Source§

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

Performs the * operation. Read more
Source§

impl<S: Ord, const FRAC: u32> Ord for QValue<S, FRAC>

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl<S: PartialEq, const FRAC: u32> PartialEq for QValue<S, FRAC>

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl<S: PartialOrd, const FRAC: u32> PartialOrd for QValue<S, FRAC>

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

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

Source§

const ZERO: Self = Self::ZERO

0.0
Source§

const ONE: Self = Self::ONE

1.0
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)).
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.
Source§

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

Source§

const ZERO: Self = Self::ZERO

0.0
Source§

const ONE: Self = Self::ONE

1.0
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)).
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.
Source§

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

Source§

fn sqrt(self) -> Self

floor(sqrt(self)) in this format; 0 for self <= 0.
Source§

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

Source§

fn sqrt(self) -> Self

floor(sqrt(self)) in this format; 0 for self <= 0.
Source§

impl<S: PartialEq, const FRAC: u32> StructuralPartialEq for QValue<S, FRAC>

Source§

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

Source§

type Output = QValue<i32, FRAC>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more
Source§

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

Source§

type Output = QValue<i16, FRAC>

The resulting type after applying the - operator.
Source§

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

Performs the - operation. Read more

Auto Trait Implementations§

§

impl<S, const FRAC: u32> Freeze for QValue<S, FRAC>
where S: Freeze,

§

impl<S, const FRAC: u32> RefUnwindSafe for QValue<S, FRAC>
where S: RefUnwindSafe,

§

impl<S, const FRAC: u32> Send for QValue<S, FRAC>
where S: Send,

§

impl<S, const FRAC: u32> Sync for QValue<S, FRAC>
where S: Sync,

§

impl<S, const FRAC: u32> Unpin for QValue<S, FRAC>
where S: Unpin,

§

impl<S, const FRAC: u32> UnsafeUnpin for QValue<S, FRAC>
where S: UnsafeUnpin,

§

impl<S, const FRAC: u32> UnwindSafe for QValue<S, FRAC>
where S: 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> Fixed for T
where T: Scalar,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.