Skip to content

Fail at the call when a step reads a float parameter from the modifier - #981

Merged
seabbs merged 4 commits into
mainfrom
claude/step-parameters-in-theta
Dec 4, 2026
Merged

seabbs merged 4 commits into
mainfrom
claude/step-parameters-in-theta

Conversation

@seabbs-bot

@seabbs-bot seabbs-bot commented Dec 2, 2026 •

Copy link
Copy Markdown
Collaborator

Closes finding 2 of #930: a step that reads a float parameter from the modifier (m.a, not θ.a) is now an ArgumentError at the call, where reverse mode used to give a zero gradient silently.

Needs your decision: a float field declared with a concrete type (a::Float64) cannot hold the marker and reads as NaN; keep that, or refuse such types when the operator is built.

Claude Opus 5.5 [medium]

Finding 2 of #930: a step that reads a float parameter as m.a and not θ.a gave a zero reverse gradient with no error, while forward mode was right.

What changes

Inside step, init, step_pullback and trace the modifier is passed with each float parameter field replaced by a marker, _InTheta().
Arithmetic, comparison or conversion on the marker is an ArgumentError that says to read the parameter from θ.
The mistake now fails in the plain call and under every backend, before any gradient is taken.

The review's reproducer, before and after:

main this branch
plain call runs ArgumentError: a float parameter was read from the modifier inside its step; read it from θ, as θ.a for a field a: the derivative is taken in θ
ForwardDiff [215.45] the same error
Mooncake forward [215.45] the same error
Mooncake reverse [0.0] the same error

Fields that are not floats are those of the modifier, so a Bool, an Int or a function is still read from m.
The registry scenario "Recurrence modifier with Bool and Int fields beside floats" is unchanged, and its gradient agrees with ForwardDiff (checked with an equivalent type under Mooncake reverse).

The check that a step returns a pair, from finding 9, landed on main in #993; this branch keeps main's version and its tests.

Why this design

The review offered three: a modifier with its parameter fields replaced, passing only the type, or a tag.

  • Passing the type (step(::Type{<:Smooth}, θ, v, s)) makes the mistake impossible, and costs every author a less natural signature, with a new trap of its own: ::Type{Smooth} does not match Smooth{Float64}.
    It also breaks every existing step, here and in both consumers.
  • A tag has the same cost and adds a name to the surface.
  • Replacing the fields keeps step(::Smooth, θ, v, s) as it is written today, changes no working step, and leaves the non-float fields readable.

The cost is a rule for what the marker can stand in for:

  • A field whose type is a type parameter of the modifier (a::A), alone or in a tuple or named tuple, holds the marker.
  • A field declared with a concrete float type (a::Float64), or bounded by AbstractFloat, cannot hold it and reads as NaN, so a misread gives NaN output in every mode and not a wrong gradient.
  • A mutable modifier is passed as it is; PieceInterface's :step check still covers it.

Both limits are stated in the step docstring.

Speed

The replaced modifier is built from the fields of m and is dead code once the step is inlined.
The optimised LLVM of a step called through the package was compared with the same step called directly, for a parametric modifier, one with Bool, Int and function fields, one with a concrete float field and Transform(*, 0.5): the same instruction count in each, and identical text in three; the fourth differs in block labels only.
_in_step is inferred and allocates nothing (tested).

benchmark/ab.jl against origin/main, cases overview, bvd_patch, bvd_census, transform and renewal, targets primal and Mooncake reverse: no cell outside its noise band.
I stopped it after one full round (base, candidate, base) as each round took 50 minutes, so the bands are wide, 13% on primal minima and 23% on Mooncake.
The widest cell is the primal overview rule arm at 1.21 on the minimum and 1.17 on the median against a 13% and a 32% band.
That run timed the first commit before its ambiguity fix and without the second commit; the LLVM comparison is of the branch as pushed.

Merge with main and CI

main was merged in (no rebase).
The earlier AD failures were not caused by the marker.
The Mooncake and ReverseDiff (compiled) failures were a DomainError from the fixture "Recurrence user copy of the hazard depletion": its pool was N - seed, negative where a backend prepares at θ zero. main has since fixed the fixture, and on the merged tree it runs at θ zero and under ForwardDiff locally.
The standards failure was a 36-word sentence in docs/src/developer/extending.md, now split.
The Threads workflow's Julia 1.11 jobs fail on main too, since the floor is 1.12 (#1033).
On 394de5c1b, every other job passes, except Mooncake reverse, Enzyme reverse and the macOS test cell, which reach their time limit and are cancelled, as on main's last run. Downgrade Compat timed out once and passed on rerun.

What I ran

  • test/unit/pure_step.jl, trace.jl, builtin_modifiers.jl on the pushed tree: 1,180 pass.
  • Before the second commit, also transform, dual_ties, adjoints.jl, allocate, truncate and the pool* unit files: 3,498 pass.
  • After the merge: every Pure step item in test/unit/pure_step.jl, 209 pass, with new tests for a stateful misread, the local-derivative route, a depletion form, each operator on the marker and a mutable modifier.
  • Aqua's ambiguity and piracy checks, and Runic on the changed files.

What I did not run

  • The AD registry and test/ad, the Reactant suite, JET, the docs build, and anything on Julia 1.11 or 1.12 or a real GPU.
  • A step that misreads inside a GPU kernel: the marker's error path would fail to compile there, which is loud but not the same message.

For the owner

This is a change of behaviour for any step that reads a float field from m: it worked under forward mode and plain AD and is now an error everywhere.
The NaN for concrete float fields is the part to accept or drop; the alternative is to refuse such a type when the operator is built.

This was opened by a bot. Please ping @seabbs for any questions.

Inside step, init, step_pullback and trace a float parameter field of the
modifier holds a marker in place of its value, and arithmetic on it is an
ArgumentError that names θ. A step that reads m.a for θ.a then fails in the
plain call and under every backend, where it used to give a zero reverse
gradient beside a correct forward one. Fields that are not floats, such as
a Bool, an Int or a function, are those of the modifier. A field declared
with a concrete float type cannot hold the marker and reads as NaN.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7izUDKjuoR5m5Ky44oy
A step that returned the value alone failed with a BoundsError from the
destructuring. The call now names the modifier and what a step returns.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WwC7izUDKjuoR5m5Ky44oy
@seabbs-bot
seabbs-bot requested a review from seabbs as a code owner December 2, 2026 10:58
seabbs-review-bot[bot]

This comment was marked as resolved.

@seabbs-review-bot seabbs-review-bot Bot added the llm-reviewed Reviewed by seabbs-review-bot label Dec 2, 2026

Copy link
Copy Markdown
Collaborator Author

This pull request is held for the owner's decision.
The design is compared with two alternatives on #930 (three routes: this one, a step given one object with its parameters already read, and a check when the operator is built), and nothing more is pushed here until that is decided.
The second commit, a step that returns the value alone, is now #993 on its own.

On the review's points, for whichever route is taken:

  • The silent NaN for a concretely typed float field and the pass-through of a mutable modifier are the two holes of this design; the comparison lists both, and its fallback recommendation removes the NaN arm by refusing such a type when the operator is built.
  • The call sites: step, init, step_pullback and trace are called on a modifier at 12 places in src (canonical.jl, trace.jl, adjoints/local.jl) and all 12 pass the replaced copy; the device and threaded loops reach them through _pure_step, _step and _block_step, not directly.
  • The untested paths (the stateful arm, _draw, _init_pass!, _LocalRoute) and the unexercised operator methods on the marker are real gaps, to close if this route is chosen.

Generated by Claude Code

@github-actions

github-actions Bot commented Dec 2, 2026

Copy link
Copy Markdown
Contributor

Try this Pull Request!

Option 1: Julia Package Manager

Open Julia and type:

import Pkg
Pkg.activate(temp=true)
Pkg.add(url="https://github.com/EpiAware/ComposableRecurrences.jl", rev="claude/step-parameters-in-theta")
using ComposableRecurrences

Option 2: Local Checkout

If you have the repo locally:

git checkout claude/step-parameters-in-theta
julia --project=. -e "using Pkg; Pkg.instantiate()"

@github-actions

github-actions Bot commented Dec 3, 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 · · 1 22 1 · ·
ForwardDiff · · · 19 2 · ·
Mooncake reverse · · · 21 · · ·
Enzyme reverse · · · 19 2 · ·
Evaluation — 24 benchmarks (by time change)
Benchmark base PR time memory
Evaluation / Matrix bvd_census T200_L20_S1 1.38 μs 1.52 μs 🔴 1.1× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_primary_k5 T200_L20_S1 1.67 μs 1.56 μs 🟢 0.93× ⚪ 1.0×
Evaluation / Matrix conv_fixed T200_L20_S1 551.0 ns 540.0 ns ⚪ 0.98× ⚪ 1.0×
Convolution body / package 309.4 ns 313.3 ns ⚪ 1.01× —
Evaluation / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 2.99 μs 3.02 μs ⚪ 1.01× ⚪ 1.0×
Convolution body / BLAS axpy per lag 686.9 ns 693.8 ns ⚪ 1.01× —
Evaluation / Matrix bvd_occupancy_kernel T200_L20_S1 5.37 μs 5.42 μs ⚪ 1.01× ⚪ 1.0×
Evaluation / Matrix overview T200_L20_S3 14.02 μs 13.89 μs ⚪ 0.99× ⚪ 1.0×
Evaluation / Matrix strata_mixing T200_L20_S5 21.92 μs 22.11 μs ⚪ 1.01× ⚪ 1.0×
Evaluation / Matrix bvd_patches_call T200_L20_S1 15.39 μs 15.27 μs ⚪ 0.99× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_secondary_k5 T200_L20_S1 2.68 μs 2.66 μs ⚪ 0.99× ⚪ 1.0×
Evaluation / Matrix custom_modifier_pullback T200_L20_S1 3.32 μs 3.34 μs ⚪ 1.01× ⚪ 1.0×
Evaluation / Matrix bvd_patches_call T200_L20_S5 42.8 μs 42.99 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix renewal T200_L20_S1 2.58 μs 2.59 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix custom_modifier T200_L20_S1 3.3 μs 3.29 μs ⚪ 1.0× ⚪ 1.0×
Convolution body / native axpy per lag 408.2 ns 406.9 ns ⚪ 1.0× —
Evaluation / Matrix renewal_depletion T200_L20_S1 6.9 μs 6.88 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix bvd_renewal_call T200_L20_S1 7.17 μs 7.19 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 4.58 μs 4.59 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix delay_fixed T200_L20_S1 600.0 ns 601.0 ns ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix bvd_patch T200_L20_S5 31.86 μs 31.86 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix random_walk T30_L1_S1 70.0 ns 70.0 ns ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix user_depletion T200_L20_S1 6.94 μs 6.94 μs ⚪ 1.0× ⚪ 1.0×
Evaluation / Matrix user_depletion_pullback T200_L20_S1 6.91 μs 6.91 μs ⚪ 1.0× ⚪ 1.0×
AD gradients — 63 benchmarks (by time change)
Benchmark base PR time memory
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / Enzyme reverse 7.34 μs 8.09 μs 🔴 1.1× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / Enzyme reverse 8.14 μs 8.77 μs 🔴 1.08× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / ForwardDiff 460.99 μs 491.02 μs 🔴 1.07× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / ForwardDiff 2.46 μs 2.62 μs 🔴 1.06× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / ForwardDiff 495.59 μs 514.79 μs ⚪ 1.04× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / ForwardDiff 46.03 ms 47.64 ms ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / ForwardDiff 462.12 μs 478.05 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / Enzyme reverse 2.41 μs 2.49 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / Enzyme reverse 10.98 μs 11.33 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / Mooncake reverse 9.21 μs 9.48 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / Enzyme reverse 2.72 μs 2.64 μs ⚪ 0.97× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / Mooncake reverse 55.78 μs 57.26 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / ForwardDiff 326.56 μs 334.9 μs ⚪ 1.03× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / ForwardDiff 586.68 μs 601.04 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / Enzyme reverse 6.96 μs 7.13 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / ForwardDiff 1.52 ms 1.56 ms ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / Mooncake reverse 13.61 μs 13.92 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / ForwardDiff 587.77 μs 600.03 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / ForwardDiff 543.67 μs 554.17 μs ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / ForwardDiff 1.78 ms 1.81 ms ⚪ 1.02× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / Mooncake reverse 25.03 μs 24.66 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / Mooncake reverse 7.74 μs 7.85 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / Mooncake reverse 22.43 μs 22.73 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_primary_k5 T200_L20_S1 / Mooncake reverse 14.36 μs 14.55 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_secondary_k5 T200_L20_S1 / ForwardDiff 580.07 μs 587.76 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / Enzyme reverse 14.59 μs 14.78 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix bvd_census T200_L20_S1 / Enzyme reverse 8.85 μs 8.96 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / Mooncake reverse 19.93 μs 20.16 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / ForwardDiff 500.8 μs 506.58 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / Enzyme reverse 14.17 μs 14.01 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix user_depletion_pullback T200_L20_S1 / Mooncake reverse 19.99 μs 19.78 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix renewal_depletion T200_L20_S1 / Enzyme reverse 13.88 μs 14.02 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / Mooncake reverse 922.0 ns 931.0 ns ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / Mooncake reverse 16.78 μs 16.95 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / ForwardDiff 632.37 μs 638.42 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / Mooncake reverse 157.87 μs 159.18 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / Mooncake reverse 16.82 μs 16.7 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / Enzyme reverse 36.48 μs 36.74 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix custom_modifier T200_L20_S1 / Enzyme reverse 7.93 μs 7.88 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / Enzyme reverse 16.08 μs 16.17 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / Enzyme reverse 65.74 μs 65.35 μs ⚪ 0.99× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / ForwardDiff 6.19 ms 6.22 ms ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / Mooncake reverse 22.0 μs 22.12 μs ⚪ 1.01× ⚪ 1.0×
AD gradients / Matrix conv_fixed T200_L20_S1 / Mooncake reverse 10.33 μs 10.28 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_occupancy_kernel T200_L20_S1 / Enzyme reverse 25.04 μs 25.15 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix delay_fixed T200_L20_S1 / ForwardDiff 432.39 μs 430.86 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / ForwardDiff 14.16 ms 14.21 ms ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / Mooncake reverse 101.43 μs 101.75 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / Mooncake reverse 19.19 μs 19.14 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / Mooncake reverse 123.14 μs 123.46 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_renewal_call T200_L20_S1 / Enzyme reverse 15.58 μs 15.54 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix renewal T200_L20_S1 / Mooncake reverse 12.4 μs 12.43 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_primary_k5 T200_L20_S1 / Mooncake reverse 22.22 μs 22.17 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S5 / Enzyme reverse 110.38 μs 110.62 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patch T200_L20_S5 / Enzyme reverse 83.95 μs 84.12 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix strata_mixing T200_L20_S5 / ForwardDiff 11.83 ms 11.85 ms ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / ForwardDiff 3.04 ms 3.04 ms ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix custom_modifier_pullback T200_L20_S1 / Enzyme reverse 7.77 μs 7.76 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix user_depletion T200_L20_S1 / ForwardDiff 505.9 μs 506.42 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix tv_piecewise_renewal_secondary_k5 T200_L20_S1 / ForwardDiff 855.09 μs 855.84 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix overview T200_L20_S3 / Mooncake reverse 55.35 μs 55.38 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix bvd_patches_call T200_L20_S1 / Enzyme reverse 40.25 μs 40.24 μs ⚪ 1.0× ⚪ 1.0×
AD gradients / Matrix random_walk T30_L1_S1 / Enzyme reverse 180.0 ns 180.0 ns ⚪ 1.0× ⚪ 1.0×

@seabbs-bot seabbs-bot mentioned this pull request Dec 3, 2026
15 tasks done
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

📖 Documentation preview is ready!

View the docs for this PR at: https://EpiAware.github.io/ComposableRecurrences.jl/previews/PR981/

This preview will be updated automatically when you push new commits.

@seabbs
seabbs merged commit d1f52c3 into main Dec 4, 2026
25 of 33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

llm-reviewed Reviewed by seabbs-review-bot

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants