VainF / Torch-Pruning

[CVPR 2023] Towards Any Structural Pruning; LLMs / SAM / Diffusion / Transformers / YOLOv8 / CNNs
https://arxiv.org/abs/2301.12900
MIT License
2.63k stars 329 forks source link

Why the pruning does not occur (params & flops do not change) #416

Open U0127148 opened 1 month ago

U0127148 commented 1 month ago

I tried to use torch pruning to do filter pruning The model I want to prune is ResNet18

model.to(device)
model.eval()

example_inputs = torch.randn(1, 3, 32, 32).to(device)
before = summary(model, input_size=(1, 3, 32, 32), verbose=0, device='cuda')

DG = tp.DependencyGraph().build_dependency(model, example_inputs=example_inputs)

for name, module in model.named_modules():
    if isinstance(module, torch.nn.Conv2d):
        prune_indices = get_prune_indices(module, pruning_ratios[name])
        group = DG.get_pruning_group(module, tp.prune_conv_out_channels, idxs=prune_indices)
        if DG.check_pruning_group(group):
            group.prune()

# After pruning, count the MACs and parameters
after = summary(model, input_size=(1, 3, 32, 32), verbose=0, device='cuda')
param_drop = 100.0 * (1.0 - (after.total_params / before.total_params))
flops_drop = 100.0 * (1.0 - (after.total_mult_adds / before.total_mult_adds))

print(f'param_drop: {param_drop:.2f}%')
print(f'FLOPs drop: {flops_drop:.2f}%')

But it seems like nothing was pruned The results printed is below

param_drop: 0.00%
FLOPs drop: 0.00%

If I do something wrong? I have checked, the condition if isinstance(module, torch.nn.Conv2d) is OK. So, I don't know why nothing was pruned.

U0127148 commented 1 month ago

This is my fault, the pruning ratios for each layer are too big, so this happens. It looks normal now.