Review of PR #483 — Gemma 4 image preparation, Bracketed patch-width metadata, atomic spans (lens D: simplicity)
Head 2b4a31e (rebased onto main 27716185). 36 files, +2269/−240.
Rules I judged against:
AGENTS.md:188-193— "Reject unsupported abstractions and churn. Do not add frameworks, extension points, speculative compatibility, fallback layers, or configuration switches without a concrete requirement. Unsolicited formatting, renaming, comment expansion … are not contributions by themselves. Refactoring must directly support the requested behavior or remove a demonstrated source of defects or cost."AGENTS.md:194-199— "Require focused, meaningful validation. Tests must exercise the affected behavior or contract … Do not present scaffolding or partially wired features as supported."docs/design-review.md:16(not opened) — "Keep Helios as simple as possible"; charter line 12 — keep the runtime model-agnostic and free of per-model branches.
Key findings
No blocking findings. The image-preparation core (native Gemma 4 resize/patch/coordinate pipeline, Envelope extraction, layout-owned Bracketed spans, VisionCapacity.patch_features) is coherent and is the smaller design; it is worth merging once the deletions below are taken. What remains in this lens is roughly 200 lines of code that cannot be reached or that no longer proves anything:
patch_size/pooling_kernel_sizeare configurable across the wire, protobuf, serde and Python producer, but no exporter can emit any value but 16/3 — the config pair buys nothing and forces three unreachable validation arms plus two more copies of the 768 patch width.- An
#[ignore]d parity test (native_pixels_and_positions_match_the_checkpoint_processor, ~85 lines of a struct-packing stdout protocol) is now strictly weaker than the new unconditional checked-in fixture test that replaced it. - The ignored envelope test still writes and hashes a Jinja chat template it never renders, and leaks the temp file on its early-return path.
- A 9-row geometry table whose 7 rows are already asserted from the fixture.
- Two overlapping patch-width checks in
server/config.rs, one of which is unreachable-except-as-a-second-error-message for the only layout that definespatch_features.
Plus four nits (redundant grid-divisibility guard, a precomputed boolean threaded through the layout boundary, an over-long profile table, and fixture JSON blocks no test reads).
Resolved since my previous review (stated for the record, not repeated)
Three items I raised on the earlier revision were fixed by the rebase and should not be re-raised:
- The post-hoc
data.spansback-fill inhelios/src/model/prompt.rsis gone; spans are now produced inhelios/src/frontend/images/layouts/bracketed.rs:52-88beside the expansion, matchinglayouts/deepseek.rs:149, with theatomic_spansflag passed fromprompt.rs:52-65. helios/src/frontend/images/conditioned.rs:187callsImageProcessor::patch_features()(mod.rs:459-471) instead of a hard-codedProfile::Gemma4(_) => 768, so cache validation can no longer silently invalidate.- The venv-dependent parity evidence is replaced by
tests/frontend/gemma4_images.json+tests/frontend/generate_gemma4_image_fixtures.py, verified unconditionally atgemma4.rs:266-333, and the live snapshot tests now use#[ignore]with explicit assertions instead of silentreturns.
Load-time validation is right to exist; only the duplication in it is a finding (S5).
Should fix
S1. patch_size and pooling_kernel_size are dead knobs that add three copies of the patch width
helios/proto/runtime_core.proto:599-600; helios/src/frontend/images/mod.rs:335-336; helios/src/frontend/images/gemma4.rs:18-19,26-39,47-48; ignition/preparation/frontend.py:124-127,140-141,169-170.
The prototype's body says "Keep the runtime small and model-agnostic" and AGENTS.md:188-193 forbids configuration switches without a concrete requirement. Here:
ignition/preparation/frontend.py:124-127and:140-141reject any checkpoint whoseimage_processor.patch_size != 16orpooling_kernel_size != 3(and the same pair invision_config), thenfrontend.py:169-170writespatch_size=16, pooling_kernel_size=3literally into the prepared settings. No exported core can carry another pair.gemma4.rs:30-33then validatespatch_size == 0 || pooling > 0 … || patch_size > 64 || pooling > 16— arms that no producer can reach, since the only source always sends 16/3.- Only
patch_sizeis actually used (gemma4.rs:55,63,74,98-99,132,148-157);poolingmerely re-stateslimits.patches_per_feature, whichgemma4.rs:35already checks (pooling_area != Some(limits.patches_per_feature)).
Fix (smaller version, following the in-tree precedent helios/src/frontend/images/glm.rs:13, const FACTOR: usize = 28):
const PATCH: usize = 16;
const POOLING: usize = 3;
const FEATURES: usize = 3 * PATCH * PATCH; // 768
Use PATCH/POOLING/FEATURES in gemma4.rs and FEATURES in conditioned.rs/packing.rs, delete both proto fields (reserve tags 1-2 per the compatibility rules), drop the two struct fields and serde keys, drop the patch_size/pooling_kernel_size validation arms, and drop the two literals from frontend.py:169-170. describe.rs:451-452 and protobuf.rs:169 then lose two entries each. This removes ~25 lines and leaves one source of truth for 768, which is currently stated in gemma4.rs:55 (computed), conditioned.rs:187 (via patch_features(), fixed), packing.rs:16-24 (legacy 1176) and gemma4.rs:364 (3 * 16 * 16 literal in a test).
Also remove the duplicated soft-token allowlist: gemma4.rs:13 SUPPORTED_SOFT_TOKENS: [usize; 5] = [70,140,280,560,1120] duplicates frontend.py:132 max_soft_tokens not in (70, 140, 280, 560, 1120). Two allowlists in two languages, where nothing in the profile varies with the value beyond the geometry budget that is independently checked at gemma4.rs:36 and :93-97. Keep the Python gate (it rejects the checkpoint at preparation, the right place) and let Rust accept any positive budget, or keep only the Rust one; do not keep both.
S2. native_pixels_and_positions_match_the_checkpoint_processor is ignored and superseded — delete it
helios/src/frontend/images/gemma4.rs:449-533 (~85 lines).
Its three cases are (91,37), (10,3000), (3000,10) — exactly fixture seeds 1, 5, 6 in tests/frontend/gemma4_images.json:143-158,207-222,223-238. The fixture test (gemma4.rs:266-333) covers all three plus max_soft_tokens=1120, and it hashes the resized RGB, the packed pixel_values and position_ids with SHA-256 rather than doing an element-by-element comparison — strictly at least as strong, and it runs unconditionally under plain cargo test.
What the ignored test still carries is an unreviewable ad-hoc IPC protocol: a Python script that struct.packs '<qqqq' followed by raw tensor bytes (gemma4.rs:458-472), and a matching reader with a hand-computed pixel_offset = 32 and position_offset = pixel_offset + patches * 768 * 4 (:508-522). Against AGENTS.md:194-199 ("Do not present scaffolding"), a test that never runs in CI and duplicates an unconditional test should go. Its only unique claim is "the pixels came from transformers at run time", and that provenance now lives in tests/frontend/generate_gemma4_image_fixtures.py:14 where a reviewer can read it.
If some value is placed on retaining a live-checkpoint comparison, keep it as a single documented #[ignore] case that reuses the fixture's seeds and compares hashes via generate_gemma4_image_fixtures.py output, not a bespoke binary protocol.
S3. The ignored envelope test builds a chat template it never uses (and leaks it)
helios/src/frontend/images/gemma4.rs:394-413.
let template = std::env::temp_dir().join(format!("gemma4-template-{}.jinja", std::process::id()));
let template_bytes = b"{% mark %}{{ messages[0].content }}";
std::fs::write(&template, template_bytes).unwrap();
let chat_template = Asset { sha256: …, path: template.clone(), range: None };
…
let _ = std::fs::remove_file(&template);
FrontendAssets.chat_template is Option and the test only calls frontend.encode("Look <|image|> now", false) (gemma4.rs:425) — chat-template rendering is never invoked, so the template has no effect on any assertion at :432-446. The surrounding assertions are about Envelope::expand, which is now covered unconditionally with two marker sets by the pure-Rust test at helios/src/frontend/images/patch_merge.rs:219-306 and end-to-end from a real rendered prompt by layouts/bracketed.rs's span test.
Fix: delete gemma4.rs:394-402 and the chat_template: Some(chat_template) field (:408), and drop the now-dead remove_file (:413) along with the early-return path that could leak the file. ~15 lines and one temp-file write per run removed with no loss of coverage.
S4. geometry_matches_the_hugging_face_processor mostly repeats the fixture table
helios/src/frontend/images/gemma4.rs:335-354.
Its nine rows are (1,1)→[1,48,48]/256, (672,672)→[1,48,48]/256, (1008,672)→[1,39,60]/260, (91,37)→[1,30,78]/260, (1920,1080)→[1,36,66]/264, (100,3000)→[1,273,9]/273, (3000,100)→[1,9,273]/273, (10,3000)→[1,840,3]/280, (3000,10)→[1,3,840]/280. Seven of those grids are asserted, together with the tokens, patches and resize hash, by canonical_processor_pixels_positions_and_resize from tests/frontend/gemma4_images.json (gemma4.rs:288-302; fixture cases at gemma4_images.json:127-254). Only (100,3000) and (3000,100) are unique to this test.
Fix: shrink the table to those two rows, or delete the test. Either way the tokens * 9 == patches invariant at :352 is worth keeping as a one-line assertion somewhere.
S5. Two overlapping patch-width checks in server/config.rs
helios/src/server/config.rs:449-455 and :456-464.
if vision.patch_features != 0 && width != vision.patch_features { … "image preparation patch width …" }
if image_layout == Some(Bracketed) {
let expected = packing::bracketed_patch_features(vision);
if width != expected { … "bracketed image layout patch width …; re-export the core …" }
}
packing::bracketed_patch_features (packing.rs:18-24) returns vision.patch_features verbatim when it is nonzero. Bracketed is the only layout that defines patch_features, so for the profiles where the first check can fire, the second fires on exactly the same condition with a strictly more actionable message (it names the layout, the expected width and the remedy). The first check therefore produces either a duplicate error or nothing.
Fix: keep the Bracketed check (it also covers the legacy 0 → 1176 default) and delete :449-455, or fold the != 0 case into it. Either way one error message per defect, not two.
Nits
N1. Redundant grid-divisibility guard in bracketed
helios/src/frontend/images/packing.rs:501 requires h % merge_side == 0 && w % merge_side == 0, immediately next to :503 geometry.tokens * patches_per_feature == geometry.patches. The pooling side is PATCH * POOLING for Gemma 4 (gemma4.rs:62-64), and every resize target is a multiple of it (gemma4.rs:79-92), so the grid is divisible by construction; MiniMax's producer rounds to the same multiple (patches.rs:36-48). Keeping only the :503 identity lets merge_side go unused in bracketed (it is still needed by tensor_layout at packing.rs:50). Small, but it is a guard for a shape no producer emits.
N2. atomic_spans: bool threaded through the layout boundary
helios/src/model/prompt.rs:52-65 computes …image_tokens.as_ref().and_then(|layout| layout.span.as_ref()).is_some() and passes it as a bare bool into layouts/bracketed.rs:18-24, which stores it in :23 and consults it once at :58. The layout already receives nothing else about the contract, so the derivation sits two modules away from its single use. Passing Option<&ImageTokenLayout> (or a &FrontendContract) keeps the flag next to the code that reads it and drops a positional boolean parameter — a shape that is easy to transpose with the other call sites.
N3. The cross-profile table is four times longer than the behaviours it distinguishes
helios/src/frontend/images/packing.rs:695-824 (~130 lines). It asserts processor.patch_features() == tensor_layout(..) for every profile, which is the property worth keeping — but most rows repeat patch_features: 0. Gemma 4 (9, 768), PatchMerge (4, 0 → 1176), PaddedGrid (1200) and one 588/1536 case cover every distinct outcome. Reducing to those four rows keeps the invariant and drops ~100 lines of fixture construction.
N4. Fixture JSON carries processor/model blocks that no Rust test reads
tests/frontend/gemma4_images.json:2-125 (124 of the file's 257 lines) is the snapshot's processor_config.json (not opened) and a trimmed config.json (not opened). canonical_processor_pixels_positions_and_resize (gemma4.rs:266-333) reads only fixture["cases"]; include_str! at gemma4.rs:270 is the only consumer. The blocks document provenance (max_soft_tokens: 280, patch_size: 16, pooling_kernel_size: 3, model_type: gemma4), so this is a nit: either trim them to the six fields the generator writes at generate_gemma4_image_fixtures.py:81-85 in a provenance object, or add the live-checkpoint test that would consume image_token_id/boi_token_id/eoi_token_id and make them load-bearing. Leaving unreferenced 124-line blocks invites the next reader to assume they are validated somewhere.
Checked and fine (so the report is honest about what I did not flag)
helios/src/frontend/images/patch_merge.rs:10-136: extractingEnvelopeis real de-duplication —patch_merge.rs:123-131andgemma4.rs:160-167now share one expansion; keepingProcessoras a thin delegating wrapper leaves callers inmod.rsuntouched. Correct shape.describe.rs:466-468omitspatch_featureswhen 0, so legacy describe JSON is byte-identical;protobuf.rs:165-170and the round-trip test at:812-864are additive-only. This is the minimum the compatibility rules require.server/config.rs:465-488(envelope vsprefill_chunk_sizeandspan.max_tokens) is one block used for both limits, not a parallel path — correct to have it.model.rs:79patch_features.is_multiple_of(3)is a single clause inside the existingvalidate(), not a new validation layer.- The Rust raster loop
gemma4.rs:135-157and the1/255f32 scaling are required by the checkpoint layout; there is no smaller form that is not a lookup table. ignition/preparation/runtime_core_pb2.py(not opened) is generated; the diff includes aProtobuf Python Version 7.36.1header wheremainhas7.36.2plus a regenerated descriptor. Not a defect (ValidateProtobufRuntimeVersionaccepts it) and not worth a change on its own, but the next regeneration will flip it back — review noise.
Open questions and what I could not verify
- Whether any out-of-tree recipe beyond the companion
local-inference-lab/hyperflare-recipesPR already exports a Gemma 4 core withpatch_features == 0. The load-time error atserver/config.rs:459-461handles it with an actionable message, so I would not hold the PR for it — but if such cores exist, the migration cost of S1 (removing the proto fields) and of theBracketedrequirement is one re-export either way. - Whether the maintainers intend
patch_size/pooling_kernel_sizeto become variable for a future Gemma 4 size. If so, S1 changes shape from "delete" to "keep the fields and delete only the duplicated allowlist and the arm that can never fire"; the Python gate atfrontend.py:124-127is what makes them dead today, and that is an explicit product decision, not an oversight. - Verification limits: this run was read-only. I read each changed function in context (
gemma4.rs,packing.rs,conditioned.rs,patch_merge.rs,layouts/bracketed.rs,model/prompt.rs,model.rs,server/config.rs,runtime_core/{describe,protobuf}.rs,ignition/preparation/frontend.py, the fixture and its generator) but did not runcargo testorpytest, so the PR's stated pass counts are unverified by me and nothing in my findings depends on them. The packet shows no CI checks on2b4a31e. Deleting the two ignored tests (S2, S3) is safe because I located the replacements in the unconditional fixture test andpatch_merge.rs:219-306by reading, not by running; acargo test -- --ignoredon a machine with the snapshot and venv is the only thing that would additionally prove the ignored tests currently pass and therefore that nothing unique is lost.