Skip to content

Differentiate a ModelSpec with an intervention under plain Enzyme.Reverse - #1255

Merged
sbfnk merged 3 commits into
mainfrom
issue-1196-enzyme-with-a-modelspec-that-has-an-inte
Nov 25, 2026
Merged

sbfnk merged 3 commits into
mainfrom
issue-1196-enzyme-with-a-modelspec-that-has-an-inte

Conversation

@sbfnk-bot

Copy link
Copy Markdown
Collaborator

Building a ModelSpec inside a differentiated function, with an Isolation or attributes = clinical_presentation(...) captured from outside, failed under plain Enzyme.Reverse with EnzymeRuntimeActivityError. The constant result of _resolved_key, the state key stored in Infectiousness, was written into a struct that also holds the active offspring law. Marking _resolved_key as :foldable folds the call away for a constant name, so the store is no longer seen as constant memory in a differentiable struct.

In test/test_ad.jl, the construction case and the chain_length_distribution case now run on enzyme_bare (plain Enzyme.Reverse from Julia 1.11, as for the other closed-form cases) instead of enzyme_activity. Both failed with EnzymeRuntimeActivityError before the change. In a scratch script, those two cases, a capture-only attributes spec and a capture-only Isolation spec all gave gradient [1.0]. I did not run the full test_ad.jl or Julia 1.10. A name known only at runtime does not fold, so that case is untested.

Left for later:
EnzymeNoTypeError for two RingVaccination doses with requires_dose: Differentiating loglikelihood(ChainSizes(...), ModelSpec(BranchingProcess(Poisson(R[1])); interventions = [...])) in R with plain Enzyme reverse fails with EnzymeNoTypeError inside the differentiated function when the spec has two RingVaccination doses and the second sets requires_dose to a label. It fails the same way with a literal :prime label and with set_runtime_activity, and marking _validate_dose_schedule inactive does not remove it, so it is not a runtime-activity problem and needs its own diagnosis.

This PR closes #1196.

… runtime activity

Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@github-actions

github-actions Bot commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Benchmark comparison vs base

Minimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:

Group 🟢 <50% 🟢 50–75% 🟢 75–95% ⚪ 95–105% 🔴 105–125% 🔴 125–150% 🔴 >150%
Evaluation · · · 19 · · ·
Evaluation — 19 benchmarks (by time change)
Benchmark base PR time memory
simulation: network, live kernel with policy 4.21 ms 4.39 ms ⚪ 1.04× ⚪ 1.0×
simulation: homogeneous 26.79 ms 27.56 ms ⚪ 1.03× ⚪ 1.0×
simulation: network, live kernel 4.07 ms 3.98 ms ⚪ 0.98× ⚪ 1.0×
simulation: homogeneous, susceptibility trait 34.36 ms 33.57 ms ⚪ 0.98× ⚪ 1.0×
simulation: branching process, interventions 15.97 μs 16.14 μs ⚪ 1.01× ⚪ 1.0×
simulation: households 1.02 ms 1.02 ms ⚪ 0.99× ⚪ 1.0×
evaluation: compile_contact_pairs 26.33 μs 26.51 μs ⚪ 1.01× ⚪ 1.0×
simulation: branching process, HeldCapacity with DeferRefused 1.33 s 1.34 s ⚪ 1.01× ⚪ 1.0×
simulation: homogeneous SI, max_time 17.85 ms 17.94 ms ⚪ 1.01× ⚪ 1.0×
simulation: network, shared kernel 164.98 μs 165.28 μs ⚪ 1.0× ⚪ 1.0×
simulation: branching process 1.45 ms 1.44 ms ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonLognormal pdf, large count 37.87 μs 37.92 μs ⚪ 1.0× ⚪ 1.0×
evaluation: pairwise_surv_loglik 24.34 μs 24.3 μs ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonWeibull pdf 51.55 μs 51.5 μs ⚪ 1.0× ⚪ 1.0×
evaluation: PoissonLognormal pdf 36.67 μs 36.64 μs ⚪ 1.0× ⚪ 1.0×
evaluation: chain size loglikelihood 1.96 μs 1.96 μs ⚪ 1.0× —
evaluation: PoissonWeibull pdf, large count 50.58 μs 50.59 μs ⚪ 1.0× ⚪ 1.0×
evaluation: ChainSizes loglikelihood 460.0 ns 460.0 ns ⚪ 1.0× —
evaluation: extinction_probability 891.0 ns 891.0 ns ⚪ 1.0× ⚪ 1.0×
AD gradients — 0 benchmarks (by time change)

none

@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.81%. Comparing base (502164a) to head (b7d223d).
⚠️ Report is 12 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #1255   +/-   ##
=======================================
  Coverage   97.81%   97.81%           
=======================================
  Files         104      104           
  Lines       11806    11806           
=======================================
  Hits        11548    11548           
  Misses        258      258           
Flag Coverage Δ
epibranch-ad 29.48% <100.00%> (ø)
epibranch-analytical 40.62% <100.00%> (ø)
epibranch-engine 52.60% <100.00%> (ø)
epibranch-inference 43.64% <100.00%> (+0.01%) ⬆️
epibranch-interventions 54.12% <100.00%> (ø)
epibranch-population 53.97% <100.00%> (ø)
epihouseholds 96.94% <ø> (ø)
epinetwork 95.40% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

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

sbfnk-bot and others added 2 commits November 25, 2026 12:45
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
Co-authored-by: sbfnk <sebastian.funk@lshtm.ac.uk>
@sbfnk-bot

Copy link
Copy Markdown
Collaborator Author

The changelog check failed because the pull request had no fragment. I added changelog.d/fixed-enzyme-spec-intervention-activity.md, which the changelog assembler reads and lists. No code changed.

@sbfnk-bot
sbfnk-bot marked this pull request as ready for review November 25, 2026 13:34
@sbfnk-bot
sbfnk-bot requested a review from sbfnk as a code owner November 25, 2026 13:34
@sbfnk
sbfnk merged commit b043ec9 into main Nov 25, 2026
29 checks passed
@sbfnk
sbfnk deleted the issue-1196-enzyme-with-a-modelspec-that-has-an-inte branch November 25, 2026 13:51
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.

Enzyme with a ModelSpec that has an intervention needs runtime activity

2 participants