Skip to content

Forward and reverse Enzyme tests and rules for linalg#449

Open
kshyatt wants to merge 12 commits into
mainfrom
ksh/enz_linalg
Open

Forward and reverse Enzyme tests and rules for linalg#449
kshyatt wants to merge 12 commits into
mainfrom
ksh/enz_linalg

Conversation

@kshyatt

@kshyatt kshyatt commented Jun 10, 2026

Copy link
Copy Markdown
Member

Trying to make these a little more manageable and pick up the fwd rules where possible

@kshyatt kshyatt requested review from Jutho and lkdvos June 10, 2026 13:30
@codecov

codecov Bot commented Jun 10, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 30.67227% with 165 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
ext/TensorKitEnzymeExt/linalg.jl 0.00% 133 Missing ⚠️
ext/TensorKitEnzymeExt/utility.jl 0.00% 20 Missing ⚠️
ext/TensorKitEnzymeTestUtilsExt.jl 74.35% 10 Missing ⚠️
src/auxiliary/ad.jl 83.33% 2 Missing ⚠️
Files with missing lines Coverage Δ
ext/TensorKitChainRulesCoreExt/tensoroperations.jl 88.99% <100.00%> (+0.91%) ⬆️
ext/TensorKitChainRulesCoreExt/utility.jl 100.00% <ø> (+20.00%) ⬆️
ext/TensorKitEnzymeExt/TensorKitEnzymeExt.jl 100.00% <100.00%> (ø)
ext/TensorKitMooncakeExt/indexmanipulations.jl 96.11% <100.00%> (ø)
ext/TensorKitMooncakeExt/linalg.jl 99.10% <100.00%> (ø)
ext/TensorKitMooncakeExt/tensoroperations.jl 98.01% <100.00%> (ø)
ext/TensorKitMooncakeExt/utility.jl 100.00% <ø> (+28.57%) ⬆️
ext/TensorKitMooncakeExt/vectorinterface.jl 100.00% <100.00%> (ø)
src/TensorKit.jl 13.79% <ø> (ø)
src/auxiliary/ad.jl 83.33% <83.33%> (ø)
... and 3 more
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Comment thread ext/TensorKitEnzymeExt/linalg.jl
@github-actions

github-actions Bot commented Jun 11, 2026

Copy link
Copy Markdown
Contributor

Your PR no longer requires formatting changes. Thank you for your contribution!

@kshyatt kshyatt marked this pull request as draft June 11, 2026 07:18
@kshyatt kshyatt marked this pull request as ready for review June 11, 2026 09:26
@kshyatt

kshyatt commented Jun 12, 2026

Copy link
Copy Markdown
Member Author

The test on 1.12 is passing locally for me! I assume it's getting OOMed or something...

@kshyatt

kshyatt commented Jun 12, 2026

Copy link
Copy Markdown
Member Author

OK, everything looks happy now except the GPU stuff which is unrelated. Are we good to go?

Comment thread ext/TensorKitEnzymeExt/utility.jl Outdated
Comment thread ext/TensorKitEnzymeExt/utility.jl Outdated
Comment thread ext/TensorKitEnzymeExt/linalg.jl Outdated
Comment thread ext/TensorKitEnzymeExt/linalg.jl Outdated
!isa(A, Const) && TK.project_mul!(C.dval, A.dval, B.val, α.val, One())
!isa(B, Const) && TK.project_mul!(C.dval, A.val, B.dval, α.val, One())
end
mul!(C.val, A.val, B.val, α.val, β.val)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I guess another case could be made here for computing AB = A.val * B.val separately, in the case where !isa(α, Const). Also, I don't think we need project_mul! here, since C should be complex as soon as any of A, B, α or β is complex.

Maybe something like this:

Suggested change
mul!(C.val, A.val, B.val, α.val, β.val)
if !isa(C, Const)
scale!(C.dval, β.val)
!isa(β, Const) && add!(C.dval, C.val, β.dval)
!isa(A, Const) && mul!(C.dval, A.dval, B.val, α.val, One())
!isa(B, Const) && mul!(C.dval, A.val, B.dval, α.val, One())
end
if !isa(α, Const) && !isa(C, Const)
if iszero.val) && !iszero.val)
# this is probably quite a common case, so maybe worth specializing
mul!(C.val, A.val, B.val, α.val, β.val)
add!(C.dval, C.val, α.dval / α.val)
else
AB = A.val * B.val
add!(C.val, AB, α.val, β.val)
add!(C.dval, AB, α.dval)
end
else
mul!(C.val, A.val, B.val, α.val, β.val)
end

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Something went wrong, this is a suggestion for the whole function body above.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Also, I don't think we need project_mul! here, since C should be complex as soon as any of A, B, α or β is complex.

Can't you pass in real-valued C and complex valued other arguments if you are using the direct in-place method?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You're only guaranteed C has complex elements if you're using mul (no bang)

There's no forcing of all complex here or here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Tried the suggestion locally and it worked, thanks!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's no forcing, but the function will throw InexactError because setindex! on C will have to convert for any non-real value.
I guess you could in principle come up with an evil case where the eltype of one of the inputs is complex but the values are purely real, which then might work and give issues here?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah exactly, suppose I generate C, A, B all real then get Ac = complex(A) which is of course all "real" valued. But this doesn't need to be solved in the forward rules, this PR has been expanded enough

@lkdvos

lkdvos commented Jun 16, 2026

Copy link
Copy Markdown
Member

Do we think the test failure is a problem with how LRU interacts with Enzyme?

From the stacktrace, I seem to read this as not finding a key, even though being in an if-clause that explicitly checks this: https://github.com/JuliaCollections/LRUCache.jl/blob/1dad9fef75fef51ea1b7e984e5850ad4e374a7e0/src/LRUCache.jl#L172-L175

The really confusing part to me is that it seems to originate from a forward call, which should just be a regular function call, so I'm not sure what is really going on there. I also don't think this can really be a race condition, since 1) I don't think we are multithreading, 2) LRU protects against this?

@kshyatt

kshyatt commented Jun 16, 2026

Copy link
Copy Markdown
Member Author

It also seems to only happen in the CompatCheck tests, not the main ones

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.

3 participants