Repository navigation
Fix multiply's and matmul's Hessians when their children share a subexpression - #169
Merged
Merged
Conversation
…xpression Both evaluated the two children's Hessians and summed afterwards, so a node shared by the children could be re-evaluated with other weights before the sum read it. Fill the sum incrementally with sum_n_matrices_fill_zero / sum_n_matrices_fill_add, as add does since #159. Fixes #160. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <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.
Fixes #160.
Summary
multiplyandmatmul(chain-rule path) evaluated both children's Hessians and only then summedC + C^T + H_x + H_ywithsum_4_matrices_fill_values. When the children share a node, evaluating the second child re-evaluates that node with other weights and overwrites the first child's Hessian before the sum reads it.add. The Hessian pattern comes fromsum_n_matrices_allocwith n = 4, and each term is added withsum_n_matrices_fill_addright after it is final (CandC^Tfirst, then each child right after itseval_wsum_hess).problem.c's Lagrangian Hessian already fills incrementally. The variable × variable fast paths evaluate no child Hessians.values_version++. In a single A/B run againstmain(Release), multiply (n = 200k, elementwise children) took 1.18 vs 1.16 ms and chain-rule matmul (exp(X) @ log(Y), 60×60) took 20.5 vs 21.5 ms. That's one run each and within the usual layout noise; no repeated runs were done.sum_4_matrices_*now has no engine callers and can be dropped from the library later.Tests
test_wsum_hess_multiply_shared_subexpr(mul(log x, exp(log x))andmul(exp(log x), log x)) andtest_wsum_hess_matmul_shared_subexpr(log X @ exp(log X)andexp(log X) @ log X, X 2×2) check against the numerical Hessian with one sharedlognode.SP_TRACK_MEMORY=ON) and Release (307).🤖 Generated with Claude Code