Correctness review — PR #483 "Add Gemma 4 image preparation, Bracketed patch-width metadata, and atomic spans"
Head ab9a432b (rickybrent:core/gemma4-image-frontend), base main 27716185. 38 files, +2490/−251. Read-only review: I read the changed files in context and followed the callers, but did not execute cargo test or pytest, so every claim below is from code, not from a run.
Rules the change is judged against: charter line 12 ("Keep the runtime small and model-agnostic … shouldn't accumulate per-model branches"), charter line 42 ("verify that existing data, artifacts, and exported cores still load"), AGENTS.md (not opened) model core compatibility (protobuf additions must default so every engine still loads previous cores), and docs/design-review.md (not opened) principles 3 (shared mechanisms over per-model flags) and 10 (persist only consumed structural frontend settings in typed protobuf fields).
Key findings
- No blocking issues. Existing cores remain loadable:
patch_featuresis a defaulted additive field,0maps back to the legacy 1176 width, describe output stays byte-identical, and the span path is inert for layouts that do not declareimage_tokens.span. - One should-fix (test coverage). Both allowlists accept 70/140/280/560/1120 and Ignition writes whichever the checkpoint declares, but the Rust suite only ever loads 280 and 1120. A budget-specific geometry bug or a drift between the two lists would first appear at
flare runon a real checkpoint. - Four nits, none blocking: an over-broad
patch_features % 3rule, the duplicated allowlist, unchecked arithmetic that is only safe because of the load caps, and an untested sub-envelope prefill path. - The two findings from my earlier turns (opaque Gemma 4 load error; undisclosed
Bracketednarrowing with no core-load test) are fixed at this head — see "Resolved since the previous revision" below, and do not repeat them.
Should fix
S1. Only 2 of the 5 allowlisted soft-token budgets are ever loaded (test coverage)
helios/src/frontend/images/gemma4.rs:13—const SUPPORTED_SOFT_TOKENS: [usize; 5] = [70, 140, 280, 560, 1120];ignition/preparation/frontend.py:132— the exporter accepts the same five values, andfrontend.py:168-173writes whichever the checkpoint declared into the Gemma 4 settings.- Rust tests:
processor_with_budget(gemma4.rs:225-244) is the only constructor used by the tests,processor()isprocessor_with_budget(280)(gemma4.rs:246-248), and the fixture-driven test (gemma4.rs:314-381) readsmax_soft_tokensfrom each case — the cases are seven at 280 and one at 1120 (tests/frontend/gemma4_images.json:126-255), generated fromspecsintests/frontend/generate_gemma4_image_fixtures.py:31-40, which also only lists 280 and 1120. (The70atgemma4_images.json:67is inside thevideo_processorblock, not a case.)
Why it matters: max_soft_tokens drives the geometry directly — max_patches() (gemma4.rs:98-100), the target pixel budget (gemma4.rs:106), the aspect-clamp branches (gemma4.rs:113-124), and max_envelope_tokens() (gemma4.rs:90-92), which in turn feeds the new load-time bound against prefill_chunk_size and span.max_tokens (helios/src/server/config.rs:464-487). The clamp branches at :118-123 (one dimension rounding to zero) are exactly the kind of edge that behaves differently at a small budget. A checkpoint declaring max_soft_tokens = 70 (the value its own video_processor uses in the fixture's provenance block) prepares successfully through Ignition and would exercise an untested path in Helios, so a parity or arithmetic defect is only visible in serving.
Fix: add (91, 37, 70), (10, 3000, 140) and one 560 case to the specs list in tests/frontend/generate_gemma4_image_fixtures.py:31-40 and regenerate, or add a load-only table test that constructs a processor for each SUPPORTED_SOFT_TOKENS entry and asserts geometry for one fixed shape. Either closes this and nit N2 together.
Nits
N1. patch_features % 3 is enforced on cores that never consume the field. helios/src/model.rs:89 rejects any vision core where patch_features is not a multiple of three, but the value is only read through bracketed_patch_features() (model.rs:74-80), used from frontend/images/packing.rs:41 and :486. A non-Bracketed core declaring a width such as 1024 fails at load with the generic "invalid prepared vision capabilities" (model.rs:99) instead of a message naming the field. Fix: scope the rule to ImageLayoutKind::Bracketed, or name the field and both values in the error.
N2. The soft-token allowlist exists twice, in two languages. helios/src/frontend/images/gemma4.rs:13 and ignition/preparation/frontend.py:132 must stay identical, and no test ties them. S1's fix (a table test over the Rust constant, plus the fixture cases) is the cheapest way to make drift visible.
N3. Unchecked arithmetic that is safe only because of the load caps. helios/src/frontend/images/gemma4.rs:99 (self.max_soft_tokens * self.pooling * self.pooling) and :139 (patches / (self.pooling * self.pooling)) are unchecked multiplies/divides; they cannot overflow only because load caps max_soft_tokens <= 1120 (:41-47) and pooling_kernel_size <= 16 (:34-40). Worth a one-line comment, since adding a larger budget to SUPPORTED_SOFT_TOKENS would silently move the bound. (The new allocation and budget paths themselves are guarded: :48-51 checked_mul, :157-170 try_fold/checked_mul, :164-171 checked_mul for the reserve.)
N4. Sub-envelope prefill capacity is truncated to zero with no test. helios/src/scheduler/policy.rs:337-351 — prefill_work_through truncates end back to span.start when the computed end falls strictly inside a span, so any captured prefill capacity smaller than the envelope length contributes no work for a span-bearing image request. The new bootstrap bound (helios/src/server/config.rs:464-479, max_envelope > prefill_chunk_size is rejected) makes this unreachable when the captured capacity equals prefill_chunk_size, but nothing pins that relationship with a test. A one-case test with a capacity just below the envelope would document the invariant.
Verified clean (recorded so the report is honest about what was checked)
- Atomic spans are layout-owned and bounds-checked.
helios/src/frontend/images/layouts/bracketed.rs:52-106: spans are pushed only whenatomic_spansis set (:23,:57-63), the start and end token positions are validated on both ends (:69,:87), and a mismatch between image tokens and processed images errors (:92).atomic_spansis derived once fromimage_tokens.span.is_some()(helios/src/model/prompt.rs:52-65), so span-lessBracketedcores (MiniMax) still produce an emptyspans, satisfyinghelios/src/model.rs:467-469. The shared fallback block was deleted fromprompt.rs, and the behaviour is covered by the test athelios/src/model/prompt.rs:1172-1281(span-bearing, 64-aligned chunk too small, no-span). - Wire additions are additive and defaulted, per
AGENTS.md(not opened) core compatibility:helios/proto/runtime_core.proto(not opened)VisionCapacity.patch_features = 6,ImageSettings.gemma4 = 11,ImagePreparationKind::Gemma4 = 11(helios/src/model/contract.rs:68),#[serde(default)]inhelios/build.rs:53-56(not opened),describe.rs:466-468omits0so legacy describe JSON is unchanged, andprotobuf.rs:813-865round-trips the profile. - One width authority.
ImageProcessor::patch_features()(helios/src/frontend/images/mod.rs:459-471) covers every profile, includingPatchMerge(1176),PaddedGrid(1200) andGemma4(768); it is used by the cache validator (conditioned.rs:187) and checked againstvision.patch_features/bracketed_patch_features()athelios/src/server/config.rs:448-463, with a cross-profile test atpacking.rs:685-816. - Legacy
Bracketedcompatibility.VisionCapabilities::bracketed_patch_features()(helios/src/model.rs:74-80) maps0to 1176, so a core exported before this PR loads and stages 16×1176 floats;helios/src/model/tests.rs:1279-1452deserializes a contract withpatch_featuresabsent, assertsInputs::newsucceeds and staging is 16×1176, and asserts both a 768-wide manifest andpatches_per_feature = 3are rejected at load. - Load-time envelope bound only applies where it can.
max_image_envelope_tokens()returnsSomeonly forProfile::Gemma4(mod.rs:473-478), so theprefill_chunk_size/span.max_tokenschecks atserver/config.rs:464-487cannot newly reject MiniMax or any other upstream core. - Envelope extraction kept MiniMax semantics.
helios/src/frontend/images/patch_merge.rs:10-136preserves the validation rules and expansion behaviour; only the message text for empty/duplicate tokens changed. - Ignition authors the new field.
ignition/preparation/contract.py:109-119puts"patch_features": 0in the vision default with a docstring stating that non-1176Bracketedrecipes must set it. Choosing the actual width in the recipe is consistent withdocs/design-review.md(not opened) principle 10 (recipe-declared structural settings ride in typed protobuf fields) and with the runtime staying model-agnostic (charter line 12) — not a finding. runtime_core_pb2.py(not opened) is a pure descriptor regeneration; the earlier generator-version concern is withdrawn (the checked-in file matchesorigin/main'sProtobuf Python Version: 7.36.1).
Resolved since the previous revision
- Opaque load error (previous finding 1).
helios/src/frontend/images/gemma4.rs:25-83now returns a distinct message per rejection:patch_sizebounds (:27-33), pooling bounds (:34-40), soft-token allowlist (:41-47),pooling_area != limits.patches_per_feature(:52-60),max_soft_tokens * pooling_area > limits.max_patches_per_image(:61-71), thenEnvelope::load(:72-76). Each names the field and both values; the test atgemma4.rs:250-312asserts the numbers appear in the messages. - Undisclosed
Bracketednarrowing (previous finding 2). Disclosed in the PR body under "Narrowings" and covered byhelios/src/model/tests.rs:1279-1452. Becausevalidate()already rejectspatch_features == 0for any enabled vision core (helios/src/model.rs:83-99's companion rules) andpatches_per_feature == 0atmodel.rs:88, the only newly rejected values are non-square nonzeropatches_per_feature, which the packing path cannot represent.
Open questions / could not settle
- Recipe-repo compatibility. I could not determine whether any core already published from
local-inference-lab/hyperflare-recipesdeclares aBracketedlayout with a non-squarepatches_per_feature; such a core would now be rejected at load. The narrowing is disclosed and load-tested, so this is a compatibility question for the recipe repository rather than a defect in this tree. A scan of published core manifests would settle it. - Test-only verification. S1 and N4 are both coverage findings: only running the suite (or adding the suggested cases) proves the untested budgets and the sub-envelope capacity path behave. The claimed 47-passed
frontend::imagesrun and the 8-case fixture parity are consistent with the code I read, but I did not re-execute them. - Out of lens and not checked here: whether the resize matches PyTorch/torchvision on hardware- or EXIF-rotated inputs beyond the synthetic fixtures (an earlier pass noted EXIF orientation is applied for
Gemma4), and serving-path timing/memory, which the PR body itself lists as unmeasured.