Subtraction & Simplicity audit — PR #723
Verdict: not lean. The 2-way/3-way generalization is real work and mostly earns its lines, but the PR adds three separate surfaces of speculative generality, and the smallest one (the Python ScratchArena contract, ignition/preparation/scratch.py:59-188) can shrink from ~77 added lines to ~25 with identical behaviour and identical tests. Concretely: one public kwarg that is validated then silently discarded, one public kwarg+dict that is provably a no-op, and one validation rule implemented five times.
Scope note: the pr_diff base is stale and shows hunks that are not this PR (host_vmm, prepare_async_transfers, map_device_pool, validate_external_extents, external_extents, CacheAgreement::device_pools, validate_async_transfer_peer_copy_compatibility, heliopause/src/export/capture.rs:2940-3019). That is merged AsyncTransfer work (#711) bleeding in. All citations below are reconciled against pr_files and exist only in the #723 delta.
A. ScratchArena: three of the five new entry points / fields are dead or contradictory (highest value)
A1 — resolve_reserve_sms(..., plan=...): the plan= argument is provably a no-op. Delete the kwarg and the _parallel_reserve_sms dict.
ignition/preparation/scratch.py:137-146:
def resolve_reserve_sms(self, requested: int = 0, *, plan=None) -> int:
...
floor = self.reserve_sms_floor
if plan is not None:
floor = max(floor, self._parallel_reserve_sms.get(id(plan), 0))
return max(requested, floor)
reserve_sms_floor (:113-118) is max(self._min_reserve_sms, self._max_parallel_reserve_sms), and _parallel_reserve_sms[key] is only ever assigned max(..., reserve_sms) where _max_parallel_reserve_sms = max(self._max_parallel_reserve_sms, reserve_sms) (:184-187). Therefore every per-plan value is <= _max_parallel_reserve_sms <= floor, so max(floor, per_plan) == floor always. The body is exactly return max(requested, self.reserve_sms_floor); _parallel_reserve_sms has no other reader. The test that appears to cover it (tests/preparation/test_scratch.py:559, assert arena.resolve_reserve_sms(0, plan=plan_a) == 4) cannot distinguish the branch, because register_parallel(plan_a, plan_b, reserve_sms=4) at :556 already raised the arena-wide max to 4.
A2 — min_reserve_sms= takes a value, validates it, then discards it when overlap=False. scratch.py:59-63 accepts and range-checks it; :114-117 returns 0 unconditionally when !self.overlap. tests/preparation/test_scratch.py:588-589 codifies the contradiction:
serial = ScratchArena("cpu", overlap=False, min_reserve_sms=4)
assert serial.reserve_sms_floor == 0
A knob that is accepted and thrown away is worse than no knob. Either drop the constructor parameter entirely (require_reserve_sms already covers the use case and raises "stream overlap is disabled" consistently, :126-127), or reject min_reserve_sms > 0 with overlap=False at construction like every other invariant in this class does.
A3 — the bidirectional rule is inconsistent, which is a symptom of the two-dict design. register_cooperative_plan checks max(self._min_reserve_sms, self._parallel_reserve_sms.get(key, 0)) (:158) — i.e. per-plan plus _min_reserve_sms, but not _max_parallel_reserve_sms. So a plan registered after an unrelated sibling's reserve_sms=4 parallel group is accepted at reserve_sms=1, while reserve_sms_floor is 4 and the plan's own effective floor is 4. register_parallel has the mirror-image asymmetry (:176-187: it checks only _cooperative_plans, then writes per-plan and global max). Collapsing to one scalar _reserve_sms_floor plus one set of declared counts makes the rule a single comparison and removes the inconsistency rather than documenting it.
A4 — one 2-line validation copied five times. Identical type(...) is not int or < 0 → ValueError("... must be a nonnegative integer") at scratch.py:62-63, :122-123, :139-140, :150-151, :174-175. One helper (def _reserve_sms(value, name="reserve_sms")) removes four copies. Note the loop at tests/preparation/test_scratch.py:597-607 exercises exactly these five sites, so behaviour is preserved by construction.
A5 — if plan is None: return (scratch.py:152-153) is a silently-accepting branch in an API whose entire purpose is to fail closed, and it has no caller at all (see A6). Registration of None should raise, or the guard should go.
A6 — no in-repo consumer exists. register_cooperative_plan|require_reserve_sms|resolve_reserve_sms|reserve_sms_floor|min_reserve_sms appear only in ignition/preparation/scratch.py and tests/preparation/test_scratch.py (+62 lines). Combined with the repo docs still describing the old contract (docs/core-design.md:821 (not opened) "register_parallel(*plans) declares preparation workspaces that must coexist."; docs/preparation-development.md:256 (not opened), not updated), the whole surface is a public API committed before its first caller. Shipping A1/A2/A5 cuts, the single scalar floor, and one validation helper lands the same fail-closed semantics for recipes#157 with roughly a third of the code, and removes two of the four new public names.
B. plan_sm_split / create: Option<u32> + bool encodes a third, invalid state — use an enum
helios/src/runtime_core/persistent_worker.rs:85-91 and :247-254 both take (Option<u32>, bool, u32, u32); five positional parameters, and (None, false), (Some(0), true), (Some(0), false) are illegal. That forces:
- the reject-the-invalid-state check,
:96-100:if requested_worker_sms.is_none() && !reserve_codec_partition { return Err(invalid("green context partition requires a persistent worker or GPU codec reservation")) }; - the
Some(0)check,:101-105; - a
matcharm that must be declared unreachable,:157:(None, None) => unreachable!(),inside the error-message match at:146-158; - the duplicated green-API guard: the same condition and the same message appear twice —
mod.rs:391-396rejects first, so theok_or_elseatpersistent_worker.rs:255-261is unreachable from the load path (reachable only from unit tests).
A PartitionRequest (enum { Worker(u32), Codec, WorkerAndCodec(u32) }) makes all three illegal states unrepresentable, deletes the two Err arms and the unreachable!(), and lets requested_worker_sms/reserve_codec_partition — which are threaded separately through initialize_with_options → prepare_kernels → create (mod.rs:340-346, :776, :809-816) — collapse to a single argument on that 3-hop plumbing path. This is the textbook case in the brief for pairing options into one value.
C. The cluster-dimension decode is written twice, with a magic 4 on one side only
Export (heliopause/src/export/capture.rs:463-486) has a named constant and a helper:
const CU_LAUNCH_ATTRIBUTE_CLUSTER_DIMENSION: i32 = 4;
fn kernel_cluster_size(kernel: &wire::Kernel) -> ExportResult<u32> { ... }
Load (helios/src/runtime_core/mod.rs:1067-1078) re-implements the identical parse with a bare literal:
if attribute.id == 4 {
let cx = u32::from_ne_bytes(bytes[0..4].try_into().unwrap());
...
.ok_or_else(|| { invalid("kernel cluster dimension overflows u32") })?;
Same attribute id, same three-u32 layout, same max(1) product, same overflow message, on either side of a wire boundary that now carries max_cluster_size precisely so the loader does not have to re-derive it. Move the decode to one shared location (it already has a natural home next to the wire::Attribute type) and both sides call it. As written, a change to the attribute encoding must be made in two languages' worth of three places.
D. max_cluster_size was just recorded on the wire, yet the loader re-derives it — and the derived check is legacy-only
mod.rs:1079-1086 fails a graph whose decoded cluster_size > avail_sms. But graph_sm_requirements (capture.rs:491-500) guarantees min_compute_sms = declared.max(max_cluster_size), and mod.rs:964-980 already fails when graph.min_compute_sms > avail_sms. So for any core exported by this PR's own exporter, cluster_size > avail_sms ⇒ max_cluster_size > avail_sms ⇒ min_compute_sms > avail_sms, and the earlier check fires first: the per-node branch is only reachable for legacy cores with min_compute_sms == 0. That belongs in a comment at minimum, and more usefully the SM comparison should be done once against graph.max_cluster_size (one field read, one message), with only the genuine co-scheduled-alignment check (:1087-1094) kept per-attribute. Roughly 30 lines and a second overflow path become one field comparison. Note the coupling min_compute_sms >= max_cluster_size is itself undocumented in the description and unstated in the proto comment (helios/proto/core.proto:251-254 (not opened)).
E. Two adjacent folds that should be one, and one error suffix repeated four times
mod.rs:795-808 walks core.graphs twice, identically bar the field:
let min_compute_sms = core.graphs.iter().filter(...).map(|g| g.min_compute_sms).max().unwrap_or(0);
let max_cluster_size = core.graphs.iter().filter(...).map(|g| g.max_cluster_size).max().unwrap_or(0);
One fold((0, 0), |(a, b), g| (a.max(g.min_compute_sms), b.max(g.max_cluster_size))), or a Core::graph_sm_requirements() returning the pair — mirroring the identically-named export-side helper, which also returns (u32, u32).
The actionable hint "; use --lossless-kv-compression cpu" is written out at four code sites — mod.rs:394, mod.rs:972, persistent_worker.rs:348, :480, :259 — embedded inside five separately formed format! messages, then substring-matched in three tests (persistent_worker.rs:1251, :1283, :3569). One const CPU_CODEC_HINT: &str makes the hint consistent and the tests reference the constant instead of a copied literal.
F. Python: min_compute_sms is stored on graphs through two channels; one is unused in production
ignition/preparation/runtime_core.py:184-188 adds a min_compute_sms=0 constructor parameter to RecordedExportGraph, and :299-301 adds a fallback:
raw_min_sms = getattr(graph, "_flare_min_compute_sms", None)
if raw_min_sms is None and isinstance(graph, RecordedExportGraph):
raw_min_sms = graph.min_compute_sms
But the only producer, ignition/preparation/capture.py:237-245, sets the attribute on the value returned by record_export_graph — which is the RecordedExportGraph itself — so the constructor parameter is never populated by production code; its only users are tests/preparation/test_capture_graph_metadata.py:637,641. Drop the parameter and the isinstance branch: the attribute is the single storage location, and _graphs keeps its None → 0 default. Relatedly, the if min_compute_sms > 0: guard at capture.py:244 exists only so test_capture_graph_metadata.py:626 can assert not hasattr(..., "_flare_min_compute_sms"); assigning unconditionally and defaulting in _graphs is equivalent.
G. Minor: six locals copied out of SmSplitPlan to be shadowed immediately
persistent_worker.rs:295-301:
let baseline_coscheduled = plan.baseline_coscheduled;
let required_compute_coscheduled = plan.min_compute_coscheduled;
let required_min_compute_sms = plan.min_compute_sms;
let worker_index = plan.worker_index;
let codec_index = plan.codec_index;
let compute_index = plan.compute_index;
let group_count = plan.primary.len();
A single destructuring let SmSplitPlan { primary, fallback, worker_index, codec_index, compute_index, baseline_coscheduled, min_compute_coscheduled: required_compute_coscheduled, min_compute_sms: required_min_compute_sms } = plan; does the same in one statement and prevents the two names (min_compute_coscheduled vs baseline_coscheduled) from being confused — note they are nested (required_compute_coscheduled >= coscheduled), which the current names do not convey.
What is already lean (do not cut)
- The two proto fields are minimal and correctly additive with
0 = unspecified(helios/proto/core.proto:251-254(not opened),helios/proto/runtime_core.proto:751-753(not opened)); the pass-through churn in the 8helios/src/core/*files is unavoidable struct-literal filler, not new design. kernel_cluster_size'sx.max(1) * y.max(1) * z.max(1)withchecked_mulis the right single implementation on the export side; the problem is only that the loader has a second copy (C).- The codec partition being exactly one
min_sm_partition_size-aligned group, with no tuning knob, is the correct choice — resist any suggestion to expose a codec SM count. graph_sm_requirements' unit test (capture.rs:5621-5670-ish, asserting(8,8)/(128,8)/(1,1)/(0,0)) andtest_arena_reserve_sms_*are behaviour-level and survive every cut proposed above.
One process point that is a subtraction in itself
The Python ScratchArena contract has no in-repo caller and no corresponding issue; it rides with a CUDA change and forces its own 62 test lines. Splitting it out (the split yatesdr asked for on #430) removes ~139 lines from a PR whose load-time failure semantics are the part that needs scrutiny, and lets the only reviewable claim — "--lossless-kv-compression gpu no longer contends on non-worker cores" — stand alone.