gMath

The inference module as a gMath extension

Status: IMPLEMENTED 2026-10-17 (phase 7.5 of the width-as-type work; see section 10). Sections 1 to 9 are the proposal of 2026-10-10 and the owner's decisions of 2026-10-15, kept as written.

1. What is being decided

g_math::tq19 (the TQ1.9 ternary weight formats, their dot products and matvecs, the five-trit row-scaled form, the planar and hybrid repacks, the bfloat16 / binary16 decoders, the two quantisers, the AVX2 kernels) ships today inside the core crate behind the inference feature. The core's mathematics is open under MIT OR Apache-2.0 and stays so. The question is whether the inference layer should instead be a separate crate that depends on the core through its public surface only, maintained and licensed on its own, released in lockstep with the inference engine that drives it, and serving as the worked example of how an extension is written.

Two facts frame the decision:

Measured on the tree (2026-10-10): 4,716 lines in src/fixed_point/tq19/ (ops 999, planar 858, hybrid 653, tq5 597, mod 468, rowscaled 368, simd 253, bits 179, quantize 169) plus compute_tier.rs (172); ten integration test files reference the two modules; rayon is the feature's only dependency.

2. The boundary

Stays in the core (open) Moves to the extension
Fixed<S, F>, FixedNum, the word traits, every engine, the canonical layer tq19 wholesale: TQ19Matrix, PlanarTQ19, HybridTQ19, RowScaledTQ19, RowScaledTQ5, tq19_dot, trit_dot, packed_trit_dot, the matvec family (plain, _par, _batch, _batch_par, _q2f forms), TqWord, WideOutput, bits, quantize, simd, the trit decode table and the SCALE / MAX_RAW / MIN_RAW constants
fused (softmax, silu, rms norm, dot_many, sigmoid_mul, entropy): general kernels, public since 0.3.0, consumed by the geometry crates too the rayon dependency and the _par / _batch_par forms
compute_tier (exp, ln, sqrt, sinhcosh, sigmoid, softplus, ln1p, from_fixed, to_fixed, one, ceiling at 2F): the documented facade over the kernels, which the extension itself needs the integration tests tq19_validation, tq5_validation, weight_bits_validation, batch_into_validation, tq19_bench, and the tq19 sections of defects_065_validation, narrowing_defects_validation, width_types_validation, differential_065_validation
the inference feature name disappears with the module docs/README_TERNARY.md's TQ1.9 half (the balanced-ternary domain half stays)

Recommendation on the two borderline modules: compute_tier stays in the core and loses its feature gate. It is 172 lines of wrappers that give the kernels a documented, semver-bound name; the extension consumes it, the engine consumes it directly for its activations, and it carries no inference specific knowledge. fused stays for the same reason. If the owner wants the engine's activation kernels (sigmoid and softplus at the compute tier) closed as well, they are two functions that can be copied into the extension without touching the core; the exp and ln engines they call are the open mathematics either way.

3. The contract the extension consumes

An extension must not depend on #[doc(hidden)] items: they are not semver-bound and phase 7 may rename them. The inference module today uses these core items; all are public and documented except the ones marked.

Item Used for Status
Fixed<S, F>: raw, from_raw, raw_slice, from_raw_slice (#[repr(transparent)]) the activation and output buffers public
Compute<T> the wide accumulator and the _q2f outputs public
Storage: Compute, widen, narrow accumulate, narrow once public, documented
WideInt: one, bit, shr_bits, from_i128_checked, checked_add_wide the scale and round step public, documented
OverflowDetected the quantisers' errors public
I256, I512 the TqWord impls for the wide words public
compute_tier::{from_fixed, to_fixed, one} the five-trit scale path public
FixedNum::from_raw_storage (one use) a raw word to a value hidden; replace with Fixed::from_raw

So the core needs no new API for the extraction. What it should gain is a statement: a short docs/EXTENSIONS.md naming the contract (the items above plus FixedNum's documented methods) as the surface an extension may rely on, and the rule that hidden items are not part of it.

4. The extension crate

5. Gates

  1. Differential, both directions: g_math_inference evaluated against g_math 0.6.7 with inference (the last open module) on the 4d corpus (tq19_dot, trit_dot, packed_trit_dot, the q2f forms, the four matrix forms with their batch and parallel variants, the five-trit form, both decoders, both quantisers; outcome = value | error | panic), and against the core's own copy for as long as the core still carries one. Zero differences is the exit criterion, as for every width phase.
  2. The all-widths identities of tests/width_types_validation.rs's inference section, moved as they are.
  3. Perf: the phase 4d harness (one binary per crate, release, quiet machine, isolated calls) on the same matvec sizes; parity within the band, the hybrid matvec's two codegen states noted as in ROADMAP.
  4. The core after removal: full sweep, PUBLIC_API.md regenerated, no rayon in the dependency tree without the feature, the inference feature gone from Cargo.toml and the check-cfg list, README and docs/README_TERNARY.md updated, CHANGELOG entry under 0.7.0 naming the move and the last open version.

6. Licensing mechanics (options, owner's call)

In every option the open history remains: 0.6.7's tq19 is the floor anyone can build on. What the extension protects is the work after it: the formats, kernels and fast paths written for the engine, and the pace at which they appear.

7. The extension pattern, as a guide for other authors

The extraction doubles as the reference for writing any gMath extension. The rules it demonstrates:

  1. Depend on the documented surface only: Fixed, Compute, the word traits as bounds, compute_tier, the raw-slice boundary. Never a hidden item, never pub(crate).
  2. Be generic over the format from the first line: Fixed<S, F> in signatures, a local sealed trait per word family for the parts that differ per word, hidden hook methods with safe defaults for fast paths.
  3. Convert at the edge: the caller's buffers enter through Fixed::from_raw_slice and leave as Vec<Fixed<S, F>> (or through an _into form); no public function takes or returns a bare word.
  4. Prove bit-identity, not plausibility: a differential gate against the previous version of the same code, outcome classes included.
  5. Measure on a quiet machine with one binary per crate; a perturbed harness before believing a 2 percent reading.
  6. Keep mpmath or exact-integer references for every numeric claim; the inference module's are integer oracles (i128 row dot) and the decoders' bit tables.

8. Schedule

After phase 7 and before the 0.7.0 release (phase 8), as "phase 7.5": one move script, the two differential gates, the perf run, the core removal with its docs, then the engine's switch. Estimated at one working day of gate time on top of the mechanical move, because the module is already in its extension shape.

9. Open questions for the owner

9a. Decisions (owner sign-off, 2026-10-15)

Question Decision
How 0.7.0 ships As g_math 0.7.0 (not a new crate name)
Crate name g_math_inference
Keep the word "inference" Keep it
Where compute_tier lives Stays in the open core, without a feature gate
How licensees get the extension crates.io with a proprietary licence file. Owner's reasoning: a company using it will do so for audit or legal reasons, so use becomes evident through audits and procedures; the protection is legal, by choice. Superseded 2026-10-17, see section 10: crates.io under PolyForm Noncommercial 1.0.0 plus a grant for maniference licensees, commercial terms in the extension's COMMERCIAL.md.
When the move happens After phase 7c, before the 0.7.0 release

Owner's note on timing: a 0.6.8 release may separate the module from the 0.6 line before 0.7.0. To settle with the owner before it is cut: removing the inference feature in a patch release breaks every consumer that pins "0.6" with that feature (Cargo resolves them to 0.6.8 on their next update). A 0.6.8 that keeps the feature, marks it deprecated and points to g_math_inference avoids that; the removal itself then lands in 0.7.0. Either way the published 0.4.x to 0.6.7 releases keep the module under their open licences.

10. As built (2026-10-17)