Review of PR #483 — Gemma 4 image prep profile, bracketed 3×3 packing, atomic spans
Lens A (correctness and consequences): every caller, every supported configuration, resource lifetimes, inactive and edge-case paths, state/graph transitions, and whether existing cores and artifacts still load.
Rules applied: charter line 12 ("Keep the runtime small and model-agnostic … shouldn't accumulate per-model branches"), AGENTS.md 431-444 (protobuf additions must preserve existing behaviour; every engine version must load previous cores), docs/design-review.md principles 3 (shared mechanisms, no per-model policy flags), 10 (persist only consumed structural frontend settings in typed fields), 13 (core compatibility).
Key findings
Bracketedpacking now hard-errors at core load for apatches_per_featureit never used before.helios/src/frontend/images/packing.rs:16-22(bracketed_patch_geometry) accepts only 4 → (2, 1176) and 9 → (3, 768) and returnsErrotherwise;helios/src/model/packing.rs:92-97calls it fromPacking::newwhile the core is loading. Before this PRBracketedused a fixed 2 × 1176 geometry, so a core that declaresImageLayoutKind::Bracketedwith any otherpatches_per_featureloaded and packed fine on main and now fails to start. The PR description says "Existing cores … remain wire-compatible and unaffected"; that is true of the wire format, not of this load path. Needs a sweep of the shipped/exercised cores (the vision contract field is set per recipe inignition/preparation/contract.py:112; e.g.helios/tests/tools/test_step_images.rs:82(not opened) setspatches_per_feature = 16) before the check is trusted.- The Gemma 4 profile accepts settings the packing path cannot represent.
helios/src/frontend/images/gemma4.rs:23-31validates onlypatch_size != 0 && <= 64,pooling_kernel_size != 0 && <= 16, and amax_soft_tokenswhitelist (SUPPORTED_SOFT_TOKENS, gemma4.rs:11). It never requires the 16/3 pair thatbracketed_patch_geometrymaps and thatignition/preparation/frontend.py:116-129actually exports. A Gemma 4 core declaring, say,patch_size = 14, pooling = 2loads (itspatches_per_feature = 4satisfies the table), the processor emits 3·14·14 = 588 floats per patch (gemma4.rs:116), andbracketed()(packing.rs:479-508) slices with the 1176-wide stride — either "truncated vision pixels" or, when the slab is large enough, silently misaligned pixels into the vision graph. The cached-record check cannot catch it either (see finding 3). - Three independent derivations of the 768-float Gemma 4 patch width.
helios/src/frontend/images/conditioned.rs:192(Profile::Gemma4(_) => 768) validates cached processed images,helios/src/frontend/images/packing.rs:19(9 => (3, 768)) computes the upload stride, andhelios/src/frontend/images/gemma4.rs:116computes3 * patch_size * patch_sizewhen producing the data. The PR argues the derived-width design removes model-identity branching, but the validator still keys on the profile with a literal. If any two of the three disagree,valid_processed(conditioned.rs:185-225) passes data the upload stage slices at a different width, with no error unless the slice runs past the end. - The new atomic-span path adds failure modes that the PR does not mention.
helios/src/model/prompt.rs:137-151copies everyconditioned_inputstoken span intodata.spanswhen the layout declares anImageSpan.validate_prompt_data(helios/src/model.rs:420-424) rejects any span whoselen()exceedsconfig.limits.prefill_chunk_size; a Gemma 4 envelope ismax_soft_tokens + 1, i.e. up to 1,121 tokens with the 1,120-token setting (gemma4.rs:11,patch_merge.rs:60-69). So any deployment with a prefill chunk below the envelope size now fails prompt validation for a maximum-size image, andScheduler::set_prefill_spans(helios/src/scheduler.rs:1923-1933) additionally fails a request whose prefix-cache match ends inside an envelope;helios/src/server/runtime.rs:1458-1465turns that error into a failed request. Neither case has a test. - The claimed Hugging Face parity is not exercised by CI. Both reference tests bail out silently when the environment is absent:
gemma4.rs:238-244(FLARE_GEMMA4_TEST_SNAPSHOT) andgemma4.rs:336-339(../.venv/bin/python). With no CI on the head commit, the "exact f32 pixel and (x, y) position parity" claim rests entirely on a local run, so a regression inresize::resizeor the patch loop would pass the suite.
Callers and configurations
- Packing callers.
tensor_layouthas exactly two call sites I could find:helios/src/model/packing.rs:92-97(core load, now passingconfig.contract.vision.patches_per_feature) and the storage/layout path atpacking.rs:153andpacking.rs:479-508for uploads. The stride is now consistent between load-time sizing and upload-time slicing for 4 and 9; the incompatible-configuration risk is finding 1. patches_per_featureconsistency elsewhere.helios/src/model.rs:74-81requires everypatch_capacitiesentry to be a multiple ofpatches_per_feature, andhelios/src/model/prompt.rs:171requirestokens * patches_per_feature == patchesfor conditioned inputs. With 9 declared,helios/src/server/config.rs:442-463derivesmax_total_patchesandmax_patches_per_imagefrom it, so the server-side limits follow the new value — the packing change does not desynchronise them.helios/src/server/request.rs:777 (not opened),816already use 9 in tests.- Other profiles. All non-Gemma profiles keep their fixed widths (
packing.rs:32-45);class_token()still assertspatches_per_feature == 4(packing.rs:527) andpadded_grid.rs:186 (not opened),229sets 4. TheBracketed/9combination is new, so no existing graph shape is re-sized by the derivation. - Span consumers. Only layouts that declare
image_tokens.spanreach the new fill; Deepseek already wrote spans inhelios/src/frontend/images/layouts/deepseek.rs:149, and the fill is guarded bydata.spans.is_empty()(prompt.rs:137), so Deepseek behaviour is unchanged. For every other layout the branch is inert becausespanisNone(seehelios/tests/support/contracts/*.json), andmodel.rs:452-453still rejects spans for those layouts. So the change is correctly scoped — its consequences are finding 4. - Envelope validation.
Envelope::load(patch_merge.rs:19-30) rejects empty or duplicate markers, andexpand(patch_merge.rs:38-57) rejects a marker that does not encode to exactly one token and a placeholder/image count mismatch. The extraction frompatch_merge::Processorkeeps those checks; only the message text for empty/duplicate tokens changed to "invalid image envelope tokens".
Resource lifetimes and edge-case paths
- Nothing new allocates a handle or lease here:
gemma4::Processor::process(gemma4.rs:103-157) buildsProcessedImageswith capacity fromgeometry, useschecked_add/checked_mulfor the patch totals and buffer extent (gemma4.rs:109-122), and callslimits.check_batchtwice (before and after geometry).resized_rgb/geometrygo throughcheck_input_extentandcheck_image_patches(gemma4.rs:55,85), so the per-image and per-request budgets are enforced on the new path. - Degenerate-side clamping is safe: when both target dimensions floor to zero the code returns "image resizes below one Gemma 4 patch group" (
gemma4.rs:65-69), and the two single-sided clamps (gemma4.rs:70-76) only divide when the other dimension is non-zero, so the integer divisions cannot produce a zero grid. The unit test table (gemma4.rs:200-215) covers10×3000and3000×10; as the description admits, those are synthetic rather than real ultra-wide photos. - Alpha/EXIF:
helios/src/frontend/images/mod.rs:537,613,705applies EXIF orientation and RGB conversion for every profile except Moonvit/DeepseekRowMajor/PaddedGrid, Gemma 4 included. The parity test feeds a syntheticRgbImage(gemma4.rs:378-383), so orientation and alpha handling for Gemma 4 are untested; this is the same coverage every other profile has, so it is a test gap, not a defect in this diff.
Existing cores, artifacts and state transitions
- Wire compatibility is additive and holds:
ImageSettings.profilegains oneof tag 11 (helios/proto/runtime_core.proto:510-524) andGemma4ImageSettingsis a new message (runtime_core.proto:535-542);ImagePreparationKindgainsGEMMA4_IMAGE_PREPARATION = 11after existing values (runtime_core.proto:374-387), andImageLayoutKindis untouched, so no existing tag or enum value moves. Decoding onhelios/src/runtime_core/protobuf.rs:167-171and describing onhelios/src/runtime_core/describe.rs:442add the profile without changing other arms. A core prepared by this branch is not readable by an older binary (unknown oneof tag → "missing protobuf image profile",protobuf.rs:192-194), which is the expected forward direction. - The only back-compat break I can see is the load-time rejection in finding 1.
- Cached prepared-image records:
conditioned.rs:150-183validates a stored record withvalid_processedbefore reuse; for Gemma 4 that means the hardcoded 768 check in finding 3 and thepixel_values.len() == patches * 768comparison atconditioned.rs:220. A record written by a differently-configured processor is rejected and re-materialised (conditioned.rs:164-166), so the cache path itself is safe — provided the profile-level validation gap is closed.
Nits
ignition/preparation/runtime_core_pb2.py: besides adding the Gemma 4 message and enum, the generated header changes "Protobuf Python Version: 7.36.2" → "7.36.1" (and the version-check argument), i.e. the file was regenerated with an olderprotocthan main.helios/manifest.md:465-472documents the regeneration command; rerun it with the tree's protoc so the binding version does not regress.ignition/preparation/frontend.py:130-137hardcodes"<|image|>","<|image>",<image|>rather than deriving them from the tokenizer; the profile gate (frontend.py:114-129) pinsmodel_type = gemma4, so this is acceptable, but a mismatch would only surface at request time as "image envelope marker must encode one token".
Open questions / not settled from the code
- Does any shipped or previously exported core declare
ImageLayoutKind::Bracketedwithpatches_per_feature != 4? If yes, finding 1 is a startup regression and should block; only a sweep of the recipe-produced cores can settle it. - Should a prefix-cache match that ends inside an atomic image envelope fail the request, or should the scheduler fall back to the last envelope boundary?
scheduler.rs:2149already consultsvision.patches_per_feature, suggesting image-aware boundary handling may exist; I could not confirm it handles atomic spans, and no test asserts either behaviour. - Is
prefill_chunk_sizeguaranteed by the Gemma 4 recipe to exceedmax_soft_tokens + 1(up to 1,121)? Nothing in this PR enforces or documents it. - The Hugging Face parity result cannot be reproduced without the
google/gemma-4-31B-itsnapshot and.venv; a CI-runnable fixture (stored reference tensors) would make finding 5 moot.