Repository navigation
Description
Describe the bug
AudioLDM2Pipeline.generate_language_model runs GPT-2 with inputs_embeds and attention_mask but no position_ids (prepare_inputs_for_generation, loop). GPT-2 then uses positions 0..L-1 for every row, including masked positions. transformers' generate() avoids this by deriving position_ids from the mask; this custom loop does not.
The GPT-2 input has padding in the middle. The projection model builds [sos, clap, eos, sos_1, t5 tokens, eos_1] (code), and the T5 prompt is tokenized with padding=True (code). In a batch, a shorter prompt gets masked padding between its last T5 token and eos_1. The mask hides the padding, but eos_1 and the 8 generated tokens sit at positions shifted by the padding length. So the conditioning of a prompt, and the audio, depends on the longest prompt in the batch. The negative prompt path (code) works the same way.
Suggested change
Compute position_ids from the mask in prepare_inputs_for_generation, as in the reproduction below. generate_language_model already extends the mask by one for each generated token through _update_model_kwargs_for_generation, so slicing the last inputs_embeds.shape[1] positions also covers the cached steps.
Reproduction
CPU, a few seconds, tiny random GPT-2. It calls generate_language_model on one prompt alone and the same prompt padded in a batch, then repeats with mask-derived position ids:
import torch
from transformers import GPT2Config, GPT2LMHeadModel
from diffusers import AudioLDM2Pipeline
from diffusers.pipelines.audioldm2 import pipeline_audioldm2 as mod
torch.manual_seed(0)
config = GPT2Config(n_embd=32, n_layer=2, n_head=4, n_positions=64, vocab_size=10, bos_token_id=0, eos_token_id=0)
pipe = object.__new__(AudioLDM2Pipeline)
pipe.__dict__["language_model"] = GPT2LMHeadModel(config).eval() # only generate_language_model is used
# AudioLDM2ProjectionModel output is [sos, clap, eos, sos_1, t5 tokens, eos_1]. The T5 prompt is
# tokenized with padding=True, so in a batch a shorter prompt gets <pad> positions between its
# last T5 token and eos_1 (masked out). Alone, the same prompt has no padding.
head = torch.randn(1, 4, 32) # sos, clap, eos, sos_1
t5, eos_1 = torch.randn(1, 3, 32), torch.randn(1, 1, 32)
single = torch.cat([head, t5, eos_1], 1) # 8 positions
padded = torch.cat([head, t5, torch.zeros(1, 2, 32), eos_1], 1) # 10 positions
batch = torch.cat([padded, torch.randn(1, 10, 32)]) # with a longer prompt
mask = torch.tensor([[1] * 7 + [0, 0] + [1], [1] * 10])
def run(embeds, attention_mask):
with torch.no_grad():
return pipe.generate_language_model(embeds, attention_mask=attention_mask, max_new_tokens=8)[0]
ref = run(single, torch.ones(1, 8, dtype=torch.long))
print("main: max |batched - single| =", (run(batch, mask) - ref).abs().max().item())
# Same, with GPT-2 position ids derived from the mask (as transformers' generate() does).
native = mod.prepare_inputs_for_generation
def with_positions(inputs_embeds, attention_mask=None, past_key_values=None, **kwargs):
inputs = native(inputs_embeds, attention_mask, past_key_values, **kwargs)
position_ids = attention_mask.long().cumsum(-1) - 1
position_ids.masked_fill_(attention_mask == 0, 1)
inputs["position_ids"] = position_ids[:, -inputs["inputs_embeds"].shape[1]:]
return inputs
mod.prepare_inputs_for_generation = with_positions
print("with mask-derived position_ids: max |batched - single| =", (run(batch, mask) - ref).abs().max().item())Output:
main: max |batched - single| = 0.11253991723060608
with mask-derived position_ids: max |batched - single| = 1.1920928955078125e-07
Context
With cvssp/audioldm2 (c8e7e189), 10 s, 50 steps, guidance 3.5, float32, generating ["A dog barking.", "A dog barking as birds sing and a gentle wind blows through trees in a quiet garden."] together and the first prompt alone with the same generator, the first waveform differs by 116% relative RMS. With mask-derived position ids it differs by 0.003%. This checkpoint's language_model is a GPT2Model, so I needed the workaround from #12630 to run it on transformers 5.x; that workaround only adds the missing helper and does not affect the comparison.
Logs
System Info
diffusers main f982295, transformers 5.24.1, torch 2.13.0 (CPU repro); the pretrained run used torch 2.12.0+cu130 on an NVIDIA RTX PRO 6000.
Activity
avnps commentedon Dec 5, 2026
Hi! I'd like to take this issue. I'll derive
position_idsfromattention_maskinprepare_inputs_for_generation(pipeline_audioldm2.py), add CPU unit tests covering both batched padded prompts andattention_mask=None, and open a PR shortly.