I suppose it has nothing to do with chain rule here. The chain rule is dz|dx = dz|dy * dy|dx, but here are dz|dx and dy|dx. What your code have done is ‘calculate the gradient of current graph from node z, and keep the gradient. Then calculate the gradient of current graph from node y, and print the gradient buffer of node x’.