# Pruning and model size

**URL:** <https://discuss.pytorch.org/t/pruning-and-model-size/92985>\
**Category:** Uncategorized\
**Created:** [August 16, 2020, 9:50am UTC](https://discuss.pytorch.org/t/pruning-and-model-size/92985 "2020-08-16T09:50:16Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lusamino](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lusamino/32/27858_2.png) [@lusamino](https://discuss.pytorch.org/u/lusamino)\
**Post date:** [August 16, 2020, 9:50am UTC](https://discuss.pytorch.org/t/pruning-and-model-size/92985/1 "2020-08-16T09:50:16Z")

</div>

Hi all,

This is my first question, so I will try to be not too clumsy. Besides, I am more or less a beginner with PyTorch, so I hope my questions are not that evident. And thanks for all your help beforehand!

Well, I just trained a simple model with 3 linear layers and everything went smooth and fine. Now, I want to replicate this model in order to allow some adversarial training of a few input samples at the same time, and for that I will be using the hack Goodfellow proposed [here](https://github.com/tensorflow/tensorflow/issues/4897#issuecomment-290997283).

The problem is that the model may end up being quite big. But since many weights, all the off diagonal blocks, are going to be equal to zero, I thought: ok, let’s prune them, I don’t need them for anything. Then, the size of these repeated model will just scale with the number of replicas of the model, and not quadratically. However, I just put together a small example and this does not seem to work correctly:

```auto
# Original small model
enc_small = torch.nn.Sequential()
enc_small.add_module("dense_0", nn.Linear(2000, 4000))
torch.save(enc_small, 'orig_model.pth')

n_rep = 3

# Repeated model
enc_rep = torch.nn.Sequential()
enc_rep.add_module("dense_0", nn.Linear(2000 * n_rep, 4000 * n_rep))
torch.save(enc_rep, 'model_repeated.pth')

# Repeated model with pruning
enc_pruned = torch.nn.Sequential()
enc_pruned.add_module("dense_0", nn.Linear(2000 * n_rep, 4000 * n_rep))
# This mask is just zeroing out all elements out of the center blocks of 2000x4000
mask = np.zeros(encoder1.dense_0.weight.shape).astype(int)
for ir in range(n_rep + 1):
    mask[((ir - 1) * 4000):(4000*ir),
         ((ir - 1) * 2000):(2000*ir)] = 1

torch.nn.utils.prune.custom_from_mask(enc_pruned.dense_0, name='weight', mask = torch.tensor(mask))

torch.save(enc_pruned, 'pruned_model.pth')

```

And here is how the models look one saved:

```auto
25629256 -rw-r--r-- 1 users 1.4G Aug 16 09:47 pruned_model.pth
25629254 -rw-r--r-- 1 users 275M Aug 16 09:46 model_repeated.pth
25629253 -rw-r--r-- 1 users 31M Aug 16 09:46 orig_model.pth

```

I do believe I am miserably missing something, but I don’t know what… It might be the behaviour of `torch.nn.utils.prune.custom_from_mask`, that I am not understanding… Hence, I will really appreciate if you could offer me some help. Thank you so much!!!

P.S: I am in parallel experimenting with assigning the parameters as sparse matrices, but so far, to no avail…

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [August 18, 2020, 7:54am UTC](https://discuss.pytorch.org/t/pruning-and-model-size/92985/2 "2020-08-18T07:54:57Z")

</div>

I’m unfortunately not familiar enough with the pruning utilities, but @Michela might know, if the file size is expected.

PS: I would also generally recommend to store and load the `state_dict` instead of the complete model as described [here](https://pytorch.org/docs/stable/notes/serialization.html).

---

<div class="post-metadata">

**Author:** ![lusamino](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lusamino/32/27858_2.png) [@lusamino](https://discuss.pytorch.org/u/lusamino)\
**Post date:** [August 18, 2020, 8:40am UTC](https://discuss.pytorch.org/t/pruning-and-model-size/92985/3 "2020-08-18T08:40:19Z")

</div>

Thank you so much @ptrblck! Well, I believe @Michela will know 🙂 Regarding what you mention, I will try then using `state_dict`. But it was already surprising to me that the size was changing that much, as on top of the `weight_mask`, I cannot think about any other overhead created on the pruned model.

---

<div class="post-metadata">

**Author:** ![lusamino](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/lusamino/32/27858_2.png) [@lusamino](https://discuss.pytorch.org/u/lusamino)\
**Post date:** [August 18, 2020, 8:46am UTC](https://discuss.pytorch.org/t/pruning-and-model-size/92985/4 "2020-08-18T08:46:55Z")

</div>

Ok, here I come with updated results. They are already better with `state_dict()`, but the pruned model still takes more memory. I just added:  
`torch.save(enc_pruned.state_dict(), 'pruned_model.pth')`  
to all the models being saved.

And this is the output:

```auto
25629256 -rw-r--r-- 1 users 550M Aug 18 08:43 pruned_model.pth
25629254 -rw-r--r-- 1 users 275M Aug 18 08:43 model_repeated.pth
25629253 -rw-r--r-- 1 users 31M Aug 18 08:43 orig_model.pth

```

So, since the size is precisely twice the size of the original repeated, might it come from the mask? Is it then pruning correctly working?

Thanks again for all your help!!

---

<div class="post-metadata">

**Author:** ![PabloVD](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pablovd/32/46511_2.png) [@PabloVD](https://discuss.pytorch.org/u/PabloVD)\
**Post date:** [March 23, 2022, 6:24pm UTC](https://discuss.pytorch.org/t/pruning-and-model-size/92985/5 "2022-03-23T18:24:34Z")

</div>

Hi, I’m new with pruning and not really very familiar with this practice, but let me share my thoughts.

According to the [pruning tutorial in pytorch](https://pytorch.org/tutorials/intermediate/pruning_tutorial.html), applying a pruning routine like `torch.nn.utils.prune.custom_from_mask` only creates the masks, increasing the number of tensors stored in the model, and thus increasing its size. To really apply such masks, next one has to remove the pruned connections by running `prune.remove(module, "weight")` or similar.

In that case, saving with `torch.save` leads to stored models of the same size. However, following [this notebook](https://github.com/spellml/resnext50-panda/blob/master/notebooks/pruning.ipynb) (which is explained in [this tutorial](https://spell.ml/blog/model-pruning-in-pytorch-X9pXQRAAACIAcH9h)), if you compress the model, the pruned model becomes lighter than the original one.

Hope it helps!
