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 one-arg method Duplicated(x) #2118

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
5 changes: 4 additions & 1 deletion lib/EnzymeCore/src/EnzymeCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,13 @@ Active(i::Integer) = Active(float(i))
Active(ci::Complex{T}) where T <: Integer = Active(float(ci))

"""
Duplicated(x, ∂f_∂x)
Duplicated(x, ∂f_∂x = make_zero(x))

Mark a function argument `x` of [`autodiff`](@ref Enzyme.autodiff) as duplicated, Enzyme will
auto-differentiate in respect to such arguments, with `dx` acting as an
accumulator for gradients (so ``\\partial f / \\partial x`` will be *added to*)
`∂f_∂x`.
If the second argument is not provided, it is created by [`make_zero`](@ref).
"""
struct Duplicated{T} <: Annotation{T}
val::T
Expand All @@ -76,6 +77,8 @@ struct Duplicated{T} <: Annotation{T}
end
end

Duplicated(x) = Duplicated(x, make_zero(x), false)

"""
DuplicatedNoNeed(x, ∂f_∂x)

Expand Down
3 changes: 3 additions & 0 deletions lib/EnzymeCore/test/misc.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,8 @@ d = @view data[2:end]
y = @view data[3:end]
@test_skip @test_throws AssertionError Duplicated(d, y)

dup = Duplicated(data)
@test dup isa Duplicated

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