Skip to content

Commit

Permalink
Fix batched forward constant get (#2143)
Browse files Browse the repository at this point in the history
* Fix batched forward constant get

* fix

* Update typeunstable.jl

* Update typeunstable.jl

* Update typeunstable.jl

* fixup
  • Loading branch information
wsmoses authored Nov 30, 2024
1 parent 2a24bb5 commit c9c79ac
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
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

2 comments on commit c9c79ac

@wsmoses
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/120420

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.17 -m "<description of version>" c9c79acda8a5f4941e5912d7cd48643cda39d73a
git push origin v0.13.17

Please sign in to comment.