rule reuse consistency checks#1172
Open
AstitvaAggarwal wants to merge 2 commits into
Open
Conversation
yebai
reviewed
May 12, 2026
| end | ||
|
|
||
| # Snapshot of RNG so "Reuse" testset uses identical probe tangents to "Correctness" testset. | ||
| rng_snapshot = deepcopy(rng) |
Member
There was a problem hiding this comment.
It would be better if we could eliminate the need for sharing random seeds.
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.
closes #681 .
When a rule is compiled via
build_frule/build_rrule, the resulting closure is intended to be reusable: you compile once and call many times. However, there was no test enforcing this. A rule whose closure captures mutable state that gets corrupted during a first call would pass all existing tests but silently return wrong derivatives on any subsequent use exactly the class of bug described in DI #678.This PR adds a "Reuse" testset inside
Mooncake.TestUtils.test_ruleto catch this. After the existing "Correctness" testset has run a complete forward+backward pass, "Reuse" callstest_frule_correctness/test_rrule_correctnessa second time with the same compiledfrule/rruleclosure objects. Both correctness functions deep-copy their inputs at entry, so the only shared state between the two testsets is the rule closure itself. If the rule corrupts any mutable state it captures during a first call, the second call will produce wrong derivatives and "Reuse" will fail.RNGsnapshotting - The correctness functions draw random probe tangents from a sharedrng. Without snapshotting, "Reuse" would consume therngstate left over from "Correctness" and generate different probe tangents, causing spurious failures unrelated to rule mutation:BFloat16arithmetic has limited precision (machine epsilon~3.9e-3), so only a narrow range of FD step sizes converges. A different tangent direction can cause all seven candidate step sizes to failisapprox.Functions with domain constraints such as
logpdf(Dirichlet(...), x), which returns-Infwhenxis not on the simplex produceNaNFD estimates if the probe tangent pushes the perturbed input outside the domain. Whether this happens depends on the specific tangent direction drawn.To isolate failures to genuine rule-mutation bugs,
rng_snapshot = deepcopy(rng)is taken before testset "Correctness" and passed to testset "Reuse", ensuring both testsets use identical probe tangents.CI Summary — GitHub Actions
Documentation Preview
Mooncake.jl documentation for PR #1172 is available at:
https://chalk-lab.github.io/Mooncake.jl/previews/PR1172/
Performance
Performance Ratio:
Ratio of time to compute gradient and time to compute function.
Warning: results are very approximate! See here for more context.