[MusicgenMelody] Fix conditioning silently dropped at generation step 0 (#48679)
* [MusicgenMelody] Fix conditioning silently dropped at generation step 0
PR #43679 (`_prepare_cache_for_generation`) pre-creates a DynamicCache
(or EncoderDecoderCache) before the first generation step, so
`past_key_values` is no longer None at step 0. The guard
if past_key_values is not None:
encoder_hidden_states = None # drop conditioning
in `MusicgenMelodyForConditionalGeneration.prepare_inputs_for_generation`
then silently drops all conditioning at step 0, making every generation
call effectively unconditioned regardless of the audio/text inputs.
Fix: only suppress `encoder_hidden_states` once the self-attention cache
actually contains tokens (`past_length > 0`). An empty pre-created cache
at step 0 must pass conditioning through.
Also apply the symmetric fix to `MusicgenMelodyForCausalLM` and
`MusicgenForCausalLM` (`input_ids` trimming guard), and update the two
stale sampling test golden values that reflected the broken unconditioned
outputs.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Revert unnecessary tolerance widening in sampling tests
The two sampling tests use set_seed so they are deterministic on a given
runner; the captured golden values should match within the original
1e-4 / 2e-4 tolerances. The looser 1e-3 was overly permissive and
could mask real future regressions.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* [MusicgenMelody] Update all golden values to current CI environment (torch 2.11)
Update expected values for all 10 integration tests to match outputs from
the fixed model on the current runner (torch 2.11 / post-April-5 environment).
The 8 deterministic tests already passed with the code fix alone (max_abs_diff
well within atol), but are updated here for consistency. The 2 sampling tests
(unconditional_sampling, text_prompt_sampling) were failing since 2026-04-05
due to the unrelated torch 2.10 -> 2.11 numerical drift and required new values.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* Revert modeling_musicgen.py change; fix stereo right golden value to match CI runner
- Revert the defensive fix to MusicgenForCausalLM (base Musicgen is unaffected
by the conditioning bug; melody does not use that model)
- Revert EXPECTED_VALUES_RIGHT_SECOND_SAMPLE to original values — our test
runner produced slightly different outputs than the CI runner for this slice
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
* up-to-date code -> local slow CI run green
---------
Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
Co-authored-by: raushan <raushan@huggingface.co>