Skip to content

Add RuleConfig #45

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

Merged
merged 5 commits into from
Sep 11, 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
1 change: 1 addition & 0 deletions src/runtime.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ChainRulesCore
struct DiffractorRuleConfig <: RuleConfig{Union{HasReverseMode,HasForwardsMode}} end

@Base.constprop :aggressive accum(a, b) = a + b
@Base.constprop :aggressive accum(a::Tuple, b::Tuple) = map(accum, a, b)
Expand Down
8 changes: 7 additions & 1 deletion src/stage1/forward.jl
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ first_partial(x::CompositeBundle) = map(first_partial, getfield(x, :tup))

# TODO: Which version do we want in ChainRules?
function my_frule(args::ATB{1}...)
frule(map(first_partial, args), map(primal, args)...)
frule(DiffractorRuleConfig(), map(first_partial, args), map(primal, args)...)
end

# Fast path for some hot cases
Expand Down Expand Up @@ -118,6 +118,12 @@ function (::∂☆internal{1})(args::AbstractTangentBundle{1}...)
end
end

function ChainRulesCore.frule_via_ad(::DiffractorRuleConfig, partials, args...)
bundles = map((p,a) -> TangentBundle{1}(a, (p,)), partials, args)
result = ∂☆internal{1}()(bundles...)
primal(result), first_partial(result)
end

function (::∂☆shuffle{N})(args::AbstractTangentBundle{N}...) where {N}
∂☆p = ∂☆{minus1(N)}()
∂☆p(ZeroBundle{minus1(N)}(my_frule), map(shuffle_down, args)...)
Expand Down
6 changes: 5 additions & 1 deletion src/stage1/generated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ function (::∂⃖{N})(f::T, args...) where {T, N}
if N == 1
# Base case (inlined to avoid ambiguities with manually specified
# higher order rules)
z = rrule(f, args...)
z = rrule(DiffractorRuleConfig(), f, args...)
if z === nothing
return ∂⃖recurse{1}()(f, args...)
end
Expand All @@ -226,6 +226,10 @@ function (::∂⃖{N})(f::T, args...) where {T, N}
end
end

function ChainRulesCore.rrule_via_ad(::DiffractorRuleConfig, f::T, args...) where {T}
∂⃖{1}()(f, args...)
end

@Base.pure function (::∂⃖{1})(::typeof(Core.apply_type), head, args...)
return rrule(Core.apply_type, head, args...)
end
Expand Down
17 changes: 13 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
using Diffractor
using Diffractor: var"'", ∂⃖
using Diffractor: var"'", ∂⃖, DiffractorRuleConfig
using ChainRules
using ChainRulesCore
using ChainRules: ZeroTangent, NoTangent
using ChainRulesCore: ZeroTangent, NoTangent, frule_via_ad, rrule_via_ad
using Symbolics
using LinearAlgebra

Expand Down Expand Up @@ -198,7 +198,16 @@ end

# PR #43
loss(res, z, w) = sum(res.U * Diagonal(res.S) * res.V) + sum(res.S .* w)
x = rand(10, 10)
@test Diffractor.gradient(x->loss(svd(x), x[:,1], x[:,2]), x) isa Tuple{Matrix{Float64}}
x43 = rand(10, 10)
@test Diffractor.gradient(x->loss(svd(x), x[:,1], x[:,2]), x43) isa Tuple{Matrix{Float64}}

# PR # 45 - Calling back into AD from ChainRules
y45, back45 = rrule_via_ad(DiffractorRuleConfig(), x -> log(exp(x)), 2)
@test y45 ≈ 2.0
@test back45(1) == (ZeroTangent(), 1.0)

z45, delta45 = frule_via_ad(DiffractorRuleConfig(), (0,1), x -> log(exp(x)), 2)
@test z45 ≈ 2.0
@test delta45 ≈ 1.0

include("pinn.jl")