T5Gemma fails on MPS: incorrect beam expansion with padded batches

オープン
#2,153 コメント 1 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@TBO22 がすでに取り組んでいます。

2026年11月15日 から。

  • #2155 @TBO22 による — オープン

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
30/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
cpp, python

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
C++
スター
4.8k
フォーク
557
平均マージ
1日 6時間
マージ済み PR(30日)
24

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

OpenNMT/CTranslate2 のほかの issue

OpenNMT/CTranslate2 の issue をすべて見る

似ている issue

C++ の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。