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

source

Pruner.prune_model


def prune_model(
    
):

Execute one pruning step and restore attention layer configurations


source

Pruner.group_importance


def group_importance(
    group
):

Compute importance scores for a dependency group


source

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