Skip to content

[AudioLDM2] Derive GPT-2 position_ids from attention_mask during generation - #15500

Open
avnps wants to merge 1 commit into
huggingface:mainfrom
avnps:fix/audioldm2-position-ids
Open

avnps wants to merge 1 commit into
huggingface:mainfrom
avnps:fix/audioldm2-position-ids

Conversation

@avnps

@avnps avnps commented Dec 5, 2026

Copy link
Copy Markdown

Fixes #15497

What does this PR do?

In AudioLDM2Pipeline.generate_language_model, GPT-2 runs a custom generation loop using inputs_embeds and attention_mask via prepare_inputs_for_generation(), which previously omitted position_ids. Because AudioLDM2ProjectionModel concatenates [sos, clap, eos, sos_1, t5 tokens, eos_1] where T5 tokens are padded in the middle when batching prompts of different lengths, omitting position_ids shifted eos_1 and all generated tokens by the padding length for shorter prompts in the batch.

This PR updates prepare_inputs_for_generation() in src/diffusers/pipelines/audioldm2/pipeline_audioldm2.py to derive position_ids from attention_mask when present (and position_ids is not already supplied in kwargs), slicing to [:, -inputs_embeds.shape[1] :] on cached decoding steps (past_key_values is not None).

Changes

  • src/diffusers/pipelines/audioldm2/pipeline_audioldm2.py: compute mask-derived position_ids in prepare_inputs_for_generation().
  • tests/pipelines/audioldm2/test_audioldm2.py: add test_prepare_inputs_for_generation_position_ids and test_generate_language_model_batched_matches_single to TestAudioLDM2Pipeline covering middle-padded batches, cached decoding slices, attention_mask=None, and explicit position_ids.

Verification

  • pytest tests/pipelines/audioldm2/test_audioldm2.py -k "test_prepare_inputs_for_generation_position_ids or test_generate_language_model_batched_matches_single" -v: 2 passed (45 deselected) on CPU (torch 2.15.0+cpu, transformers 5.23.0).
  • ruff check src/diffusers/pipelines/audioldm2/pipeline_audioldm2.py tests/pipelines/audioldm2/test_audioldm2.py: passed.
  • ruff format --check src/diffusers/pipelines/audioldm2/pipeline_audioldm2.py tests/pipelines/audioldm2/test_audioldm2.py: passed.

cc @sayakpaul

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AudioLDM2: GPT-2 generation ignores the attention mask for position ids, so a prompt's audio depends on the other prompts in the batch

1 participant