T5Gemma fails on MPS: incorrect beam expansion with padded batches

Aperta
#2,153 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

@TBO22 ci sta già lavorando.

Dal 15/11/2026.

  • #2155 di @TBO22 — aperta

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
30/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
cpp, python

Direzione di ricerca

Start from MultiHeadAttention::forward_merged, where the payload says beam size is inferred from memory->dim(0) after padding removal; trace how the padded batch is restored before encoder keys/values are expanded for beam search. Reproduce with the given script on an MPS device to confirm the LLDB shapes (decoder [16, 4, 1, 256] vs encoder [8, 4, 348, 256]) at the concatenation. Done when beam size comes from the restored encoder key tensor's batch dimension and a regression test covers a variable-length batch with beam_size=2.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

T5Gemma generation fails with PyPI CTranslate2 4.9.0:

RuntimeError: MPS pointer is not backed by a registered Metal buffer

Observed in CI and reproduced on an M4 Pro running macOS 15.7.7.

Reproduction
pip install ctranslate2==4.9.0 transformers==5.22.2 \
  torch==2.15.0 datasets==5.1.0 sentencepiece==0.2.3
from pathlib import Path

import ctranslate2
from datasets import load_dataset
from transformers import AutoTokenizer

model = "jordimas/t5gemma-2-270m-270m"
revision = "dfb6d0d619d8e6d42f279a6197abb90dec0137c1"
output_dir = "t5gemma-ct2-float32"

if not Path(output_dir).exists():
    ctranslate2.converters.TransformersConverter(
        model, revision=revision
    ).convert(output_dir, quantization="float32")

tokenizer = AutoTokenizer.from_pretrained(model, revision=revision)
samples = load_dataset(
    "rajpurkar/squad",
    split="validation",
    revision="7b6d24c440a36b6815f21b70d25016731768db1f",
).shuffle(seed=42).select(range(8))

inputs = [
    tokenizer.convert_ids_to_tokens(tokenizer.encode(
        f"Context: {s['context']} Question: {s['question']} Answer:",
        truncation=True, max_length=512,
    ))
    for s in samples
]

translator = ctranslate2.Translator(
    output_dir, device="mps", compute_type="float32",
    intra_threads=2, inter_threads=1,
)
translator.translate_batch(
    inputs, beam_size=2, sampling_topk=1,
    max_decoding_length=64, max_batch_size=8,
)
Cause and suggested fix

On the first decoding step, MultiHeadAttention::forward_merged infers beam size from memory->dim(0). After padding removal, this dimension counts packed tokens rather than examples, so encoder keys/values are not expanded for beam search.

LLDB confirmed mismatched shapes at concatenation:

  • Decoder keys: [16, 4, 1, 256]
  • Encoder keys: [8, 4, 348, 256]

Metal concatenation consequently requests more bytes than the encoder buffer contains.

Infer beam size from the restored encoder key tensor's batch dimension before expansion. Add regression coverage for variable-length batches with beam_size=2.

Lingua principale
C++
Stelle
4.8k
Fork
557
Merge medio
1g 6h
PR unite (30g)
24

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di OpenNMT/CTranslate2

Tutte le issue di OpenNMT/CTranslate2

Issue simili

Altre issue su C++

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.