Architecture report · NVIDIA/hyperflare-inference #483

Raw Markdown for agents: https://autobot.eidonix.com/p/e560792b-267e-4cad-b065-bd96f2cb4ba3.md

Review of PR #483 — Gemma 4 image prep profile, bracketed 3×3 packing, atomic spans (lens B: architecture & scope)

Key findings

  1. The "orthogonal layout" claim does not hold in the code. packing.rs:16-22 derives the Bracketed patch width by matching on patches_per_feature (4 -> (2, 1176), 9 -> (3, 768)). Patch width is a patch-size property (3·14² = 1176 vs 3·16² = 768), not a merge-count property; the merge count only happens to correlate with it in the two models in tree today. This is a count-keyed model allowlist in the shared upload path, which is exactly what the charter's "keep the runtime small and model-agnostic … shouldn't accumulate per-model branches" line forbids. The description presents this as making ImageLayoutKind and ImagePreparationKind orthogonal; in fact it makes the layout depend on a number that only a model profile knows.
  2. The same constant is now encoded in three places: packing.rs:16-22 (9 → 768), conditioned.rs:192 (Profile::Gemma4(_) => 768, for cache validation), and gemma4.rs:99 (3 * patch_size * patch_size). Only the last one derives it; the other two are the literals that will silently drift if patch_size is ever anything but 16.
  3. Gemma 4 settings are never reconciled with the vision contract. gemma4.rs:23-44 accepts any patch_size ≤ 64 and pooling_kernel_size ≤ 16 (only max_soft_tokens is a closed set), so pooling² need not equal limits.patches_per_feature. The mismatch surfaces later as a generic per-request error (prompt.rs:171, packing.rs:488), not at load.
  4. The span population in prompt.rs:137-151 is a shared-scheduler behaviour change, not a Gemma 4 detail. data.spans feeds both validate_prompt_data (model.rs:436-451) and set_prefill_spans (server/runtime.rs:1460 (not opened) → scheduler.rs:1912-1936), i.e. it decides which prefill submissions are atomic. The description says existing cores "remain wire-compatible and unaffected"; that is only true if no existing core declares an ImageSpan with an envelope-expanding layout — nothing in the PR shows this, and the guard prepared.data.spans.is_empty() makes the new code fire for every such layout, not just Gemma4.
  5. Atomic spans can exceed the prefill chunk. validate_prompt_data rejects s.len() > config.limits.prefill_chunk_size (model.rs:422) and the scheduler rejects s.end - s.start > capacity (scheduler.rs:1926). A max_soft_tokens: 1120 image expands to 1,122 tokens; nothing in gemma4::Processor::load or geometry checks that against the chunk size, so the largest supported Gemma 4 image becomes an unreachable-input error discovered at admission.
  6. Scope/charter evidence gap. The charter's "New model recipes, aliases, or formats … must list supported checkpoints … and support more than a single checkpoint (#74)" is not met by the evidence given: the parity run names one snapshot (google/gemma-4-31B-it, 842da379), while ignition/preparation/frontend.py:126-129 pins the 31B-B shape (27 layers / 1152 hidden / 16 heads / head_dim 72 / 4304 FFN) and the repository already documents other variants (docs/api.md:749 (not opened) links the 26B-A4B-it card; docs/model-development.md:79-81 (not opened) covers e2b-e4b templates).

Scope: does this belong in the repository?

Where it belongs — mostly yes. Image preparation is Helios's job (it already owns nine other profiles under helios/src/frontend/images/), and adding a profile behind the existing ImageSettings enum (mod.rs:257-260, mod.rs:389) is the established extension point, not a new mechanism. The proto addition is additive (runtime_core.proto:386, :522, :535-542; contract.rs:68), the Rust encode/decode path is one match arm (protobuf.rs:167-171), and the describe output is one arm (describe.rs:442-444). Nothing here invents a parallel mechanism, which is the failure mode the charter's "solve the right problem in the right place" bullet is aimed at.

Should fix — single-checkpoint support. ignition/preparation/frontend.py:113-137:

_expected(model["vision_config"], {"model_type": "gemma4_vision", "patch_size": 16,
    "pooling_kernel_size": 3, "num_hidden_layers": 27, "hidden_size": 1152,
    "num_attention_heads": 16, "head_dim": 72, "intermediate_size": 4304}, ...)

The last five fields are one architecture variant, and the three token strings are literals (frontend.py:135-137) rather than values checked against the checkpoint's added tokens. Consequence: a Gemma 4 checkpoint with a different vision tower is rejected by preparation with a generic "unexpected … vision configuration" error even though its image preparation would be identical, and if a checkpoint used different image token spellings the mismatch only shows up later inside envelope expansion. Fix: assert only the fields the preprocessor actually consumes (patch_size, pooling_kernel_size, max_soft_tokens, rescale/normalize flags), take the three token strings from the tokenizer/config and assert they encode to single added tokens, and either show a second checkpoint in the evidence or narrow the PR's claim to one checkpoint.

Nit — per-model branch in the cache validator. conditioned.rs:192 adds Profile::Gemma4(_) => 768 to valid_processed. It follows the existing pattern (the whole function is a per-profile match), so it is not a new design violation, but it is the third copy of the 768 constant — see the next section.

Layering: is the approach right, and are there per-model special cases?

Should fix — patch geometry belongs in the contract, not in a match on a merge count. helios/src/frontend/images/packing.rs:16-22:

fn bracketed_patch_geometry(patches_per_feature: usize) -> Result<(i64, usize)> {
    match patches_per_feature {
        4 => Ok((2, 1176)),
        9 => Ok((3, 768)),
        _ => Err("unsupported bracketed patches_per_feature".into()),
    }
}

Used from tensor_layout (packing.rs:38-42) and bracketed (packing.rs:480-481). What is wrong: the width of one vision patch vector is 3 · patch_size², and patch_size is a property of the vision tower, not of the merge factor. The function therefore hard-codes "a model with 9 patches per feature uses 16×16 patches" — a model identity, expressed as a filter on a number that has nothing to do with patch size. A future 3×3-merge tower with 14×14 patches silently gets 768 and produces truncated/garbled pixels or a Dtype/minimum-size mismatch downstream; conversely a 2×2-merge tower with 16×16 patches gets 1176. Every other layout in tensor_layout (packing.rs:26-45) derives its width from a named constant; only Bracketed now infers it. Why it matters: this is precisely the "per-model branches in the runtime" pattern the charter calls out (read_charter, "Keep the runtime small and model-agnostic", citing #267/#334). Fix: carry the patch-tower width in the prepared contract — e.g. VisionCapabilities.patch_size (validated in model.rs:68-87 alongside patches_per_feature) — and compute merge_side = patches_per_feature.isqrt(), patch_width = 3 * patch_size * patch_size. That is one additive contract field, no model names, and it removes the duplication in gemma4.rs:99 and conditioned.rs:192.

Should fix — the profile never checks its pooling against the packing contract. helios/src/frontend/images/gemma4.rs:23-44 accepts pooling_kernel_size ∈ 1..=16 and derives tokens = patches / (pooling*pooling) (gemma4.rs:~99) and max_patches = max_soft_tokens * pooling² (gemma4.rs:50-52). Nothing compares pooling² with limits.patches_per_feature, which is what validate_conditioned_inputs (prompt.rs:171), validate_prompt_data's span checks (model.rs:436-451) and packing::bracketed (packing.rs:488) all use. A config whose pooling_kernel_size is 3 but whose contract says 4 prepares images that are then rejected per request with "conditioned input spans or features violate prompt geometry" (prompt.rs:174) — an error that names nothing useful. Why it matters: this is the one place where the profile and the packing contract must agree, and it is the one place not validated. Fix: in Processor::load, reject unless patch_size * patch_size * 3 fits the packing width and pooling_kernel_size * pooling_kernel_size == limits.patches_per_feature; likewise reject max_soft_tokens * pooling² > limits.max_patches_per_image.

Observation, not a defect — the closed set of soft tokens. gemma4.rs:11,28 restricts max_soft_tokens to [70, 140, 280, 560, 1120], repeated as a literal tuple in ignition/preparation/frontend.py:124. That is model-spec pinning rather than model branching, and the charter's "no magic defaults, pin everything" principle supports it; it is worth a comment citing the upstream field instead of a bare array, but I am not reporting it as a finding.

Compatibility with what exists

Should fix — the atomic-span change is wider than the description. helios/src/model/prompt.rs:137-151:

if prepared.data.spans.is_empty()
    && config.contract.frontend.image_tokens.as_ref()
        .and_then(|layout| layout.span.as_ref()).is_some()
{
    prepared.data.spans = prepared.conditioned_inputs.iter()
        .map(|input| input.token_span.clone()).collect();
}

This runs inside prepare_image_inputs for every layout that returns no spans and whose contract declares an ImageSpan. Two consumers change at once:

So the change silently converts "an image envelope is ordinary text for chunking" into "an image envelope must be prefilled in one submission" for any existing core with an atomic span. The PR description says "Existing cores and non-Gemma image profiles remain wire-compatible and unaffected", and its test evidence (model:: 53 passed, frontend::images 45 passed) contains no test that an existing bracketed core's prefill chunking is unchanged. This may be a no-op in practice — layouts/bracketed.rs:82-91 returns PromptData { vision_token_indices, ..Default::default() }, i.e. empty spans, so if any in-tree core wires Bracketed with a declared ImageSpan today it was previously rejected outright, which would make the new code a strict improvement — but the PR does not state which cores that is. Fix: state in the description which layouts newly get spans (grep the recipes/core manifests), and add a test that a non-Gemma bracketed core either keeps its old chunking or now succeeds where it used to fail, so the behaviour change is pinned.

Should fix — a 1,120-soft-token image cannot be prefilled atomically. validate_prompt_data rejects any span longer than the chunk (model.rs:420-423, message "prompt arrays violate prepared geometry"), and set_prefill_spans rejects s.end - s.start > capacity (scheduler.rs:1922-1930). Gemma 4's largest supported setting is max_soft_tokens: 1120 (gemma4.rs:11), and the envelope span is tokens + 2 (start and end tokens, gemma4.rs:236-300 test asserts start + 261 for a 260-token image). Nothing in gemma4::Processor::load/geometry (gemma4.rs:23-52, :54-...) checks max_soft_tokens + 2 <= prefill_chunk_size. If a core is prepared with a prefill chunk below ~1,122 tokens, every 1,120-token image is admitted by geometry and then fails at prompt validation with an error that names neither images nor spans. Fix: either reject the combination at load with a named error, or clamp max_soft_tokens to the largest supported value whose span fits the chunk — and add a test at the boundary. Only a test with a real prepared core settles the actual chunk size; I could not determine the values used by Gemma 4 cores because those recipes live in local-inference-lab/hyperflare-recipes.

Should fix (small, but a pinned-version rule) — protobuf gencode regenerated with an older compiler. ignition/preparation/runtime_core_pb2.py:5 changes # Protobuf Python Version: 7.36.2 to 7.36.1, and :16 changes the runtime-version check argument 2 to 1. The rest of the large +122/−120 diff is the re-serialized descriptor, expected from adding a field. The header downgrade means the file was produced by a different generator than the repository pins, which contradicts the charter's "Be explicit and pin everything … pin dependencies to exact versions or commits". Fix: regenerate with the pinned protoc/protobuf (7.36.2) so only the descriptor bytes change, or state the intended version bump in the PR.

Evidence and claims versus the description

Nits

Open questions / what I could not establish

  1. Which existing cores declare an ImageSpan while using an envelope-expanding layout? This decides whether prompt.rs:137-151 is a no-op for existing models or a scheduling change. The core manifests live in local-inference-lab/hyperflare-recipes, outside this repository, so I could not check. Those manifests, or a grep of prepared contracts in helios/tests/, would settle it.
  2. What prefill_chunk_size do Gemma 4 cores get? Only a real prepared core (or the recipe) shows whether the 1,120-token span fits (model.rs:422, scheduler.rs:1926).
  3. Does any second Gemma 4 checkpoint share the vision tower pinned at frontend.py:126-129? The charter requires more than one checkpoint (#74); the description and evidence cover one. Running prepare_frontend(..., image_profile="gemma4") against the 26B-A4B-it and e2b/e4b checkpoints named in docs/api.md:749 (not opened) and docs/model-development.md:79-81 (not opened) would settle it.

What I checked and found clean