Two forward/backward passes of the same y = w*x, and the 'gradient' about to drive the weight update reads 6.0 — double the true dy/dw of 3.0. backward() ADDS into .grad, and nothing zeroed it between the passes. Fix it by zeroing both grads between step 1 and step 2 — the miniature of optimizer.zero_grad().
The break is on line 28 — but read the whole snippet first.