Skip to content

fix: don't write the inline linear SCC solution into b - #168

Open
AJ0070 wants to merge 1 commit into
JuliaComputing:mainfrom
AJ0070:fix/inline-scc-shared-b
Open

AJ0070 wants to merge 1 commit into
JuliaComputing:mainfrom
AJ0070:fix/inline-scc-shared-b

Conversation

@AJ0070

@AJ0070 AJ0070 commented Oct 18, 2026

Copy link
Copy Markdown
Contributor

#160 copies the solution back into b, but codegen emits identical expressions once, so inline linear SCCs with the same b share that buffer and the second solve uses the first one's solution as its right-hand side. The solution now goes into a new array, and the LinearCache is still reused: a 2x2 block's RHS call takes 135 ns vs 122 ns on main (151 ns on 1.20.6).

@ambujone ambujone left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will conflict now (the Enzyme extension landed after you branched), and more importantly it breaks it. ModelingToolkitTearingEnzymeCoreExt.jl is written against numeric_ldiv! overwriting b: the forward rule returns b.val as the primal, augmented_primal returns b.val and snapshots it as x, and the reverse rule solves in place into db. With this change b.val still holds the RHS.

I merged current main into this branch (src merges cleanly) and ran test/enzyme.jl: 13 failures. For instance enzyme_test_loss(x0) returns 193.74 where the reference is 2.3305, and the reverse rule leaves dupb.val == [1, 2, 2.7] (the RHS) instead of the solution.

The shared-b bug is real, but #192 fixes it in safe_ldiv by solving a copy, which leaves numeric_ldiv!'s in-place contract and the Enzyme rules alone and does not add an allocation inside the solve. It may be simplest to let #192 cover the shared-b case; or, if you prefer the "return a new array" contract, updating the Enzyme rules and enzyme.jl in the same PR and adding a ForwardDiff check to the new test.

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.

2 participants