Skip to content

@grad_from_chainrules does not support where clauses #320

Closed

Description

@devmotion

@grad_from_chainrules rejects signatures with a where clause:

using ReverseDiff, ChainRulesCore

f(x) = sum(x)
ChainRulesCore.rrule(::typeof(f), x) = f(x), Δ -> (NoTangent(), fill(Δ, size(x)))
ReverseDiff.@grad_from_chainrules f(x::ReverseDiff.TrackedArray{T}) where {T <: AbstractFloat}
ERROR: LoadError: `@grad_from_chainrules` has to be applied to a function signature

Expected: the rule is imported for TrackedArray{<:AbstractFloat}, as with @grad, which accepts where clauses (covered by the f6/f7 tests in test/MacrosTests.jl). Without them you can't import a rule only for tracked arrays of a given element type, or tie the types of several arguments together (e.g. x::TrackedArray{T}, y::AbstractArray{T}).

Cause: the macro only accepts an Expr(:call, ...). A where clause wraps the call in Expr(:where, ...), and the generated method definitions don't carry whereparams through either.

ReverseDiff master (b796032, v1.18.4), Julia 1.13.1.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions