Skip to content

Commit

Permalink
fix array (#1884)
Browse files Browse the repository at this point in the history
* fix array

* fix

* Update absint.jl

* fix

* fix

* Fix flake

* sym
  • Loading branch information
wsmoses authored Sep 24, 2024
1 parent 6392179 commit fe41647
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 37 deletions.
98 changes: 62 additions & 36 deletions src/absint.jl
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ function absint(arg::LLVM.Value, partial::Bool = false)
end

function actual_size(@nospecialize(typ2))
if typ2 <: Array || typ2 <: AbstractString
if typ2 <: Array || typ2 <: AbstractString || typ2 <: Symbol
return sizeof(Int)
elseif Base.isconcretetype(typ2)
return sizeof(typ2)
Expand Down Expand Up @@ -359,52 +359,78 @@ function abs_typeof(
end
if byref == GPUCompiler.BITS_REF || byref == GPUCompiler.MUT_REF
dl = LLVM.datalayout(LLVM.parent(LLVM.parent(LLVM.parent(arg))))
if offset === nothing
byref = GPUCompiler.BITS_VALUE
legal = true
typ2 = typ
while actual_size(typ2) != sizeof(dl, value_type(arg))
if fieldcount(typ2) > 0
typ2 = fieldtype(typ, 1)
if !Base.allocatedinline(typ2)
if byref != GPUCompiler.BITS_VALUE
legal = false
break
function should_recurse(typ2, arg_t)
if actual_size(typ2) != sizeof(dl, arg_t)
return true
else
if Base.isconcretetype(typ2)
if fieldcount(typ2) > 0
if actual_size(fieldtype(typ2,1)) == actual_size(fieldtype(typ2, 1))
return true
end
byref = GPUCompiler.MUT_REF
continue
end
end
legal = false
break
end
if legal
return (true, typ2, byref)
return false
end
else
end

byref = GPUCompiler.BITS_VALUE
legal = true

while offset !== nothing && legal
@assert Base.isconcretetype(typ)
seen = false
lasti = 1
for i = 1:fieldcount(typ)
fo = fieldoffset(typ, i)
if fieldoffset(typ, i) == offset
subT = fieldtype(typ, i)
fsize = if i == fieldcount(typ)
sizeof(typ)
else
fieldoffset(typ, i + 1)
end - offset
if fsize == sizeof(dl, value_type(arg))
if Base.isconcretetype(subT) &&
is_concrete_tuple(subT) &&
length(subT.parameters) == 1
subT = subT.parameters[1]
end
if Base.allocatedinline(subT)
return (true, subT, GPUCompiler.BITS_VALUE)
else
return (true, subT, GPUCompiler.MUT_REF)
offset = nothing
typ = fieldtype(typ, i)
if !Base.allocatedinline(typ)
if byref != GPUCompiler.BITS_VALUE
legal = false
end
byref = GPUCompiler.MUT_REF
end
seen = true
break
elseif fieldoffset(typ, i) > offset
offset = offset - fieldoffset(typ, lasti)
typ = fieldtype(typ, lasti)
if !Base.allocatedinline(typ)
legal = false
end
seen = true
break
end

if fo != 0 && fo != fieldoffset(typ, i-1)
lasti = i
end
end
if !seen
legal = false
end
end

typ2 = typ
while should_recurse(typ2, value_type(arg))
if fieldcount(typ2) > 0
typ2 = fieldtype(typ2, 1)
if !Base.allocatedinline(typ2)
if byref != GPUCompiler.BITS_VALUE
legal = false
break
end
byref = GPUCompiler.MUT_REF
continue
end
end
legal = false
break
end
if legal
return (true, typ2, byref)
end
end
elseif legal && if typ <: Ptr && Base.isconcretetype(typ)
Expand Down
2 changes: 1 addition & 1 deletion test/internal_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ end
TM in (Const, Duplicated, BatchDuplicated),
TB in (Const, Duplicated, BatchDuplicated)
are_activities_compatible(Const, TY, TM, TB) || continue
test_reverse(f!, TY, (Y, TY), (M, TM), (B, TB), (_A, Const))
test_reverse(f!, TY, (Y, TY), (M, TM), (B, TB), (_A, Const); atol = 1.0e-5, rtol = 1.0e-5)
end
end
@testset "test through `Adjoint` wrapper (regression test for #1306)" begin
Expand Down
29 changes: 29 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3959,6 +3959,35 @@ function harmonic_f!(inter_list, coords, inters)
return si
end

function invwsumsq(w::AbstractVector, a::AbstractVector)
s = zero(zero(eltype(a)) / zero(eltype(w)))
for i in eachindex(w)
s += abs2(a[i]) / w[i]
end
return s
end

_logpdf(d, x) = invwsumsq(d.Σ.diag, x .- d.μ)

function demo_func(x::Any=transpose([1.5 2.0;]);)
m = [-0.30725218207431315, 0.5492115788562757]
d = (; Σ = LinearAlgebra.Diagonal([1.0, 1.0]), μ = m)
logp = _logpdf(d, reshape(x, (2,)))
return logp
end

demof(x) = demo_func()

@testset "Type checks" begin
x = [0.0, 0.0]
Enzyme.autodiff(
Enzyme.Reverse,
Enzyme.Const(demof),
Enzyme.Active,
Enzyme.Duplicated(x, zero(x)),
)
end

@testset "Decay preservation" begin
inters = [HarmonicAngle(1.0, 0.1), HarmonicAngle(2.0, 0.3)]
inter_list = [1, 3]
Expand Down

2 comments on commit fe41647

@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/115882

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.2 -m "<description of version>" fe41647c22a1e939e7d5ef1ba1d9470833c420a3
git push origin v0.13.2

Please sign in to comment.