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

Utilities for splitting and unsplitting mode objects #1979

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
86 changes: 85 additions & 1 deletion lib/EnzymeCore/src/EnzymeCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ Subtype of [`Mode`](@ref) for split reverse mode differentiation, to use in [`au
- [`set_abi`](@ref)
- [`ReverseSplitModified`](@ref), [`ReverseSplitWidth`](@ref)
"""
struct ReverseModeSplit{ReturnPrimal,ReturnShadow,Width,RuntimeActivity,ModifiedBetween,ABI,Holomorphic,ErrIfFuncWritten,ShadowInit} <: Mode{ABI, ErrIfFuncWritten,RuntimeActivity} end
struct ReverseModeSplit{ReturnPrimal,ReturnShadow,RuntimeActivity,Width,ModifiedBetween,ABI,Holomorphic,ErrIfFuncWritten,ShadowInit} <: Mode{ABI, ErrIfFuncWritten,RuntimeActivity} end
gdalle marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Probably keep this the same?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

As explained in this comment, this just fixes a typo that doesn't change functionality but makes the code harder to understand. Semantically, the runtime activity parameter comes before width in all the uses of this struct, even though they are "labelled" wrong here.


"""
const ReverseSplitNoPrimal
Expand Down Expand Up @@ -578,4 +578,88 @@ Return a new mode with its [`ABI`](@ref) set to the chosen type.
"""
function set_abi end

"""
split_mode(::ReverseMode)

Turn a [`ReverseMode`](@ref) object into a [`ReverseModeSplit`](@ref) object while preserving as many of the settings as possible.

This function acts as the identity on a [`ReverseModeSplit`](@ref).

See also [`unsplit_mode`](@ref).
"""
function split_mode(::ReverseMode{
ReturnPrimal,
RuntimeActivity,
ABI,
Holomorphic,
ErrIfFuncWritten
}) where {
ReturnPrimal,
RuntimeActivity,
ABI,
Holomorphic,
ErrIfFuncWritten
}
ReturnShadow = true
Width = 0
gdalle marked this conversation as resolved.
Show resolved Hide resolved
ModifiedBetween = true
ShadowInit = false
gdalle marked this conversation as resolved.
Show resolved Hide resolved
mode_split = ReverseModeSplit{
ReturnPrimal,
ReturnShadow,
RuntimeActivity,
Width,
ModifiedBetween,
ABI,
Holomorphic,
ErrIfFuncWritten,
ShadowInit
}()
return mode_split
end

split_mode(mode::ReverseModeSplit) = mode

"""
unsplit_mode(::ReverseMode)

Turn a [`ReverseModeSplit`](@ref) object into a [`ReverseMode`](@ref) object while preserving as many of the settings as possible.

This function acts as the identity on a [`ReverseMode`](@ref).

See also [`split_mode`](@ref).
"""
function unsplit_mode(::ReverseModeSplit{
gdalle marked this conversation as resolved.
Show resolved Hide resolved
ReturnPrimal,
ReturnShadow,
RuntimeActivity,
Width,
ModifiedBetween,
ABI,
Holomorphic,
ErrIfFuncWritten,
ShadowInit
}) where {
ReturnPrimal,
ReturnShadow,
RuntimeActivity,
Width,
ModifiedBetween,
ABI,
Holomorphic,
ErrIfFuncWritten,
ShadowInit
}
mode_unsplit = ReverseMode{
ReturnPrimal,
RuntimeActivity,
ABI,
Holomorphic,
ErrIfFuncWritten
}()
return mode_unsplit
end

unsplit_mode(mode::ReverseMode) = mode

end # module EnzymeCore
15 changes: 15 additions & 0 deletions lib/EnzymeCore/test/mode_modification.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using EnzymeCore
using EnzymeCore: split_mode, unsplit_mode
using Test

@testset "Split / unsplit mode" begin
@test split_mode(Reverse) == ReverseSplitNoPrimal
@test split_mode(ReverseWithPrimal) == ReverseSplitWithPrimal
@test split_mode(ReverseSplitNoPrimal) == ReverseSplitNoPrimal
@test split_mode(ReverseSplitWithPrimal) == ReverseSplitWithPrimal

@test unsplit_mode(Reverse) == Reverse
@test unsplit_mode(ReverseWithPrimal) == ReverseWithPrimal
gdalle marked this conversation as resolved.
Show resolved Hide resolved
@test unsplit_mode(ReverseSplitNoPrimal) == Reverse
@test unsplit_mode(ReverseSplitWithPrimal) == ReverseWithPrimal
end
4 changes: 3 additions & 1 deletion lib/EnzymeCore/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ y = @view data[3:end]
@test_throws ErrorException Duplicated(d, y)

@test_throws ErrorException Active(data)
@test_throws ErrorException Active(d)
@test_throws ErrorException Active(d)

include("mode_modification.jl")
Loading