Repository: huggingface/transformers
Pull request: Added handling for length <2 of suppress_tokens for whisper

Added handling for <2 length of suppress_tokens for whisper

# What does this PR do?

<!--
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.
-->

<!-- Remove if not applicable -->

Encountered the following error when implementing a fine-tuned whisper model:
```
site-packages/transformers/models/whisper/generation_whisper.py", line 1783, in _prepare_decoder_input_ids
    prev_start_of_text = suppress_tokens[-2]
                         ~~~~~~~~~~~~~~~^^^^
IndexError: index -2 is out of bounds for dimension 0 with size 0
```
The current code assumes that if `suppress_tokens` is not None it has at least 2 elements. This throws `IndexError` when `suppress_tokens` is empty but not None (or length 1) as below:
```
prev_start_of_text = suppress_tokens[-2] if suppress_tokens is not None else None
```

I believe the initial code may have intended to check for empty list (`if suppress_tokens` would have worked) although this would still miss the case of length 1.

My change checks that the list is not None and has a length greater than or equal to 2 (needed to reference second last elem as per original code)

## 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#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.

My apologies if I am tagging the wrong individuals, using list from comments
Models:
- speech models: @ylacombe, @eustlb 
Library:
- generate: @gante 


Changed files:
filename: src/transformers/models/whisper/generation_whisper.py
patch: @@ -1779,7 +1779,10 @@ def _prepare_decoder_input_ids(
 
         prev_start_of_text = getattr(generation_config, "prev_sot_token_id", None)
         if prev_start_of_text is None:
-            prev_start_of_text = suppress_tokens[-2] if suppress_tokens is not None else None
+            if suppress_tokens is not None and len(suppress_tokens) >= 2:
+                prev_start_of_text = suppress_tokens[-2]
+            else:
+                prev_start_of_text = None
 
         if any(do_condition_on_prev_tokens) and len(current_segments[0]) > 0:
             # according to https://github.com/openai/whisper/blob/e58f28804528831904c3b6f2c0e473f346223433/whisper/decoding.py#L609