Skip to content

fix(vision): Gemma 3n VLM prefill aborts with [broadcast_shapes] mismatch when per-layer-inputs length diverges from the padded token stream #35

Closed

Description

@inureyes

Summary

Gemma 3n VLM single-row prefill can abort with a [broadcast_shapes] error when the projected per-layer-inputs tensor's sequence length diverges from the merged input-embeddings length. This occurs on code paths that pad the token stream and merged embeddings up to a tile-aligned length (for example Neural Accelerator tile alignment), because the per-layer-inputs tensor is projected and cached before that generic padding step runs.

Symptom

libc++abi: terminating due to uncaught exception of type std::invalid_argument:
[broadcast_shapes] Shapes (1,288,256) and (1,273,256) cannot be broadcast.

The 273-token side matches the prompt + image-token count for "What is in this image?" against tests/fixtures/test_image.png. The 288-token side is the tile-padded length (273 padded up to the next tile boundary). Hidden dim 256 matches Gemma 3n's per-layer-inputs hidden size, so the broken broadcast is between the per-layer-inputs tensor and the main token stream.

Reproduction

cargo build --release --bin mlxcel-bench-decode
./target/release/mlxcel-bench-decode \
    -m ./models/gemma3n-e2b-4bit \
    -p "What is in this image?" \
    --image tests/fixtures/test_image.png \
    -n 30 \
    --warmup-tokens 20

All three Gemma 3n VLM checkpoints (gemma3n-e2b-4bit, gemma3n-e4b-4bit, gemma3n-e4b-bf16) abort identically when the tile-padding prefill path is active.

Scope

  • Affected: VLM mode (--image) for Gemma 3n checkpoints when prefill pads the token stream to a tile-aligned length.
  • Not affected: Text-only mode; VLM mode on paths that do not pad the sequence dimension.

Suspected cause / fix direction

The projected per-layer tensor is captured in Gemma3nVLModel::get_input_embeddings before the generic prefill padding step aligns the token stream and merged embeddings to a tile length. The per-layer blend in forward_with_embeddings then tries to broadcast the unpadded per-layer tensor against the padded embeddings. The fix is to align the per-layer-inputs tensor to the embeddings sequence length (pad with zeros when shorter, slice when longer, leave untouched when equal) before the blend.

  • src/vision/gemma3n_vl.rs — the get_input_embeddings projection capture and the forward_with_embeddings per-layer blend.

Acceptance criteria

  • mlxcel-bench-decode single-row Gemma 3n VLM prefill completes without aborting on tile-padded paths for all three Gemma 3n VLM checkpoints.
  • A regression test in tests/vlm_concurrency.rs covers a single-row Gemma 3n VLM prefill so a future shape drift is caught.
  • Unit coverage for the per-layer-inputs alignment helper (pad / slice / no-op cases).

Related

Activity

  1. added
    type:bug
    priority:high
    area:models
    on May 19, 2026
  2. self-assigned this
    on May 19, 2026
  3. added this to the 0.1 milestone on Jun 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area:modelspriority:highstatus:donetype:bug

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions