🔴 Fix EnCodec internals and integration tests
Author: ebezzam
Pull request: yes
Labels: name: Audio

# What does this PR do?

Fixes [integration tests](https://github.com/huggingface/transformers/actions/runs/16282903241/job/45976159004) for EnCodec, which were all failing:
- Two because of mismatch with expected output
- One doesn't even compute outputs because of bad padding
```
FAILED tests/models/encodec/test_modeling_encodec.py::EncodecIntegrationTest::test_batch_48kHz - NotImplementedError: Only 2D, 3D, 4D, 5D padding with non-constant padding are supported for now
```

cc @eustlb 

---

# Following changes were necessary:

- Use chunking from [original model](https://github.com/facebookresearch/encodec/blob/0e2d0aed29362c8e8f52494baf3e6f99056b214f/encodec/model.py#L142). This seems to resolve an [issue](https://github.com/huggingface/transformers/pull/23655#issuecomment-1585724859) in the model addition PR of padding in the 48khz model, which doesn't seem like it was resolved. This also required adding a new output `last_frame_pad_length`. 
- [This PR](https://github.com/huggingface/transformers/pull/34756) introduced an error by adding a dimension to padding mask. This would cause an issue when calling the `forward` method, as `padding_mask` would be [initialized](https://github.com/huggingface/transformers/blob/53c9dcd6fd31cb9e8a10248693a905d0223b8316/src/transformers/models/encodec/modeling_encodec.py#L761) with the correct shape.

Scripts for new expected outputs can be found here: https://gist.github.com/ebezzam/2a34e249e729881130d1f5a42229d31f

---

# 🔴 Breaking change (minimal)

Previous code will break if people use `return_dict=False` when calling [encode](https://github.com/huggingface/transformers/blob/4b4f04fccaaa3020c5462cf31d286d83fbfc6d38/src/transformers/models/encodec/modeling_encodec.py#L527) and they expect just TWO outputs, as now there are THREE.
```
# old (which would break with current change)
audio_codes, audio_scales = model.encode(
    input_values, padding_mask, bandwidth, return_dict=False
)
# new
audio_codes, audio_scales, last_frame_pad_length = model.encode(
    input_values, padding_mask, bandwidth, return_dict=False
)

# works for both, and there would be new entry "last_frame_pad_length" in output dict
encoded_frames = model.encode(
    input_values, padding_mask, bandwidth, return_dict=True
)
```
If users don't (or forget) to pass the new output `last_frame_pad_length` to [decode](https://github.com/ebezzam/transformers/blob/15d44de835d8024472e34713250fc5a403a4d220/src/transformers/models/encodec/modeling_encodec.py#L662), it won't break but may lead to slightly different outputs, but only at the end of the audio.

Comments:
The docs for this PR live [here](https://moon-ci-docs.huggingface.co/docs/transformers/pr_39431). All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

@vasqu thanks for the helpful comments!

one thing I haven't addressed on output tuples, see [here](https://github.com/huggingface/transformers/pull/39431#discussion_r2209409953)

run-slow: encodec

run-slow: encodec

This comment contains run-slow, running the specified jobs: 

models: ['models/encodec']
quantizations: [] ...

run-slow: encodec

This comment contains run-slow, running the specified jobs: 

models: ['models/encodec']
quantizations: [] ...

run-slow: encodec

This comment contains run-slow, running the specified jobs: 

models: ['models/encodec']
quantizations: [] ...

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: encodec