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

Add constant_function kwarg to AutoEnzyme #72

Merged
merged 3 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Expand Up @@ -3,7 +3,7 @@ uuid = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
authors = [
"Vaibhav Dixit <vaibhavyashdixit@gmail.com>, Guillaume Dalle and contributors",
]
version = "1.5.4"
version = "1.6.0"

[deps]
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
Expand Down
19 changes: 15 additions & 4 deletions src/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,18 @@ struct AutoDiffractor <: AbstractADType end
mode(::AutoDiffractor) = ForwardOrReverseMode()

"""
AutoEnzyme{M}
AutoEnzyme{M,constant_function}

Struct used to select the [Enzyme.jl](https://github.com/EnzymeAD/Enzyme.jl) backend for automatic differentiation.

Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).

# Constructors

AutoEnzyme(; mode=nothing)
AutoEnzyme(; mode=nothing, constant_function::Bool=false)

The `constant_function` keyword argument (and type parameter) determines whether the function object itself should be considered constant or not during differentiation with Enzyme.jl.
For simple functions, this should usually be set to `false`, but in the case of closures or callable structs which contain differentiated data, it should be set to `true`.
gdalle marked this conversation as resolved.
Show resolved Hide resolved

# Fields

Expand All @@ -56,8 +59,16 @@ Defined by [ADTypes.jl](https://github.com/SciML/ADTypes.jl).
+ an object subtyping `EnzymeCore.Mode` (like `EnzymeCore.Forward` or `EnzymeCore.Reverse`) if a specific mode is required
+ `nothing` to choose the best mode automatically
"""
Base.@kwdef struct AutoEnzyme{M} <: AbstractADType
mode::M = nothing
struct AutoEnzyme{M, constant_function} <: AbstractADType
mode::M
end

function AutoEnzyme(mode::M; constant_function::Bool = false) where {M}
return AutoEnzyme{M, constant_function}(mode)
end

function AutoEnzyme(; mode::M = nothing, constant_function::Bool = false) where {M}
return AutoEnzyme{M, constant_function}(mode)
end

mode(::AutoEnzyme) = ForwardOrReverseMode() # specialized in the extension
Expand Down
14 changes: 10 additions & 4 deletions test/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,25 @@ end
@testset "AutoEnzyme" begin
ad = AutoEnzyme()
@test ad isa AbstractADType
@test ad isa AutoEnzyme{Nothing}
@test ad isa AutoEnzyme{Nothing, false}
@test mode(ad) isa ForwardOrReverseMode
@test ad.mode === nothing

ad = AutoEnzyme(EnzymeCore.Forward; constant_function = true)
@test ad isa AbstractADType
@test ad isa AutoEnzyme{typeof(EnzymeCore.Forward), true}
@test mode(ad) isa ForwardMode
@test ad.mode == EnzymeCore.Forward

ad = AutoEnzyme(; mode = EnzymeCore.Forward)
@test ad isa AbstractADType
@test ad isa AutoEnzyme{typeof(EnzymeCore.Forward)}
@test ad isa AutoEnzyme{typeof(EnzymeCore.Forward), false}
@test mode(ad) isa ForwardMode
@test ad.mode == EnzymeCore.Forward

ad = AutoEnzyme(; mode = EnzymeCore.Reverse)
ad = AutoEnzyme(; mode = EnzymeCore.Reverse, constant_function = true)
@test ad isa AbstractADType
@test ad isa AutoEnzyme{typeof(EnzymeCore.Reverse)}
@test ad isa AutoEnzyme{typeof(EnzymeCore.Reverse), true}
@test mode(ad) isa ReverseMode
@test ad.mode == EnzymeCore.Reverse
end
Expand Down
5 changes: 0 additions & 5 deletions test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ end
@test length(string(sparse_backend1)) < length(string(sparse_backend2))
end

import ADTypes

struct FakeSparsityDetector <: ADTypes.AbstractSparsityDetector end
struct FakeColoringAlgorithm <: ADTypes.AbstractColoringAlgorithm end

for backend in [
# dense
ADTypes.AutoChainRules(; ruleconfig = :rc),
Expand Down
Loading