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

WIP+RFC add chunked mode #570

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 8 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
16 changes: 16 additions & 0 deletions src/config.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,19 @@ See also: [`frule_via_ad`](@ref), [`RuleConfig`](@ref) and the documentation on
[rule configurations and calling back into AD](@ref config)
"""
function rrule_via_ad end

abstract type ChunkedRuleCapability end
"""
HasChunkedMode

This trait indicates that a `RuleConfig{>:HasChunkedMode}` can perform chunked AD.
"""
struct HasChunkedMode <: ChunkedRuleCapability end

"""
NoChunkedMode

This is the complement to [`HasChunkedMode`](@ref). To avoid ambiguities [`RuleConfig`]s
that do not support chunked AD should be `RuleConfig{>:NoChunkedMode}`.
"""
struct NoChunkedMode <: ChunkedRuleCapability end
28 changes: 27 additions & 1 deletion src/rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,32 @@ function (::typeof(frule_kwfunc))(kws::Any, ::typeof(frule), ::RuleConfig, args.
return frule_kwfunc(kws, frule, args...)
end

struct ProductTangent{P}
partials::P
end
Comment on lines +81 to +83
Copy link
Member

Choose a reason for hiding this comment

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

How much is gained from this kind of chunking? I mean storing several partials in some tuple. For reverse mode, nothing, I think. For forward mode, maybe derivatives_using_output has the same benefits.

The big gain seems to be from making a solid matrix to represent many vectors, and e.g. getting matrix multiplication. So I think the initial design ought to make that work.

Copy link
Member Author

Choose a reason for hiding this comment

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

This design does this (I think). partials here can either be a Tuple{Tuple} or an eachrow(Matrix)

Copy link
Member

Choose a reason for hiding this comment

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

Ok, I guess a vector of vectors can have the same meaning as an EachCol. Just a different path, by dispatch on ProductTangent{<:EachSlice}.


function frule(::RuleConfig{>:HasChunkedMode}, (Δf, Δx...), f, args...)
return frule((Δf, Δx...), f, args...)
end

function frule(::RuleConfig{>:HasChunkedMode}, (Δf, Δx)::Tuple{Any,ProductTangent}, f, args...)
oscardssmith marked this conversation as resolved.
Show resolved Hide resolved
fx = frule((Δf, first(Δx)), args...)[1]
dfx = ProductTangent(map(Δrow->frule((Δf, Δrow), f, args...)[2], Δx))
oscardssmith marked this conversation as resolved.
Show resolved Hide resolved
return (fx, dfx)
end

function rrule(::RuleConfig{>:HasChunkedMode}, f, args...)
y, back = rrule(args...)
return y, ApplyBack(back)
end

struct ApplyBack{F}
back::F
end

(a::ApplyBack)(dy) = a.back(dy)
(a::ApplyBack)(dy::ProductTangent) = ProductTangent(map(a.back, dy.partials)) # or some Tangent recursion?

"""
rrule([::RuleConfig,] f, x...)

Expand Down Expand Up @@ -149,7 +175,7 @@ const NO_RRULE_DOC = """
This is an piece of infastructure supporting opting out of [`rrule`](@ref).
It follows the signature for `rrule` exactly.
A collection of type-tuples is stored in its method-table.
If something has this defined, it means that it must having a must also have a `rrule`,
If something has this defined, it means that it must having a must also have a `rrule`,
defined that returns `nothing`.

!!! warning "Do not overload no_rrule directly"
Expand Down
5 changes: 3 additions & 2 deletions src/tangent_types/thunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,9 @@ Define a [`Thunk`](@ref) wrapping the `expr`, to lazily defer its evaluation.
macro thunk(body)
# Basically `:(Thunk(() -> $(esc(body))))` but use the location where it is defined.
# so we get useful stack traces if it errors.
func = Expr(:->, Expr(:tuple), Expr(:block, __source__, body))
return :(Thunk($(esc(func))))
#func = Expr(:->, Expr(:tuple), Expr(:block, __source__, body))
#return :(Thunk($(esc(func))))
return esc(body)
end

"""
Expand Down