-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
magnitude pruner view to reshape and static sparsifier
- Loading branch information
Showing
4 changed files
with
120 additions
and
4 deletions.
There are no files selected for viewing
This file contains 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 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 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,92 @@ | ||
import torch | ||
from torch.ao.pruning.sparsifier.base_sparsifier import BaseSparsifier | ||
|
||
from sparsimony.dst.base import DSTMixin | ||
from sparsimony.distributions.base import BaseDistribution | ||
from sparsimony.parametrization.fake_sparsity import FakeSparsity | ||
from sparsimony.pruners.unstructured import UnstructuredMagnitudePruner | ||
from sparsimony.utils import get_mask | ||
|
||
|
||
# TODO - double check if the current default init_method is good to go for static sparsity | ||
class StaticMagnitudeSparsifier(DSTMixin, BaseSparsifier): | ||
def __init__( | ||
self, | ||
optimizer: torch.optim.Optimizer, | ||
distribution: BaseDistribution, | ||
sparsity: float, | ||
init_method: str = "sparse_torch", | ||
): | ||
|
||
optimizer = optimizer | ||
self.distribution = distribution | ||
self.sparsity = sparsity | ||
self.init_method = init_method | ||
defaults = dict(parametrization=FakeSparsity) | ||
super().__init__(optimizer=optimizer, defaults=defaults) | ||
|
||
def _initialize_masks(self): | ||
self._distribute_sparsity(self.sparsity) | ||
for config in self.groups: | ||
# Prune to target sparsity for this step | ||
mask = get_mask(config["module"], config["tensor_name"]) | ||
original_weights = getattr( | ||
config["module"].parametrizations, config["tensor_name"] | ||
).original | ||
|
||
print(f"Original weights shape: {original_weights.shape}") | ||
print(f"Mask shape: {mask.shape}") | ||
|
||
mask.data = UnstructuredMagnitudePruner.calculate_mask( | ||
config["sparsity"], mask, original_weights | ||
) | ||
print(f"Mask 1s after pruning: {mask.sum()}") | ||
self._assert_sparsity_level(mask.data, self.sparsity) | ||
|
||
def _step(self): | ||
self._step_count += 1 | ||
# Basically do nothing to change the mask | ||
|
||
def grow_mask(self): | ||
pass | ||
|
||
def prune_mask(self): | ||
pass | ||
|
||
def update_mask(self): | ||
pass | ||
|
||
def __str__(self) -> str: | ||
# TODO: Errors if sparsifier has not been prepared. Fix me | ||
def neuron_is_active(neuron): | ||
return neuron.any() | ||
|
||
global_sparsity = self.calculate_global_sparsity().item() | ||
layerwise_sparsity_target = [] | ||
layerwise_sparsity_actual = [] | ||
active_neurons = [] | ||
total_neurons = [] | ||
for config in self.groups: | ||
layerwise_sparsity_target.append(config["sparsity"]) | ||
mask = get_mask(**config) | ||
layerwise_sparsity_actual.append( | ||
self.calculate_mask_sparsity(mask).item() | ||
) | ||
active_neurons.append( | ||
torch.vmap(neuron_is_active)(mask).sum().item() | ||
) | ||
total_neurons.append(len(mask)) | ||
active_vs_total_neurons = [] | ||
for a, t in list(zip(active_neurons, total_neurons)): | ||
active_vs_total_neurons.append(f"{a}/{t}") | ||
# TODO: Should list ignored_layers from distribution | ||
return ( | ||
f"{self.__class__.__name__}\n" | ||
f"Step No.: {self._step_count}\n" | ||
f"Distribution: {self.distribution.__class__.__name__}\n" | ||
f"Global Sparsity Target: {self.sparsity}\n" | ||
f"Global Sparsity Actual: {global_sparsity}\n" | ||
f"Layerwise Sparsity Targets: {layerwise_sparsity_target}\n" | ||
f"Layerwise Sparsity Actual: {layerwise_sparsity_actual}\n" | ||
f"Active/Total Neurons: {active_vs_total_neurons}" | ||
) |
This file contains 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