Subtraction and simplicity scratchpad · NVIDIA/hyperflare-inference #664

Raw Markdown for agents: https://autobot.eidonix.com/p/fcd64cfc-486e-4500-9aee-72043f4e2912.md

#1 [high] Research summary

Subtraction / Simplicity / Conventions review — PR #664 (head fd6dad6)

The core shape of this PR is lean and idiomatic: Planes { weights: Plane, scales: Option<Plane> } with iter()/iter_mut() chained over the Option (helios/src/engram/reader.rs:60-102) is exactly the right collapse of a fixed [Plane; 2], and the #[cfg(test)] single/two constructors keep ~15 fixture call sites unchanged. Docs updates are consistent. Four things are worth deleting or collapsing; the rest of the diff is already tight.

1. helios/src/core/schema.rs:428-436,452,496 — the encoding is decided three times per table

has_scales is already computed at :367 (engram_encoding_has_scales(table.encoding)?), then engram_scales re-derives it at :331 on both call sites, and the BF16 tag is re-derived a third way:

        let bf16_row_bytes = if table.encoding == wire::engram_table::Encoding::Bf16 as i32 {
            Some(
                u64::from(table.embedding_width)
                    .checked_mul(2)
                    .ok_or_else(|| invalid("BF16 engram row width overflow"))?,
            )
        } else {
            None
        };

Replacement: engram_scales takes the already-known bool instead of encoding (fn engram_scales<'a, T>(has_scales: bool, scales: &'a Option<T>)), and validate_engram sets bf16_row_bytes from it — let bf16_row_bytes = (!has_scales).then(...) — since !has_scales is "encoding == Bf16" after :318-327 has rejected unknown/Unspecified. That removes one enum re-parse per table and deletes the raw tag comparison.

2. helios/src/runtime_core/selection.rs:52-70 — has_tables is dead once has_scale_planes exists

        if self.embedding_table_resident_scales == Some(true) {
            if !has_tables {
                return Err(invalid(
                    "resident embedding scales require an embedding table catalog",
                ));
            }
            if !has_scale_planes {

The only production caller is helios/src/runtime_core/model.rs:477-481, which passes (!engram.is_empty(), has_scale_planes) where has_scale_planes = engram.iter().flat_map(...).any(|table| table.planes.scales.is_some()) — so has_scale_planes == true implies has_tables == true and the first arm is unreachable. Drop the has_tables parameter and its error (the second message already says "requires an embedding table with a scale plane"); the test at :1049 keeps working via (false).

3. heliopause/src/embedding_tables.rs:41-62 — four overlapping presence checks for one optional plane

        if planes.is_empty() || planes.len() > 2 {
            return Err(PyValueError::new_err(
                "embedding table requires weight plane and optional scale plane",
            ));
        }
        let weights_plane = planes.remove(0);
        let scales_plane = (!planes.is_empty()).then(|| planes.remove(0));
        let scales_name: Option<String> = match table.get_item("scales") { ... };
        let scales = match (scales_plane, scales_name) {
            (Some(plane), Some(name)) => Some((plane, name)),
            (None, None) => None,
            _ => return Err(PyValueError::new_err(
                "embedding table scale plane and scale buffer binding must both be present or both absent",
            )),
        };

TableSource.scales: Option<(PlaneSource, String)> (helios/src/engram/prepared.rs:43) makes the pairing unrepresentable once built, so the 3-arm match is only patching an artefact of reading the two things separately. Read scales_name first, then require planes.len() == 1 + usize::from(scales_name.is_some()) and scales = scales_name.map(|name| Ok((planes.remove(0), name))).transpose()?. Collapses 22 lines to ~8, and drops one PyKeyError use.

4. heliopause/src/export/core.rs:99-111, :122-138, :411-419, :426-436, :1538-1592 — five hand-copies of "weights plane + optional scales plane"

The same 8-line shape appears twice in the role builder, twice in the payload locator, and again in core_engram's job planner:

                result.push(Role { kind: 4, owner: None, span: table.weights.clone()
                        .ok_or_else(|| invalid("embedding table lacks payload plane"))? });
                if let Some(scales) =
                    helios_core::core::schema::engram_scales(table.encoding, &table.scales)?
                {
                    result.push(Role { kind: 4, owner: None, span: scales.clone() });
                }

Now that plane count is variable this is the place to introduce one helper — fn planes<'a>(encoding: i32, weights: Option<&'a Span>, scales: &'a Option<Span>) -> Result<Vec<&'a Span>> — and have all five sites iterate it. It also removes the four duplicated engram_scales calls (:108, :131, :416, :431, :1538, :1580).

Minor (optional, not blocking)

No AGENTS.md (not opened)-level convention violation: the new enum tags are appended, the proto is regenerated, and no new module or flag knob was introduced (the resident-scales guard reuses an existing option).

#2 [high] Lens report: Subtraction/Simplicity — PR #664 head fd6dad6

VERDICT: diff is lean and idiomatic. The Planes { weights: Plane, scales: Option<Plane> } refactor (helios/src/engram/reader.rs:60-102) with iter()/iter_mut() chained over the Option is the right collapse of [Plane; 2]; #[cfg(test)] single()/two() constructors keep ~15 fixture sites unchanged. Docs consistent. No AGENTS.md structural violation: enum tags appended, proto regenerated, no new module/flag knob (resident-scales guard reuses an existing option). 4 worthwhile subtractions + 3 optional minors.

--- S1 (HIGH) schema.rs decides encoding 3x per table --- helios/src/core/schema.rs:367 already computes let has_scales = engram_encoding_has_scales(table.encoding)?; then engram_scales re-derives it at :331 (fn at :330-341), and the BF16 tag is re-derived a third way: helios/src/core/schema.rs:428-436 verbatim:

        let bf16_row_bytes = if table.encoding == wire::engram_table::Encoding::Bf16 as i32 {
            Some(
                u64::from(table.embedding_width)
                    .checked_mul(2)
                    .ok_or_else(|| invalid("BF16 engram row width overflow"))?,
            )
        } else {
            None
        };

Call sites :452 and :496 use engram_scales(table.encoding, ...). FIX: make engram_scales take has_scales: bool instead of encoding: i32; in validate_engram set let bf16_row_bytes = (!has_scales).then(...) — !has_scales IS "encoding == Bf16" after :318-327 rejects unknown/Unspecified. Removes one enum re-parse/table + the raw tag compare.

--- S2 (HIGH) dead has_tables param --- helios/src/runtime_core/selection.rs:52-70 verbatim head:

    pub(super) fn validate_embedding_resident_scales(
        &self,
        has_tables: bool,
        has_scale_planes: bool,
    ) -> Result<()> {
        if self.embedding_table_resident_scales == Some(true) {
            if !has_tables {
                return Err(invalid(
                    "resident embedding scales require an embedding table catalog",
                ));
            }
            if !has_scale_planes {

Only prod caller helios/src/runtime_core/model.rs:477-481 passes (!engram.is_empty(), has_scale_planes) where has_scale_planes = engram.iter().flat_map(...).any(|t| t.planes.scales.is_some()) — so has_scale_planes==true implies has_tables==true; the first arm is unreachable. Delete the has_tables param + that error arm; test at selection.rs:1049 still compiles with one arg.

--- S3 (MEDIUM) 4 overlapping presence checks for one optional plane --- heliopause/src/embedding_tables.rs:41-62 verbatim:

        if planes.is_empty() || planes.len() > 2 {
            return Err(PyValueError::new_err(
                "embedding table requires weight plane and optional scale plane",
            ));
        }
        let weights_plane = planes.remove(0);
        let scales_plane = (!planes.is_empty()).then(|| planes.remove(0));
        let scales_name: Option<String> = match table.get_item("scales") {

(then 3-arm match (Some,Some)/(None,None)/_ at :54-62.) TableSource.scales is Option<(PlaneSource, String)> (prepared.rs:43) so the pairing is unrepresentable once built; the 3-arm match only patches reading the two things separately. FIX: read scales_name first, require planes.len() == 1 + usize::from(scales_name.is_some()), scales = scales_name.map(|name| Ok((planes.remove(0), name))).transpose()?. ~22 lines -> ~8; drops one PyKeyError use (:13,:51).

--- S4 (MEDIUM) five hand-copies of weights+optional scales --- heliopause/src/export/core.rs:99-111 verbatim:

                result.push(Role {
                    kind: 4,
                    owner: None,
                    span: table
                        .weights
                        .clone()
                        .ok_or_else(|| invalid("embedding table lacks payload plane"))?,
                });
                if let Some(scales) =
                    helios_core::core::schema::engram_scales(table.encoding, &table.scales)?
                {
                    result.push(Role { kind: 4, owner: None, span: scales.clone() });
                }

Same 8-line shape repeats at :122-138 (shard), :411-419 (locate shared), :426-436 (locate shard), and in core_engram job planning :1538-1592. Six engram_scales call sites total: :108, :131, :416, :431, :1538, :1580. FIX: one helper fn planes<'a>(encoding: i32, weights: Option<&'a Span>, scales: &'a Option<Span>) -> Result<Vec<&'a Span>> and iterate it at all five sites.