使用 PyTorch Lightning 构建可扩展 AI 模型
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]]) # 剪枝前
tensor([[ 0.25, -0.13, 0.00, 0.70],
[ 0.00, 0.31, 0.00, 0.44]]) # 剪枝后(40% 权重置为 0)
$$
$$
$$
Sequential(
(fc): Linear(
in_features=128, out_features=64,
bias=True
(weight): PrunedParam()
)
) # 在 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)
) # 在 prune.remove 之后

使用 PyTorch Lightning 构建可扩展 AI 模型