Scalable AI Models with PyTorch Lightning
Sergiy Tkachuk
Director, GenAI Productivity
$$
$$
$$
$$
import torch.nn.utils.prune as prune
prune.l1_unstructured(model.fc, name="weight",
amount=0.4)
print(model.fc.weight.data)
tensor([[ 0.25, -0.13, 0.05, 0.70],
[-0.88, 0.31, -0.02, 0.44]]) # Before pruning
tensor([[ 0.25, -0.13, 0.00, 0.70],
[ 0.00, 0.31, 0.00, 0.44]]) # After pruning (40% weights set to 0)
$$
$$
$$
Sequential(
(fc): Linear(
in_features=128, out_features=64,
bias=True
(weight): PrunedParam()
)
) # Before prune.remove
import torch.nn.utils.prune as prune
prune.remove(model.fc, 'weight')
# Print model structure
print(model)
Sequential(
(fc): Linear(in_features=128,
out_features=64,
bias=True)
) # After prune.remove
Scalable AI Models with PyTorch Lightning