T5Gemma fails on MPS: incorrect beam expansion with padded batches

Đang mở
#2,153 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

@TBO22 đang làm issue này rồi.

Từ ngày 15/11/2026.

  • #2155 của @TBO22 — đang mở

Đánh giá

Độ khó
4/5
Thời gian dự kiến
3-5 ngày
Mức phù hợp với người mới
30/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
cpp, python
Lĩnh vực
machine-learning

Hướng nghiên cứu

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.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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.

Ngôn ngữ chính
C++
Star
4.8k
Fork
557
Merge trung bình
1 ngày 6 giờ
Pull request đã merge (30 ngày)
24

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của OpenNMT/CTranslate2

Tất cả issue của OpenNMT/CTranslate2

Issue tương tự

Thêm issue về C++

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.