add bnb support for Ascend NPU
Author: ji-huazhong
Pull request: yes
Labels: name: Quantization

# 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 -->

This PR can make bnb nf4 QLoRA out of the box on Ascend NPUs. 🤗 

Related PR: https://github.com/bitsandbytes-foundation/bitsandbytes/pull/1422


## 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?
- [ ] 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: @zucchini-nlp (visual-language models) or @gante (all others)
- pipelines: @Narsil
- tensorflow: @gante and @Rocketknight1
- tokenizers: @ArthurZucker
- trainer: @muellerzr and @SunMarc

Integrations:

- deepspeed: HF Trainer/Accelerate: @muellerzr
- 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 @SunMarc 

Thanks @statelesshz ! 
Let us know also with @Titus-von-Koeller once you have a WIP branch on bitsandbytes as well to add that in the multi-backend ongoing effort

Hey all!

I'm only aware of [a backend stub that was added on the multi-backend-refactor branch by @statelesshz](https://github.com/TimDettmers/bitsandbytes/pull/1223/files), no implementation logic. There wasn't much context given on the PR at the time, but since the implementation was yet to come, for me that was ok as is; as an incremental step.

@statelesshz Could you please elaborate a bit more what you mean by the following statement? Where does your implementation live right now?
> internally we first added NPU support based on bitsandbytes-0.42.0

Generally, there've been quite a lot of changes in 0.43.0 and even 0.43.1 (the setup logic), so it would be really advisable to already take those changes into account.

Regarding the timeline: we'll go into public alpha/beta testing of the Intel/AMD backends, finalize the refactoring (integrating with the Torch dispatcher through the torch.library API) in the next weeks and do a full release soon after.

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.

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

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.

@younesbelkada @Titus-von-Koeller  Good day! I have submitted a PR to add nf4 quant/dequant support to Ascend NPU. The corresponding end-to-end test results are as follows:
https://github.com/bitsandbytes-foundation/bitsandbytes/pull/1422#issuecomment-2490880479
PTAL 🤗 

@SunMarc I have deleted the changes of "Bnb4BitHfQuantizer". But CI has an error when executing check_repository_consistency. The specific error information is as follows:
```
Traceback (most recent call last):
  File "/root/transformers/utils/check_copies.py", line 1110, in <module>
    check_copies(args.fix_and_overwrite, args.file)
  File "/root/transformers/utils/check_copies.py", line 860, in check_copies
    raise Exception(
Exception: Found the following copy inconsistencies:
- src/transformers/quantizers/quantizer_bnb_4bit.py: copy does not match quantizers.quantizer_bnb_8bit.Bnb8BitHfQuantizer.update_device_map at line 271
Run `make fix-copies` or `python utils/check_copies.py --fix_and_overwrite` to fix them.
```
I tried to fix this problem locally based on the error message. After executing `python utils/check_copies.py --fix_and_overwrite`, it seemed that all the changes in this PR were rolled back. The specific result is shown in the figure below.
![image](https://github.com/user-attachments/assets/7cc3c5ab-0105-49ba-904c-aec6746c9d21)

Any guidance would be appreciated.

Hey @statelesshz , this happens because this method have the following comment above: 
`# Copied from transformers.quantizers.quantizer_bnb_8bit.Bnb8BitHfQuantizer.update_device_map`. So you just need to delete that line ! 


> Hey @statelesshz , this happens because this method have the following comment above: `# Copied from transformers.quantizers.quantizer_bnb_8bit.Bnb8BitHfQuantizer.update_device_map`. So you just need to delete that line !

Hey @SunMarc, thanks for your suggestion, it worked. CI reported that there was one test case failing, I think it's not caused by this PR.

The failing test case can pass in my local environment:
![image](https://github.com/user-attachments/assets/40319e50-9f31-408e-9991-b5d1e65a0127)

cc @SunMarc @younesbelkada 

> The failing test case can pass in my local environment:

Indeed this is not related. Can you try to rebase the PR ? I've also pinged Arthur for a final check before merging

@SunMarc done! 🤗 

Hey @ArthurZucker, could you help take a look at this PR, thanks :-)

Hey @ArthurZucker , this PR addresses a key issue I'm also facing, and I'm hoping it can be merged soon. Thanks!