-
Notifications
You must be signed in to change notification settings - Fork 622
Add layerwise decompression and compression to sequential pipeline #2995
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鈥檒l 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?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -4,11 +4,13 @@ | |||||||||||||||||||||||
| greedy_bin_packing, | ||||||||||||||||||||||||
| is_distributed, | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| from compressed_tensors.quantization import enable_quantization | ||||||||||||||||||||||||
| from compressed_tensors.quantization.utils import is_module_quantized | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| from llmcompressor.core import Event, State | ||||||||||||||||||||||||
| from llmcompressor.modifiers import Modifier | ||||||||||||||||||||||||
| from llmcompressor.modifiers.quantization.calibration import ( | ||||||||||||||||||||||||
| freeze_module_quantization, | ||||||||||||||||||||||||
| observe, | ||||||||||||||||||||||||
| update_qparams, | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
@@ -65,20 +67,28 @@ def on_initialize(self, state: State, **kwargs) -> bool: | |||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Then, according to the module's quantization scheme, observers and calibration | ||||||||||||||||||||||||
| hooks are added. These hooks are disabled until the modifier starts. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Skipped when layerwise_decompression is enabled because modules are still | ||||||||||||||||||||||||
| compressed; per-layer setup happens in start_layerwise_calibration instead. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| if not QuantizationMixin.has_config(self): | ||||||||||||||||||||||||
| raise ValueError( | ||||||||||||||||||||||||
| "QuantizationModifier requires that quantization fields be specified" | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| QuantizationMixin.initialize_quantization(self, state.model) | ||||||||||||||||||||||||
| if not getattr(state, "layerwise_decompression", False): | ||||||||||||||||||||||||
| QuantizationMixin.initialize_quantization(self, state.model) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| return True | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def on_calibration_start(self, state: State, event: Event, **kwargs): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| Begin calibrating activations. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Skipped when layerwise_decompression is enabled; per-layer calibration | ||||||||||||||||||||||||
| setup is handled by start_layerwise_calibration in the pipeline. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| QuantizationMixin.start_calibration(self, state.model) | ||||||||||||||||||||||||
| if not getattr(state, "layerwise_decompression", False): | ||||||||||||||||||||||||
| QuantizationMixin.start_calibration(self, state.model) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def on_sequential_epoch_end( | ||||||||||||||||||||||||
| self, state: State, event: Event, modules: list[torch.nn.Module], **kwargs | ||||||||||||||||||||||||
|
|
@@ -91,24 +101,35 @@ def on_sequential_epoch_end( | |||||||||||||||||||||||
| if not is_distributed(): | ||||||||||||||||||||||||
| observe(modules, "weight") | ||||||||||||||||||||||||
| update_qparams(modules, "weight") | ||||||||||||||||||||||||
| return | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| ### Distributed | ||||||||||||||||||||||||
| rank = dist.get_rank() | ||||||||||||||||||||||||
| world_size = dist.get_world_size() | ||||||||||||||||||||||||
| else: | ||||||||||||||||||||||||
| ### Distributed | ||||||||||||||||||||||||
| rank = dist.get_rank() | ||||||||||||||||||||||||
| world_size = dist.get_world_size() | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| module_list, rank_to_modules, module_to_rank = greedy_bin_packing( | ||||||||||||||||||||||||
| modules, | ||||||||||||||||||||||||
| world_size, | ||||||||||||||||||||||||
| item_weight_fn=lambda mod: mod.weight.numel(), | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| module_list, rank_to_modules, module_to_rank = greedy_bin_packing( | ||||||||||||||||||||||||
| modules, | ||||||||||||||||||||||||
| world_size, | ||||||||||||||||||||||||
| item_weight_fn=lambda mod: mod.weight.numel(), | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
| observe(rank_to_modules[rank], "weight") | ||||||||||||||||||||||||
| update_qparams(rank_to_modules[rank], "weight") | ||||||||||||||||||||||||
| broadcast_qparams_and_cleanup( | ||||||||||||||||||||||||
| module_list, module_to_rank, _WEIGHT_Q_PARAMS | ||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| observe(rank_to_modules[rank], "weight") | ||||||||||||||||||||||||
| update_qparams(rank_to_modules[rank], "weight") | ||||||||||||||||||||||||
| broadcast_qparams_and_cleanup(module_list, module_to_rank, _WEIGHT_Q_PARAMS) | ||||||||||||||||||||||||
| if getattr(state, "layerwise_decompression", False): | ||||||||||||||||||||||||
| self.remove_hooks(self._calibration_hooks) | ||||||||||||||||||||||||
| for module in modules: | ||||||||||||||||||||||||
| freeze_module_quantization(module) | ||||||||||||||||||||||||
| enable_quantization(module) | ||||||||||||||||||||||||
|
Comment on lines
+121
to
+125
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. The
Suggested change
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| def on_calibration_end(self, state: State, event: Event, **kwargs): | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| Finish calibrating by removing observers and calibration hooks | ||||||||||||||||||||||||
| Finish calibrating by removing observers and calibration hooks. | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Skipped when layerwise_decompression is enabled; per-layer cleanup | ||||||||||||||||||||||||
| is handled in on_sequential_epoch_end. | ||||||||||||||||||||||||
| """ | ||||||||||||||||||||||||
| QuantizationMixin.end_calibration(self, state.model) | ||||||||||||||||||||||||
| if not getattr(state, "layerwise_decompression", False): | ||||||||||||||||||||||||
| QuantizationMixin.end_calibration(self, state.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.
When
layerwise_decompressionorlayerwise_compressionis enabled, the sequential pipeline is strictly required. If another pipeline (such asbasicorindependent) is used, the global initialization and calibration steps inQuantizationModifierwill be silently skipped, resulting in a model that is never actually quantized or calibrated. Adding a validation check here prevents this silent failure.