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

Rules for cumsum #573

Merged
merged 4 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "ChainRules"
uuid = "082447d4-558c-5d27-93f4-14fc19e9eca2"
version = "1.20.1"
version = "1.21"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
31 changes: 31 additions & 0 deletions src/rulesets/Base/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,37 @@ for Config in (RuleConfig, RuleConfig{>:HasReverseMode})
end
end

#####
##### `cumsum`
#####

function frule((_, xdot), ::typeof(cumsum), x::AbstractArray; dims::Integer)
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps an frule for cumsum! here as well?

Copy link
Member Author

Choose a reason for hiding this comment

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

Are forward rlues for mutating functions safe? I haven't thought that through.

Copy link
Member Author

Choose a reason for hiding this comment

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

For reverse mode, #521 has thorny examples.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, I believe so, since the primal and tangent are mutated simultaneously. We already have several frules for mutating functions. See e.g. https://github.com/JuliaDiff/ChainRules.jl/blob/main/src/rulesets/LinearAlgebra/factorization.jl.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, nice.

Then someone should do a big PR adding this to half the Base functions, right? sum! and circshift! et. al.

Copy link
Member Author

Choose a reason for hiding this comment

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

Must we define methods like cumsum!([0,0,0], ZeroTangent())?

Copy link
Member

Choose a reason for hiding this comment

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

Hm, not certain. @oxinabox didn't we decide at some point that it's up to the AD to handle cases where all (co)tangents are zero tangents?

Copy link
Member Author

Choose a reason for hiding this comment

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

I guess we can solve this when or if it comes up.

Copy link
Member

Choose a reason for hiding this comment

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

yeah we expect the AD to handle cases in reverse mode when inputs are all ZeroTangent.
For forwards mode, i would need to think more, but i think even with mutation https://en.wikipedia.org/wiki/Nothing_comes_from_nothing

We can absolutely add fixes as things come up.

return cumsum(x; dims=dims), cumsum(xdot; dims=dims)
end
frule(tang, ::typeof(cumsum), x::AbstractVector) = frule(tang, cumsum, x; dims=1)

function frule((_, ydot, xdot), ::typeof(cumsum!), y::AbstractArray, x::AbstractArray; dims::Integer)
return cumsum!(y, x; dims=dims), cumsum!(ydot, xdot; dims=dims)
end
frule(t, ::typeof(cumsum!), y::AbstractVector, x::AbstractVector) = frule(t, cumsum!, y, x; dims=1)

function rrule(::typeof(cumsum), x::AbstractArray; dims::Integer)
project = ProjectTo(x)
function cumsum_pullback(dy)
step1 = reverse(unthunk(dy); dims=dims)
if ChainRulesCore.is_inplaceable_destination(step1) && VERSION >= v"1.6"
step2 = cumsum!(step1, step1; dims=dims)
step3 = reverse!(step2; dims=dims)
else
step2 = cumsum(step1; dims=dims)
step3 = reverse(step2; dims=dims)
end
return (NoTangent(), project(step3))
end
return cumsum(x; dims=dims), cumsum_pullback
end
rrule(::typeof(cumsum), x::AbstractVector) = rrule(cumsum, x; dims=1)

#####
##### `prod`
#####
Expand Down
15 changes: 15 additions & 0 deletions test/rulesets/Base/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,21 @@ const CFG = ChainRulesTestUtils.ADviaRuleConfig()
end

@testset "Accumulations" begin
@testset "cumsum" begin
v = round.(10 .* randn(9), sigdigits=3)
m = round.(10 .* randn(4, 5), sigdigits=3)
mcabbott marked this conversation as resolved.
Show resolved Hide resolved

# Forward
test_frule(cumsum, v)
test_frule(cumsum, m; fkwargs=(;dims=1))
test_frule(cumsum!, rand(9), v)
test_frule(cumsum!, rand(4, 5), m; fkwargs=(;dims=1))

# Reverse
test_rrule(cumsum, v)
test_rrule(cumsum, v; fkwargs=(;dims=1))
test_rrule(cumsum, m; fkwargs=(;dims=2))
end
@testset "cumprod" begin
v = round.(10 .* randn(9), sigdigits=3)
test_rrule(cumprod, v)
Expand Down