Why autograd adds gradients instead of setting them

L2WorkingCode reading8 minCodingML depth

RolesLLM Product EngineeringAI Data SystemsInference Engineering

The question

This is the multiply operation of a small autograd engine.

python
def __mul__(self, other):
    other = other if isinstance(other, Value) else Value(other)
    out = Value(self.data * other.data, (self, other), '*')
    def _backward():
        self.grad += other.data * out.grad
        other.grad += self.data * out.grad
    out._backward = _backward
    return out

A contributor replaces both += with =, with the comment that a gradient should be set, not added. The XOR training loop still runs and the loss still falls. What did the change break, and how do you prove it in one small test?

Answer it first, aloud or on paper. Then open the answer.

The answer

Show short answer

A value that feeds two or more operations receives a gradient from each consumer, and the chain rule says those contributions add. With assignment, the last consumer to run overwrites the others, so every shared node gets a wrong gradient, including each weight that the summed loss uses once per sample. A numerical gradient check exposes the error in one call.

Show full answer

What the chain rule says about shared nodes

When y depends on x through two paths, the derivative of y with respect to x is the sum of the derivatives along each path. In the engine, a node that is used twice has two closures that write to its grad. The topological order runs every consumer before the node passes its gradient to its own children, so both closures run, and the second must add to the first.

Why the loop still ran

In the XOR loop the loss is the sum of four squared errors, one per sample, and every weight takes part in all four forward passes. With assignment, each weight keeps only the contribution of the sample that ran last. That is still a direction that often points downhill, so the loss moves. The gradient is wrong for every parameter even so.

The one-line test

python
x = Value(3.0)
y = x * x
y.backward()

Here self and other are the same object. The correct gradient of x squared at 3 is 6.0. The two closures each add 3.0, which gives 6.0. With assignment the result is 3.0. The lesson's gradient_check does the same job for any expression: it compares the engine with a centered finite difference and reports the difference, which must be far below 1e-5.

The other side of the same rule

Because gradients add, the training loop must set every grad back to 0.0 before each backward call. The lesson does this with a loop over the parameters. A framework does the same with a zero_grad call. Both habits come from the same fact: gradient storage is an accumulator.

Follow-ups

  1. L2WorkingWhy must the training loop set every gradient to zero before backward?

    Show expected answer

    gradients accumulate, so a second backward would stack onto the first; the loop sets each parameter's grad to 0.0.

  2. L3SeniorWrite the smallest expression that fails with assignment.

    Show expected answer

    a value multiplied by itself; the gradient at 3 must be 6.0, and assignment gives 3.0.

  3. L3SeniorWhy does the topological order matter for this bug?

    Show expected answer

    all consumers of a node must run before the node passes gradient on; with assignment the answer depends on which consumer ran last.

  4. L4StaffWhere does the same accumulation rule show up in a framework you use?

    Show expected answer

    a tensor's grad accumulates across uses and across backward calls, which is why the optimizer's zero_grad runs before each step.

Wrong answers

Show the wrong answers that interviewers hear
  • “Assignment is fine because each node has one parent.”

    A node that feeds two operations has two consumers in the graph, and a weight in a summed loss has one consumer per sample.

  • “The loss still goes down, so the gradients are correct.”

    A wrong gradient can still point partly downhill. Only a numerical check proves it.

  • “Use a running average instead of a sum.”

    The chain rule sums the path contributions. An average divides the true gradient by the number of consumers.

Taught in

The chain rule and autodiff lesson builds the Value class and the gradient check that catches this bug.