Forward-mode AD via the shared scalar rule table#84
Open
dpsanders wants to merge 1 commit into
Open
Conversation
Introduces `tangent_code` and `tangent` as the forward-mode counterparts to `gradient_code` and `gradient`. Both pull partials from the same `unary_rule` / `binary_rule` dispatch table, so there is only one source of truth for scalar derivatives. - `dotted(v)` — tangent variable for `v`, written with a combining overdot (parallel to `bar(v)` for reverse mode). - `tangent_eq(eq)` — `_ȧ = Σᵢ (∂f/∂xᵢ) · ẋᵢ` for one forward assignment. - `tangent_code(ex_or_ssa, vars)` — appends the tangent pass and surfaces `input_tangents` and `output_tangent` in `variables`. - `tangent(ex, vars)` — compiles to `(__inputs, __tangents) -> (value, directional_derivative)`. The argument name is deliberately `__inputs`, not `__args`: the latter collides with RuntimeGeneratedFunctions' own internal binding and causes a spurious `BoundsError` at call time. Verified: `tangent((x,y), (1,0))` reproduces `gradient`'s first component, `tangent((x,y), dir)` gives the full Gateaux derivative. Bumps to v0.7.0. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
6 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Stacked on #83. Adds forward-mode AD, reusing the
unary_rule/binary_ruledispatch table that PR #83 introduced for reverse-mode. One rule table, two AD passes — no second source of truth to drift.API
dotted(v)— tangent variable forv, written with a combining overdot (parallel to the internalbar(v)for reverse mode).tangent_code(ex_or_ssa, vars)— appends one tangent assignment per forward assignment, surfacinginput_tangentsandoutput_tangentinSSAFunction.variables.tangent(ex, vars)— exported. Compiles a callable(__inputs, __tangents) -> (value, directional_derivative)Behaviour
Verified against reverse-mode
gradientin tests.Note on RGF naming
The compiled function's first argument is
__inputs, not__args. The latter collides withRuntimeGeneratedFunctions' own internal binding and triggers a spuriousBoundsErrorat call time. Documented inline where the Expr is built.Test plan
⟨∇f, v⟩.Follow-ups
Hessian via forward-over-reverse (applying
tangent_codeto the SSA produced bygradient_code) will land in a separate PR on top of this one.🤖 Generated with Claude Code