[Whisper] Fix word-level timestamps for audio < 30 seconds
Author: xenova
Pull request: yes

# What does this PR do?

In OpenAI's [original implementation for word-level timestamps](https://github.com/openai/whisper/blob/e8622f9afc4eba139bf796c210f5c01081000472/whisper/timing.py#L206), they crop the cross attentions before perform dynamic time warping (to only run the algorithm on valid audio; this prevents getting stuck when backtracking). The current transformers implementation misses this, so this PR fixes that.

<!--
Congratulations! You've made it this far! You're not quite done yet though.

Once merged, your PR is going to appear in the release notes with the title you set, so make sure it's a great title that fully reflects the extent of your awesome contribution.

Then, please replace this with a description of the change and which issue is fixed (if applicable). Please also include relevant motivation and context. List any dependencies (if any) that are required for this change.

Once you're done, someone will review your PR shortly (see the section "Who can review?" below to tag some potential reviewers). They may suggest changes to make the code even better. If no one reviewed your PR after a week has passed, don't hesitate to post a new comment @-mentioning the same persons---sometimes notifications get lost.
-->

Testing code:
```python
from transformers import pipeline
pipe = pipeline("automatic-speech-recognition", "openai/whisper-base")
url = 'https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/japanese-audio.wav'
output = pipe(url, return_timestamps="word", chunk_length_s=30, generate_kwargs={'language': 'japanese'})
print(output)
```

Fixed:
```
{
'text': '森長の美味しい牛乳は濃い青いように牛乳ビーンを足らった絶対にのパック牛乳である',
'chunks': [
  {'text': '森', 'timestamp': (0.18, 0.64)},
  {'text': '長', 'timestamp': (0.64, 0.82)},
  {'text': 'の', 'timestamp': (0.82, 1.04)},
  {'text': '美味', 'timestamp': (1.04, 1.2)},
  {'text': 'しい', 'timestamp': (1.2, 1.46)},
  {'text': '牛', 'timestamp': (1.46, 1.68)},
  {'text': '乳', 'timestamp': (1.68, 1.92)},
  {'text': 'は', 'timestamp': (1.92, 2.14)},
  {'text': '濃', 'timestamp': (2.14, 2.32)},
  {'text': 'い', 'timestamp': (2.32, 2.44)},
  {'text': '青', 'timestamp': (2.44, 2.64)},
  {'text': 'い', 'timestamp': (2.64, 2.76)},
  {'text': 'ように', 'timestamp': (2.76, 2.92)},
  {'text': '牛', 'timestamp': (2.92, 3.16)},
  {'text': '乳', 'timestamp': (3.16, 3.36)},
  {'text': 'ビ', 'timestamp': (3.36, 3.58)},
  {'text': 'ーン', 'timestamp': (3.58, 3.66)},
  {'text': 'を', 'timestamp': (3.66, 3.82)},
  {'text': '足', 'timestamp': (3.82, 4.0)},
  {'text': 'ら', 'timestamp': (4.0, 4.12)},
  {'text': 'った', 'timestamp': (4.12, 4.3)},
  {'text': '絶', 'timestamp': (4.3, 4.52)},
  {'text': '対', 'timestamp': (4.52, 4.68)},
  {'text': 'に', 'timestamp': (4.68, 4.78)},
  {'text': 'の', 'timestamp': (4.78, 4.94)},
  {'text': 'パ', 'timestamp': (4.94, 5.1)},
  {'text': 'ック', 'timestamp': (5.1, 5.2)},
  {'text': '牛', 'timestamp': (5.2, 5.44)},
  {'text': '乳', 'timestamp': (5.44, 5.64)},
  {'text': 'で', 'timestamp': (5.64, 5.84)},
  {'text': 'ある', 'timestamp': (5.84, 6.04)}
]
}
```

Previous (broken):
```
{
'text': '森長の美味しい牛乳は濃い青いように牛乳ビーンを足らった絶対にのパック牛乳である',
'chunks': [
  {'text': '森', 'timestamp': (29.98, 29.98)},
  {'text': '長', 'timestamp': (29.98, 29.98)},
  {'text': 'の', 'timestamp': (29.98, 29.98)},
  {'text': '美味', 'timestamp': (29.98, 29.98)},
  {'text': 'しい', 'timestamp': (29.98, 29.98)},
  {'text': '牛', 'timestamp': (29.98, 29.98)},
  {'text': '乳', 'timestamp': (29.98, 29.98)}, 
  'text': 'は', 'timestamp': (29.98, 29.98)},
  {'text': '濃', 'timestamp': (29.98, 29.98)},
  {'text': 'い', 'timestamp': (29.98, 29.98)},
  {'text': '青', 'timestamp': (29.98, 29.98)},
  {'text': 'い', 'timestamp': (29.98, 29.98)},
  {'text': 'ように', 'timestamp': (29.98, 29.98)},
  {'text': '牛', 'timestamp': (29.98, 29.98)},
  {'text': '乳', 'timestamp': (29.98, 29.98)},
  {'text': 'ビ', 'timestamp': (29.98, 29.98)},
  {'text': 'ーン', 'timestamp': (29.98, 29.98)},
  {'text': 'を', 'timestamp': (29.98, 29.98)},
  {'text': '足', 'timestamp': (29.98, 29.98)},
  {'text': 'ら', 'timestamp': (29.98, 29.98)},
  {'text': 'った', 'timestamp': (29.98, 29.98)},
  {'text': '絶', 'timestamp': (29.98, 29.98)},
  {'text': '対', 'timestamp': (29.98, 29.98)},
  {'text': 'に', 'timestamp': (29.98, 29.98)},
  {'text': 'の', 'timestamp': (29.98, 29.98)},
  {'text': 'パ', 'timestamp': (29.98, 29.98)},
  {'text': 'ック', 'timestamp': (29.98, 29.98)},
  {'text': '牛', 'timestamp': (29.98, 29.98)},
  {'text': '乳', 'timestamp': (29.98, 29.98)},
  {'text': 'で', 'timestamp': (29.98, 29.98)},
  {'text': 'ある', 'timestamp': (29.98, 29.98)}
]
}
```

<!-- Remove if not applicable -->

Fixes #25605  (issue)


## Before submitting
- [ ] This PR fixes a typo or improves the docs (you can dismiss the other checks if that's the case).
- [x] Did you read the [contributor guideline](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md#start-contributing-pull-requests),
      Pull Request section?
- [x] 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.
- [x] 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).
- [x] 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.

<!-- 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 and @younesbelkada
- vision models: @amyeroberts
- speech models: @sanchit-gandhi
- graph models: @clefourrier

Library:

- flax: @sanchit-gandhi
- generate: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @sgugger

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- ray/raytune: @richardliaw, @amogkam

Documentation: @sgugger, @stevhliu and @MKhalusova

HF projects:

- accelerate: [different repo](https://github.com/huggingface/accelerate)
- datasets: [different repo](https://github.com/huggingface/datasets)
- diffusers: [different repo](https://github.com/huggingface/diffusers)
- rust tokenizers: [different repo](https://github.com/huggingface/tokenizers)

Maintained examples (not research project or legacy):

- Flax: @sanchit-gandhi
- PyTorch: @sgugger
- TensorFlow: @Rocketknight1

 -->
@sanchit-gandhi @ArthurZucker 

Comments:
_The documentation is not available anymore as the PR was closed or merged._

Looks like there were some failing unit tests, but they were actually wrong 😅 

### Original unit test:
```python
{
    "text": " Conquered returned to its place amidst the tents.",
    "chunks": [
        {"text": " Conquered", "timestamp": (29.78, 29.9)},
        {"text": " returned", "timestamp": (29.9, 29.9)},
        {"text": " to", "timestamp": (29.9, 29.9)},
        {"text": " its", "timestamp": (29.9, 29.9)},
        {"text": " place", "timestamp": (29.9, 29.9)},
        {"text": " amidst", "timestamp": (29.9, 29.9)},
        {"text": " the", "timestamp": (29.9, 29.9)},
        {"text": " tents.", "timestamp": (29.9, 29.9)}
    ]
}
```

### New (fixed) unit test:
```python
{
    "text": " Conquered returned to its place amidst the tents.",
    "chunks": [
        {"text": " Conquered", "timestamp": (0.5, 1.2)},
        {"text": " returned", "timestamp": (1.2, 1.64)},
        {"text": " to", "timestamp": (1.64, 1.84)},
        {"text": " its", "timestamp": (1.84, 2.02)},
        {"text": " place", "timestamp": (2.02, 2.28)},
        {"text": " amidst", "timestamp": (2.28, 2.78)},
        {"text": " the", "timestamp": (2.78, 2.96)},
        {"text": " tents.", "timestamp": (2.96, 3.48)},
    ],
},
```


Gently pinging @ArthurZucker for the final 👍 before merge - thank you again for the PR @xenova!

Sorry for the miss reviewing now! 

Thank you for your contribution @xenova!
Is this PR approved and merged in the latest version? I just installed Transformers and I am still getting the old results:
{'text': ' Okay, you ready?', 'chunks': [{'text': ' Okay,', 'timestamp': (29.98, 29.98)}, {'text': ' you', 'timestamp': (29.98, 29.98)}, {'text': ' ready?', 'timestamp': (29.98, 29.98)}]}

Thanks!

Hi there - It's not yet merged, but will hopefully be soon!

Do you happen to know when the next version will be out?

Hi @xenova,

I just upgraded to the latest version of Transformers (4.33.1) and tried the following. It seems that the word timestamps are still incorrect.
What am I doing wrong?

Thanks!

###########
```
pipe = pipeline(
  "automatic-speech-recognition",
  model="openai/whisper-large",
  chunk_length_s=4,
  device=device,
)
vx0 = pipe(x0, batch_size=4, return_timestamps="word")
```

vx0['chunks']:
{'text': ' Okay,', 'timestamp': (29.98, 29.98)},
 {'text': ' you', 'timestamp': (29.98, 29.98)},
 {'text': ' ready?', 'timestamp': (29.98, 29.98)},
 {'text': ' So', 'timestamp': (32.65, 32.65)},
 {'text': ' now', 'timestamp': (32.65, 32.65)},
 {'text': ' it', 'timestamp': (35.31, 35.31)},
 {'text': ' is', 'timestamp': (35.31, 35.31)},
 {'text': " it's", 'timestamp': (35.31, 35.31)},
 {'text': ' a', 'timestamp': (35.31, 35.31)},

This is because a full release hasn't come out yet. To fix it, you can install from source (see [docs](https://huggingface.co/docs/transformers/installation#install-from-source)):
```
pip install --upgrade git+https://github.com/huggingface/transformers
```

Hi guys, I just tried whisper v3 and find that your updated code is gone in the current main branch.
And it gives me 29.98... There are multiple commits after this merge, can someone check what is going on?