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

Torchao weights only compability #34355

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open

Torchao weights only compability #34355

wants to merge 3 commits into from

Conversation

SunMarc
Copy link
Member

@SunMarc SunMarc commented Oct 23, 2024

What does this PR do ?

This PR makes torchao serialized model loadable with weights_only=True which is the default. Otherwise, you need to set weights_only=False which is not recommended.

cc @jerryzh168 cc @MekkCyber

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

@@ -73,6 +73,50 @@ def validate_environment(self, *args, **kwargs):
)
else:
self.offload = True
if self.pre_quantized:
safe_globals = []
Copy link
Contributor

@jerryzh168 jerryzh168 Oct 23, 2024

Choose a reason for hiding this comment

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

if we do import torchao, I think we should get everything here (classes etc. being added to safeglobals)? otherwise we'd need to fix torchao

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm using torchao 0.5.0 and it's not working on my side. I can try with the latest tomorrow !

Copy link
Contributor

Choose a reason for hiding this comment

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

I see, it's not expected I think, I think it should be fixed in torchao side, I feel 0.5 should have this functionality already actually. if you can have a standalone repro that will be very helpful for us. I remember I have tested in https://huggingface.co/docs/transformers/main/en/quantization/torchao

Copy link
Member Author

@SunMarc SunMarc Oct 24, 2024

Choose a reason for hiding this comment

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

Actually, we ran into this issue with @MekkCyber on the example you shared in the docs.
Here's a the reproducer, let us know if you also have this issue :

from transformers import TorchAoConfig, AutoTokenizer, AutoModelForCausalLM
import torch

model_name = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"
quant_config = TorchAoConfig("int4_weight_only", group_size=32)
quantized_model = AutoModelForCausalLM.from_pretrained(
    model_name,
    torch_dtype=torch.bfloat16,
    device_map="cuda:0",
    quantization_config=quant_config,
)
output_dir = "llama3-8b-int4wo-128"
quantized_model.save_pretrained(output_dir, safe_serialization=False)

loaded_quantized_model = AutoModelForCausalLM.from_pretrained(output_dir, device_map="cuda:0")

Copy link
Contributor

Choose a reason for hiding this comment

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

OK will test and report back

@MekkCyber
Copy link
Contributor

Thanks for this PR @SunMarc, really helpful !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants