Sure. As I said, you can write a normal python function as long as you don’t break the pytorch magic i.e. do the calculations on tensors to not break the gradients.
import torch

def myloss(t1, t2):
    loss_per_batch = (t1 - t2).abs().mean(dim=1)
    return loss_per_batch.mean()


tensor1 = torch.randn(2, 3)
tensor2 = torch.randn(2, 3)

loss = myloss(tensor1, tensor2)
print(loss)