[Flash Attention 2] Add flash attention 2 for GPT-J
Author: bytebarde
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 -->

Adds Flash Attention 2 for `GPT-J` 
Fixes #[26350](https://github.com/huggingface/transformers/issues/26350)


## 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#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.
- [ ] 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.

cc: @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:
Current progress with running `flash_attn_test`. Will dive deeper to fix the error.



![2023-12-31 8 43 06](https://github.com/huggingface/transformers/assets/154845754/8a6feba0-f858-4bd9-938a-50fac499cc36)



Hi @bytebarde, what is the error message? 
If it is something like - "IndexError: tensors used as ...", then updating CUDA could solve the error (At least it was for my case in OPT).
 
BTW run `make fixup` to make the CI green!

Hi @susnato, thank you so much for for your attention to this PR!

I believe the error originates from two factors: (1) my preliminary implementation of `GPTJFlashAttention2`, which aimed to eliminate "redundant" transposing of the key and query, and (2) the execution of `test_flash_attn_2_generate_padding_right` using the testing configuration.

To address these issues, I have reinstated the original transposing operations and reverted the QKV cache concatenation. Additionally, I overwrote `test_flash_attn_2_generate_padding_right` by using the actual checkpoint and passed all eight tests, similar to what @younesbelkada and you did for llama2 and phi2.

Currently, the code has some problems with `make fixup`. Will work on this for the next step.

![2024-01-01 6 50 52](https://github.com/huggingface/transformers/assets/154845754/15ca602a-1041-403a-8091-3de8fb7050c0)






Hi @younesbelkada,

I believe this pull request is now ready for your review.

I'd like to highlight a few changes, especially regarding `check_copies.py`, that I'm not entirely confident about. To ensure the branch passes the `make fixup` check, I removed the "copies" lines before both `modeling_codegen.CodeGenBlock` and `test_modeling_gptj.test_flash_attn_2_generate_padding_right`. This was done because the changes involved are somehow complex.

I would really appreciate your guidance on this. If there's a more standard or preferable way to handle such intricate changes, please let me know so I can make the necessary adjustments.

Thank you for your time on this!

Hi @younesbelkada, thank you very much for your valuable input and guidance! I apologize for the delayed response.

I've addressed the comment regarding the copy mechanism, and the branch successfully passed the `make fixup` test.

Additionally, I've conducted the speed test. However, the observed speedup was not as significant as what we noted with OPT. The test was performed on an Nvidia RTX 4090, utilizing `max-batch-size=8` and `max-seqlen=32` to conserve memory. The model checkpoint used was `EleutherAI/gpt-j-6b` with the revision set to "float16". I've attached the speedup graph below for your review.

![2024-01-27 9 51 37](https://github.com/huggingface/transformers/assets/154845754/be5038a5-c957-4f58-a3ae-b497d82d7356)




Could you also perform the test on an A100 GPU for comparison?

Thank you once again for your time. I look forward to hearing your thoughts on this!





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

Hi @ArthurZucker and @younesbelkada ,

Thank you so much for your additional suggestions!

I am sorry. I had assumed that `GPTJ_ATTENTION_CLASSES` had already been introduced by @ArthurZucker previously...

I have now added GPTJ_ATTENTION_CLASSES and made the necessary code modifications. 
Furthermore, I re-ran the test suite and successfully passed all the tests.

Please let me know if there's anything more I can do!
Thank you so much! 

Good for me merging! 🤗 

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.

Hey @bytebarde could you rebase and add the explicit expected outputs? Or should I do it? 🤗 

Hi @ArthurZucker , good morning!

I have added the `@require_bitsandbytes` and expected outputs in the test function.
Please let me know if there is anything needed to be addressed!

Thank you so much!

Hi @younesbelkada,

Thank you for taking the time to review this!

I have run the `make fix-copies` and believe that the previous issues regarding consistency have been addressed.

Please let me know if any further changes are needed. Thank you!