Repository navigation
Say at the call when a step does not return a pair - #993
Conversation
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
There was a problem hiding this comment.
Adds _step_pair, which makes _pure_step throw an ArgumentError naming the modifier and the returned type when a step does not return a 2-tuple. The check is sound and tested for both the stateless and stateful arms. One suggestion: two other forward step call sites skip the check.
Detail
The check wraps only the two arms of _pure_step (src/interface/canonical.jl:287-294). Other forward calls to a user's step skip it:
- src/interface/trace.jl:218 (
_unit_trace_back) doesv′, s′ = @inline step(m, θ, v, s). - src/adjoints/local.jl:32 and :376 call
stepdirectly. - src/interface/declarations.jl:278 compares
stepresults directly.
I read the pullback paths (_local_grads, canonical.jl:313-320), and they run after the forward pass, so a bad step has already failed. The trace path, and any local-adjoint path that runs without the forward pass, can still destructure a non-pair into the generic 'iteration requires...' or BoundsError-style message. That is the failure this PR sets out to replace. If those paths are reachable without _pure_step running first, route them through _step_pair as well. If they are not, the PR description should not say the problem is solved 'at the call' in general.
The test would fail if the body were removed, because it matches on the message text. It does not cover a step that returns a 3-tuple or a NamedTuple. Those take the same Tuple{Any, Any} dispatch, so adding them would not change coverage in a useful way.
I did not look into the failing Documenter check. Nothing in this diff touches docs.
About this review
Automated first pass by seabbs-review-bot (Claude sonnet), triggered by: first pass. Not a human review. Comment @seabbs-review-bot to ask for another pass: @seabbs any time, the author's agent once it has pushed changes. Add the no-review label to opt this PR out. Ping @seabbs with any questions.
|
|
||
| # == The step and its pullback on scalars == | ||
|
|
||
| # A step returns the pair `(v′, s′)`; any other result is said at the call. |
There was a problem hiding this comment.
suggestion Reword the comment to say why the check exists, and cover or justify the other step call sites.
Why and how to fix
'any other result is said at the call' is vague. Write something like # A step that returns a bare value would otherwise fail on destructuring with an unrelated error. The check only guards _pure_step. _unit_trace_back (src/interface/trace.jl:218) destructures step directly, and src/adjoints/local.jl:32 calls step directly. Either call _step_pair(m, @inline step(m, θ, v, s)) there too, or say in the comment that those paths always follow a checked forward step.
|
|
||
| # A step returns the pair `(v′, s′)`; any other result is said at the call. | ||
| @inline _step_pair(m, r::Tuple{Any, Any}) = r | ||
| @noinline function _step_pair(m, r) |
There was a problem hiding this comment.
suggestion Shorten the error message, and say why the method is @noinline.
Why and how to fix
The message is three clauses long: what was returned, what a step must return, and the nothing state for stateless modifiers. Cut it to "step(::$(nameof(typeof(m))), θ, v, s) must return (v′, s′), not a $(typeof(r)); a stateless modifier returns nothing as s′". @noinline on a throwing path is reasonable, so keep it, but a one-line comment would stop someone removing it as unexplained.
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-returns-a-pair")
using ComposableRecurrencesOption 2: Local CheckoutIf you have the repo locally: git checkout claude/step-returns-a-pair
julia --project=. -e "using Pkg; Pkg.instantiate()" |
Part of finding 9 of #930, split out of #981 so it can merge on its own.
A
stepthat returns the value alone,step(::M, θ, v, s) = v / 2, failed withBoundsError: attempt to access Float64 at index [2]from the destructuring of its result.The call now throws
ArgumentError: step(::M, θ, v, s) returned a Float64; a step returns the new value and state, (v′, s′), with nothing as the state of a stateless modifier.The check is a dispatch on the result's type,
_step_pair(m, r::Tuple{Any, Any}) = r, so a correct step compiles to the code it had: the error method is never reached and is not inlined.What I ran
test/unit/pure_step.jlon this branch (onmainat 6159cbf): 176 pass, with the new assertions on a stateless modifier and on one that keeps state.What I did not run
test/ad, and a timing: the claim on the compiled code rests on the dispatch and on the LLVM comparison made for Fail at the call when a step reads a float parameter from the modifier #981, which included this check.Generated by Claude Code