Bases: Modifier
Abstract base class which implements functionality related to oneshot sparsity.
Inheriters must implement calibrate_module and compress_modules
Methods:
-
on_initialize
–
Initialize and run the SparseGPT algorithm on the current state
on_initialize
on_initialize(state: State, **kwargs) -> bool
Initialize and run the SparseGPT algorithm on the current state
Parameters:
-
state
(State)
–
session state storing input model and calibration data
Source code in src/llmcompressor/modifiers/pruning/sparsegpt/sgpt_base.py
| def on_initialize(self, state: "State", **kwargs) -> bool:
"""
Initialize and run the SparseGPT algorithm on the current state
:param state: session state storing input model and calibration data
"""
model: torch.nn.Module = state.model
dataloader: torch.utils.data.DataLoader = state.data.calib
# infer module and sequential targets
# Note: only pass sequential_targets from kwargs, not the full kwargs dict
# which may contain 'model' and cause duplicate argument errors
self._sequential_targets = infer_sequential_targets(
model, sequential_targets=kwargs.get("sequential_targets")
)
layers = dict(match_named_modules(model, self._sequential_targets))
self._target_layers = dict(
match_named_modules(model, self.targets)
) # layers containing targets
# infer layer sparsities
if self.sparsity_profile == "owl":
logger.info(
"Using OWL to infer target layer-wise sparsities from "
f"{len(dataloader) if dataloader else 0} calibration samples..."
)
self.sparsity = self._infer_owl_layer_sparsity(model, layers, dataloader)
# get layers and validate sparsity
if isinstance(self.sparsity, (list, dict)) and len(self._target_layers) != len(
self.sparsity
):
raise ValueError(
f"{self.__repr_name__} was initialized with {len(self.sparsity)} "
f"sparsities values, but model has {len(layers)} target layers"
)
return True
|