How does pytorch.autograd.Function calculate dL_dy?
This code calculate grad of y=x**2.
In this code, dL_dy is  [0., 2., 8.]  How do they calculate dL_dy? Where did this tensor came from ?
import torch 
from torch.autograd import Function
class Square(Function):
    @staticmethod
    def forward(ctx,input):
        ctx.save_for_backward (input)
        return torch.square(input)

    @staticmethod
    def backward(ctx,dL_dy):
        print('dL_dy',dL_dy)
        print('ctx.saved_tensors',ctx.saved_tensors)
        x, = ctx.saved_tensors
        return dL_dy * 2 * x
    
    
square = Square.apply 
x = torch.arange(3).to(torch.float64).requires_grad_(True)
y = square(x)
L = torch.sum(y*y)
L.backward()
print(x.grad)


Let say your f(y) = x**2, so f'(y) = 2*x
x = torch.arange(3).to(torch.float64).requires_grad_(True) means x=[0,1,2], we can compute y = x**2 = [0,1,4].
So when you call L.backward(), it will apply f'(y) (calculating the gradient of y), and stored in dL_dy.
That's why dL_dy = f'(y) = 2 * [0,1,4] = [0,2,8]
