extend-primitives
Use this sub-skill when a user needs to make a new primitive differentiable or extend an existing primitive with custom reverse- or forward-mode rules.
Route here for
primitive-based authoring of custom primitives.- staged
defvjp,defjvp,defvjp_argnums, anddefjvp_argnumsclosures. - compatibility work for deprecated
.defvjp,.defgrad, and.defvjp_is_zerowrappers. - gradient checking with
check_gradsandcombo_check.
Route elsewhere for
- ordinary differentiation-operator questions, Jacobians, Hessians, and higher-level derivative semantics: differentiation-core
- built-in NumPy/SciPy wrapper behavior, array protocol behavior, and wrapper limits: numpy-scipy-primitives
Evidence anchors
autograd/extend.py, autograd/core.py, autograd/test_util.py, docs/updateguide.md, docs/tutorial.md, examples/define_gradient.py, tests/test_wrappers.py
Bundled runtime files
references/api-reference.mdreferences/workflows.mdreferences/troubleshooting.mdscripts/custom_primitive_smoke.py
Operating guidance
- Start from
autograd.extend.primitive. - Register a staged VJP/JVP rule immediately after the primitive is defined.
- Use the staged maker to capture only the forward-pass values needed by the derivative.
- Prefer
defvjp_argnums/defjvp_argnumswhen one cached calculation should serve several argnums. - Use
Noneonly when a rule is intentionally zeroed; omit the argnum entirely when you want the missing-pathNotImplementedError. - Validate tiny fixtures with
check_grads; usecombo_checkwhen several positional or keyword combinations need coverage. - Keep deprecated wrapper APIs only for compatibility with existing code and test fixtures; new authoring should use the staged API.
Quick smoke
Run python scripts/custom_primitive_smoke.py before handing off a draft.
Cross-links
differentiation-corefor operator semantics and derivative wrappers.numpy-scipy-primitivesfor primitives that wrap ndarray or SciPy behavior.