Skip to content

Allow weights_only=True load for gemlite layout #2081

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

Closed
wants to merge 1 commit into from

Conversation

jerryzh168
Copy link
Contributor

Summary:
This PR adds a few imports from gemlite so that the gemlite checkpoint can be loaded with weights_only = True in huggingface (which is the default)

torch.load(gemlite_checkpoint, weights_only=True

Note: we need to remove getattr in the future

Test Plan:

import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig

model_id = "jerryzh168/phi4-mini-int4wo-gemlite"

quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)

prompt = "Hey, are you conscious? Can you talk to me?"
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
output_text = tokenizer.batch_decode(
    generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)

Reviewers:

Subscribers:

Tasks:

Tags:

Summary:
This PR adds a few imports from gemlite so that the gemlite checkpoint
can be loaded with weights_only = True in huggingface (which is the default)

`torch.load(gemlite_checkpoint, weights_only=True`

Note: we need to remove `getattr` in the future

Test Plan:
```
import torch
from transformers import AutoModelForCausalLM, AutoTokenizer, TorchAoConfig

model_id = "jerryzh168/phi4-mini-int4wo-gemlite"

quantized_model = AutoModelForCausalLM.from_pretrained(model_id, device_map="auto", torch_dtype="auto")
tokenizer = AutoTokenizer.from_pretrained(model_id)

prompt = "Hey, are you conscious? Can you talk to me?"
inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
generated_ids = quantized_model.generate(**inputs, max_new_tokens=128)
output_text = tokenizer.batch_decode(
    generated_ids, skip_special_tokens=True, clean_up_tokenization_spaces=False
)
print(output_text)
```

Reviewers:

Subscribers:

Tasks:

Tags:
Copy link

pytorch-bot bot commented Apr 19, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/ao/2081

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

✅ You can merge normally! (1 Unrelated Failure)

As of commit e91f88d with merge base 34421b1 (image):

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Apr 19, 2025
@jerryzh168 jerryzh168 added the topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories) label Apr 19, 2025
from gemlite.core import DType, GemLiteLinearTriton

# TODO: we need to remove `getattr` since it's unsafe (by picklescan)
torch.serialization.add_safe_globals([DType, GemLiteLinearTriton, getattr])

Choose a reason for hiding this comment

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

Since hf flags this, would it make sense to wait until we see whether gemlite can remove the need for getattr on this one

Copy link
Collaborator

@mobicham mobicham Apr 22, 2025

Choose a reason for hiding this comment

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

We can remove this part, I fixed this issue in #2096

@jerryzh168
Copy link
Contributor Author

no longer needed since the serialization issue is fixed in #2096

@jerryzh168 jerryzh168 closed this Apr 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. topic: improvement Use this tag if this PR is an improvement (doesn't fit into any of the other categories)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants