I have summarized what I am doing with custom operations
class Name(Function):
     @staticmethod
     def forward(ctx, input):
         ctx.save_for_backward(input)
         ctx.intermediate_results = tensor
         return loss

     @staticmethod
     def backward(ctx, grad_output):
        if torch.is_tensor(grad_output):
            tensor = ctx.intermediate_results
            inputs = ctx.saved_tensors
        else:
            tensor = Variable(ctx.intermediate_results)
            inputs = ctx.saved_variables
        # do gradient computation
        return grad_input
  
##OR Case 2

class Name(Function):
     @staticmethod
     def forward(ctx, input):
         ctx.save_for_backward(input)
         ctx.intermediate_results = Variable(tensor)
         return loss

     @staticmethod
     def backward(ctx, grad_output):
        tensor = ctx.intermediate_results
        inputs = ctx.saved_variables
        # do gradient computation
        return grad_input

The second one works fine when I feed Variables?
which one is correct?
I don’t know how to test the backward of the first one using just tensors?
What is the correct methodology?