Does the support for the Mistral Model seem inconsistent or incomplete with it official?
Author: MrYxJ
Pull request: no

### Feature request

Hello , thank you very much for creating the library **transformers** which provides a very convenient way to use the different models. 

Recently, when I use Mistral Model by transformers (4.38.2) library to perform the model inference, the execution did not seem to be completely consistent with the official mistral papers and the [code](https://github.com/mistralai/mistral-src/tree/main/mistral) they provided.

Specifically, firstly the mistral have a  **Pre-fill and Chunking** mechanism when prompt input encoding(As shown in the following picture). Is this method included in the transformers library's implementation of mistral's generate function? 

![图片](https://github.com/huggingface/transformers/assets/21152077/2799bd96-77f7-4a8d-b0dd-35dd596db186)

Secondly,  Mistral use a **Rolling Buffer Cache** mechanism to enable key values to be updated in the cache as the sliding window moves. (As shown in the following picture). 
![图片](https://github.com/huggingface/transformers/assets/21152077/465e0b8e-c2bb-4f92-8a8c-489c50296e1f)
 
When I read this piece of the [transformers source code](https://github.com/huggingface/transformers/blob/main/src/transformers/models/mistral/modeling_mistral.py), this mechanism is implemented in the transformer library using the **DynamicCache** update method, and this operation is actually to concatenate all the past keys and values with the current keys and values.  https://github.com/huggingface/transformers/blob/b338a6c3b8eda29610d4d472cad8cd87cbfdaaed/src/transformers/cache_utils.py#L126C1-L132C105

Am I mistaken in the logic of this program or is the implementation not fully consistent with the mistral paper?  In addition, I read the official source code of mistral and found that they do release two kinds of model inference code, one of which is [simple version](https://github.com/mistralai/mistral-src/blob/main/one_file_ref.py). Is this your current implementation?

### Motivation

I want to know how to use transformers library on the mistral model to achieve the same way as in mistral paper. 
Whether I read the code wrong or do I need to make some additional changes based on transformer?

### Your contribution

I can try fix this piece code to achieve the same implementation as in the mistral paper based on transformers.

Comments:
Possibly related: #29519

cc @ArthurZucker @gante 

Hi @MrYxJ 👋 

The sliding window logic from the pre-fill and chunking mechanism is implemented differently depending on the attention layer:
- eager (i.e. vanilla) attention: the attention mask with sliding window is created [here](https://github.com/huggingface/transformers/blob/697f05bab39ce067a7984ed98da2743898352e47/src/transformers/models/mistral/modeling_mistral.py#L1012)
- flash attention 2: the sliding window is applied in the forward pass of the attention layer itself. 
- SDPA: it is not enabled on `main`, [this PR](https://github.com/huggingface/transformers/pull/29407) enables it

Regarding the rolling buffer cache mechanism, we can see that it is not needed for the correctness of the model -- the sliding window discards the same older values, i.e. the two mechanisms are redundant. The rolling buffer cache is, however, extremely beneficial for memory purposes. We are working on it (for all models), although currently only Llama and Gemma support it -- search for "static cache".

(@ArthurZucker please correct if what I wrote above is not correct :) )

This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.

Please note that issues that do not follow the [contributing guidelines](https://github.com/huggingface/transformers/blob/main/CONTRIBUTING.md) are likely to be ignored.

Hi @gante,  thank you very much for your detailed answer. By your answer, I have already confirmed it clear. Thank you very much. 

But still StaticCache is not implemented in Mistral yet, right?

#30642 is coming 😉