Cleaner Cache `dtype` and `device` extraction for CUDA graph generation for quantizers compatibility
Author: BlackSamorez
Pull request: yes

# 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 -->

As of now, this PR fixes a small problem preventing one from using CUDA graph generation from #28937 with quantized models.

In the long run, It would be great to have compiled generation actually working for GPTQ/AQLM/other quantization methods.


## 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?
- [ ] 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: @gante
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @pacman100

Integrations:

- deepspeed: HF Trainer/Accelerate: @pacman100
- 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:
cc @younesbelkada 

@younesbelkada The error I get on main is quite simple: 
```
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[8], [line 22](vscode-notebook-cell:?execution_count=8&line=22)
     [19](vscode-notebook-cell:?execution_count=8&line=19)     return new_token
     [21](vscode-notebook-cell:?execution_count=8&line=21) with torch.no_grad():
---> [22](vscode-notebook-cell:?execution_count=8&line=22)     model._setup_cache(StaticCache, BS, max_cache_len=max_cache_length)
     [24](vscode-notebook-cell:?execution_count=8&line=24)     ### PREFILL
     [25](vscode-notebook-cell:?execution_count=8&line=25)     # input_pos = torch.arange(seq_length, device=device)
     [26](vscode-notebook-cell:?execution_count=8&line=26)     cache_position = torch.arange(seq_length , device=device)

File [~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:817](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:817), in LlamaPreTrainedModel._setup_cache(self, cache_cls, max_batch_size, max_cache_len)
    [814](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:814)     self.register_buffer("causal_mask", torch.triu(causal_mask, diagonal=1), persistent=False)
    [816](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:816) for layer in self.model.layers:
--> [817](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:817)     weights = layer.self_attn.o_proj.weight
    [818](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:818)     layer.self_attn.past_key_value = cache_cls(
    [819](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:819)         self.config, max_batch_size, max_cache_len, device=weights.device, dtype=weights.dtype
    [820](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/transformers/models/llama/modeling_llama.py:820)     )

File [~/AQLM/.conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1706](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1706), in Module.__getattr__(self, name)
   [1704](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1704)     if name in modules:
   [1705](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1705)         return modules[name]
-> [1706](https://vscode-remote+ssh-002dremote-002bdas8gpu4.vscode-resource.vscode-cdn.net/nfs/scistore14/alistgrp/apanfero/AQLM/~/AQLM/.conda/lib/python3.10/site-packages/torch/nn/modules/module.py:1706) raise AttributeError(f"'{type(self).__name__}' object has no attribute '{name}'")

AttributeError: 'QuantizedLinear' object has no attribute 'weight'
```

That's why, I believe, it would make sense to source `device` and `dtype` from outside the linear layer to be quantization-agnostic.

And I don't really understand what you're proposing with the code you referenced.

@BlackSamorez thanks! 
Sorry I sent the wrong link, it should be: https://github.com/huggingface/transformers/blob/2a9b1f80c45cab19b542bc7cc004937d39d6f6fb/src/transformers/models/llama/modeling_llama.py#L473 
you can do something like:
```python
if hasattr(self.config, "_pre_quantization_dtype")
    target_dtype = self.config._pre_quantization_dtype
else:
    target_dtype = layer.self_attn.o_proj.weight
```
Does that fixes the issue?

What you proposed seems to work fine with both FP16 and AQLM with a notebook test based off @ArthurZucker's [test script](https://gist.github.com/ArthurZucker/ae0a86ef8f841c0ef69aaa52ccbc0b03).
With what other models shall I try and run it?

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

@younesbelkada CUDA graph generation diverges at some point:
Prefix: `Hello my name is`
Normal: `Hello my name is Katie. I am a 20 year old college student. I am a very outgoing person. I love to have fun and be active. I am very easy going and love to make`
CUDA graph: `Hello my name is Katie. I am a 20 year old college student. I am a very outgoing person. I love to have fun and be active. I am a very hard worker and I`

A stupid solution would be to generate shorter texts but I'm not sure if it's a good idea to have unstable tests.

P.S. As you might have guessed, I added a CUDA graph generation test for AQLM.