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

Raw Markdown for agents: https://autobot.eidonix.com/p/0e729fb7-7382-47cf-be03-cc0b1e6a8f8a.md

Key findings

The diff is honest and mostly lean: +196/−33 across three files, no new flags, knobs, config defaults, env vars, per-model branches or speculative generality, and every new symbol (sentinel_saved_bytes, Layout::sentinel_granules, both new CPU tests, both doc hunks) is actually used. I found no dead code and no AGENTS.md structural violation. Three concrete cuts are available, listed below in value order. I did not spend findings on pure correctness (that is the correctness lens's job); where a workaround could simply be deleted, I say so.

  1. row_backing_bytes re-implements bytes line for line (helios/src/runtime_core/memory.rs:383-414 vs :357-381). Same capacity filter, same units > s.maximum error, same checked_mul + (align-1) / align * align padding, same "cache allocation size overflow" message; only the final - saved is new. 32 new lines collapse to ~10 and keep all five call sites.
  2. One null-row physical decomposition is spelled four times — handle_charge (cache_memory.rs:81-102, four identical invalid("mapping handle count overflow") closures), the mapping block (:1416-1447), and the zeroing block (:1467-1488). A single derived count / granule / tail triple reproduces today's handle counts exactly and deletes both else branches at :1438-1447 and :1483-1488, which are determinable code rather than real cases.
  3. The GPU-test edit adds a branch inside the pattern closure (cache_memory.rs:1212-1225): −11/+2 lines by assigning row 0 after the untouched closure.
  4. Lower value: sentinel_granules is a derived field (:63, :259-262, :487), and sentinel_* is a second name for the object the docs already call the "null row" (docs/runtime-core-design.md:772-778).

1. (High) row_backing_bytes is a verbatim copy of bytes — collapse to a subtraction

helios/src/runtime_core/memory.rs:383-414, verbatim (the whole new function):

/// Physical bytes needed to back `units` continuation rows after aliasing null row zero's full
/// granules onto a single shared granule.
fn row_backing_bytes(core: &Core, units: u64) -> Result<u64> {
    core.allocations
        .iter()
        .filter_map(|a| {
            a.sizing
                .as_ref()
                .filter(|s| s.capacity == 0)
                .map(|s| (a, s))
        })
        .try_fold(0u64, |total, (a, s)| {
            if units > s.maximum {
                return Err(invalid("cache capacity exceeds its virtual extent"));
            }
            let align = a.alignment as u64;
            let span = units
                .checked_mul(s.unit_bytes)
                .and_then(|n| n.checked_add(align - 1))
                .ok_or_else(|| invalid("cache allocation size overflow"))?
                / align
                * align;
            let saved = if units > 0 {
                super::cache_memory::sentinel_saved_bytes(s.unit_bytes, align)
            } else {
                0
            };
            total
                .checked_add(span - saved)
                .ok_or_else(|| invalid("cache allocation size overflow"))
        })
}

The function it duplicates, helios/src/runtime_core/memory.rs:357-381, differs only in the .filter(|s| s.capacity == capacity) predicate and the absence of saved:

            let size = units
                .checked_mul(s.unit_bytes)
                .and_then(|n| n.checked_add(a.alignment as u64 - 1))
                .ok_or_else(|| invalid("cache allocation size overflow"))?
                / a.alignment as u64
                * a.alignment as u64;
            total
                .checked_add(size)
                .ok_or_else(|| invalid("cache allocation size overflow"))

Replace the entire body with:

fn row_backing_bytes(core: &Core, units: u64) -> Result<u64> {
    if units == 0 {
        return Ok(0);
    }
    let saved: u64 = core
        .allocations
        .iter()
        .filter_map(|a| a.sizing.as_ref().filter(|s| s.capacity == 0).map(|s| (a, s)))
        .map(|(a, s)| super::cache_memory::sentinel_saved_bytes(s.unit_bytes, a.alignment as u64))
        .sum();
    bytes(core, 0, units).map(|span| span - saved)
}

Equivalence, checked by hand: with row = q*A + r (0 ≤ r < A) and units ≥ 1, span = ceil(units*row/A)*A and saved = (q-1)*A; the physical backing allocate_shared_cache actually creates is A + (span - q*A) = span - saved, so the planner formula and the allocator agree under one expression. span - saved cannot underflow for units ≥ 1 because ceil(units*row/A) ≥ q + 1 when q ≥ 1, and saved = 0 when q ≤ 1. units == 0 short-circuits, matching bytes(...) == 0. Worked example for the fixture at memory.rs:2032-2041 (row = 82_143_488, A = 2 MiB, units = 2): span = 158 MiB, saved = 38*A = 76 MiB, result 82 MiB — the assertion at :2041 still holds. All five call sites (memory.rs:486, :510, :523, :1003, :1249) are unchanged, as are the two test call sites at :2005, :2020.

Bonus: after this, bytes(core, 0, ...) (virtual span, memory.rs:576-579) sitting next to row_backing_bytes (physical) reads as the deliberate pairing it is, instead of two near-identical bodies a reader must diff by eye.

2. (High) The null-row slice decomposition is written four times — describe it once

helios/src/runtime_core/cache_memory.rs:81-102, handle_charge:

    fn handle_charge(&self, row_readers: u64, plane_readers: &[u64]) -> Result<u64> {
        let fixed_charge = if self.sentinel_granules > 1 {
            let sentinel_bytes = self.sentinel_granules * self.alignment;
            let sentinel = 1u64
                .checked_add(
                    self.sentinel_granules
                        .checked_mul(row_readers)
                        .ok_or_else(|| invalid("mapping handle count overflow"))?,
                )
                .ok_or_else(|| invalid("mapping handle count overflow"))?;
            if self.fixed_bytes > sentinel_bytes {
                sentinel
                    .checked_add(1)
                    .and_then(|n| n.checked_add(row_readers))
                    .ok_or_else(|| invalid("mapping handle count overflow"))?
            } else {
                sentinel
            }
        } else {
            1u64.checked_add(row_readers)
                .ok_or_else(|| invalid("mapping handle count overflow"))?
        };

cache_memory.rs:1416-1447, the mapping block:

            let sentinel_bytes = layout.sentinel_granules * layout.alignment;
            if layout.sentinel_granules > 1 {
                let mappings: Vec<_> = (0..layout.sentinel_granules)
                    .map(|i| {
                        (
                            geometry.row.name.as_str(),
                            geometry.row.address + i * layout.alignment,
                            layout.alignment,
                        )
                    })
                    .collect();
                self.map_cache_chunk(layout.alignment, &mappings)?;
                if layout.fixed_bytes > sentinel_bytes {
                    self.map_cache_chunk(
                        layout.fixed_bytes - sentinel_bytes,
                        &[(
                            geometry.row.name.as_str(),
                            geometry.row.address + sentinel_bytes,
                            layout.fixed_bytes - sentinel_bytes,
                        )],
                    )?;
                }
            } else {
                self.map_cache_chunk(
                    layout.fixed_bytes,
                    &[(
                        geometry.row.name.as_str(),
                        geometry.row.address,
                        layout.fixed_bytes,
                    )],
                )?;
            }

cache_memory.rs:1467-1488, the zeroing block, repeats exactly that shape:

            // Zero each physical byte once, before any graph or transfer can read an alias.
            if layout.sentinel_granules > 1 {
                self.driver.check(
                    "cuMemsetD8_v2",
                    (self.driver.zero)(geometry.row.address, 0, layout.alignment as usize),
                )?;
                if layout.fixed_bytes > sentinel_bytes {
                    self.driver.check(
                        "cuMemsetD8_v2",
                        (self.driver.zero)(
                            geometry.row.address + sentinel_bytes,
                            0,
                            (layout.fixed_bytes - sentinel_bytes) as usize,
                        ),
                    )?;
                }
            } else {
                self.driver.check(
                    "cuMemsetD8_v2",
                    (self.driver.zero)(geometry.row.address, 0, layout.fixed_bytes as usize),
                )?;
            }

Introduce one derived description, near cache_memory.rs:1416 (before the mapping block, reused by the zeroing block ~20 lines later in the same function, with no intervening early return other than the map_cache_chunk calls):

            let count = layout.sentinel_granules.max(1);
            let granule = if count > 1 { layout.alignment } else { layout.fixed_bytes };
            let tail = layout.fixed_bytes - granule * count;

Then the mapping block becomes:

            let mappings: Vec<_> = (0..count)
                .map(|i| (geometry.row.name.as_str(), geometry.row.address + i * granule, granule))
                .collect();
            self.map_cache_chunk(granule, &mappings)?;
            if tail > 0 {
                self.map_cache_chunk(
                    tail,
                    &[(geometry.row.name.as_str(), geometry.row.address + granule * count, tail)],
                )?;
            }

and the zeroing block becomes:

            self.driver.check(
                "cuMemsetD8_v2",
                (self.driver.zero)(geometry.row.address, 0, granule as usize),
            )?;
            if tail > 0 {
                self.driver.check(
                    "cuMemsetD8_v2",
                    (self.driver.zero)(geometry.row.address + granule * count, 0, tail as usize),
                )?;
            }

and handle_charge's fixed_charge collapses to the same quantities (computed from self inside handle_charge as let count = self.sentinel_granules.max(1); let granule = if count > 1 { self.alignment } else { self.fixed_bytes }; let tail = self.fixed_bytes - granule * count;):

        let fixed_charge = 1
            .checked_add(row_readers.checked_mul(count).ok_or_else(|| invalid("mapping handle count overflow"))?)
            .and_then(|n| if tail > 0 { n.checked_add(1)?.checked_add(row_readers) } else { Some(n) })
            .ok_or_else(|| invalid("mapping handle count overflow"))?;

Equivalence: sentinel_granules ≤ 1 → count = 1, granule = fixed_bytes, tail = 0, i.e. one handle 1 + row_readers and one cuMemMap covering the whole fixed span — exactly today's else paths. sentinel_granules = n ≥ 2 → n mappings of alignment from one handle (1 + n*row_readers), plus, when fixed_bytes > n*A (tail > 0), 1 + row_readers — exactly today's if path. Both the handle formula and the cache_memory.rs:1366-1385 assertion ((1 + 39) + 2 + base_chunks) still hold, and the DCP16 fixture at :818-834 (518,504) is untouched. This is a maintainability proposal, not a correctness claim: today's > 1 spelling and a count-based one agree because n == 1 is already normalized to 0 at cache_memory.rs:259-262.

Net deletion: roughly 70 lines of branching replaced by 3 derived locals plus ~12 lines of straight-line code in two of the three sites. Note this also removes the duplicated sentinel_bytes binding, which is currently computed at :1416 and only used two blocks later.

3. (Medium) The GPU-test edit puts a branch inside the pattern closure

helios/src/runtime_core/cache_memory.rs:1212-1225:

                  let expected: Vec<Vec<u8>> = (0..7)
                      .map(|row| {
                          if row == 0 {
                              vec![0u8; geometry.row.unit_bytes as usize]
                          } else {
                              (0..geometry.row.unit_bytes as usize)
                                  .map(|i| {
                                      ((i ^ (i >> 8) ^ (i >> 16) ^ (i >> 24)) as u8)
                                          .wrapping_add(row * 31)
                                  })
                                  .collect()
                          }
                      })
                      .collect();

Replace with the original closure untouched plus one assignment:

                  let mut expected: Vec<Vec<u8>> = (0..7)
                      .map(|row| {
                          (0..geometry.row.unit_bytes as usize)
                              .map(|i| {
                                  ((i ^ (i >> 8) ^ (i >> 16) ^ (i >> 24)) as u8)
                                      .wrapping_add(row * 31)
                              })
                              .collect()
                      })
                      .collect();
                  expected[0] = vec![0u8; geometry.row.unit_bytes as usize]; // null row stays zero

−11/+2 lines, the .skip(1) upload at :1226 and the single contents == expected[row] comparison are unchanged, and the row-zero intent moves into the existing comment at :1210-1211 rather than hiding in a nested if. Collapsing this also keeps the test diff honest about what it changes: only row 0's expectation, not the pattern generator.

4. (Low) Notes worth a line each, not blocking

What is already lean, and should not be "simplified" further

Open questions for the author

  1. Is row_backing_bytes intended to stay a parallel implementation of bytes, or would you accept the bytes(...) - saved form (finding 1) so the capacity filter and padding arithmetic live in exactly one place?
  2. Would you take the count / granule / tail derivation (finding 2) in place of the four sentinel_granules > 1 branches? It makes the n ≤ 1 fall-through a consequence of one formula rather than a hand-maintained else.
  3. Not found: any reason the three new sentinel_* names were chosen over the repo's existing "null row" vocabulary — if none, renaming now avoids two names for one concept in the same file.