pub struct TritFloat(/* private fields */);Expand description
A floating-point number encoded in balanced ternary with a native confidence field.
The confidence field propagates automatically through arithmetic, giving any computation a live uncertainty estimate without a separate Bayesian layer.
Use TritFloat::from_f32 to construct, .to_f32() to read the value,
and .confidence() to read the certainty in [0, 1].
Implementations§
Source§impl TritFloat
impl TritFloat
Sourcepub fn from_f32(x: f32) -> Self
pub fn from_f32(x: f32) -> Self
Convert an f32 to TritFloat with maximum confidence (certainty=1.0).
Sourcepub fn from_f32_with_confidence(x: f32, confidence: f32) -> Self
pub fn from_f32_with_confidence(x: f32, confidence: f32) -> Self
Convert an f32 to TritFloat with a specified confidence in [0, 1].
Sourcepub fn to_f32(self) -> f32
pub fn to_f32(self) -> f32
Convert to f32. Confidence is discarded; use .confidence() separately.
Sourcepub fn mantissa(self) -> u32
pub fn mantissa(self) -> u32
The mantissa as a u32 in [0, 728]. Represents fractional part as M/729.
Sourcepub fn confidence(self) -> f32
pub fn confidence(self) -> f32
Confidence as a float in [0.0, 1.0].
0.0 = completely unknown, 0.5 = neutral/unset, 1.0 = maximally certain.
Sourcepub fn is_uncertain(self) -> bool
pub fn is_uncertain(self) -> bool
True if confidence is below 0.5 (both confidence trits ≤ 0).
Sourcepub fn mul_confidence(a: Self, b: Self) -> f32
pub fn mul_confidence(a: Self, b: Self) -> f32
Propagation rule for multiplication: weakest link. The result is only as confident as the less certain operand.
Sourcepub fn add_confidence(a: Self, b: Self) -> f32
pub fn add_confidence(a: Self, b: Self) -> f32
Propagation rule for addition: average the evidence.
Sourcepub fn mul(self, rhs: Self) -> Self
pub fn mul(self, rhs: Self) -> Self
Multiplication with confidence propagation (weakest-link rule).
Sourcepub fn dot(a: &[Self], b: &[Self]) -> Self
pub fn dot(a: &[Self], b: &[Self]) -> Self
Dot product of two slices of TritFloats.
Confidence of the result = min confidence across all terms. Zero-phase terms are skipped entirely (@sparseskip at activation level).
Sourcepub fn dot_with_skips(a: &[Self], b: &[Self]) -> (Self, usize)
pub fn dot_with_skips(a: &[Self], b: &[Self]) -> (Self, usize)
Dot product returning (result, skip_count) for sparsity instrumentation.
Sourcepub fn should_route(self, threshold: f32) -> bool
pub fn should_route(self, threshold: f32) -> bool
Returns true if this activation should be routed to an expert.
Uncertain activations (confidence < threshold) can skip expensive expert layers entirely — the confidence field directly gates MoE routing.
threshold = minimum confidence to route (suggested: 0.3–0.5)
Sourcepub fn div(self, rhs: Self) -> Self
pub fn div(self, rhs: Self) -> Self
Division with weakest-link confidence. Division by zero returns zero
with 0 confidence — the caller can detect this via is_uncertain.
Sourcepub fn recip(self) -> Self
pub fn recip(self) -> Self
Reciprocal: 1/x. Confidence preserved; zero input returns 0-confidence zero.
Sourcepub fn sqrt(self) -> Self
pub fn sqrt(self) -> Self
Square root. Negative input returns 0-confidence zero (not a real number).
Sourcepub fn clamp(self, lo: f32, hi: f32) -> Self
pub fn clamp(self, lo: f32, hi: f32) -> Self
Clamp the value to [lo, hi]. Confidence is preserved unchanged.
Sourcepub fn cmp_trit(self, rhs: Self) -> Self
pub fn cmp_trit(self, rhs: Self) -> Self
Ternary comparison: returns +1 if self > rhs, −1 if self < rhs, 0 if equal. Confidence = min(conf_self, conf_rhs) — comparison is only as reliable as inputs.
Sourcepub fn softmax(slice: &[Self]) -> Vec<Self>
pub fn softmax(slice: &[Self]) -> Vec<Self>
Numerically stable softmax over a slice of TritFloats.
Values are computed in f32; each output element carries the minimum confidence of all inputs (softmax mixes every element, so the whole slice’s certainty bounds the result).
Sourcepub fn phase_digits(slice: &[Self]) -> Vec<u8> ⓘ
pub fn phase_digits(slice: &[Self]) -> Vec<u8> ⓘ
Extract phase digits (0=neg, 1=zero, 2=pos) for a slice into a Vec<u8>.
The pre-scan buffer: a single contiguous pass over raw u32 values (% 3) before the arithmetic loop. Separating phase-check from f32 math eliminates branch misprediction in the hot loop at high sparsity (≥50% zeros).
Sourcepub fn pack_phases_u64(slice: &[Self]) -> u64
pub fn pack_phases_u64(slice: &[Self]) -> u64
Pack zero-phase flags for up to 64 TritFloats into a u64 bitmask.
Bit i = 1 if slice[i].is_zero(), else 0. mask.count_ones() instantly
gives the skip count for a 64-element chunk. mask == 0 means all
elements are active — no branch needed in the arithmetic loop.
This is the preparation layer for AVX2 vectorization of the dot product.
Sourcepub fn dot_prescan(a: &[Self], b: &[Self]) -> (Self, usize)
pub fn dot_prescan(a: &[Self], b: &[Self]) -> (Self, usize)
Dot product with two-pass pre-scan for reduced branch misprediction.
Pass 1: extract all phase flags into u8 arrays (cache-hot, no branching). Pass 2: arithmetic only for active (non-zero-phase) pairs.
Outperforms dot_with_skips at ≥50% sparsity where misprediction of the
inline zero-check dominates. At low sparsity the extra allocation cost
makes it slightly slower — profile before choosing.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for TritFloat
impl<'de> Deserialize<'de> for TritFloat
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Copy for TritFloat
impl Eq for TritFloat
impl StructuralPartialEq for TritFloat
Auto Trait Implementations§
impl Freeze for TritFloat
impl RefUnwindSafe for TritFloat
impl Send for TritFloat
impl Sync for TritFloat
impl Unpin for TritFloat
impl UnsafeUnpin for TritFloat
impl UnwindSafe for TritFloat
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more