Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid unnecessary keyword arguments check in fallback rrule #308

Merged
merged 2 commits into from
Mar 24, 2021
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
28 changes: 21 additions & 7 deletions src/rule_definition_tools.jl
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Base.Meta

# These are some macros (and supporting functions) to make it easier to define rules.
"""
@scalar_rule(f(x₁, x₂, ...),
Expand Down Expand Up @@ -198,7 +200,7 @@ function scalar_rrule_expr(f, call, setup_stmts, inputs, partials)
end
end

# For context on why this is important, see
# For context on why this is important, see
# https://github.com/JuliaDiff/ChainRulesCore.jl/pull/276
"Declares properly hygenic inputs for propagation expressions"
_propagator_inputs(n) = [esc(gensym(Symbol(:Δ, i))) for i in 1:n]
Expand Down Expand Up @@ -307,11 +309,11 @@ macro non_differentiable(sig_expr)
unconstrained_args = _unconstrain.(constrained_args)

primal_invoke = if !has_vararg
:($(primal_name)($(unconstrained_args...); kwargs...))
:($(primal_name)($(unconstrained_args...)))
else
normal_args = unconstrained_args[1:end-1]
var_arg = unconstrained_args[end]
:($(primal_name)($(normal_args...), $(var_arg)...; kwargs...))
:($(primal_name)($(normal_args...), $(var_arg)...))
end

quote
Expand All @@ -320,11 +322,19 @@ macro non_differentiable(sig_expr)
end
end

"changes `f(x,y)` into `f(x,y; kwargs....)`"
function _with_kwargs_expr(call_expr::Expr)
@assert isexpr(call_expr, :call)
return Expr(
:call, call_expr.args[1], Expr(:parameters, :(kwargs...)), call_expr.args[2:end]...
)
end

function _nondiff_frule_expr(primal_sig_parts, primal_invoke)
return esc(:(
function ChainRulesCore.frule($(gensym(:_)), $(primal_sig_parts...); kwargs...)
# Julia functions always only have 1 output, so return a single DoesNotExist()
return ($primal_invoke, DoesNotExist())
return ($(_with_kwargs_expr(primal_invoke)), DoesNotExist())
end
))
end
Expand All @@ -349,11 +359,15 @@ function _nondiff_rrule_expr(primal_sig_parts, primal_invoke)
Expr(:call, propagator_name(primal_name, :pullback), :_),
Expr(:tuple, DoesNotExist(), Expr(:(...), tup_expr))
)
return esc(:(
function ChainRulesCore.rrule($(primal_sig_parts...); kwargs...)
return esc(quote
Keno marked this conversation as resolved.
Show resolved Hide resolved
# Manually defined kw version to save compiler work. See explanation in rules.jl
function (::Core.kwftype(typeof(rrule)))(kwargs::Any, rrule::typeof(ChainRulesCore.rrule), $(primal_sig_parts...))
return ($(_with_kwargs_expr(primal_invoke)), $pullback_expr)
end
function ChainRulesCore.rrule($(primal_sig_parts...))
return ($primal_invoke, $pullback_expr)
end
))
end)
end


Expand Down
14 changes: 13 additions & 1 deletion src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,16 @@ true

See also: [`frule`](@ref), [`@scalar_rule`](@ref)
"""
rrule(::Any, ::Vararg{Any}; kwargs...) = nothing
rrule(::Any, ::Vararg{Any}) = nothing

# Manual fallback for keyword arguments. Usually this would be generated by
#
# rrule(::Any, ::Vararg{Any}; kwargs...) = nothing
#
# However - the fallback method is so hot that we want to avoid any extra code
# that would be required to have the automatically generated method package up
# the keyword arguments (which the optimizer will throw away, but the compiler
# still has to manually analyze). Manually declare this method with an
# explicitly empty body to save the compiler that work.

(::Core.kwftype(typeof(rrule)))(::Any, ::Any, ::Vararg{Any}) = nothing