Fix Break change of AWQ FusedModules due to Attention Refactor
Author: fanqiNO1
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 -->

Fixes #41910


## 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#create-a-pull-request),
      Pull Request section?
- [x] 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.

@SunMarc @MekkCyber

<!-- 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 @Cyrilvallez
- vision models: @yonigozlan @molbap
- audio models: @eustlb @ebezzam @vasqu
- multimodal models: @zucchini-nlp
- graph models: @clefourrier

Library:

- generate: @zucchini-nlp (visual-language models) or @gante (all others)
- continuous batching: @remi-or @ArthurZucker @McPatate
- pipelines: @Rocketknight1
- tokenizers: @ArthurZucker and @itazap
- trainer: @SunMarc
- attention: @vasqu @ArthurZucker @CyrilVallez
- model loading (from pretrained, etc): @CyrilVallez
- distributed: @3outeille @ArthurZucker
- CIs: @ydshieh

Integrations:

- ray/raytune: @richardliaw, @amogkam
- Big Model Inference: @SunMarc
- quantization: @SunMarc @MekkCyber
- kernels: @MekkCyber @drbh
- peft: @BenjaminBossan @githubnemo

Devices/Backends:

- AMD ROCm: @ivarflakstad
- Intel XPU: @IlyasMoutawwakil
- Ascend NPU: @ivarflakstad 

Documentation: @stevhliu

Research projects are not maintained and should be taken as is.

 -->


Comments:
Certainly! I will add some tests after implementing `AWQRoPE` to support models like `LLaMA3`~

I modified modeling_llama because autoawq uses the use_cache flag to determine whether to use KV cache by `start_pos`.

https://github.com/casper-hansen/AutoAWQ/blob/88e4c76b20755db275574e6a03c83c84ba3bece5/awq/modules/fused/attn.py#L231

Therefore, use_cache must be passed down to the decoder layer.

I’ll add some tests this weekend~

Sorry for the late update.

I've added a test using `hugging-quants/Meta-Llama-3.1-8B-Instruct-AWQ-INT4` to verify the correctness of the `AWQRoPE` implementation, and I think this PR is ready for review~

In fact, I believe that as `Transformers` continue to evolve—with an increasing number of models and ever-larger model sizes—the demand for AWQ will inevitably grow. Since `AutoAWQ` has already been archived, compatibility issues are likely to become more frequent. It might be a good solution for HuggingFace to fork and maintain an `AutoAWQ` repository. Should such a day come, I would be very willing to help you maintain it.

> In fact, I believe that as Transformers continue to evolve—with an increasing number of models and ever-larger model sizes—the demand for AWQ will inevitably grow. Since AutoAWQ has already been archived, compatibility issues are likely to become more frequent. It might be a good solution for HuggingFace to fork and maintain an AutoAWQ repository. Should such a day come, I would be very willing to help you maintain it.

Thanks for your concerns ! We don't think we will maintain a fork of AutoAWQ as this is quite a huge tasks tbh with our current staff. I think the best solution for all quantization methods and not only AutoAWQ is to put the critical modeling code inside transformers (eg Linear4bit, QuantAttentionFused) if they want to make sure that nothing breaks and avoid monkey patching as much as possible + move the kernels to kernels-community repo. A good example is FP8-finegrained method.
If you are willing to upstream + simplify `AutoAWQ` modeling code into transformers like `QuantAttentionFused` class, happy to review the PRs ! 

> > In fact, I believe that as Transformers continue to evolve—with an increasing number of models and ever-larger model sizes—the demand for AWQ will inevitably grow. Since AutoAWQ has already been archived, compatibility issues are likely to become more frequent. It might be a good solution for HuggingFace to fork and maintain an AutoAWQ repository. Should such a day come, I would be very willing to help you maintain it.
> 
> Thanks for your concerns ! We don't think we will maintain a fork of AutoAWQ as this is quite a huge tasks tbh with our current staff. I think the best solution for all quantization methods and not only AutoAWQ is to put the critical modeling code inside transformers (eg Linear4bit, QuantAttentionFused) if they want to make sure that nothing breaks and avoid monkey patching as much as possible + move the kernels to kernels-community repo. A good example is FP8-finegrained method.

Thank you for your reply!

I indeed overlooked the human effort required to maintain such a library before. Next, I’ll open a PR to migrate, simplify, and integrate the key code from `AutoAWQ` into `transformers` as possible, and also move the relevant implementations from `AutoAWQ-kernels` into `kernel-community`.

I plan to first draft a well-thought-out design proposal and include it in the new PR description so we can discuss it together. Thanks so much!

I've created a new PR #42256, which includes an analysis of AutoAWQ and its kernels.

Is there anything else I need to do before this PR can be merged?

you need to do make `fix-copies` to propagate some changes you did in the modeling file of llama

**[For maintainers]** Suggested jobs to run (before merge)

run-slow: apertus, arcee, aria, bitnet, cohere, csm, deepseek_v2, deepseek_v3, diffllama, emu3, ernie4_5, glm, glm4, glm4_moe, helium, hunyuan_v1_dense

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