Skip to content
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

pass module to Params4bit.from_prequantized to ensure quant_state #32524

Merged
merged 5 commits into from
Aug 29, 2024

Conversation

winglian
Copy link
Contributor

@winglian winglian commented Aug 8, 2024

What does this PR do?

see #32477 #32477

Let's start with making sure pre-quantized models can be loaded properly for FSDP. This at least unlocks pre-quant models with qlora+fsdp where CPU RAM isn't a blocker.

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,
    Pull Request section?
  • Was this discussed/approved via a Github issue or the forum? 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, and
    here are tips on formatting docstrings.
  • 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.

@amyeroberts
Copy link
Collaborator

cc @SunMarc

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Thanks for the work ! Left a comment !

@@ -212,6 +212,7 @@ def create_quantized_param(
quantized_stats=quantized_stats,
requires_grad=False,
device=target_device,
module=module,
Copy link
Member

Choose a reason for hiding this comment

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

Can we just do what was done in the reverted PR ?

        param_kwargs = {}
            sig = inspect.signature(bnb.nn.Params4bit.from_prequantized)
            if "module" in sig.parameters:
                param_kwargs["module"] = module

or we could perform the version check here to device if we pass module or not !

Copy link
Member

@SunMarc SunMarc left a comment

Choose a reason for hiding this comment

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

Nice ! Thanks for fixing @winglian ! I left a minor question !

Comment on lines 211 to 214
param_kwargs = {}
sig = inspect.signature(bnb.nn.Params4bit.from_prequantized)
if "module" in sig.parameters:
param_kwargs["module"] = module

Copy link
Member

@SunMarc SunMarc Aug 9, 2024

Choose a reason for hiding this comment

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

Quick question about inspect.signature, does this make the quantization slower ? I'm a bit concerned since create_quantized_param is called for each param cc @muellerzr as I remember that you prefer not to use inspect

Copy link
Contributor Author

@winglian winglian Aug 9, 2024

Choose a reason for hiding this comment

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

@SunMarc also happy to do it as

    @cached_property
    def is_bnb_gte_0_43_3(self) -> bool:
        if self._is_bnb_gte_0_43_3 is not None:
            return self._is_bnb_gte_0_43_3

        self._is_bnb_gte_0_43_3 = version.parse(importlib.metadata.version("bitsandbytes")) >= version.parse("0.43.3")
        return self._is_bnb_gte_0_43_3

and then we can use self. is_bnb_gte_0_43_3()

Copy link
Contributor

Choose a reason for hiding this comment

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

Version parsing is far more preferred!

Copy link
Contributor

Choose a reason for hiding this comment

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

Though generally this would exist in import_utils as a min_version to is_bnb_available(). (We should then cache the bnb_version if we don't already, similar to _torch_version)

Copy link
Member

Choose a reason for hiding this comment

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

Minor nit: I think if there's a variable or function named for this that the name should be more meaningful than is_bnb_gte_0_43_3. It should describe what that version check is about, e.g. 'bnb_can_load_from_float_storage`.

That said, I like the suggestion to model it the way I see is_accelerate_available(min_version) in import_utils.

Copy link
Collaborator

Choose a reason for hiding this comment

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

can we do the version parsing? 🤗

@SunMarc SunMarc requested a review from ArthurZucker August 9, 2024 01:40
@winglian winglian force-pushed the bnb-load-quant-state-fix branch from 4ae9391 to 7a8c1f5 Compare August 28, 2024 15:28
Copy link
Contributor

@muellerzr muellerzr left a comment

Choose a reason for hiding this comment

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

Thanks!

@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
Collaborator

@ArthurZucker ArthurZucker left a comment

Choose a reason for hiding this comment

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

cool thanks

@ArthurZucker ArthurZucker merged commit 39bfb2f into huggingface:main Aug 29, 2024
21 checks passed
@winglian winglian deleted the bnb-load-quant-state-fix branch September 3, 2024 20:20
BernardZach pushed a commit to BernardZach/transformers that referenced this pull request Dec 5, 2024
…ggingface#32524)

* pass module to Params4bit.from_prequantized to ensure quant_state

* make sure to check bnb version

* revert min bnb version and use inspect on method instead

* use version instead of inspect to prevent performance hit

* make the property name readable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants