Hi,
This code is a bit out of context. Where did you get it from?
So I am going to guess that you try to do the backward pass by hand for a network whole loss function is loss = ||y_pred - y||^2 with weights w1 and w2. And y_pred = w2 * h_relu and h_relu = relu(w1 * x).
If this is true, then the code above computes the backward pass using the chain rule by hand.
With each grad_xxx being the derivative of the loss wrt xxx. You can write the gradient of each formula above and you will get these expressions.
The .clone() and [h<0] are here because grad_h[h < 0] = 0 changes grad_h inplace to set all the values where the inputs were negative to 0 (the backward of the relu). But because they don’t want to change grad_h_relu inplace (that contains the derivative of the output of the relu). They .clone() that before.