Skip to main content

Module lmodel3

Module lmodel3 

Source
Expand description

Learned probabilities for TCG3 code-blocks, in 32-bit integer arithmetic end to end, so a WGSL kernel (no i64) reproduces every probability.

  • Features: the TCG3-visible context (Feats::raw: block-local causal neighbours, coarser-level parent, residual magnitudes) plus the class one-hot, as signed Q8 log-magnitudes (integer lg_q8).
  • Network: M1b’s MLP (learned::Net, ReLU, Q8 weights, Q16 biases), evaluated with i32 accumulators: weights clamped to ±2047 (±8.0), activations to [0, 2^14] (hidden) / ±2^14 (outputs), biases to ±2^24, so no partial sum can overflow (28·2^11·2^14 + 2^24 < 2^31).
  • Output: a 2-component discretized logistic mixture (π logit, μ₁, μ₂, log₂ s₁, log₂ s₂, all Q8). CDF in Q16 from two integer tables (σ in Q16 over |z| ≤ 16, 2^{f/256} in Q16), built on the CPU with basic IEEE operations only and uploaded to the GPU as data.
  • Binarization (as M1b, without the APM): zero flag, sign, exp-Golomb bucket by unary tail ratios, mantissa by integer bisection; each decision’s P(1) is an integer ratio of Q16 CDF masses.

Structs§

AdCfg
Adaptive-layer settings (stream header).
Adapt
Per-lane adaptive state over the MLP output: an adaptive probability map (secondary estimation) per decision type, interpolated over AB stretch-domain bins, count-based power-of-two rate. Integer only; reset per block.
Mix3
Mixture parameters.
Net32
A network prepared for 32-bit inference.

Constants§

AB
APM bins per context (step 1.0 in the logistic domain, ±8).
AC
APM contexts.
NFL
Features actually used (the rest of the NF inputs are zero).
ZM
|z| range of the σ table (Q8).

Functions§

ad_init
A lane’s fresh adaptive state as the kernels hold it: (p << 8) | count per bin (count 0), the layout of the adaptive residual graph’s adt.
code_learned
Code residual r under mx on lane.
code_learned_ad
code_learned through the lane’s adaptive layer.
feat_vec3
Integer feature vector of a coefficient.
ratio16
P(1)·2^16 from masses (num ≤ den ≤ 2^16), clamped for rANS.
sig16
σ(z), z in Q8 → Q16.
stretch_tab
Inverse of sig16 on a p >> 4 grid: the smallest Q8 z with sig16(z) ≥ p (p = 16·i + 8), so stretch(p) ∈ [−ZM, ZM].
tables
σ table (Q16, k/256, k = 0..=ZM) and 2^{f/256} table (Q16).
zval
d · 2^{−ls/256} (both Q8; result Q8) saturated to ±(ZM+1), with ls clamped to [−16·256, 24·256]: t = ⌊d·m/2^16⌉ with the Q16 mantissa m = 2^{(256−f)/256}, then an arithmetic or saturating shift.