-
Notifications
You must be signed in to change notification settings - Fork 130
Enhance save_pretrained #1376
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?
Enhance save_pretrained #1376
Conversation
👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review. Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed. |
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.
Pull Request Overview
This PR enhances the save_pretrained method by replacing the weakref-based approach with an inspect-based implementation, ensuring the signature is maintained and error handling improved. Key changes include:
- Replacing weakref wrapping with a direct method wrapping using inspect.
- Introducing a helper to generate an enhanced function signature.
- Adding unit tests and updated documentation to validate and explain the new behavior.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.
File | Description |
---|---|
tests/llmcompressor/transformers/sparsification/test_compress_tensor_utils.py | Adds unit tests to verify the modified save_pretrained signature and functionality. |
src/llmcompressor/transformers/sparsification/compressed_tensors_utils.py | Updates modify_save_pretrained to use inspect and adds a helper for signature updates. |
docs/save_pretrained.md | Provides documentation on the enhanced save_pretrained parameters and usage examples. |
src/llmcompressor/transformers/sparsification/compressed_tensors_utils.py
Outdated
Show resolved
Hide resolved
Refactor: modify_save_pretrained Signed-off-by: Rahul Tuli <rtuli@redhat.com>
Signed-off-by: Rahul Tuli <rtuli@redhat.com>
…rs_utils.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Signed-off-by: Rahul Tuli <rtuli@redhat.com>
0695eb5
to
b403c25
Compare
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.
Pull Request Overview
This PR enhances the save_pretrained functionality by replacing the weakref‐based wrapping with a direct method wrapping that preserves and augments the original function signature with compression parameters. Key changes include:
- Replacing weakref-based method wrapping with an inspect-based approach.
- Adding a helper function (_create_compression_signature) that creates an enhanced signature.
- Updating tests and documentation to verify and explain the new behavior.
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
File | Description |
---|---|
tests/llmcompressor/transformers/sparsification/test_compress_tensor_utils.py | Updated tests to validate the enhanced save_pretrained signature and its functionality. |
src/llmcompressor/transformers/sparsification/compressed_tensors_utils.py | Refactored modify_save_pretrained to use a direct wrapping with a preserved signature and removed weakref. |
docs/save_pretrained.md | Added documentation to explain the new compression parameters and usage examples. |
src/llmcompressor/transformers/sparsification/compressed_tensors_utils.py
Show resolved
Hide resolved
tests/llmcompressor/transformers/sparsification/test_compress_tensor_utils.py
Outdated
Show resolved
Hide resolved
…tensor_utils.py Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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.
awesome! nice work
|
||
# Apply compression signature | ||
save_pretrained_wrapper.__signature__ = sig_with_compression_params | ||
save_pretrained_wrapper._overridden = True |
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.
95% sure ._overridden
is not saved with the model, just want to confirm that this change won't break any previously saved models that had ._overriden
@@ -306,3 +295,59 @@ def update_and_save_recipe(model_stub: str, save_directory: str): | |||
# save recipe | |||
recipe_path = os.path.join(save_directory, RECIPE_FILE_NAME) | |||
recipe.yaml(recipe_path) | |||
|
|||
|
|||
def _create_compression_signature(orig_sig: inspect.Signature) -> inspect.Signature: |
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.
very nice!
This PR improves the
save_pretrained
method wrapper to use a more maintainable and robust approach for extending the function signature with compression parameters. The current version uses an approach withweakref
that can be difficult to maintain and debug.This is an initial implementation addressing ticket feedback welcome!
Changes
weakref
-based approach with a direct method wrapping usinginspect
save_pretrained
method_overriden
to_overridden
)Benefits of New Approach
More Robust: The new implementation properly preserves and displays the original function signature
Before:
After:
Better Maintainability: Code is more modular with a separate signature creation function
Before:
After:
Improved Error Handling: Somewhat better handling of common pitfalls
Before:
After:
Improved Documentation: documentation of parameters and usage
Plus a dedicated
docs/save_pretrained.md
file with examples.Testing
Added tests that verify:
This is an initial implementation to improve the developer experience, and I'm open to suggestions for further refinements.