Diagnostic Failure Case Analysis: Pinpointing the Overwrite Defect
Agent-Assisted Engineering Check
Prerequisites
Day 32 gradient accumulation and numerical checking principles.
Learning Objective
Pinpoint a minimal reproducible autograd failure case where faulty implementation leads to silent gradient corruption, and diagnose the root cause using numerical differences.
Instructions
Execute the diagnostic failure demonstration using the CLI:
python3 projects/autograd/cli.py diagnose --bug-type overwrite-accumulation
Observe how the diagnostic harness isolates node a in $f = (a + b) \cdot a$, showing analytical gradient $7.0$ versus numerical finite difference $10.0$ ($30\%$ relative error).
Practice & Prediction Log
1. Write down the Minimal Reproducible Example (MRE) that triggers the overwrite bug.
2. Explain why a single-line test case like $z = x \cdot y$ would PASS gradcheck even if the engine contains the overwrite defect.
3. Identify the exact line of code in _backward() that fixes the bug.
In graphs without fan-out (where each variable is used only once), each node has only a single consumer, so accumulation (+=) and assignment (=) yield identical results. The bug ONLY manifests when a variable branches into multiple operations.
Reference Solution:
1. MRE: $a = \text{Value}(3.0); b = \text{Value}(4.0); f = (a + b) * a; f.\text{backward}()$.
2. Because in linear graphs without branching, each node receives only one incoming gradient contribution, so = happens to produce the correct value by accident.
3. Replace self.grad = ... with self.grad += ... across all backward closures.
The learner constructs an MRE that reliably differentiates between accumulating and overwriting autograd implementations.
Authoritative Source Resources & Provenance
micrograd: A tiny scalar-valued autograd engine
Relevant Section / Range: micrograd/engine.py: class Value (_backward closures and backward() topological sort); README 'Example usage'
Verified Version & Date: commit a9a9f94 (Verified: 2026-10-30)
Direct demonstration of how simple variable reuse creates branching DAGs requiring accumulation.
Autograd backward pass and multivariable calculus
Local diagnostic harness in projects/autograd/diagnostics.py.
Compared Candidate Sources & Selection Analysis (2)
Production scale, but too large and complex for tracing by hand in introductory lessons.
Vectorized autograd; micrograd's pure scalar model is pedagogical for hand backprop.