News for hackers. Stuff that matters.
Re:Minimal auto-differentiation engine in Rust (for educatio... by amelius on Friday June 20, 2025 @06:17PM (#44332636)
Looks like this uses mutation of global/shared state. For the example:

z=x*y+3,

what if there is another function that does:

w=x+2*y

and then both functions do a backward pass (simultaneously, perhaps in different threads or otherwise); then it seems dangerous to collect the results of the backward pass (partial derivatives) in the shared variables x and y and make them accessible through x.get_grad() and y.get_grad(). Imho, in a better design, you'd say z.get_grad(x) and z.get_grad(y), and w.get_grad(x) and w.get_grad(y) to get the partial derivatives.

Re:Minimal auto-differentiation engine in Rust (for educatio... by lschneider on Saturday June 21, 2025 @05:33AM (#44336064)
That's a great point, it would be better to keep the gradients separate from the Scalars.

However, I think PyTorch does it the same way (?), at least they say something like this in their docs.

"This function accumulates gradients in the leaves - you might need to zero .grad attributes or set them to None before calling it." - https://docs.pytorch.org/docs/stable/generated/torch.autogra...

The rust burn crate does it better, they store the backprop'd gradients in a separate container and return it: https://github.com/tracel-ai/burn/blob/af381ee18566fc27f5c98...