pytorch / tutorials

PyTorch tutorials.
https://pytorch.org/tutorials/
BSD 3-Clause "New" or "Revised" License
8.08k stars 4k forks source link

how to calculate the macs after prune? #2313

Open machengjie321 opened 1 year ago

machengjie321 commented 1 year ago

🚀 Descirbe the improvement or the new tutorial

I use torch.nn.utils.prune as prune to prune the model, then I use torchprofile.profile_macs() to calculate the macs of Pruned_model, but I find the macs will be increase before prune.remove() to make the pruning permanent. it is normal because additional calculate wil be weight * mask. but after I use prune.remove() to make the pruning permanent, the macs calculated by torchprofile.profile_macs() still same as the model befor prune. 2023-05-08_17-01-34

Existing tutorials on this topic

No response

Additional context

No response

svekars commented 1 year ago

Hi @machengjie321, which to tutorial are you having trouble with?

machengjie321 commented 1 year ago

Hi @machengjie321, which to tutorial are you having trouble with? I have trouble in this: https://pytorch.org/tutorials/intermediate/pruning_tutorial.html?highlight=torch%20nn%20utils%20prune image prune can compress models by reducing the number of parameters. I want calculate the macs(flops) of model after prune, so I use the torchprofile.profile_macs() to calculate it, but after I use prune.remove() to make the pruning permanent, the macs calculated by torchprofile.profile_macs() still same as the model befor prune. I found some possible answers in the other responses: ‘Pruning allows for impressive theoretical reduction of models sizes and complexity. However it usually offers little practical benefits as it is most often limited to just zeroing out weights, without actually removing the pruned filters/weights. This precludes from actual advantages provided by sparsification methods.’https://github.com/pytorch/pytorch/issues/59835

I initially thought that the macs of the pruned model would be less than the unpruned model, but since it is not actually removing the pruned filters/weights, the macs obtained using torchprofile.profile_macs() is same as the model prune.

Is there any way can make the macs of pruned model is less than the unpruned model, Such as if I convert the pruned model into a sparse form, can the macs of model be reduced?