Q-format in Rust

A Q-format value is an integer read as a fixed-point number: Q16.16 stores 16 integer bits (including the sign) and 16 fractional bits in an i32, so the raw value 65536 means 1.0. For the background and the full C++ feature set, see tinymind (C++)’s Q-Format page. This page covers what the Rust port implements and where it differs.

The networks are generic over a value-type trait, Scalar (called Fixed up to 0.1.5, which remains as a deprecated alias), implemented for these Q formats and for f32 and f64.

The type

pub struct QValue<S, const FRAC: u32>(S);

pub type Q16_16 = QValue<i32, 16>;
pub type Q8_8 = QValue<i16, 8>;

S is the storage type and FRAC the number of fractional bits.

Storage S Wide type for * and / Aliases with lookup tables
i32 i64 Q16_16
i16 i32 Q8_8

The C++ QValue<FixedBits, FractionalBits> chooses its storage type from the bit counts at compile time. Stable Rust cannot compute a type from const-generic arithmetic, so in Rust the storage type is an explicit parameter. A macro generates the methods and operators for each storage type, the same way the Rust standard library implements its integer methods.

Any FRAC from 1 to one less than the storage width compiles (an invalid one is a compile-time error), but tanh, sigmoid and the DenseHead softmax exist only for the formats this port has lookup tables for: Q16_16 and Q8_8. Using them on another format is a compile error, as it is in C++ when the matching table is not compiled in.

Arithmetic

The port implements tinymind (C++)’s default policies, TruncatePolicy and WrapPolicy, with exactly the C++ results:

Operation Behaviour
a + b, a - b Wrap modulo the storage width
a * b Product in the wide type, arithmetic shift right by FRAC (rounds toward negative infinity), wrap to the storage type
a / b (a × 2^FRAC) / b in the wide type, truncating toward zero
a / 0 The storage type’s maximum for a positive a, its minimum for a negative a, 0 for 0
a.mul_round_toward_zero(b) As a * b, but the product is rounded toward zero instead of floored (C++ multiplyRoundTowardZero); SGD uses it for the momentum terms
a.mul_round_nearest(b) As a * b, but the product is rounded to nearest, ties away from zero, instead of floored (C++ multiplyRoundNearest); SGD uses it for the step learning rate * gradient
a.div_int(n) Division by a whole number, computed in i64; not the same as a / QValue::from_int(n)
QValue::from_int(n) n truncated to the integer bits: in Q8.8, 200 becomes -56
comparisons On the raw integer
a.sqrt() (trait Sqrt) Floor of the exact root: integer Newton iteration on raw << FRAC in the wide type; 0 for zero and negative values

Display prints the raw integer, as the C++ operator<< does.

Activation lookup tables

Tanh, sigmoid and the fine-tuning head’s softmax exp read 96-entry tables covering [-6, 6] in steps of 1/8, with linear interpolation between entries and clamping outside the range. The tables are generated from tinymind (C++)’s cpp/lookupTables.cpp by scripts/extract-luts.py, never typed by hand, and CI checks that they still match tinymind (C++). For how the tables are built, see tinymind (C++)’s Activation LUTs page.

The API reference documents every method.


Back to top

Dan McLeran — danmcleran@gmail.com — MIT License

This site uses Just the Docs, a documentation theme for Jekyll.