Repository navigation
Description
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_inputsinstead of its own → incorrect outputs. - If prepares interleave with prefills such that the slot is already drained → panic on
Option::unwrap()inforward_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:
- Add
src/vision/gemma3n_per_layer_inputs_state.rs— aGemma3nPerLayerInputsStatewith a per-SequenceIdmap 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). - Replace
cached_per_layer_inputsinGemma3nVLModelwith the new container. Implementforward_with_embeddings_and_sequence_idso prefill resolves the tensor from the seq-keyed map first, falling back to the legacy slot for CLI / bench / single-row callers. Route the legacyforward_with_embeddingsthrough the seq-aware variant withseq_id = None. Wirerelease_sequence_state_by_idto drain the map alongside the scheduler's per-sequence cache cleanup. - Add the matching
LoadedModelcapability surface (bind_gemma3n_per_layer_inputs_to_sequence,take_gemma3n_per_layer_inputs_entry,install_gemma3n_per_layer_inputs_entry) insrc/loaded_model_capabilities.rs, mirroring the Gemma 4 capability methods, so the scheduler can drive the container without depending on the concrete VLM variant. - Hook it into
src/server/batch/scheduler.rs: indrain_incoming_requestsright afterprepare_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
-
Gemma3nPerLayerInputsStateadded 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). -
Gemma3nVLModeluses the per-sequence container; the legacy single-row path is preserved via the fallback slot. -
LoadedModelcapability 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, andcargo testpass with themetal,acceleratefeature set.
References
- In-repo precedent:
src/vision/gemma4_per_layer_inputs_state.rs(Gemma4PerLayerInputsState) and its scheduler wiring insrc/server/batch/scheduler.rs.
Activity