Avoid probagating gradient through a certain subtree

Assume a have variables a and b, and c = f(a, b) and d = g(a, b). I want to differentiate z = c+d wrt a and b assuming (!) that d is fixed and not propagating through it. Can I set requires_grad of d to zero or using d.data instead of d whenever I don’t want to propagate gradients? Or use d.detach() somehow? Thanks!

if d is fixed, then why don’t differentiate c wrt a and b?

Ok, suppose you want to differentiate z = c * d wrt a and b. Yes, you could do z = c*d.detach(), then d will not be propogated through.