I guess you want to set your own backward method. If it’s the case, you should implement a autograd Function rather than an nn.Module. Something like this:
class Loss(Function):
    
    def forward(ctx, input, target):
        ctx.save_for_backward(input, target)
        output = torch.sum((input - target).pow(2), 1)
        return output
    
    def backward(ctx, grad_output):
        input, target = ctx.saved_variables
        # should be: 
        # grad_input = torch.diag(grad_output).mm(2*(input - target))
        # but you want:
        grad_input = torch.zero(input.size())
        grad_target = None
        return grad_input, grad_target