-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Update torchao save #3679
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
base: main
Are you sure you want to change the base?
Update torchao save #3679
Conversation
Summary of ChangesHello @metascroy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
@danielhanchen can you have a look. cc @jerryzh168 @andrewor14 as FYI |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Codex Review
Here are some automated review suggestions for this pull request.
ℹ️ About Codex in GitHub
Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".
unsloth/save.py
Outdated
| arguments = dict(locals()) | ||
| arguments["model"] = self |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid NameError when preparing PTQ save arguments
_unsloth_save_torchao_with_given_config now builds arguments using self, but the function signature only accepts model, so any PTQ call (i.e., when torchao_config is provided) will raise a NameError before merging or saving the model. This blocks the post-training quantization workflow entirely because the helper cannot run past the first few lines.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
The pull request refactors the unsloth_save_pretrained_torchao function into two internal helper functions, _unsloth_save_torchao_with_attached_config and _unsloth_save_torchao_with_given_config, to handle saving QAT (Quantization-Aware Training) models and applying PTQ (Post-Training Quantization) with a provided configuration, respectively. The main function now acts as a dispatcher, including new assertions for correct usage (e.g., requiring a token for push_to_hub and a torchao_config for PTQ, while disallowing torchao_config for QAT models). The changes also involve updating model and tokenizer loading/saving logic, removing default torchao_config behavior, and adjusting variable names from self to model in the refactored parts. Review comments suggest improving type hints for the token parameter in the new helper functions to Optional[Union[str, bool]] for consistency, and changing a bare except: block to except Exception: for more robust error handling.
unsloth/save.py
Outdated
| tokenizer, | ||
| push_to_hub: bool = False, | ||
| token: Optional[Union[str, bool]] = None, | ||
| token: Optional[str] = None, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The type hint for token here is Optional[str], but the calling function unsloth_save_pretrained_torchao can pass a boolean value. While this will likely work at runtime due to dynamic typing, it's good practice to have consistent type hints. Consider changing the type hint to Optional[Union[str, bool]] to match the caller and the underlying push_to_hub API.
| token: Optional[str] = None, | |
| token: Optional[Union[str, bool]] = None, |
3923abc to
ab6b22c
Compare
ac4f21a to
c4f9e5c
Compare
unsloth/save.py
Outdated
| token: Optional[Union[str, bool]] = None, | ||
| ): | ||
| """Save a QAT-trained model by converting fake-quantized weights to real quantized weights.""" | ||
| from unsloth.models._utils import _convert_torchao_model |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probs move this out and do a relative import ie from .models
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed to relative import
unsloth/save.py
Outdated
| # TorchAO does not support safe_serialization reliably | ||
| safe_serialization = False | ||
|
|
||
| torchao_save_directory = save_directory + "-torchao" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes we can remove -torchao it's probs unncessary
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Removed
8397e56 to
82cd10d
Compare
for more information, see https://pre-commit.ci
This updates unsloth_save_pretrained_torchao.
With this change, after QAT, we can do this to save the model:
Previously we had to manually convert with something like this:
unsloth_save_pretrained_torchao now either calls _unsloth_save_torchao_with_given_config (previous behavior) or _unsloth_save_torchao_with_attached_config.
As the name suggests, _unsloth_save_torchao_with_given_config requires a torchao config is provided and starts from a floating point model.
_unsloth_save_torchao_with_given_config uses the config on the model (_torchao_config) that gets attached when a qat_scheme is provided to FastLanguageModel.
The save_directory is always appended with "-torchao" to preserve the previous behavior, but from an API perspective I think this is a bit odd. cc @danielhanchen for thoughts