Hi,
Pytorch tracks the gradients at a tensor level. Sooo if you just “store” the variable, it can be reused.
The only drawback of the code you have posted is it is recommended to run everything inside the forward method.
so ideally you would have:
class MyModel(nn.Module):
    def __init__(self, args):
        ... some setup here

    def common_operation(self, x):
        ...
        return common_val
    
    def operation1(self, x):
        tmp = common_operation(x)
        ... some computation
        return val1

    def operation2(self, x):
        tmp = common_operation(x)
        ... some computation
        return val2
   def forward(self,x):
        model.operation1(x)
        model.operation2(x)
model = MyModel(...)


You can even have several models and pass the output of one of them as the input of another. Since the gradients are computed at tensor level you don’t really need to use a nn.Module. You can have a dirty script which just use tensors as weights and it would work.