Pruning

ModelOpt provides three pruning workflows:

  • Puzzletron v2 for guided, resumable heterogeneous pruning campaigns.

  • Minitron for structured pruning of large language models.

  • FastNAS for computer vision subnet search.

Minitron and FastNAS are available through the unified mtp.prune API. FastNAS and Minitron auto pruning find a subnet that meets the given deployment constraints (e.g. FLOPs or parameters). Manual Minitron pruning instead applies explicitly configured target dimensions. Depending on how aggressive the pruning is, the resulting model may have little to no accuracy degradation from the base model. These pruning methods support pruning the convolutional and linear layers, and attention heads of the model. More details on these pruning modes are as follows:

  1. mcore_minitron: A pruning method developed by NVIDIA Research for pruning GPT, Mamba and Hybrid Transformer Mamba models in NVIDIA Megatron-Bridge or Megatron-LM framework. It uses the activation magnitudes to prune the embedding hidden size, mlp ffn hidden size, transformer attention heads, GQA query groups, mamba heads and head dimension, and number of layers of the model. Checkout more details of the algorithm in the paper.

  2. fastnas: A pruning method recommended for Computer Vision models. Given a pretrained model, FastNAS finds the subnet which maximizes the score function while meeting the given constraints.

The remainder of this guide covers the unified Minitron and FastNAS API. Puzzletron v2 instead uses its setup wizard and campaign runner.

Follow the steps described below to obtain the optimal model satisfying your requirements using mtp:

  1. Pruning: Prune the model using our provided mtp.prune API and get an optimal subnet describing the pruned network architecture.

  2. Fine-tuning: Fine-tune the resulting subnet to recover the accuracy.

To find out more about the concepts behind NAS and pruning, please refer to NAS concepts.

Fine-tuning

The final step of architecture search is to fine-tune the pruned model on your dataset. This way you can ensure to obtain the best possible performance for your pruned model.

Prerequisites

  1. To perform fine-tuning you need a pruned subnet as explained in the previous section.

  2. You can reuse your existing training pipeline. We recommend running fine-tuning with your original training schedule:

    • 1x training epochs (or 1x downstream task fine-tuning),

    • same or smaller (0.5x-1x) learning rate.

Load the pruned model

You can simply restore your pruned model (weights and architecture) using mto.restore():

import modelopt.torch.opt as mto
from torchvision.models import resnet50

# Build original model
model = resnet50()

# Restore the pruned architecture and weights
pruned_model = mto.restore(model, "modelopt_pruned_model.pth")

Run fine-tuning

Now, please go ahead and fine-tune the pruned subnet using your standard training pipeline with the pre-configured hyperparameters. A usually good fine-tuning schedule is to repeat the pre-training schedule with 0.5x-1x initial learning rate.

Do not forget to save the model using mto.save().

train(pruned_model)

mto.save(pruned_model, "modelopt_pruned_finetuned_model.pth")

Deploy

The pruned and finetuned model is now ready for downstream tasks like deployment. The model you have in hand now should be the best neural network meeting your deployment-aware search constraint.

import modelopt.torch.opt as mto
from torchvision.models import resnet50

# Build original model
model = resnet50()

model = mto.restore(model, "modelopt_pruned_finetuned_model.pth")

# Continue with downstream tasks like deployment (e.g. TensorRT or TensorRT-LLM)
...

Pruning Concepts

Pruning is the process of removing redundant components from a neural network for a given task. Conceptually, pruning is similar to NAS, but has less computational overhead compared to NAS at the cost of potentially finding a less optimal architecture compared to NAS. Most APIs are based on the corresponding NAS APIs but are adapted to reflect the simpler workflow.

Specifically, for pruning we do not specifically train the search space and all its subnets. Instead, a pre-trained checkpoint is used to approximate the search space. Therefore, we can skip the (potentially expensive) search space training step and directly search for a subnet architecture before fine-tuning the resulting subnet.

Note

If you want to learn more about the concept behind NAS and pruning, take a look at NAS Concepts including a more detailed comparison between NAS and pruning.