-
-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[Quantization] Modify the logic of BNB double quantization #19742
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -492,8 +492,6 @@ def load_weights(self, model: nn.Module, model_config: ModelConfig) -> None: | |
| raise ValueError("Following weights were not initialized from " | ||
| f"checkpoint: {weights_not_loaded}") | ||
|
|
||
| torch.cuda.empty_cache() | ||
|
|
||
| param_dict = dict(model.named_parameters()) | ||
| stacked_quant_state_dict: dict[str, dict[int, Any]] = {} | ||
| # TODO: Change this lazy import to normal import | ||
|
|
@@ -545,6 +543,8 @@ def load_weights(self, model: nn.Module, model_config: ModelConfig) -> None: | |
| for param_name, param in param_dict.items(): | ||
| if param_name in stacked_quant_state_dict: | ||
| quant_states = stacked_quant_state_dict[param_name] | ||
| # Dequantize double quantized values during weight loading. | ||
| dequantize_dq(quant_states) | ||
| set_weight_attrs(param, {"bnb_quant_state": quant_states}) | ||
|
|
||
| pack_ratio = getattr(param, "pack_factor", -1) | ||
|
|
@@ -565,6 +565,28 @@ def load_weights(self, model: nn.Module, model_config: ModelConfig) -> None: | |
| if load_8bit: | ||
| set_weight_attrs( | ||
| param, {"matmul_state": [None] * len(quant_states)}) | ||
|
|
||
| torch.cuda.empty_cache() | ||
| def download_model(self, model_config: ModelConfig) -> None: | ||
| self._prepare_weights(model_config.model, model_config.revision) | ||
|
|
||
|
|
||
| def dequantize_dq(quant_states: dict) -> None: | ||
| """ | ||
| When BNB employs Double Quantization, we perform the dequantization of | ||
| these constants during weight loading rather than at inference time, | ||
| thereby avoiding this computational overhead during inference. This comes | ||
| at the cost of increased memory usage. | ||
| """ | ||
| from bitsandbytes.functional import dequantize_blockwise | ||
| for _, quant_state in quant_states.items(): | ||
| # Copied from: https://github.com/bitsandbytes-foundation/bitsandbytes/blob/0.45.3/bitsandbytes/functional.py#L1352-#L1356 | ||
| if quant_state.nested: | ||
| absmax = dequantize_blockwise(quant_state.absmax, | ||
| quant_state.state2) | ||
| absmax += quant_state.offset | ||
|
Comment on lines
+583
to
+586
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if absmax.dtype != torch.float32: | ||
| absmax = absmax.float() | ||
|
Comment on lines
+587
to
+588
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| quant_state.absmax = absmax | ||
| quant_state.nested = False | ||
| quant_state.offset = None | ||
| quant_state.state2 = None | ||
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.
Uh oh!
There was an error while loading. Please reload this page.