Repository navigation
Conversation
dpsanders
force-pushed
the
add-derivative-contractor
branch
from
April 19, 2026 19:08
8c8313a to
81a8ef6
Compare
Avoids world-age issues when building and invoking a generated gradient or forward-backward contractor in the same dynamic extent. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Patch bump for the RuntimeGeneratedFunctions swap: no changes to the exported API's calling convention, just a correctness fix (no more world-age errors) and a substantial build-time speedup. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Build forward-backward contractors for partial derivatives via reverse-mode AD. The gradient_code SSA is binarized (decomposed into elementary ops) and then fed into forward_backward_contractor through the SSAFunction interface. - Add derivative_ssa(ex, diff_var, vars): builds SSAFunction for ∂ex/∂var - Add derivative_contractor(ex, diff_var, vars): builds compiled contractor - Add _binarize_ssa: decomposes n-ary and compound-argument assignments from gradient_code into elementary binary/unary ops for rev() - Filter non-call assignments (constant/identity) from reverse pass in forward_backward_code, since they can't be reversed - Export derivative_ssa and derivative_contractor Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Five independent fixes plus a registry-wide regression test: - Drop `sign`, `max`, `min` from `unary_functions`. `sign_rev` is not defined anywhere (the registration has always been broken), and `max`/`min` are binary, not unary — ChainRules' adjoint for them synthesises boolean branch selectors (`!(y<x)`) which aren't reversible ops. - Override ChainRules' `abs` adjoint with `x/abs(x)`; the default uses `sign(x)`, whose `_rev_sign` we just removed. - Override ChainRules' `exp2`/`exp10`/`log2`/`log10` adjoints to use plain `Float64` logs instead of the `IrrationalConstants.Logtwo`/ `Logten` that `IntervalArithmetic` refuses to accept. - Extend `IntervalArithmetic.intersect_interval` with `(Real, Interval)` and `(Real, BareInterval)` methods so the constraint intersect works when the output variable comes through as a plain `Real` (e.g. from `d/dx(x^exact(2))`). - Promote integer seeds (`_ā := 1`, unassigned `:= 0`) in the binarized derivative SSA to intervals. Without this, identity-propagating gradients such as `d/dx(x-y) = 1` leave `Int`-valued entries that drive `IntervalContractors`' `mul_rev(a,b,c) = mul_rev(promote(a,b,c)...)` fallback into infinite recursion on `(Int, Int, Int)`. Also fixes PR 81's stale destructuring of `gradient_code` (it now returns an `SSAFunction` rather than a tuple, post-#80). Adds a regression test that loops over every entry in the binary and unary registries and builds+runs a `derivative_contractor` — catches each of these regressions. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
dpsanders
force-pushed
the
add-derivative-contractor
branch
from
April 19, 2026 19:30
81a8ef6 to
0e227af
Compare
dpsanders
force-pushed
the
add-ssa-function
branch
from
April 19, 2026 19:30
f1ea4a5 to
4ce0682
Compare
Member
Author
|
Closing in favour of a split: the actual |
2 tasks done
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
derivative_ssa(ex, diff_var, vars)— builds anSSAFunctionfor the partial derivative ∂ex/∂diff_var using reverse-mode AD (gradient_code)derivative_contractor(ex, diff_var, vars)— builds a compiled forward-backward contractor for the derivativeThis builds on #80 (SSAFunction type). The gradient SSA is binarized and then fed through
forward_backward_contractorvia the SSAFunction interface.Example
Test plan
d/dx(x²+y²) = 2x, equality and interval constraintsd/dx(sin(xy)+x²)produces contracted result🤖 Generated with Claude Code