Repository navigation
Evaluate shared DAG nodes once per pass - #172
Merged
Merged
Conversation
A node with several parents was re-evaluated once per path from the root in the forward, Jacobian and Hessian passes and in is_affine, which is exponential for deep compositions with reused nodes. - forward: per-pass epoch; atoms recurse through expr_forward - Jacobian: the jacobian_evaluated latch applies to every node and is cleared when a forward pass re-evaluates a non-affine node - Hessian: wsum_hess_init counts in-edges; a shared node sums the weights of its in-edges and runs its impl once. neg, scalar_mult and quad_form fold their factor into the child's weights - is_affine: memoized Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Threads can again run forward passes on DAGs that share no nodes, as before the per-pass epoch was introduced. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…mment Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
The epoch and its thread-local macro are now private to expr.c. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.
Problem
No pass cached results per node, so a node with several parents was re-evaluated once for every path from the root. Deep compositions with reused nodes cost exponential time. Repro:
x_{k+1} = x_k + h*(x_k - x_k**3) + h*u_k, objectivex_N**2. The Hessian took 0.09 ms at N=8 and about 5 s at N=18 (roughly 3^N). Forward, Jacobian, Hessian andis_affinewere all affected.Fix
node->forwardis now a wrapper (expr_forward_root) that bumps a thread-local epoch. Atoms recurse throughexpr_forward(child, u), which returns early if the node was already evaluated in this pass. The atom's rule lives in the newforward_implslot.jacobian_evaluatedlatch applies to every node. A forward pass clears it for the non-affine nodes it evaluates;expr_set_needs_refreshclears it as before.wsum_hess_initcounts each call as an in-edge, andeval_wsum_hesssums the incoming weights of a node with more than one in-edge and runs the impl once, on the last call. Earlier callers read zeros. Affine nodes return immediately.neg,scalar_multandquad_formnow fold their factor into the weights they pass to the child. All other atoms already copy or add the child's Hessian unchanged.is_affine. Memoized with a tri-stateaffinityenum inExpr_Workbehind the existing slot (the atom's rule moves tois_affine_impl); this replacesis_affine_cached.Atom signatures, Hessian storage, sparsity patterns and the term1/term2 sums are unchanged.
Behaviour changes to be aware of
wsum_hess_init(node)call must be matched by exactly oneeval_wsum_hess(node, w)per Hessian pass. Atoms do this for each child andproblemdoes it for its roots, so a root that also sits inside another root's tree gets all its in-edges everyproblem_hessiancall. A caller outsideproblemthat inits a node twice but evaluates it once per pass gets a wrong Hessian.check_wsum_hessintests/numerical_diff.cdid exactly that and now inits only when the Hessian is not yet allocated.eval_jacobiantwice at the same point no longer bumpsvalues_version; it bumps again after a new forward pass.test_values_version_non_affineis updated.expr.c(__thread, or__declspec(thread)on MSVC, so the code stays C99). Threads can run forward passes on DAGs that share no nodes, as before.Tests
New
tests/wsum_hess/composite/test_shared_dag.h:neg,scalar_multandmultiply(g, g);quad_form;The full engine suite passes locally, and the SparseDiffPy tests pass against this branch. No sanitizer build was run locally.
Timings
Repro after the fix:
Regression check on diffengine-paper-benchmarks,
spdbackend, base vs this change, runs alternated. Ratio is this change / base, geometric mean of median times over all sizes:Optimal power flow is faster at every size (Hessian about 17%, Jacobian about 8%). The other problems are within noise. Factor-fitting was not measured because its
spdmodel is not written yet.🤖 Generated with Claude Code