Skip to main content
QUICK REVIEW

[Paper Review] Streamlining Tensor and Network Pruning in PyTorch

M. Paganini, Jessica Zosa Forde|arXiv (Cornell University)|Apr 28, 2020
Computational Physics and Python ApplicationsComputer Science11 references7 citations
TL;DR

This paper introduces the PyTorch `torch.nn.utils.prune` module, a unified, open-source interface for applying structured and unstructured pruning to neural network layers during training, inference, or post-training. It enables researchers and practitioners to reduce model size and computation with minimal code changes, using a consistent API that supports iterative pruning, global magnitude comparison, and easy serialization of pruned models.

ABSTRACT

In order to contrast the explosion in size of state-of-the-art machine learning models that can be attributed to the empirical advantages of over-parametrization, and due to the necessity of deploying fast, sustainable, and private on-device models on resource-constrained devices, the community has focused on techniques such as pruning, quantization, and distillation as central strategies for model compression. Towards the goal of facilitating the adoption of a common interface for neural network pruning in PyTorch, this contribution describes the recent addition of the PyTorch torch.nn.utils.prune module, which provides shared, open source pruning functionalities to lower the technical implementation barrier to reducing model size and capacity before, during, and/or after training. We present the module's user interface, elucidate implementation details, illustrate example usage, and suggest ways to extend the contributed functionalities to new pruning methods.

Motivation & Objective

  • To address the growing challenge of deploying large, over-parameterized deep learning models on resource-constrained devices such as mobile, IoT, and AR/VR systems.
  • To lower the technical barrier for implementing model pruning by providing a shared, open-source interface within PyTorch.
  • To enable researchers to easily experiment with and contribute new pruning techniques through a common API.
  • To support both training-time and post-training pruning with consistent, modular, and extensible design principles.
  • To facilitate model compression for improved efficiency, reduced energy consumption, and enhanced privacy through on-device inference.

Proposed method

  • Introduces `BasePruningMethod` as an abstract base class that defines a shared interface for all pruning techniques, requiring implementation of `compute_mask`.
  • Uses reparametrization: replaces pruned parameters with a masked version by storing the original tensor as `name_orig` and the mask as `name_mask` in module buffers.
  • Applies pruning via a forward pre-hook that dynamically multiplies the original tensor with the mask during forward passes, preserving computational graph integrity.
  • Supports both structured and unstructured pruning through dedicated classes like `L1Unstructured`, `RandomUnstructured`, and `LnStructured`, with configurable pruning amounts and dimensions.
  • Enables iterative pruning using `PruningContainer` to track multiple pruning operations on the same parameter across multiple calls.
  • Provides utility functions like `prune.global_unstructured` to perform global magnitude-based pruning across an entire network, pooling all parameters for comparison.

Experimental results

Research questions

  • RQ1How can a unified, extensible, and user-friendly pruning interface be designed within PyTorch to support diverse pruning strategies?
  • RQ2What architectural patterns enable safe, reversible, and composable pruning operations that integrate seamlessly into PyTorch’s autograd and serialization workflows?
  • RQ3How can global pruning across an entire model be efficiently implemented while maintaining compatibility with layer-wise and iterative pruning?
  • RQ4What mechanisms ensure that pruned models remain serializable and can be permanently saved or reverted without data loss?
  • RQ5How can the API be structured to allow researchers to easily implement and contribute new pruning methods without deep internal knowledge of PyTorch’s module system?

Key findings

  • The `torch.nn.utils.prune` module successfully provides a consistent, open-source interface for applying both structured and unstructured pruning in PyTorch with minimal code changes.
  • Pruning operations are fully compatible with PyTorch’s autograd system and can be applied before, during, or after training, with results preserved in the model’s state_dict.
  • The module supports global pruning across an entire network via `global_unstructured`, enabling pruning of the bottom 20% of connections by magnitude across all layers.
  • Pruning can be iteratively applied to the same parameter using `PruningContainer`, allowing for progressive compression strategies such as pruning 3 entries followed by 50% of remaining channels.
  • The module supports both hard pruning (binary masks) and soft pruning, with the ability to permanently remove pruning via `prune.remove` to restore the pruned tensor to the original parameter name.
  • The design enables seamless serialization and deserialization of pruned models, ensuring compatibility with standard PyTorch model saving and loading workflows.

Read this paper

Open the full text in the Nubint reader and ask about any passage as you read.

Read this paper on Nubint

This review was generated by AI from the original paper. Please check the paper before citing it.