Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/interface/canonical.jl
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,27 @@ end

# == The step and its pullback on scalars ==

# A step returns the pair `(v′, s′)`; any other result is said at the call.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@inline _step_pair(m, r::Tuple{Any, Any}) = r
@noinline function _step_pair(m, r)

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

throw(
ArgumentError(
"step(::$(nameof(typeof(m))), θ, v, s) returned a $(typeof(r)); " *
"a step returns the new value and state, (v′, s′), with " *
"nothing as the state of a stateless modifier"
)
)
end

# A stateless modifier's step gets no state and keeps the one it is given.
# The choice is a dispatch, so the step is compiled for the modifier's own
# type on either arm.
@inline _pure_step(m, θ, v, s) = _pure_step(Val(isstateless(m)), m, θ, v, s)
@inline _pure_step(::Val{false}, m, θ, v, s) = @inline step(m, θ, v, s)
@inline function _pure_step(::Val{false}, m, θ, v, s)
return _step_pair(m, @inline step(m, θ, v, s))
end
@inline function _pure_step(::Val{true}, m, θ, v, s)
v′, _ = @inline step(m, θ, v, nothing)
v′, _ = _step_pair(m, @inline step(m, θ, v, nothing))
return v′, s
end

Expand Down
14 changes: 14 additions & 0 deletions test/unit/pure_step.jl
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,20 @@ end
@test_throws "types its arguments after the modifier" r(
1.0; history = [1.0], stop = 2
)
# A step that returns the value alone is said at the call too, for a
# stateless modifier and for one that keeps state.
struct OnlyValue end
CR.isstateless(::OnlyValue) = true
CR.step(::OnlyValue, θ, v, s) = v / 2
struct OnlyValueKept end
CR.init(::OnlyValueKept, θ, history) = 0
CR.step(::OnlyValueKept, θ, v, s) = v / 2
for m in (OnlyValue(), OnlyValueKept())
r = Recurrence([0.5]; modifiers = (m,))
@test_throws "returns the new value and state" r(
1.0; history = [1.0], stop = 2
)
end
end

@testitem "Pure step: a state needs an init the call can see" begin
Expand Down
Loading