Skip to content

Evaluate shared DAG nodes once per pass - #172

Merged
dance858 merged 6 commits into
mainfrom
dag-shared-nodes
Nov 26, 2026
Merged

dance858 merged 6 commits into
mainfrom
dag-shared-nodes

Conversation

@dance858

@dance858 dance858 commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

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, objective x_N**2. The Hessian took 0.09 ms at N=8 and about 5 s at N=18 (roughly 3^N). Forward, Jacobian, Hessian and is_affine were all affected.

Fix

  • Forward. node->forward is now a wrapper (expr_forward_root) that bumps a thread-local epoch. Atoms recurse through expr_forward(child, u), which returns early if the node was already evaluated in this pass. The atom's rule lives in the new forward_impl slot.
  • Jacobian. The jacobian_evaluated latch applies to every node. A forward pass clears it for the non-affine nodes it evaluates; expr_set_needs_refresh clears it as before.
  • Hessian. A node's Hessian is linear in its weights, so wsum_hess_init counts each call as an in-edge, and eval_wsum_hess sums 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.
  • Rescaling atoms. The scheme needs atoms to add a child's Hessian and never rescale it. neg, scalar_mult and quad_form now 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-state affinity enum in Expr_Work behind the existing slot (the atom's rule moves to is_affine_impl); this replaces is_affine_cached.

Atom signatures, Hessian storage, sparsity patterns and the term1/term2 sums are unchanged.

Behaviour changes to be aware of

  • Every wsum_hess_init(node) call must be matched by exactly one eval_wsum_hess(node, w) per Hessian pass. Atoms do this for each child and problem does it for its roots, so a root that also sits inside another root's tree gets all its in-edges every problem_hessian call. A caller outside problem that inits a node twice but evaluates it once per pass gets a wrong Hessian. check_wsum_hess in tests/numerical_diff.c did exactly that and now inits only when the Hessian is not yet allocated.
  • eval_jacobian twice at the same point no longer bumps values_version; it bumps again after a new forward pass. test_values_version_non_affine is updated.
  • The forward epoch is thread-local and private to 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:

  • the repro chain at N=30 (does not finish without the fix), Jacobian and Hessian against numerical differentiation;
  • a shared child under neg, scalar_mult and multiply(g, g);
  • a shared child under quad_form;
  • a problem where a constraint root is also inside the objective and appears as a constraint twice, compared with the same problem built without sharing.

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:

N forward Jacobian Hessian
8 0.0009 ms 0.0015 ms 0.005 ms
18 0.0026 ms 0.0041 ms 0.033 ms
30 0.0040 ms 0.0083 ms 0.11 ms
300 0.030 ms 0.20 ms 36 ms

Regression check on diffengine-paper-benchmarks, spd backend, base vs this change, runs alternated. Ratio is this change / base, geometric mean of median times over all sizes:

Phase Ratio Runs
setup 1.003 45
gradient 1.001 45
Jacobian 0.968 30
Hessian 0.950 45

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 spd model is not written yet.

🤖 Generated with Claude Code

dance858 and others added 6 commits November 26, 2026 11:48
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>
@dance858
dance858 merged commit e1d46ff into main Nov 26, 2026
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant