Pruner

Remove useless filters to recreate a dense network

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

Usage Examples

Let’s try the Pruner with a VGG16 model

model = resnet18(); model

The Prunercan either remove filters based on local criteria (i.e. each layer will be trimmed of the same % of filters)

pruner = Pruner(model, 30, 'local', large_final)
pruner.prune_model()
print(model)

The Prunercan also remove filters based on global criteria (i.e. each layer will be trimmed of a different % of filters, but we specify the sparsity of the whole network)

model = resnet18()
pruner = Pruner(model, 50, 'global', large_final)
pruner.prune_model()
print(model)

See Also