Skip to content

Commit

Permalink
Merge pull request #108 from JuliaGaussianProcesses/tgf/fix-rrule-fill
Browse files Browse the repository at this point in the history
Fix `rrule` for more than 2 Fill
  • Loading branch information
theogf committed Apr 27, 2023
2 parents fa8b4af + e94f668 commit 7a6dc8f
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "TemporalGPs"
uuid = "e155a3c4-0841-43e1-8b83-a0e4f03cc18f"
authors = ["willtebbutt <wt0881@my.bristol.ac.uk> and contributors"]
version = "0.6.3"
version = "0.6.4"

[deps]
AbstractGPs = "99985d1d-32ba-4be9-9821-2ec096f28918"
Expand Down
17 changes: 11 additions & 6 deletions src/util/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,20 @@ function ChainRulesCore.rrule(config::RuleConfig{>:HasReverseMode}, ::typeof(_ma
return Fill(y_el, axes(x)), _map_Fill_rrule
end

function ChainRulesCore.rrule(config::RuleConfig{>:HasReverseMode}, ::typeof(_map), f, x::Fill, y::Fill)
z_el, back = ChainRulesCore.rrule_via_ad(config, f, x.value, y.value)
# Somehow needed to avoid the _map -> map indirection
function _map(f, xs::Fill...)
all(==(axes(first(xs))), axes.(xs)) || error("All axes should be the same")
Fill(f(FillArrays.getindex_value.(xs)...), axes(first(xs)))
end

function ChainRulesCore.rrule(config::RuleConfig{>:HasReverseMode}, ::typeof(_map), f, xs::Fill...)
z_el, back = ChainRulesCore.rrule_via_ad(config, f, FillArrays.getindex_value.(xs)...)
function _map_Fill_rrule(Δ)
Δf, Δx_el, Δy_el = back(unthunk(Δ).value)
return NoTangent(), Δf, Fill(Δx_el, axes(x)), Fill(Δy_el, axes(x))
Δf, Δxs_el... = back(unthunk(Δ).value)
return NoTangent(), Δf, Fill.(Δxs_el, axes.(xs))...
end
return Fill(z_el, axes(x)), _map_Fill_rrule
return Fill(z_el, axes(first(xs))), _map_Fill_rrule
end

### Same thing for `StructArray`


Expand Down
6 changes: 5 additions & 1 deletion test/util/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,17 @@ include("../test_util.jl")
test_rrule(_map, x -> 2.0 * x, x; check_inferred=false)
test_rrule(ZygoteRuleConfig(), (x,a)-> _map(x -> x * a, x), x, 2.0; check_inferred=false, rrule_f=rrule_via_ad)
end
@testset "_map(f, x1::Fill, x2::Fill)" begin
@testset "_map(f, x::Fill....)" begin
x1 = Fill(randn(3, 4), 3)
x2 = Fill(randn(3, 4), 3)
x3 = Fill(randn(3, 4), 3)

@test _map(+, x1, x2) == _map(+, collect(x1), collect(x2))
test_rrule(_map, +, x1, x2; check_inferred=true)

@test _map(+, x1, x2, x3) == _map(+, collect(x1), collect(x2), collect(x3))
test_rrule(_map, +, x1, x2, x3; check_inferred=true)

fsin(x, y) = sin.(x .* y)
test_rrule(_map, fsin, x1, x2; check_inferred=false)

Expand Down

2 comments on commit 7a6dc8f

@theogf
Copy link
Member Author

@theogf theogf commented on 7a6dc8f Apr 27, 2023

Choose a reason for hiding this comment

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

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

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

Registration pull request created: JuliaRegistries/General/82415

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.6.4 -m "<description of version>" 7a6dc8fd49b7e3ac32fd4a8193f5cedc8c9268ff
git push origin v0.6.4

Please sign in to comment.