structured
structured
¶
Per-layer structured weight pruning using Lp-norm channel ranking.
StructuredPruner
¶
Bases: BasePruner
Per-layer structured pruning along a specified dimension.
Ranks channels by their Lp-norm magnitude and zeros the lowest-ranked
fraction. The norm_order parameter specifies p in the Lp-norm
(e.g., 1 for L1-norm, 2 for L2-norm).
Initialize with pruning parameters.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
amount
|
float
|
Fraction of channels to zero per layer, must be in (0, 1). |
required |
norm_order
|
int
|
The p in Lp-norm used to rank channels (e.g., 1 for L1, 2 for L2). |
2
|
dimension
|
int
|
Weight tensor dimension along which to prune. |
0
|
layer_types
|
list[str] | None
|
PrunableLayerType values to target. Defaults to Conv1d, Conv2d, and Linear. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If amount is not in the open interval (0, 1). |
Source code in src/versatil/post_training_compression/pruning/structured.py
prune
¶
Apply per-layer Ln structured pruning.
Iterates over all target layers and applies Ln structured pruning individually, then removes the pruning reparametrization.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
module
|
Module
|
Neural network module to prune. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, int]
|
Tuple of (total_parameters, zero_parameters). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the target module contains no prunable layers. |