BasePruningMethod¶

class torch.nn.utils.prune.BasePruningMethod[source]

Abstract base class for creation of new pruning techniques.

Provides a skeleton for customization requiring the overriding of methods such as compute_mask() and apply().

classmethod apply(module, name, *args, importance_scores=None, **kwargs)[source]

Adds the forward pre-hook that enables pruning on the fly and the reparametrization of a tensor in terms of the original tensor and the pruning mask.

Parameters
• module (nn.Module) – module containing the tensor to prune

• name (str) – parameter name within module on which pruning will act.

• args – arguments passed on to a subclass of BasePruningMethod

• importance_scores (torch.Tensor) – tensor of importance scores (of same shape as module parameter) used to compute mask for pruning. The values in this tensor indicate the importance of the corresponding elements in the parameter being pruned. If unspecified or None, the parameter will be used in its place.

• kwargs – keyword arguments passed on to a subclass of a BasePruningMethod

apply_mask(module)[source]

Simply handles the multiplication between the parameter being pruned and the generated mask. Fetches the mask and the original tensor from the module and returns the pruned version of the tensor.

Parameters

module (nn.Module) – module containing the tensor to prune

Returns

pruned version of the input tensor

Return type

pruned_tensor (torch.Tensor)

abstract compute_mask(t, default_mask)[source]

Computes and returns a mask for the input tensor t. Starting from a base default_mask (which should be a mask of ones if the tensor has not been pruned yet), generate a random mask to apply on top of the default_mask according to the specific pruning method recipe.

Parameters
• t (torch.Tensor) – tensor representing the importance scores of the

• to prune. (parameter) –

• that need to be respected after the new mask is (iterations,) –

• Same dims as t. (applied.) –

Returns

mask to apply to t, of same dims as t

Return type

prune(t, default_mask=None, importance_scores=None)[source]

Computes and returns a pruned version of input tensor t according to the pruning rule specified in compute_mask().

Parameters
• t (torch.Tensor) – tensor to prune (of same dimensions as default_mask).

• importance_scores (torch.Tensor) – tensor of importance scores (of same shape as t) used to compute mask for pruning t. The values in this tensor indicate the importance of the corresponding elements in the t that is being pruned. If unspecified or None, the tensor t will be used in its place.

• default_mask (torch.Tensor, optional) – mask from previous pruning iteration, if any. To be considered when determining what portion of the tensor that pruning should act on. If None, default to a mask of ones.

Returns

pruned version of tensor t.

remove(module)[source]

Removes the pruning reparameterization from a module. The pruned parameter named name remains permanently pruned, and the parameter named name+'_orig' is removed from the parameter list. Similarly, the buffer named name+'_mask' is removed from the buffers.

Note

Pruning itself is NOT undone or reversed!
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.31407618522644043, "perplexity": 4006.873431320633}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2021-04/segments/1610703524858.74/warc/CC-MAIN-20210121132407-20210121162407-00528.warc.gz"}