Autograd architecture: tracing plus derivative rules
Document@documents/root
# Autograd architecture
Autograd differentiates Python functions built from supported operations. The public operators in `differential_operators.py` adapt ordinary function arguments and request vector-Jacobian or Jacobian-vector products from `core.py`. `tracer.py` records executed primitive calls using boxed values; the derivative registries in the core supply the local rules.
```text
user function → grad / jacobian
↓
make_vjp → trace → primitive nodes
↓
backward_pass → input derivatives
```
NumPy wrappers and derivative definitions extend this mechanism to arrays. `VSpace` describes zeros, addition, and inner products for each supported value type. Forward mode uses JVP nodes; reverse mode uses VJP nodes. Keeping tracing separate from derivative rules makes the same executed Python control flow usable by both modes.
Explore `grad`, `primitive`, and `backward_pass` to follow the main chain.
Evidence: [autograd/core.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Fcore.py%3Afile%3Acore.py%3A), [autograd/tracer.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Ftracer.py%3Afile%3Atracer.py%3A), [autograd/differential_operators.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Fdifferential_operators.py%3Afile%3Adifferential_operators.py%3A).
How Autograd checks its derivatives
Document@documents/root
`check_grads` checks requested forward and reverse modes, then recursively checks derivatives when `order > 1` (default order two). The numerical directional derivative uses symmetric finite differences. Reverse-mode checking compares inner products of analytical and numerical directional results.
`tests/test_core.py` also checks primitive arithmetic without importing the NumPy wrapper. Other test modules exercise NumPy operations, linear algebra, containers, and graph behavior.
Validation scope for this demo: source and index inspected. Upstream test suites were not executed; the export checks validate the knowledge graph and package.
Evidence: [autograd/test_util.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Ftest_util.py%3Afile%3Atest_util.py%3A), [tests/test_core.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Atests%2Ftest_core.py%3Afile%3Atest_core.py%3A).
Source snapshot, index coverage, and validation scope
Document@documents/root
# Export provenance
Upstream: [HIPS/autograd](https://github.com/HIPS/autograd).
This graph was generated on 2026-09-28 from the existing local source folder. The folder has no Git metadata, so an exact upstream commit is unknown; no branch or commit is guessed. It was not updated from upstream during export.
Source snapshot fingerprint: `88c3a289c49faec4fda5180d7e10a7d823332306a46680972e44d6a95f9816a8` (SHA-256 over sorted relative paths, NUL separators, and raw file SHA-256 digests; excludes Git/runtime/generated cache directories and symlinks). Regular source files: 136. Indexed semantic elements: 4086. File/text parser records: 121 (10 plain_text, 111 parsed); images have separate semantic kinds.
No syntax or conversion warnings were reported for indexed text files.
Static extraction is best effort. Unresolved dynamic calls are not evidence that dependencies are absent. Knowledge explanations were checked against selected local source; upstream test suites, notebooks, model inference, and model downloads were not run. The task validates index/export contents and readability.
Functional meaning: __init__.py
Specificationexamples/__init__.py
## Job
Marks the examples directory as a Python package via an empty __init__.py
## Source Interface
empty module (no definitions, imports, or statements)
## Receives
- None declared by immediate child artifacts.
## Outcome
Package marker only; the file contains no executable code
## Notable Effects
- None declared by immediate child artifacts.
Primitive calls connect values to local derivative rules
Specificationautograd/tracer.py
The primitive wrapper finds boxed arguments at the highest active trace, unwraps their numerical values, evaluates the wrapped function, and creates a node linking the result to its parent nodes. Calls without boxed inputs run the raw function directly.
A primitive becomes differentiable through registered VJP/JVP rules in `core.py`. Unsupported differentiation raises a missing-rule error rather than manufacturing a derivative. This boundary is where custom numerical operations integrate with the system.
Evidence: [autograd/tracer.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Ftracer.py%3Afile%3Atracer.py%3A), [autograd/core.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Fcore.py%3Afile%3Acore.py%3A).
Reverse mode accumulates contributions along shared paths
Specificationautograd/core.py
`backward_pass` starts with the seed gradient at the terminal node, traverses nodes in topological reverse order, applies each node's VJP, and accumulates contributions for every parent using `add_outgrads`. A value used more than once must receive all contributions.
`make_vjp` returns both the primal result and a pullback function. If the result is independent of the input, the pullback returns zeros in the input's vector space. The same core handles scalar and array-shaped derivatives through `VSpace`.
Evidence: [autograd/core.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Fcore.py%3Afile%3Acore.py%3A).
Start here: autograd knowledge graph
SpecificationREADME.md
# autograd source tour
This demo combines the complete published semantic index with selected explanations attached to real files, folders, classes, and functions. Start with architecture, then follow the core concepts:
1. [Autograd architecture: tracing plus derivative rules](lumvise://document/popular-demo-20260928%3Aautograd%3Aarchitecture)
2. [grad: differentiate a scalar-output function](lumvise://document/popular-demo-20260928%3Aautograd%3Agrad)
3. [Tracing follows the operations actually executed](lumvise://document/popular-demo-20260928%3Aautograd%3Atrace)
4. [Primitive calls connect values to local derivative rules](lumvise://document/popular-demo-20260928%3Aautograd%3Aprimitive)
5. [Reverse mode accumulates contributions along shared paths](lumvise://document/popular-demo-20260928%3Aautograd%3Areverse)
6. [How Autograd checks its derivatives](lumvise://document/popular-demo-20260928%3Aautograd%3Atests)
7. [Source snapshot, index coverage, and validation scope](lumvise://document/popular-demo-20260928%3Aautograd%3Aprovenance)
Select an artifact to inspect its owning semantic element. Evidence links point to indexed source. The source snapshot and coverage report records the exact scope and parser limitations.
Tracing follows the operations actually executed
Specificationautograd/tracer.py
`trace` creates a trace level, boxes the input with a root node, and calls the user's function. If the returned box belongs to the same trace, it returns the underlying result plus the final node. An output independent of the input produces a warning and no terminal node.
This is an execution trace of supported primitives. Python branching and loops determine which operations run; Autograd does not differentiate the source text. Nested trace levels support differentiation of derivative computations.
Evidence: [autograd/tracer.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Ftracer.py%3Afile%3Atracer.py%3A), [autograd/core.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Fcore.py%3Afile%3Acore.py%3A).
grad: differentiate a scalar-output function
Specificationautograd/differential_operators.py
`grad` builds a vector-Jacobian product, checks that the result has one real scalar output, then seeds that output with its vector-space representation of one. The returned derivative has the shape/type of the differentiated argument. The `unary_to_nary` decorator supplies the familiar argument-selection interface.
For array-valued outputs, `jacobian` or `elementwise_grad` expresses a different question; the scalar restriction prevents silently treating a vector as a scalar loss. Higher derivatives compose the same differentiable machinery.
Example: `grad(lambda x: x * x)(3.0)` has mathematical result `6.0`. This example explains the API; it was not executed during export.
Evidence: [autograd/differential_operators.py](lumvise://element/filesystem%3A9fa464819bb8f798%3Aautograd%2Fdifferential_operators.py%3Afile%3Adifferential_operators.py%3A).