If mask_compute is not using PyTorch methods, which would thus detach the computation graph and Autograd won’t be able to create the backward pass automatically, you would have to implement a custom torch.autograd.Function for it and provide the forward and backward for these operations.
Here 4 is an example.