Support for caching prompt hidden states through multiple calls of `generate()`
### Feature request

Hi there,

I'd like to be able to re-use the hidden states for a common (potentially long) prompt across multiple calls to `model.generate()` in order to reduce redundant computation. Here is how I envision a final API, though I'm sure there are multiple ways to do it.

```python
# Load stuff
model = AutoModel.from_pretrained('huggyllama/llama-7b')
tokenizer = AutoTokenizer.from_pretrained('huggyllama/llama-7b')

# Common prompt that we'd prepend to every example
prompt = "This is a common prompt in every example."
prompt_ids = tokenizer(prompt, return_tensors='pt')

# Examples to pass to generate
examples = ["Ackbar went to", "Billaba enjoys", "Cody is eating some"]

# Generation loop
outputs = []
prompt_hidden_state = None
for ex in examples:
    # Current way of doing things
    out = model.generate(
        **tokenizer(prompt + ex, return_tensors='pt'),   
    )

    # Proposed method to re-use prompt_hidden_state
    out = model.generate(
        **tokenizer(x, return_tensors='pt'),   
        common_prompt_ids=prompt_ids,
        prompt_hidden_state=prompt_hidden_state
    )
    prompt_hidden_state = out.prompt_hidden_state
    outputs.append(out.sequences)
```

Thanks in advance. 

### Motivation

A very common pattern for LLM usage is having a common prompt (e.g., instructions and input/output pairs), a sample input, and asking it to generate the sample output. For example:

```
You are a programmer's assistant which converts English descriptions to Python functions.

English: <example 1 description>
Python: <example 1 function>

English: <example 2 description>
Python: <example 2 function>

English: <example 3 description>
Python: <example 3 function>

English: <input description>
Python: 
```

I'd like to be able to cache the common part of the prompt across inputs, that is, everything before `<input description>` which appears in every example to avoid potentially expensive re-computation.

### Your contribution

The only existing info I could find is the short discussion [here](https://discuss.huggingface.co/t/avoid-recalculating-hidden-states-between-generate-calls/34209). I tried messing around a bit to get this to work but had little luck. I'm not familiar with the inner-workings of `transformers` and ran into numerous errors. One problem is padding, which if we're using left padding, can cause some misalignment with the prompt hidden states, e.g.:
```
<p> <p> <p> common prompt x_1 x_2 x_3
<p> <p> common prompt x_1 x_2 x_3 x_4
<p> <p> <p> <p> common prompt x_1 x_2
```
I don't know the best way to solve this. Do we dynamically pad every tensor in `past_key_values`? That seems slow but I don't know if it actually is.

If someone can suggest a better/easier way or maybe give some more pointers on how to solve padding. I'd be happy to try again myself.

Thanks in advance. 
cc @gante <|||||>Hey @offendo 👋 

This is a relevant request, and one that I can't give an exact solution at the moment. In general, `generate()` + prompting is very manual at the moment, and we really want to improve it. As such, the solution to your proposal will depend on our next iteration of prompt-handling!

I'm assigning the issue to me, and I'll keep you posted 🤗 

(cc @patrickvonplaten  -- this is related to our brainstorming session yesterday, about prompting)<|||||>This feature would be useful not only for long prompts, but also for incrementally building dialogs / conversations without recomputing the generated parts. And I think there's a simple solution: just expose `past_key_values` in the various `generate` functions, and allow `past_key_values` to be passed in as the parameter for all decoder-only models.<|||||>@gante can't we already pass `past_key_values` to `generate` through `model_kwargs` ? 
So we could pre-encode the prompt into `past_key_values` with a single forward pass and then re-use it in generation already I think. 

See this similar (old) issue maybe: https://github.com/huggingface/transformers/issues/4368 should help<|||||>@patrickvonplaten Yeah I guess this solves the issue for @offendo , but still `past_key_values` are not being returned with the generated sequences. Hopefully there could be a flag (possibly called `return_past_key_values`) that allows them to be returned.<|||||>@lqf96 absolutely. I'll work on adding that, since I've seen others mentioning its usefulness :)<|||||>Looks like this is being addressed by #25086... Hopefully it can be accepted soon!<|||||>你好，我已经接收到你的邮件！<|||||>> @gante can't we already pass `past_key_values` to `generate` through `model_kwargs` ? So we could pre-encode the prompt into `past_key_values` with a single forward pass and then re-use it in generation already I think.
> 
> See this similar (old) issue maybe: #4368 should help

Does this approach work even with the padding issue? If we encode the common prompt (which has no padding) and then call generate on a batch of values with left padding, the hidden states won’t align properly and we’d have to do some dynamic padding of the `past_key_values` or something.

Please correct me if I am misunderstanding something! 

And also, thanks to everyone for their work on this!<|||||>@offendo If the inputs are properly passed, then the position ids can be correctly inferred, which will result in the correct output :) Possibly we may need additional logic to ensure this position id inference can happen seamlessly