torch.nn.utils.prune.global_unstructured — PyTorch 2.7 documentation (original) (raw)

torch.nn.utils.prune.global_unstructured(parameters, pruning_method, importance_scores=None, **kwargs)[source][source]

Globally prunes tensors corresponding to all parameters in parameters by applying the specified pruning_method.

Modifies modules in place by:

  1. adding a named buffer called name+'_mask' corresponding to the binary mask applied to the parameter name by the pruning method.
  2. replacing the parameter name by its pruned version, while the original (unpruned) parameter is stored in a new parameter namedname+'_orig'.

Parameters

Raises

TypeError – if PRUNING_TYPE != 'unstructured'

Note

Since global structured pruning doesn’t make much sense unless the norm is normalized by the size of the parameter, we now limit the scope of global pruning to unstructured methods.

Examples

from torch.nn.utils import prune from collections import OrderedDict net = nn.Sequential(OrderedDict([ ... ('first', nn.Linear(10, 4)), ... ('second', nn.Linear(4, 1)), ... ])) parameters_to_prune = ( ... (net.first, 'weight'), ... (net.second, 'weight'), ... ) prune.global_unstructured( ... parameters_to_prune, ... pruning_method=prune.L1Unstructured, ... amount=10, ... ) print(sum(torch.nn.utils.parameters_to_vector(net.buffers()) == 0)) tensor(10)