Skip to content

Use torch.device instead of current device index for BnB quantizer #10069

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 5, 2024

Conversation

a-r-r-o-w
Copy link
Member

As discussed in #10009 (comment)

cc @sayakpaul

@a-r-r-o-w a-r-r-o-w requested a review from sayakpaul December 1, 2024 15:13
@a-r-r-o-w a-r-r-o-w changed the title Use torch.dtype instead of current device index for BnB quantizer Use torch.device instead of current device index for BnB quantizer Dec 1, 2024
@HuggingFaceDocBuilderDev

The docs for this PR live here. All of your documentation changes will be reflected on that endpoint. The docs are available until 30 days after the last update.

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you run some slow tests and maybe the

class SlowBnb4BitFluxTests(Base4bitTests):

suite?

If not, it will need to wait till tomorrow when I can find time to run it. LMK.

@@ -836,7 +836,7 @@ def from_pretrained(cls, pretrained_model_name_or_path: Optional[Union[str, os.P
param_device = "cpu"
# TODO (sayakpaul, SunMarc): remove this after model loading refactor
elif is_quant_method_bnb:
param_device = torch.cuda.current_device()
param_device = torch.device(torch.cuda.current_device())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me!

@yiyixuxu WDYT?

Cc: @SunMarc as well.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let's throw an error in load_model_dict_into_meta when device is passed as index??

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Throws a value error now. @yiyixuxu

@sayakpaul, the integration tests pass:

(nightly-venv) (nightly-venv) aryan@hf-dgx-01:~/work/diffusers$ RUN_SLOW=1 CUDA_VISIBLE_DEVICES="3" pytest -s tests/quantization/bnb/test_4bit.py::SlowBnb4BitFluxTests
========================================================================================================================================= test session starts ==========================================================================================================================================
platform linux -- Python 3.10.14, pytest-8.3.2, pluggy-1.5.0
rootdir: /home/aryan/work/diffusers
configfile: pyproject.toml
plugins: timeout-2.3.1, requests-mock-1.10.0, xdist-3.6.1, anyio-4.6.2.post1
collected 1 item                                                                                                                                                                                                                                                                                       

tests/quantization/bnb/test_4bit.py Unused kwargs: ['_load_in_4bit', '_load_in_8bit', 'quant_method']. These kwargs are not used in <class 'transformers.utils.quantization_config.BitsAndBytesConfig'>.
`low_cpu_mem_usage` was None, now default to True since model is quantized.
Loading pipeline components...:  14%|████████████████████████████████▋                                                                                                                                                                                                    | 1/7 [00:00<00:00,  9.00it/s]You set `add_prefix_space`. The tokenizer needs to be converted from the slow tokenizers
Loading pipeline components...: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 7/7 [00:00<00:00,  9.26it/s]
100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 10/10 [00:14<00:00,  1.50s/it]
.

========================================================================================================================================== 1 passed in 52.27s ===================================================================

@sayakpaul sayakpaul requested a review from yiyixuxu December 1, 2024 15:21
@sayakpaul sayakpaul mentioned this pull request Dec 2, 2024
9 tasks
@sayakpaul sayakpaul mentioned this pull request Dec 4, 2024
6 tasks
@DN6 DN6 added the roadmap Add to current release roadmap label Dec 4, 2024
@a-r-r-o-w a-r-r-o-w requested a review from sayakpaul December 4, 2024 21:33
Copy link
Collaborator

@yiyixuxu yiyixuxu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks!

Copy link
Member

@sayakpaul sayakpaul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Merge away! Thank you very much.

Maybe we could also update the type hint of device in load_model_dict_into_meta()?

@sayakpaul sayakpaul merged commit 98d0cd5 into main Dec 5, 2024
18 checks passed
@sayakpaul sayakpaul deleted the param-device-bnb branch December 5, 2024 02:35
@a-r-r-o-w
Copy link
Member Author

Maybe we could also update the type hint of device in load_model_dict_into_meta()?

I think it's already correctly set to str | torch.device

sayakpaul added a commit that referenced this pull request Dec 23, 2024
…10069)

* update

* apply review suggestion

---------

Co-authored-by: Sayak Paul <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
close-to-merge roadmap Add to current release roadmap
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants