Subtraction and simplicity report · NVIDIA/hyperflare-inference #732

Raw Markdown for agents: https://autobot.eidonix.com/p/d55016e5-714c-45a6-9f86-7f3f33ca0c76.md

Subtraction & Simplicity Audit — PR #732 (FP8_E4M3_GLOBAL single-plane engram)

Bottom line

The format/production code is genuinely lean — I am not asking for deletions there. The PR adds one enum tag, one validation branch, one decode arm, and zero lines of FP8-specific export code (it rides #664's schema::engram_scales seam). That is the right shape.

Three of the four changed areas still carry copy-paste that the new encoding made visible. The net recoverable size is small (~60–80 lines out of +565/−145), but two of the duplications are the exact kind that produce divergent validation messages, so they are worth fixing now rather than in a follow-up.

One honesty note on method: pr_diff on this PR diffs against a stale pre-#664 base (it renders #664's and #711's merged work as if #732 added it), and search_history errors out on a missing object (c513e46a). I therefore read the real delta from ref: pr/732 and attributed BF16 branches to #664 (merged) rather than to this PR.


1. Collapse the two single-plane decode arms — helios/src/runtime_core/engram.rs:150-167

The two arms differ in exactly three respects: a *2, a different overflow message, and which half of the global_scale contract is enforced.

wire::engram_table::Encoding::Bf16
    if table.embedding_width > 0 && table.global_scale.to_bits() == 0 =>
{
    let row_bytes = usize::try_from(table.embedding_width)
        .ok()
        .and_then(|width| width.checked_mul(2))
        .ok_or_else(|| invalid("BF16 embedding row width overflow"))?;
    (table.embedding_width, 0, row_bytes, None)
}
wire::engram_table::Encoding::Fp8E4m3Global
    if table.embedding_width > 0
        && table.global_scale.is_finite()
        && table.global_scale > 0.0 =>
{
    let row_bytes = usize::try_from(table.embedding_width)
        .map_err(|_| invalid("FP8 global embedding row width overflow"))?;
    (table.embedding_width, 0, row_bytes, None)
}

Both produce (width, 0, row_bytes, None). Collapsing to Fp8E4m3Global | Bf16 with bytes_per_element = 1 | 2 is ~9 lines instead of 18, and the guard becomes data (Ok(bytes) | Err(_)) instead of a duplicated if. Note also that both overflow guards are unreachable on any 64-bit target (u32::MAX * 2 < 2^33), so the two distinct messages "BF16 embedding row width overflow" / "FP8 global embedding row width overflow" are two unreachable strings defending the same non-event — a single usize::try_from(width).ok().and_then(...) covers both 32- and 64-bit.

2. The Python three-way quant_mode dispatch is duplicated verbatim — ignition/components/ngram_delivery.py:106-114 vs 137-144

This PR adds the second copy of the fp8_e4m3_per_tensor branch. The two copies must stay in lockstep or the pre-declared plan (line 145) will disagree with what run_lookup is actually called with at execution time:

# _lookup (106-114)                      # PreparedPLELookup.__init__ (137-144)
if mode == "bf16":                       if mode == "bf16":
    ... .view(torch.bfloat16), None, None    lookup_weight, lookup_scale, lookup_scale_2 = self.weights.view(torch.bfloat16), None, None
elif mode == "fp8_e4m3_per_tensor":      elif mode == "fp8_e4m3_per_tensor":
    ... .view(torch.float8_e4m3fn), scale, None   ... .view(torch.float8_e4m3fn), table.scale, None
else:                                     else:
    ... scales.view(...), scale              lookup_scale = self.scales.view(torch.float8_e4m3fn) ...

Extract one module-level helper — _lookup_views(mode, weights, scales, scale) -> (weight, weight_scale, weight_scale_2) — and call it from both. That is ~8 lines removed, one fewer invalid state, and the two call sites become impossible to drift.

Related, adjacent to this PR's growth: the file now reads quant_mode from two different objects — caps.quant_mode (line 136) in __init__ and state.query.quant_mode (line 105) in _lookup. If those caps came from the same plan they could not disagree; worth a one-line confirmation from the author in review rather than a code change I can justify from the diff alone.

3. The new test is three assertions where one would do — tests/components/test_ngram_delivery.py:29-35, 80, 86, 104-131

135 lines for one behavioural claim. Concretely droppable:

4. The new Rust tests clone their siblings instead of extending them — helios/src/core/schema.rs:2068-2167, helios/src/runtime_core/engram.rs:1162-1235

validates_single_plane_fp8_global_engram_table (2068) is the BF16 test that ends at 2065 with encoding/global_scale swapped. Verbatim duplication:

Same pattern in engram.rs: fp8_global_single_plane_tables_decode_shared_and_partitioned (1162) rebuilds a make(|global_scale, width, partitioned|) closure whose body is a near-copy of the existing make(|width, row_bytes, scale_bytes|) at 1113-1146, including the three buffer-geometry lines at 1212-1214 vs 1135-1137. Including the encoding in the existing make and adding a global_scale argument would merge both tests at roughly net-negative line count.

5. engram_scales already collapses the pair; validate_engram should match it — helios/src/core/schema.rs:504-512, 561-571

This PR puts the new encoding in the good group at 443-457 (engram_encoding_has_scales), which lets engram_scales (460-471) express plane present ⟺ encoding needs it in one expression. But validate_engram still hand-inlines the same predicate twice, and the two adjacent branches now need the same message:

} else if !table.scale_buffer.is_empty() || table.scale_block != 0 {
    return Err(invalid("single-plane engram table cannot carry scale metadata"));
} else if table.encoding == wire::engram_table::Encoding::Bf16 as i32 {
    if table.global_scale.to_bits() != 0 {
        return Err(invalid("single-plane engram table cannot carry scale metadata"));
    }

Folding the BF16 global_scale.to_bits() != 0 test into the preceding || collapses two branches and one duplicate string into one condition. Separately, single_plane_row_bytes (561-571) is a 4-branch if/else if/else over a minted Option<u64> whose Some-side overflow guard (566) is unreachable as noted in §1; the guard reduces to u64::from(table.embedding_width) * u64::from(stride) once the arm is chosen.

6. One predicate, two copies — schema.rs:504-505 vs engram.rs:204-206

The same rule is stated twice with different words for the same user-visible failure:

engram_encoding_has_scales/engram_scales already live in schema.rs and are already imported by engram.rs:35,45,59, so a schema::single_plane_bindings(table, &scales) -> Result<()> next to them would be a drop-in for both. Note the decoder copy (204) is dead for any core that passed validate_engram — it is defence in depth, which is fine, but it does not need its own wording.

7. What is already lean — do not touch

One maintainability note rather than a deduction: the new encoding's contract is now stated in prose in three places — core.proto:267-268, runtime_core.proto:663-664, and helios/manifest.md:526-528 (not opened). That triplication is pre-existing, but it is exactly what let the PR body's tag numbers drift to FP8_E4M3_GLOBAL = 3 … BF16 = 2 when the code has = 4 … = 3. Single-sourcing the comment (cite manifest.md (not opened) from the protos) would have caught it.


# Where Ask Est. saving
1 ngram_delivery.py:106-114 + 137-144 One _lookup_views(mode, …) helper ~8 lines, removes a drift class
2 engram.rs:150-167 Merge the two single-plane arms ~9 lines
3 schema.rs:504-512 Fold BF16 global_scale guard into the `
4 test_ngram_delivery.py Drop has_scales/scale_dtype columns and the 120-131 re-assert ~15–20 lines
5 schema.rs:2068 / engram.rs:1162 Extend the BF16 tests' existing fixtures rather than cloning them ~40–50 lines
6 schema.rs:505 vs engram.rs:205 One shared predicate + one message consistency

None of these is a correctness bug, and #732 should not be blocked on them — but #1 and #2 are cheap enough that they belong in this PR while the seam is fresh, and #6 prevents the decoder and the validator from describing the same rejection differently to operators.

Overall: the diff is lean where it counts (format, export, docs) and repetitive where it does not (tests, the mode dispatch, the second single-plane arm). Ship-able; polishable in an hour.