Skip to content

fix(vision): Gemma 3n VLM per_layer_inputs is not sequence-aware (concurrent-request race) #85

Closed

Description

@inureyes

Summary

The Gemma 3n VLM wrapper (src/vision/gemma3n_vl.rs) stashes its projected per_layer_inputs tensor in a single shared RefCell<Option<UniquePtr<MlxArray>>> field (cached_per_layer_inputs). get_input_embeddings writes it; forward_with_embeddings later drains it via .take().unwrap(). The batch scheduler runs the embedding-prep step once per request at enqueue time but runs the prefill step asynchronously at scheduling time, so two or more concurrent Gemma 3n VLM requests can overwrite that single slot before the first one prefills.

Impact

When 2+ Gemma 3n VLM requests are enqueued before the first one prefills (a typical burst on an idle server):

  • The first prefill consumes the latest writer's per_layer_inputs instead of its own → incorrect outputs.
  • If prepares interleave with prefills such that the slot is already drained → panic on Option::unwrap() in forward_with_embeddings.

This affects only the server's batched path (mlxcel-server); single-row CLI / bench paths are unaffected.

Root cause

A single-instance cache cannot represent N in-flight requests — the slot carries no per-sequence identity, so concurrent writes race.

Proposed fix

Mirror the existing, already-merged Gemma4PerLayerInputsState container (src/vision/gemma4_per_layer_inputs_state.rs), which solved the identical problem for the Gemma 4 VLM:

  1. Add src/vision/gemma3n_per_layer_inputs_state.rs — a Gemma3nPerLayerInputsState with a per-SequenceId map plus a fallback slot for legacy single-row callers, mirroring the Gemma 4 container's API (set_fallback, bind_fallback_to_sequence, take_for_sequence, take_fallback, release_sequence, bind_for_sequence).
  2. Replace cached_per_layer_inputs in Gemma3nVLModel with the new container. Implement forward_with_embeddings_and_sequence_id so prefill resolves the tensor from the seq-keyed map first, falling back to the legacy slot for CLI / bench / single-row callers. Route the legacy forward_with_embeddings through the seq-aware variant with seq_id = None. Wire release_sequence_state_by_id to drain the map alongside the scheduler's per-sequence cache cleanup.
  3. Add the matching LoadedModel capability surface (bind_gemma3n_per_layer_inputs_to_sequence, take_gemma3n_per_layer_inputs_entry, install_gemma3n_per_layer_inputs_entry) in src/loaded_model_capabilities.rs, mirroring the Gemma 4 capability methods, so the scheduler can drive the container without depending on the concrete VLM variant.
  4. Hook it into src/server/batch/scheduler.rs: in drain_incoming_requests right after prepare_request_vlm_embeddings (next to the equivalent Gemma 4 / Qwen VL bind calls) and into the preemption round trip (next to Gemma 4).

Acceptance criteria

  • Gemma3nPerLayerInputsState added with unit tests covering the burst-enqueue race, the fallback round trip, release semantics, and the preemption round trip (mark #[ignore = "requires serial MLX execution"], consistent with the Gemma 4 helper tests).
  • Gemma3nVLModel uses the per-sequence container; the legacy single-row path is preserved via the fallback slot.
  • LoadedModel capability methods added and wired into the scheduler enqueue + preemption paths, mirroring Gemma 4.
  • A concurrent burst of Gemma 3n VLM requests no longer panics, and each request consumes its own per_layer_inputs.
  • cargo build, cargo clippy, and cargo test pass with the metal,accelerate feature set.

References

  • In-repo precedent: src/vision/gemma4_per_layer_inputs_state.rs (Gemma4PerLayerInputsState) and its scheduler wiring in src/server/batch/scheduler.rs.

Activity

  1. added
    type:bug
    priority:high
    status:ready
    area:inference
    area:models
    on May 25, 2026
  2. self-assigned this
    on May 25, 2026
  3. added this to the 0.1 milestone on Jun 5, 2026
  4. added
    status:done
    and removed
    status:ready
    on Sep 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:inferencearea:modelspriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions