Repository: huggingface/transformers
Pull request: [VITS] Add `speaking_rate` as an optionl forward argument

# What does this PR do?

Currently, to adjust the speed of generated audio with VitsModel, I have to modify the `.speaking_rate` attribute

```python
from transformers import VitsModel, AutoTokenizer
import torch
from IPython.display import Audio

model_id = "facebook/mms-tts-eng"
model = VitsModel.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)

text = "some example text in the English language"
inputs = tokenizer(text, return_tensors="pt")

with torch.no_grad():
    model.speaking_rate = 2.0
    output = model(**inputs).waveform

Audio(output.numpy(), rate=model.config.sampling_rate)
```

To make it more convenient, I added `speaking_rate` as a forward argument, so speaking rate can be adjusted per-forward basis.

```python
with torch.no_grad():
    output = model(**inputs, speaking_rate=0.5).waveform
```

The default behavior is preserved - if speaking_rate is not specified, it fallbacks to `self.speaking_rate`

## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [ ] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#create-a-pull-request),
      Pull Request section?
- [ ] Was this discussed/approved via a Github issue or the [forum](https://discuss.huggingface.co/)? Please add a link
      to it if that's the case.
- [ ] Did you make sure to update the documentation with your changes? Here are the
      [documentation guidelines](https://github.com/huggingface/transformers/tree/main/docs), and
      [here are tips on formatting docstrings](https://github.com/huggingface/transformers/tree/main/docs#writing-source-documentation).
- [ ] Did you write any new necessary tests?


## Who can review?

Anyone in the community is free to review the PR once the tests have passed. Feel free to tag
members/contributors who may be interested in your PR.

@eustlb

<!-- Your PR will be replied to more quickly if you can figure out the right person to tag with @

 If you know how to use git blame, that is the easiest way, otherwise, here is a rough guide of **who to tag**.
 Please tag fewer than 3 people.

Models:

- text models: @ArthurZucker @Cyrilvallez
- vision models: @yonigozlan @molbap
- audio models: @eustlb @ebezzam @vasqu
- multimodal models: @zucchini-nlp
- graph models: @clefourrier

Library:

- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- continuous batching: @remi-or @ArthurZucker @McPatate
- pipelines: @Rocketknight1
- tokenizers: @ArthurZucker and @itazap
- trainer: @SunMarc
- attention: @vasqu @ArthurZucker @CyrilVallez
- model loading (from pretrained, etc): @CyrilVallez
- distributed: @3outeille @ArthurZucker
- CIs: @ydshieh

Integrations:

- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization: @SunMarc @MekkCyber
- kernels: @MekkCyber @drbh
- peft: @BenjaminBossan @githubnemo

Devices/Backends:

- AMD ROCm: @ivarflakstad
- Intel XPU: @IlyasMoutawwakil
- Ascend NPU: @ivarflakstad 

Documentation: @stevhliu

Research projects are not maintained and should be taken as is.

 -->


Changed files:
filename: src/transformers/models/vits/modeling_vits.py
patch: @@ -1277,6 +1277,7 @@ def forward(
         output_hidden_states: bool | None = None,
         return_dict: bool | None = None,
         labels: torch.FloatTensor | None = None,
+        speaking_rate: float | None = None,
         **kwargs,
     ) -> tuple[Any] | VitsModelOutput:
         r"""
@@ -1285,6 +1286,8 @@ def forward(
         labels (`torch.FloatTensor` of shape `(batch_size, config.spectrogram_bins, sequence_length)`, *optional*):
             Float values of target spectrogram. Timesteps set to `-100.0` are ignored (masked) for the loss
             computation.
+        speaking_rate (`float`, *optional*):
+            Speaking rate.
 
         Example:
 
@@ -1354,7 +1357,9 @@ def forward(
         else:
             log_duration = self.duration_predictor(hidden_states, input_padding_mask, speaker_embeddings)
 
-        length_scale = 1.0 / self.speaking_rate
+        if speaking_rate is None:
+            speaking_rate = self.speaking_rate
+        length_scale = 1.0 / speaking_rate
         duration = torch.ceil(torch.exp(log_duration) * input_padding_mask * length_scale)
         predicted_lengths = torch.clamp_min(torch.sum(duration, [1, 2]), 1).long()