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

Fix large dynamic tape issue #509

Merged
merged 3 commits into from
Oct 12, 2022
Merged
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
8 changes: 2 additions & 6 deletions src/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3414,10 +3414,6 @@ nfields(Type::LLVM.VectorType) = size(Type)
nfields(Type::LLVM.ArrayType) = length(Type)
nfields(Type::LLVM.PointerType) = 1

mutable struct EnzymeTape{N, T}
data::NTuple{N, T}
end

mutable struct EnzymeTapeToLoad{T}
vchuravy marked this conversation as resolved.
Show resolved Hide resolved
data::T
end
Expand Down Expand Up @@ -3617,7 +3613,7 @@ function julia_allocator(B, LLVMType, Count, AlignedSize, IsDefault, ZI)
if Count isa LLVM.ConstantInt
N = convert(Int, Count)

ETT = N == 1 ? EnzymeTapeToLoad{TT} : EnzymeTape{N, TT}
ETT = N == 1 ? TT : NTuple{N, TT}
if sizeof(ETT) != N*convert(Int, AlignedSize)
@safe_error "Size of Enzyme tape is incorrect. Please report this issue" ETT sizeof(ETT) TargetSize = N*convert(Int, AlignedSize)
emit_error(B, nothing, "Enzyme: Tape allocation failed.") # TODO: Pick appropriate orig
Expand All @@ -3642,7 +3638,7 @@ function julia_allocator(B, LLVMType, Count, AlignedSize, IsDefault, ZI)
f_apply_type = get_function!(mod, "jl_f_apply_type", generic_FT)


ptr = unsafe_to_pointer(EnzymeTape)
ptr = unsafe_to_pointer(NTuple)

EnzymeTapeT = LLVM.ConstantInt(reinterpret(Int, ptr); ctx)
EnzymeTapeT = LLVM.const_inttoptr(EnzymeTapeT, T_prjlvalue_UT)
Expand Down
25 changes: 25 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1559,3 +1559,28 @@ end
@test c ≈ [1.0, 1.0, 1.0]
@test fres ≈ [1.0, 1.0, 1.0]
end

@testset "Large dynamic tape" begin

function ldynloss(X, Y, ps, bs)
ll = 0.0f0
for (x, y) in zip(X, Y)
yhat = ps * x .+ bs
ll += (yhat[1] - y)^2
end
return ll
end

ps = randn(Float32, (1, 5))
bs = randn(Float32)

X = map(x->rand(Float32, 5), 1:1000)
Y = map(x->rand(Float32), 1:1000)

grads = zero(ps)
for epoch=1:1000
fill!(grads, 0)
autodiff(Reverse, ldtnloss, Const(X), Const(Y), Duplicated(ps, grads), Active(bs))
vchuravy marked this conversation as resolved.
Show resolved Hide resolved
end

end