Repository navigation
Fail at the call when a step reads a float parameter from the modifier - #981
Conversation
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
|
This pull request is held for the owner's decision. On the review's points, for whichever route is taken:
Generated by Claude Code |
Try this Pull Request!Option 1: Julia Package ManagerOpen 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 ComposableRecurrencesOption 2: Local CheckoutIf you have the repo locally: git checkout claude/step-parameters-in-theta
julia --project=. -e "using Pkg; Pkg.instantiate()" |
Benchmark comparison vs baseMinimum time per call. Buckets are PR time as a % of base, so lower is faster (🟢 faster, ⚪ within 5%, 🔴 slower). Counts of benchmarks per bucket:
Evaluation — 24 benchmarks (by time change)
AD gradients — 63 benchmarks (by time change)
|
|
📖 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. |
Closes finding 2 of #930: a step that reads a float parameter from the modifier (
m.a, notθ.a) is now anArgumentErrorat 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 asNaN; keep that, or refuse such types when the operator is built.Claude Opus 5.5 [medium]
Finding 2 of #930: a
stepthat reads a float parameter asm.aand notθ.agave a zero reverse gradient with no error, while forward mode was right.What changes
Inside
step,init,step_pullbackandtracethe modifier is passed with each float parameter field replaced by a marker,_InTheta().Arithmetic, comparison or conversion on the marker is an
ArgumentErrorthat 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:
mainArgumentError: 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 θ[215.45][215.45][0.0]Fields that are not floats are those of the modifier, so a
Bool, anIntor a function is still read fromm.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
mainin #993; this branch keepsmain'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.
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 matchSmooth{Float64}.It also breaks every existing step, here and in both consumers.
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::A), alone or in a tuple or named tuple, holds the marker.a::Float64), or bounded byAbstractFloat, cannot hold it and reads asNaN, so a misread givesNaNoutput in every mode and not a wrong gradient.PieceInterface's:stepcheck still covers it.Both limits are stated in the
stepdocstring.Speed
The replaced modifier is built from the fields of
mand 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,Intand function fields, one with a concrete float field andTransform(*, 0.5): the same instruction count in each, and identical text in three; the fourth differs in block labels only._in_stepis inferred and allocates nothing (tested).benchmark/ab.jlagainstorigin/main, casesoverview,bvd_patch,bvd_census,transformandrenewal, 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
overviewrule 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
mainand CImainwas merged in (no rebase).The earlier AD failures were not caused by the marker.
The Mooncake and ReverseDiff (compiled) failures were a
DomainErrorfrom the fixture "Recurrence user copy of the hazard depletion": its pool wasN - seed, negative where a backend prepares at θ zero.mainhas 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
Threadsworkflow's Julia 1.11 jobs fail onmaintoo, 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 onmain's last run. Downgrade Compat timed out once and passed on rerun.What I ran
test/unit/pure_step.jl,trace.jl,builtin_modifiers.jlon the pushed tree: 1,180 pass.transform,dual_ties,adjoints.jl,allocate,truncateand thepool*unit files: 3,498 pass.Pure stepitem intest/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.What I did not run
test/ad, the Reactant suite, JET, the docs build, and anything on Julia 1.11 or 1.12 or a real GPU.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
NaNfor 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.