Pruner
Overview
The Pruner class provides structured pruning capabilities using the torch-pruning library. Unlike unstructured pruning (which zeros individual weights), structured pruning removes entire filters/channels, resulting in a genuinely smaller and faster model.
Key Features: - Automatic dependency handling across layers - Support for both local (per-layer) and global (cross-layer) pruning - Automatic detection and handling of attention layers in transformers - Compatible with various importance criteria from fasterai.core.criteria
Sparsifier vs Pruner: When to Use Which?
| Aspect | Sparsifier | Pruner |
|---|---|---|
| What it removes | Individual weights (unstructured) | Entire filters/channels (structured) |
| Model size | Same architecture, sparse weights | Smaller architecture |
| Speedup | Requires sparse hardware/libraries | Immediate speedup on any hardware |
| Accuracy impact | Generally lower at same sparsity | May need fine-tuning |
| Best for | Research, sparse-aware inference | Production deployment |
Pruner.prune_model
def prune_model(
):
Execute one pruning step and restore attention layer configurations
Pruner.group_importance
def group_importance(
group
):
Compute importance scores for a dependency group
Pruner.print_sparsity
def print_sparsity(
)->None:
Print pruning report showing channel counts and parameter reduction
Usage Examples
Let’s try the Pruner with a VGG16 model
model = resnet18()
pruner = Pruner(model, 30, 'local', large_final)
pruner.prune_model()See Also
- PruneCallback - Apply structured pruning during fastai training
- Criteria - Different importance measures for selecting what to prune
- Schedules - Control pruning progression during training
- Sparsifier - Unstructured pruning (zeroing weights without removing them)
- torch-pruning documentation - The underlying library used by Pruner