[LLaVa] Add past_key_values to _skip_keys_device_placement to fix multi-GPU dispatch
Author: aismlv
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.
-->


Fixes #27917

Fixes cache and (key, value) tensors ending up on different devices when using accelerate's dispatch in `LlavaPreTrainedModel` (and `VipLlavaPreTrainedModel`) by adding `_skip_keys_device_placement = "past_key_values"` attribute to the class, similar to how Llama handles the issue

```
  File "/home/pooyam/hf_llava/lib/python3.9/site-packages/transformers/cache_utils.py", line 127, in update
    self.key_cache[layer_idx] = torch.cat([self.key_cache[layer_idx], key_states], dim=-2)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:0 and cuda:1! (when checking argument for argument tensors in method wrapper_CUDA_cat)
```


## 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#start-contributing-pull-requests),
      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.
- [x] 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).
- [x] 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.

@ArthurZucker @younesbelkada 

<!-- 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:
Per my understanding this would only be applied for models that went through the recent cache refactoring. Llava is impacted since it uses Llama and Mistral under the hood, which are part of this PR: https://github.com/huggingface/transformers/pull/26681 
The recent Mixtral has it: https://github.com/huggingface/transformers/blob/main/src/transformers/models/mixtral/modeling_mixtral.py#L863 so I would say to just keep this in mind for future models that copy from Llama/Mistral and/or uses Llama or Mistral under the hood such as Llava

@younesbelkada Great - thanks for explaining and providing the relevant links. We're good to merge then! 

@aismlv I'm hitting a similar error with Llava 1.6 + Mistral 7B. What was the fix previously and what changes (if any) need to be made for Mistral 7B?

```
    return torch.layer_norm(input, normalized_shape, weight, bias, eps, torch.backends.cudnn.enabled)
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! (when checking argument for argument weight in method wrapper_CUDA__native_layer_norm)
```

The problem arises due to:

`<bound method AlignDevicesHook.pre_forward of AlignDevicesHook(execution_device=1, offload=False, io_same_device=False, offload_buffers=False, place_submodules=True, skip_keys=['past_key_values', 'causal_mask'])>`

which moves the the input tensors from the correct device (in my setting, 0) to the wrong device (in my setting, 1).

If you want some help you need to provide a reproducer 😉 If you still have the issue with the latest `transformers` then open a new issue 🤗 

@ArthurZucker here is the reproducer. Please let me know if I'm misusing `accelerate` and if so how. Otherwise, the error is:

`RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! (when checking argument for argument weight in method wrapper_CUDA__native_layer_norm)`

https://gist.github.com/RylanSchaeffer/8198233a445ab3373aff01b038cc4b8b

@ArthurZucker I provided the reproducer. Please let me know if you can help solve this problem :)

Sorry was off for a week. Could you try to isolate the bug / have a smaller reproducer? 
`AlignDevicesHook` is related to accelerate, not `transformers` directly in this case. Are you using `device_map="auto"` 

@ArthurZucker 
Still getting the error.
RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cuda:1 and cuda:0! (when checking argument for argument weight in method wrapper_CUDA__native_layer_norm)

I am trying with the fine-tuned model("yezg/llava-v1.6-mistral-7b-test")

Could you share a small reproducer so we can work on this?