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 batched forward constant get #2143

Merged
merged 6 commits into from
Nov 30, 2024
Merged
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
15 changes: 14 additions & 1 deletion src/errors.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ function julia_error(
B::LLVM.API.LLVMBuilderRef,
)::LLVM.API.LLVMValueRef
msg = Base.unsafe_string(cstr)
julia_error(msg, val, errtype, data, data2, B)
end

function julia_error(
msg::String,
val::LLVM.API.LLVMValueRef,
errtype::API.ErrorType,
data::Ptr{Cvoid},
data2::LLVM.API.LLVMValueRef,
B::LLVM.API.LLVMBuilderRef,
)::LLVM.API.LLVMValueRef
bt = nothing
ir = nothing
if val != C_NULL
Expand Down Expand Up @@ -331,7 +342,9 @@ function julia_error(
sres
end
shadowres = insert_value!(prevbb, shadowres, res, idx - 1)
push!(created, shadowres)
if shadowres isa LLVM.Instruction
push!(created, shadowres)
end
end
return shadowres
end
Expand Down
11 changes: 9 additions & 2 deletions src/rules/typeunstablerules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,7 @@ function common_jl_getfield_fwd(offset, B, orig, gutils, normalR, shadowR)
shadowres = UndefValue(
LLVM.LLVMType(API.EnzymeGetShadowType(width, value_type(normal))),
)
position!(B, LLVM.Instruction(LLVM.API.LLVMGetNextInstruction(normal)))
for idx = 1:width
shadowres = insert_value!(B, shadowres, normal, idx - 1)
end
Expand Down Expand Up @@ -1534,8 +1535,13 @@ end
end
origops = collect(operands(orig))
width = get_width(gutils)
if !is_constant_value(gutils, origops[1])
shadowin = invert_pointer(gutils, origops[1], B)
if !is_constant_value(gutils, origops[1]) || !get_runtime_activity(gutils)
shadowin = if !is_constant_value(gutils, origops[1])
invert_pointer(gutils, origops[1], B)
else
estr = "Mismatched activity for: " * string(orig) * " const input " *string(origops[1]) * ", differentiable return"
LLVM.Value(julia_error(estr, orig.ref, API.ET_MixedActivityError, gutils.ref, origops[1].ref, B.ref))
end
if width == 1
args = LLVM.Value[
shadowin
Expand Down Expand Up @@ -1565,6 +1571,7 @@ end
shadowres = UndefValue(
LLVM.LLVMType(API.EnzymeGetShadowType(width, value_type(normal))),
)
position!(B, LLVM.Instruction(LLVM.API.LLVMGetNextInstruction(normal)))
for idx = 1:width
shadowres = insert_value!(B, shadowres, normal, idx - 1)
end
Expand Down
22 changes: 21 additions & 1 deletion test/typeunstable.jl
Original file line number Diff line number Diff line change
Expand Up @@ -101,4 +101,24 @@ end
res = Enzyme.autodiff(Forward, toactivepair, BatchDuplicated(2.7f0, (2.0f0, 5.0f0)), BatchDuplicated(3.1, (3.0, 7.0)))
@test res[1][1] ≈ 2.7f0 * 3.0 + 2.0f0 * 3.1
@test res[1][2] ≈ 2.7f0 * 7.0 + 5.0f0 * 3.1
end
end

struct InsFwdNormal1{T<:Real}
σ::T
end

struct InsFwdNormal2{T<:Real}
σ::T
end

insfwdlogpdf(d, x) = d.σ

function insfwdfunc(x)
dists = [InsFwdNormal1{Float64}(1.0), InsFwdNormal2{Float64}(1.0)]
return sum(Base.Fix2(insfwdlogpdf, x), dists)
end

@testset "Forward Batch Constant insertion" begin
res = Enzyme.gradient(Enzyme.Forward, insfwdfunc, [0.5, 0.7])[1]
@test res ≈ [0.0, 0.0]
end
Loading