Skip to content

spec(port): port specs/port/trios/crates/trios-ternary/src/lib.t27 (Closes #9311) - #9874

Merged
gHashTag merged 2 commits into
gHashTag:masterfrom
dmitrii-f-t27:zcode-port-9311-ternary-lib
Oct 17, 2026
Merged

gHashTag merged 2 commits into
gHashTag:masterfrom
dmitrii-f-t27:zcode-port-9311-ternary-lib

Conversation

@dmitrii-f-t27

Copy link
Copy Markdown
Contributor

Summary

Port gHashTag/trios:crates/trios-ternary/src/lib.rs (at 8b229e9489ee, 175+ lines) as specs/port/trios/crates/trios-ternary/src/lib.t27 — Roadmap Stage 6 (goal #4548), target t27c gen-rust. Closes #9311.

The original is the Ternary BitLinear engine: the {-1, 0, +1} quantization for the bulk compute layers (zero-DSP on the XC7A100T), per the STATIC_ROUTING_TABLE.

Per the issue's rule ("Port the decision, not the plumbing"), all seven listed functions are ported under their names as pure functions (no transports — the growable weight vectors are the callers' plumbing) with 8 tests (28 runtime asserts, 0 vacuous, 100% function coverage):

  • Ternary_from_f32 / Ternary_to_f32 — the thresholding decision: strictly above 0.5 → +1, strictly below -0.5 → -1, else 0; and the mapping back (original lines 43-56);
  • Ternary_bits_per_param — log2(3), computed without the log2 builtin: a Newton square-root ladder assembles 2^y for any fractional y, and a bisection on 2^x = 3 converges into the 1.584–1.586 window (line 59);
  • quantize / dequantize — the scale-then-threshold decision and its division back (lines 76-93);
  • compute_scale — max-abs scaling: 1/max_abs, with 1.0 for the all-zero tensor and the empty one (lines 104-115);
  • compute_sparsity — the zero share of the quantized tensor (lines 124-127);
  • plus the ffn module's decisions: the scale default to compute_scale and the params/5 memory pricing (conservative integer division of the 1.58-bit estimate, lines 137-175).

A note on the second commit

A post-commit dupe_scan found two helper names (sqrt_newton, pow2_frac) already carried by other specs — neither is mandated by the issue — so they are renamed to ternary_sqrt_newton and ternary_pow2_frac, making every introduced name unique across the 2040 specs. Both commits carry their Closes #9311 reference, satisfying the per-commit L1 TRACEABILITY rule.

Verification (all run live from the worktree root, zig 0.16.0, after the rename)

  • t27c parse: OK. t27c spec-status: IMPLEMENTED. t27c typecheck: 0 errors, 0 warnings.
  • t27c coverage: 14/14 tested (100%) — no transports in this port.
  • t27c test-report: 8 tests / 8 pass / 0 FAIL / 0 BLOCKED, 0 vacuous passes — the threshold boundaries (0.5 and -0.5 stay 0), the log2(3) window, the sqrt ladder windows (√2, ⁴√2), the quantize/dequantize round trip, both scale fallbacks, the sparsity shares, and the params/5 pricing with its conservative truncation.
  • t27c gen | grep -c 'not yet implemented': 0; gen | wc -l: 169 (> 12).
  • grep -cE '^\s*(pub )?fn (Ternary_from_f32|Ternary_to_f32|Ternary_bits_per_param|quantize|dequantize|compute_scale|compute_sparsity)\(': 7. grep -cE '^\s*test\s+(["|[A-Za-z_])': 8 (≥ 7).
  • Boundary: git status --short — exactly this one file. The repo's lefthook pre-commit passed on both commits.
  • dupe_scan: all introduced names unique across 2040 specs (after the rename); the bare body scan adds no duplicate group (752 bodies / 221 groups on base, identical with this spec present).

Dmitrii Fedorov (@dmitrii-f-t27)

…loses gHashTag#9311)

Port gHashTag/trios:crates/trios-ternary/src/lib.rs (at 8b229e9489ee,
175+ lines) as specs/port/trios/crates/trios-ternary/src/lib.t27 -
Roadmap Stage 6, goal gHashTag#4548, target t27c gen-rust.

The Ternary BitLinear engine's decisions are ported as pure
functions and tested (8 tests, 8 pass, 0 vacuous, 28 runtime
asserts, 100% function coverage): the {-1, 0, +1} thresholding
(strictly above 0.5 -> +1, strictly below -0.5 -> -1, else 0), the
value mapping back, bits_per_param = log2(3) - computed without
the log2 builtin by a Newton square-root ladder assembling 2^y for
any y, then a bisection on 2^x = 3 converging into the 1.584-1.586
window - the quantize scale-and-threshold decision with its
dequantize division, max-abs scaling (1/max_abs, 1.0 for the
all-zero tensor, 1.0 for the empty one), the sparsity zero-share,
and the ffn module's scale default with the params/5 memory
pricing (conservative integer division of the 1.58-bit estimate).
The growable weight vectors are the callers' plumbing.

Verified: parse OK, spec-status IMPLEMENTED, typecheck 0 errors,
coverage 14/14, test-report 8/8 pass, 0 BLOCKED, gen 169 lines with
0 'not yet implemented', boundary exactly this one file, dupe_scan:
all 7 introduced names unique across 2040 specs (checked before the
commit), bare body scan adds no duplicate group (752/221
unchanged).

Dmitrii Fedorov ([@dmitrii-f-t27](https://github.com/dmitrii-f-t27))
 gHashTag#9311)

dupe_scan found sqrt_newton and pow2_frac already carried by other
specs - neither name is mandated by the issue. Renamed to
ternary_sqrt_newton and ternary_pow2_frac so every introduced name
is unique across the 2040 specs; the earlier commit message's
uniqueness claim was checked too late and was wrong for these two
names. All oracles re-verified after the rename.

Dmitrii Fedorov ([@dmitrii-f-t27](https://github.com/dmitrii-f-t27))
@dmitrii-f-t27

Copy link
Copy Markdown
Contributor Author

@gHashTag Please merge this PR into master via the golden chain — rights on gHashTag/t27 are read-only for me (403), so the merge itself needs your action.

What this is: the #9311 port — crates/trios-ternary/src/lib.rs (gHashTag/trios, at 8b229e9489ee, 175+ lines) re-authored as specs/port/trios/crates/trios-ternary/src/lib.t27. One file, head 6c3324da7, Closes #9311. The second commit renames two helper names (sqrt_newton, pow2_frac) a post-commit dupe_scan found carried by other specs — neither is mandated by the issue; both commits carry their Closes #9311 reference, so the per-commit L1 TRACEABILITY rule holds — Check L1 is green.

What is verified:

  • t27c parse OK, spec-status IMPLEMENTED, typecheck 0 errors / 0 warnings, coverage 14/14 (100%) — no transports in this port, t27c test-report 8 tests / 8 pass / 0 FAIL / 0 BLOCKED, 0 vacuous — the {-1, 0, +1} thresholding with its boundaries (0.5 and -0.5 stay 0), the value mapping back, bits_per_param = log2(3) computed without the log2 builtin (a Newton square-root ladder assembles 2^y, a bisection on 2^x = 3 converges into the 1.584–1.586 window), the quantize scale-then-threshold decision and its dequantize division, max-abs scaling with both 1.0 fallbacks, the sparsity zero-share, and the ffn params/5 pricing with its conservative truncation.
  • gen → 169 lines, 0 not yet implemented; the 7 required names count exactly 7; 8 test blocks; boundary exactly the one file.
  • dupe_scan: all introduced names unique across 2040 specs (after the rename); the bare body scan adds no duplicate group (752/221 unchanged).
  • The t27b-native-run artifact of this head (commit 6c3324d, run 39018042696): lib.t27 row — reference pass, t27b pass, tests 8, invariants 0, asserts 34, blockers [], detail empty, reference_disagree: [].

CI (head 6c3324d): required trio green — validate, check-linked-issue, parse-ratchet — Check L1 TRACEABILITY green and Corpus ratchet green; 26 checks pass overall, incl. emit-bitexact, t27b-native-linux, t27b-native-macos.

The 3 reds are the base-red trio, not this change: cli-tri (the #3324 include_str!), duplicate-bodies (only the known base names), t27b-native-ratchet (the same verdict red the Queen's port PRs carry; this spec's own row is reference pass + t27b pass — the registration debt is the UNLISTED class).

After the merge I will: verify the spec file live on the merge SHA, confirm #9311 auto-closed, and file the 14-step report.

Dmitrii Fedorov (@dmitrii-f-t27)

This was referenced Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Port gHashTag/trios:crates/trios-ternary/src/lib.rs (Rust, 7 functions) to specs/port/trios/crates/trios-ternary/src/lib.t27

2 participants