Skip to content

Protect from writing into dval if it's === val - #305

Merged
lkdvos merged 5 commits into
mainfrom
ksh/guard
Sep 30, 2026
Merged

lkdvos merged 5 commits into
mainfrom
ksh/guard

Conversation

@kshyatt

@kshyatt kshyatt commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

Enzyme My doc reading ability currently has a strange issue where some objects that should be Const don't appear to be marked that way. Adding a guard here (which should be a pretty cheap check) prevents things from being overwritten until that gets fixed.

@kshyatt
kshyatt requested a review from lkdvos September 23, 2026 11:49
@Jutho

Jutho commented Sep 23, 2026

Copy link
Copy Markdown
Member

So instead of being marked as Const, Enzyme makes these objects such that dval === val? Is that a valid thing to do, like, ever? That in itself seems like a much more serious issue than simply missing a constant value.

@kshyatt

kshyatt commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

I agree, I'm trying to figure out where the problem comes from (I have a MWE) and will file an issue with Enzyme.

@kshyatt

kshyatt commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

Here's the issue EnzymeAD/Enzyme.jl#3623

@kshyatt

kshyatt commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

actually it's a doc issue rather than a real issue, see https://enzymead.github.io/Enzyme.jl/dev/faq/#faq-runtime-activity. So I think we'll still want this because we may need runtime activity turned on for the Enzyme equivalent of rrule_via_ad

@Jutho

Jutho commented Sep 23, 2026

Copy link
Copy Markdown
Member

Ok, so should the check be x isa Const || (EnzymeRules.runtime_activity(config) && x.dval === x.val) as they propose in that linked issue. Or should we have our own helper function for this, as they also propose to make there:
EnzymeAD/Enzyme.jl#3597 (comment)

@kshyatt

kshyatt commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

Ideally the fix would be to have no RA at all, which I'm trying to get set up on my PEPSKit branch. I think perhaps we should get Enzyme.jl to add the helper (I could try to add it) then just use that?

@lkdvos

lkdvos commented Sep 23, 2026

Copy link
Copy Markdown
Member

If you want it fixed already I'd probably propose already adding our own helper and then swapping it out once the upstream version is released, makes it also easier to swap out since it is a single function/signature

@codecov

codecov Bot commented Sep 23, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

Files with missing lines Coverage Δ
...orOperationsEnzymeExt/TensorOperationsEnzymeExt.jl 74.74% <100.00%> (ø)

... and 7 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

lkdvos
lkdvos previously approved these changes Sep 30, 2026
Under runtime activity, Enzyme can pass an argument that is inactive at run
time as a `Duplicated` whose shadow aliases the primal. Replace the inline
`dval !== val` guards with a single helper, `is_inactive(config, x)`, which
checks `x isa Const || (runtime_activity(config) && x.dval === x.val)`, and
apply it to all tensor arguments (A, B, C) in the forward and reverse rules
of `tensorcontract!`, `tensoradd!` and `tensortrace!`. This also skips
copying `C` for `Δβ` when `C` is inactive.

The helper is meant to be replaced by an upstream EnzymeRules equivalent
once available. Add a regression test adapted from EnzymeAD/Enzyme.jl#3623.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@kshyatt

kshyatt commented Sep 30, 2026

Copy link
Copy Markdown
Member Author

New is_inactive lgtm

@lkdvos
lkdvos merged commit b44b1fe into main Sep 30, 2026
11 of 12 checks passed
@lkdvos
lkdvos deleted the ksh/guard branch September 30, 2026 17:19
@lkdvos lkdvos mentioned this pull request Sep 30, 2026
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