# How to compute grad\_sample for large model

**URL:** <https://discuss.pytorch.org/t/how-to-compute-grad-sample-for-large-model/145339>\
**Category:** Opacus\
**Created:** [March 2, 2022, 12:44am UTC](https://discuss.pytorch.org/t/how-to-compute-grad-sample-for-large-model/145339 "2022-03-02T00:44:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![doudeimouyi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/doudeimouyi/32/47154_2.png) [@doudeimouyi](https://discuss.pytorch.org/u/doudeimouyi)\
**Post date:** [March 2, 2022, 12:44am UTC](https://discuss.pytorch.org/t/how-to-compute-grad-sample-for-large-model/145339/1 "2022-03-02T00:44:08Z")

</div>

Hi,  
I am trying to Opacus for Vision Transformer model. Here is what ViT look like:

```auto
class ViT(nn.Module):
    def __init__ (self, ...):
        .......
       self.class_token = nn.Parameter(torch.zeros(1, 1, dim))
       ........

    def forward(self, x):
        ......
        x = torch.cat((self.class_token.expand(b, -1, -1), x), dim=1)  
       ......
        return x    

```

Using Opacus for this model will raise an error because of the nn.Parameter here `self.class_token = nn.Parameter(torch.zeros(1, 1, dim))`. Unless we compute grad\_sample for the whole ViT model:

```auto
@register_grad_sampler(ViT)
def compute_vit_grad_sample():
   ......

```

But if I re-write ViT like this:

```auto
class cls_token(nn.Module):
    def __init__ (self, dim):
       self.class_token = nn.Parameter(torch.zeros(1, 1, dim))
           
    def forward(self, x):
        x = torch.cat((self.class_token.expand(b, -1, -1), x), dim=1) 
        return x    

class ViT(nn.Module):
    def __init__ (self, ...):
        .......
       self.class_token = cls_token(dim)
           
    def forward(self, x):
        ......
        x = cls_token(x)
       ......
        return x    

```

In this way, it is true that now I only need to compute grad\_sample for the `cls_token` instead of the whole `ViT` module?

```auto
@register_grad_sampler(cls_token)
def compute_cls_grad_sample():
   ......

```

Thanks!

---

<div class="post-metadata">

**Author:** ![pstock](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/pstock/32/993_2.png) [@pstock](https://discuss.pytorch.org/u/pstock)\
**Post date:** [March 4, 2022, 7:17pm UTC](https://discuss.pytorch.org/t/how-to-compute-grad-sample-for-large-model/145339/2 "2022-03-04T19:17:06Z")

</div>

Hi doudeimouyi,

Thanks for your interest! The second approach (wrapping the `cls_token` in a `nn.Module` and only implementing the `grad_sampler` for this module) would be correct.

Indeed, in this approach, you are calling the `forward` method of the module `cls_token`, hence Opacus is able to correctly compute the grad samples.

Note however that you need to replicate your approach (encapsulate `nn.Parameters` in a `nn.Module` for which you define a custom grad sampler) every time you directly use a parameter in a forward pass instead of relying on a `forward` method of a child module.

Do not hesitate if you have further questions.
