A solutions that seems to work for me now is computing the loss using both the result from the Pytorch graph and also the result of the final numpy part. So for making more clear the loss now is calculated like this:
self.loss_fun = nn.MSELoss()

def my_loss(self, var1, var1_pred, var2, var2_pred):
   loss = 0.5 * self.loss_fun(var1, var1_pred) + 
          0.5 * self.loss_fun(var2, var2_pred)

   return loss

It seems that using this loss and passing to it in the var1 the result of the first part of the graph and in the var2 the result of the last part of the graph with the numpy operations Pytorch is able to compute the backward pass. Even if it is not clear to me why in this case is able to do that and not in the previous.

You could either implement custom autograd.Function s as described here

If in this case I want to build a my defined autograd.Function, given that the numpy part has not weights and so not gradient, do you think that the solution would be to simply return the gradient of the output, don’t you?
Thank you.