MixtralFlashAttention2: put "plus 1" inside parentheses when calculating rotary_seq_len, allowing None position_ids input.
Author: xenshinu
Pull request: yes

MixtralFlashAttention2: put "plus 1" inside the parentheses when calculating rotary_seq_len, allowing None position_ids input.

# What does this PR do?
`MixtralFlashAttention2.forward()` treats `position_ids` as an optional input, however, it is used before checking if it is `None`. 
https://github.com/huggingface/transformers/blob/25245ec26dc29bcf6102e1b4ddd0dfd02e720cf5/src/transformers/models/mixtral/modeling_mixtral.py#L447
I also checked the definition of `apply_rotary_pos_emb`, since `cos` and `sin` are selected by `position_ids`, I suppose there is no need to create a rotary sequence of length `kv_seq_len+1`. By changing the code to 
```
        # Because the input can be padded, the absolute sequence length depends on the max position id.
        if position_ids is not None:
            # original code: rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()) + 1 # "+1" is outside
            rotary_seq_len = max(kv_seq_len, position_ids[:, -1].max().item()+1)
        else:
            rotary_seq_len = kv_seq_len
```
By default, the `rotary_seq_len` is `kv_seq_len`, and when `position_ids` is specified, `rotary_seq_len` will be the max position plus 1.    

Fixes #31326 


## 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?
- [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.
- [ ] 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.

<!-- 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: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization (bitsandbytes, autogpt): @SunMarc and @younesbelkada

Documentation: @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: See Models above and tag the person corresponding to the modality of the example.
- TensorFlow: @Rocketknight1

 -->


Comments:
Anyone take a look?

Hi @Luke20000429, apologies for the delay. cc @ArthurZucker as it relates to Mixtral

I see. Let me update the others then.

@ArthurZucker I've propagate the change to `startcoder2, phi2, mixtral and qwen2(_moe)`.

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