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

make_zero! for immutable #1958

Open
wants to merge 5 commits 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
9 changes: 8 additions & 1 deletion src/make_zero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ function make_zero_immutable!(prev::T, seen::S)::T where {T<:Tuple,S}
end

function make_zero_immutable!(prev::NamedTuple{a,b}, seen::S)::NamedTuple{a,b} where {a,b,S}
NamedTuple{a,b}(ntuple(Val(length(T.parameters))) do i
NamedTuple{a,b}(ntuple(Val(length(a))) do i
Base.@_inline_meta
make_zero_immutable!(prev[a[i]], seen)
end)
Expand Down Expand Up @@ -384,6 +384,13 @@ end

push!(seen, prev)

# For make_zero!(NamedTuple) we want to recurse and zero out
# the storage
if !Base.ismutabletype(T)
make_zero_immutable!(prev, seen)
return nothing
end

for i = 1:nf
if isdefined(prev, i)
xi = getfield(prev, i)
Expand Down
16 changes: 16 additions & 0 deletions test/abi.jl
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using Enzyme
using Test
using Random

@testset "ABI & Calling convention" begin

Expand Down Expand Up @@ -493,6 +494,21 @@ end
@test dv.y ≈ 0.0
end

@testset "Make Zero!" begin
params = (; q = rand(64), p = rand(64))
dparams = make_zero(params)
@test all(==(0), dparams.q)
@test all(==(0), dparams.p)

rand!(dparams.q)
rand!(dparams.p)

make_zero!(dparams)
@test all(==(0), dparams.q)
@test all(==(0), dparams.p)
end


@testset "Type inference" begin
x = ones(10)
@inferred autodiff(Enzyme.Reverse, abssum, Duplicated(x,x))
Expand Down
Loading