Skip to content

Commit

Permalink
add one-arg Duplicated(x)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed Nov 23, 2024
1 parent 5c373fb commit 9e26b67
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
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)

0 comments on commit 9e26b67

Please sign in to comment.