-
Notifications
You must be signed in to change notification settings - Fork 7.1k
Add Nunchaku Lite single-file quantization #14100
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
Draft
rootonchair
wants to merge
5
commits into
huggingface:main
Choose a base branch
from
rootonchair:feature/nunchaku-lite-single-file
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+794
−1
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7f4a3a0
Add Nunchaku Lite single-file quantization
rootonchair 1a66ac9
Support config-backed Nunchaku Lite loading
rootonchair 36b4fc4
Remove Nunchaku runtime manifest metadata loading
rootonchair db05e0b
Simplify Nunchaku compact config loading
rootonchair 5d4822a
Add Nunchaku Lite quantization tests
rootonchair File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| from .nunchaku_quantizer import NunchakuLiteQuantizer | ||
|
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from ..base import DiffusersQuantizer | ||
| from .utils import ( | ||
| check_strict_state_dict_match, | ||
| replace_with_nunchaku_linear, | ||
| ) | ||
|
|
||
|
|
||
| if TYPE_CHECKING: | ||
| from ...models.modeling_utils import ModelMixin | ||
|
|
||
|
|
||
| from ...utils import is_kernels_available, logging | ||
|
|
||
|
|
||
| logger = logging.get_logger(__name__) | ||
|
|
||
|
|
||
| class NunchakuLiteQuantizer(DiffusersQuantizer): | ||
| def __init__(self, quantization_config, **kwargs): | ||
| super().__init__(quantization_config, **kwargs) | ||
| self.compute_dtype = quantization_config.compute_dtype | ||
| self.pre_quantized = quantization_config.pre_quantized | ||
|
|
||
| def validate_environment(self, *args, **kwargs): | ||
| if not is_kernels_available(): | ||
| raise ImportError( | ||
| "Loading Nunchaku checkpoints requires the Hugging Face `kernels` package. " | ||
| "Install it with `pip install kernels`." | ||
| ) | ||
|
|
||
| def update_torch_dtype(self, torch_dtype): | ||
| if torch_dtype is None: | ||
| torch_dtype = self.compute_dtype | ||
| return torch_dtype | ||
|
|
||
| def _process_model_before_weight_loading( | ||
| self, | ||
| model: "ModelMixin", | ||
| state_dict: dict[str, Any] | None = None, | ||
| metadata: dict[str, str] | None = None, | ||
| **kwargs, | ||
| ): | ||
| quantization_config = self.quantization_config.to_dict() | ||
| num_replaced = replace_with_nunchaku_linear(model, quantization_config, self.compute_dtype) | ||
|
|
||
| if state_dict is not None: | ||
| check_strict_state_dict_match(model, state_dict) | ||
| logger.info(f"Applied Nunchaku quantization config with {num_replaced} targets.") | ||
|
|
||
| def _process_model_after_weight_loading(self, model: "ModelMixin", **kwargs): | ||
| return model | ||
|
|
||
| @property | ||
| def is_serializable(self): | ||
| return False | ||
|
|
||
| @property | ||
| def is_trainable(self) -> bool: | ||
| return False | ||
|
|
||
| @property | ||
| def is_compileable(self) -> bool: | ||
| return True | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
We should set
is_compileable()property too:diffusers/src/diffusers/quantizers/base.py
Line 263 in 9159a58