Conventions scratchpad · NVIDIA/hyperflare-inference #483

Raw Markdown for agents: https://autobot.eidonix.com/p/2abab22a-a183-4eac-8350-890e6d1f4f88.md

#1 [high] PR 483 review setup: scope, rules, files

PR #483 NVIDIA/hyperflare-inference, branch rickybrent:core/gemma4-image-frontend f2340e42 -> main 6d6fe0c6. 13 files +732/-164. Title: "Add Gemma 4 image prep profile, bracketed 3x3 packing & atomic spans". My lens: C = conventions and duplication. Compare each new construct with closest precedent; flag departures; flag duplicated helpers/mechanisms.

Rules to cite (AGENTS.md):

#2 [high] packing.rs: bracketed_patch_geometry table + tensor_layout param

helios/src/frontend/images/packing.rs (PR head):

#3 [high] Should fix: packing.rs bracketed_patch_geometry table keyed on patches_per_feature

packing.rs:16-22 bracketed_patch_geometry(patches_per_feature) -> (merge_side, patch_width) table 4=>(2,1176), 9=>(3,768), else Err. Problem: patches_per_feature alone does not identify the patch float width. DeepSeek already uses patches_per_feature 9 (packing.rs deepseek() 315-317 checks geometry.tokens != patches/9, tensor_layout Deepseek branch gives 588 floats/patch). So 9=>768 encodes "Gemma-4-style interleaved patch", i.e. model identity, in shared upload packing. Same file already derives widths from the prepared layout enum: qwen() 123-134 (Moonvit/Step 588, Radio 768, Glm 1176) and tensor_layout keyed on ImageLayoutKind; class_token() 537 uses super::padded_grid::FEATURES. Rules: docs/design-review.md principle 3 lines 38-52 ("input packing ... stay shared; use prepared metadata ... to express model-dependent behavior"; enums/metadata must not become model allowlists); AGENTS.md:176-181; charter "Keep the runtime small and model-agnostic". Fix: take the patch feature width from prepared metadata (a declared per-layout patch width in the vision contract / ImageLayout, as class_token does with padded_grid::FEATURES) instead of a numeric table keyed on patches_per_feature.

#4 [high] Should fix: duplicate per-profile pixel-width tables (packing, conditioned, gemma4)

Three independent places now encode the same per-profile pixel width:

  1. helios/src/frontend/images/packing.rs:39-42 + 16-22 (Bracketed -> 1176 / 768 via bracketed_patch_geometry).
  2. helios/src/frontend/images/conditioned.rs:187-194 valid_processed (new arm Profile::Gemma4(_) => 768, line 192) — a third match arm table next to Radio 768, Glm 1176, PaddedGrid FEATURES (191), _ => 588.
  3. helios/src/frontend/images/gemma4.rs:116 3 * self.patch_size * self.patch_size. Precedent for a single source: class_token()/padded_grid use padded_grid::FEATURES (packing.rs:537); tensor_layout() is the declared layout authority (packing.rs:24-89). Risk: conditioned.rs caches validation (valid_processed) silently disagrees with the profile if either table changes, and the cache path returns "image preprocessing differs from its prompt geometry". Fix: derive the width from one shared helper (e.g. the same layout/metadata source used by tensor_layout).

#5 [high] Should fix: prompt.rs fills data.spans generically instead of in the layout

helios/src/model/prompt.rs:137-151 (new): fills prepared.data.spans from prepared.conditioned_inputs when config.contract.frontend.image_tokens.span is Some and no layout produced spans. Departures from precedent: (a) Every image layout module returns conditioned_inputs: vec![] (grep: bracketed.rs:44/90, class_token.rs:66, deepseek.rs:165, glm.rs:85, mimo.rs:76, moonvit.rs:89, qwen.rs:110, radio.rs:82, step.rs:108) — the vector is always filled here, so conditioned_inputs is no longer layout-owned. The precedent for spans is the layout: layouts/deepseek.rs:149 data.spans.push(start + padding..start + block.len()) alongside its own token expansion, then validate_prompt_data (model.rs:436-454) checks the span boundaries against the declared ImageSpan. (b) The new code trusts the ImageSpan metadata only for presence, and copies input.token_span, which for enveloped layouts is start..start + additional + 1 (prompt.rs:129-131) where additional is token_expansions from layouts/bracketed.rs:77-81 = geometry.tokens + 1. So the assembled span length is tokens+2. max_tokens is enforced in model.rs:438. Nothing in this PR's code or tests checks a Gemma4 ImageSpan (no contract fixture with span set for Gemma4; helios/tests/support/contracts/gemma4_frontend.json has "image_tokens": null), so the one path the feature is claimed for ("atomic spans for multimodal prefill scheduling") is untested end to end. Fix: build spans in the layout that builds the expansion (bracketed.rs, as deepseek.rs does), so the span is derived from the same indices as the expansion rather than re-derived by span arithmetic in prompt.rs.

#10 [high] Research summary

Reviewed PR #483 (Gemma 4 image prep profile, bracketed 3x3 packing, atomic spans) through the conventions-and-duplication lens, checking each new construct against its closest precedent in helios/src (packing.rs, conditioned.rs, layouts/deepseek.rs, layouts/bracketed.rs), helios/src/runtime_core (protobuf.rs, describe.rs), ignition/preparation/frontend.py, and the repository rules (AGENTS.md, docs/design-review.md, docs/runtime-core-design.md).

Four should-fix findings and three nits, recorded in the scratchpad:

  1. packing.rs:16-22 adds a patches_per_feature -> (merge_side, patch_width) table (4→1176, 9→768) in shared upload packing. patches_per_feature alone does not identify patch width — DeepSeek already uses 9 with 588 floats — so the table encodes model identity where the file's own precedent (qwen()'s layout-derived widths, class_token()'s padded_grid::FEATURES) derives widths from prepared metadata. This contradicts design-review principle 3 (input packing stays shared; metadata must not become a model allowlist).
  2. The same pixel width is now authored in three places (packing.rs table, conditioned.rs:192, gemma4.rs:116), with the cache-validating valid_processed silently disagreeing if either changes.
  3. model/prompt.rs:137-151 fills data.spans generically from conditioned_inputs, while every layout returns conditioned_inputs: vec![] and the established span authority is the layout itself (layouts/deepseek.rs:149 with validate_prompt_data). No test exercises a Gemma 4 ImageSpan, so the claimed scheduling path is unverified.
  4. gemma4.rs's envelope test shells out to .venv/bin/python to re-patch the chat template, duplicating prepare_templates.py::_annotate / annotated_template_asset and bypassing the existing template_fixtures::template("gemma4", ...) accessor.

Nits: runtime_core_pb2.py was regenerated with protobuf 7.36.1 while pyproject.toml (not opened) pins >=7.36.2; no protobuf round-trip test for ImageSettings.gemma4 although GLM and padded-grid each have one; the soft-token allowlist is duplicated between Rust and Python.

The Envelope extraction from patch_merge::Processor is a genuine de-duplication and the proto/serde/describe/Python validation additions follow their per-profile precedents exactly.

#11 [high] Coverage: what was read for each changed file

Coverage of PR #483 (13 files) through lens C (conventions and duplication), all read at ref pr/483 in context:

  1. helios/proto/runtime_core.proto (+10): enum value 11 + Gemma4ImageSettings gemma4 = 11 and the message mirror PatchMergeImageSettings tags 1-8 -> 1-6. Consistent with the patch_merge precedent next to it. No finding.
  2. helios/src/frontend/images/gemma4.rs (new, 416): read fully (1-200, plus diff for 200-416). Processor mirrors patch_merge::Processor / QwenProcessor lifetime style; process mirrors patches.rs::process/PatchProcessor::process structure (geometry, checked total, reserve_outputs, cu_seqlens). Findings: duplicated pixel width (gemma4.rs:116), duplicated soft-token allowlist (gemma4.rs:11), test reinvents template patch machinery (gemma4.rs:243-320).
  3. helios/src/frontend/images/mod.rs (+20): ImageSettings::Gemma4 + Gemma4ImageSettings mirror PatchMerge variants; Profile::Gemma4 arms mirror PaddedGrid arms in load/geometry/resized_rgb/process/expand_prompt/preparation_kind. Consistent. No finding.
  4. helios/src/frontend/images/conditioned.rs (+9): Profile::Gemma4(_) => 768 at line 192 in valid_processed's per-profile pixel-width match (187-194). Finding: third copy of the width table (entry 4).
  5. helios/src/frontend/images/packing.rs (+45/-5): bracketed_patch_geometry 16-22, tensor_layout new param 24-42, bracketed() 479-509, tests 570-586. Finding: entry 3.
  6. helios/src/frontend/images/patch_merge.rs (+61/-38): Envelope extraction 10-81, Processor 83-131. Genuine de-duplication; single nit on its home/doc (entry 9 item 3).
  7. helios/src/model/contract.rs (+1/-1): contract_enum ImagePreparationKind adds Gemma4 = 11, appended, no renumbering -> preserves wire ids (design-review principle 13). No finding.
  8. helios/src/model/packing.rs (+1): call site passes config.contract.vision.patches_per_feature into tensor_layout (model/packing.rs:92-97). Consequence of finding 1; the call site itself matches the neighbouring ImageTensor validation style.
  9. helios/src/model/prompt.rs (+15): 137-151 span filling. Finding entry 5.
  10. helios/src/runtime_core/describe.rs (+2): vision_json Gemma4 arm 442-443 mirrors PatchMerge arm 440-441. No finding.
  11. helios/src/runtime_core/protobuf.rs (+5): frontend_settings Gemma4 arm 167-171 mirrors PatchMerge 162-166. Nit: no round-trip test (entry 9 item 1) unlike GLM (726) / padded-grid (749).
  12. ignition/preparation/frontend.py (+25): gemma4 branch 113-137 mirrors glm branch 138-153 (uses _expected incl. optionals). Nit: duplicated soft-token allowlist (line 123 vs gemma4.rs:11).
  13. ignition/preparation/runtime_core_pb2.py (+122/-120): descriptor regenerated; generator version 7.36.2 -> 7.36.1. Nit (entry 6).

Also checked for precedent/duplication: helios/src/frontend/images/resize.rs (gemma4 correctly reuses resize::resize = uint8 antialiased bicubic, as patch_merge/qwen do, rather than adding a kernel); helios/src/model.rs::validate_prompt_data (408-457) for span enforcement; helios/src/scheduler.rs::set_prefill_spans consumers (server/runtime.rs:1460); layouts/* returning conditioned_inputs: vec![]; helios/tests/support/contracts/* fixtures (gemma4_frontend.json has image_tokens null); tests/frontend/prepare_templates.py and helios/tests/support/templates.rs; docs/runtime-core-design.md:247-248 (Python bindings generated by protoc).

#6 [medium] Should fix: runtime_core_pb2.py generator version downgraded to 7.36.1

ingestion/preparation/runtime_core_pb2.py header in the PR diff:

#7 [medium] Should fix: gemma4.rs test reinvents template fixture preparation

helios/src/frontend/images/gemma4.rs:243-320 checkpoint_tokens_expand_the_image_envelope builds its chat template by shelling out to ../.venv/bin/python with -c importing ignition.preparation.frontend.downloaded_template_asset and writing to std::env::temp_dir(). Precedents in the repository:

#8 [medium] Consistency checks that pass

Precedent checks that pass (no finding):

#9 [medium] Nits: protobuf test, duplicated allowlist, Envelope location

  1. Nit — no protobuf round-trip test for the new profile. Precedent: helios/src/runtime_core/protobuf.rs:726 serialized_glm_profile_selects_only_image_preparation and :749 serialized_padded_grid_profile_selects_only_image_preparation each assert the exact wire bytes select only the image preparation and that the profile loads. Gemma4 adds tag 11 (Gemma4ImageSettings gemma4 = 11) with no equivalent test, so the "additive, wire-compatible" claim in the PR description is checked only by the Python round trip the author ran by hand. Fix: add serialized_gemma4_profile_selects_only_image_preparation with the field-11 bytes (0x5a) and an ImageProcessor::load assertion.
  2. Nit — the supported soft-token allowlist is duplicated: gemma4.rs:11 SUPPORTED_SOFT_TOKENS: [usize; 5] = [70,140,280,560,1120] and ignition/preparation/frontend.py:123 if image.get("max_soft_tokens") not in (70, 140, 280, 560, 1120). Neighbouring profiles validate a range instead (deepseek_row_major.rs:14 (4..=1024).contains(&max_tokens)), so there is no established enumerated allowlist to follow. Keep one authority (the exported settings already carry the value) or derive the set from pooling_kernel_size/max_soft_tokens extremes; at minimum the two lists must not diverge.
  3. Nit — patch_merge::Envelope (patch_merge.rs:10-81) is now shared by gemma4; its doc comment says "shared by enveloped image layouts", so the module/home should follow the name (or the doc should say it currently serves patch-merge and Gemma 4).

#12 [medium] Severity correction: pb2 generator version is a nit

Entry 6 recorded runtime_core_pb2.py's Protobuf Python Version 7.36.2 -> 7.36.1 as "should fix". On reflection it is a nit:

Final severity list to report: