|
| 1 | +using Enzyme, Test |
| 2 | + |
| 3 | +# A function Enzyme differentiates as a callee, or in split mode, moves its |
| 4 | +# stack slots to the heap. On Julia 1.10 (typed pointers) Julia passes such a |
| 5 | +# slot to `jl_object_id_` through an addrspacecast that also changes its pointee |
| 6 | +# type to `i8`. `objectid` of an immutable value that holds references is how |
| 7 | +# `view` reaches it: `unalias`, `mightalias`, then Base's default `dataids`. The |
| 8 | +# moved slot kept its own pointer type there, and the derivative failed LLVM's |
| 9 | +# verifier ("Call parameter type does not match function signature!"). |
| 10 | + |
| 11 | +struct SlotPoint{B, A} <: AbstractVector{Float64} |
| 12 | + base::B |
| 13 | + active::A |
| 14 | +end |
| 15 | +Base.size(p::SlotPoint) = size(p.base) |
| 16 | +Base.IndexStyle(::Type{<:SlotPoint}) = IndexLinear() |
| 17 | +@inline function Base.getindex(p::SlotPoint, i::Int) |
| 18 | + @boundscheck checkbounds(p.base, i) |
| 19 | + return i <= length(p.active) ? @inbounds(p.active[i]) : @inbounds(p.base[i]) |
| 20 | +end |
| 21 | + |
| 22 | +# Holds its arguments in one tuple, a stack slot, as ReactiveKernels' statement |
| 23 | +# wrapper does. |
| 24 | +@noinline @generated slot_call(f::F, args::Vararg{Any, N}) where {F, N} = |
| 25 | + :(f($((:(args[$i]) for i in 1:N)...))) |
| 26 | + |
| 27 | +function slot_sumsq(v) |
| 28 | + t = 0.0 |
| 29 | + for x in v |
| 30 | + t += x * x |
| 31 | + end |
| 32 | + return t |
| 33 | +end |
| 34 | + |
| 35 | +slot_window(active, base) = view(SlotPoint(base, active), 1:length(base)) |
| 36 | + |
| 37 | +slot_view_loss(active, base, rows) = |
| 38 | + slot_sumsq(slot_call(view, slot_window(active, base), rows)) |
| 39 | + |
| 40 | +function slot_copy_loss(active, base, rows) |
| 41 | + out = Vector{Float64}(undef, length(rows)) |
| 42 | + slot_call(copyto!, out, view(slot_window(active, base), rows)) |
| 43 | + return slot_sumsq(out) |
| 44 | +end |
| 45 | + |
| 46 | +# The slot in the function that is differentiated, in split mode. |
| 47 | +function slot_view_direct(active, base, rows) |
| 48 | + args = (slot_window(active, base), rows) |
| 49 | + return slot_sumsq(view(args...)) |
| 50 | +end |
| 51 | + |
| 52 | +function slot_expected(active, base, rows) |
| 53 | + g = zero(active) |
| 54 | + for r in rows |
| 55 | + r <= length(active) && (g[r] += 2 * active[r]) |
| 56 | + end |
| 57 | + return g |
| 58 | +end |
| 59 | + |
| 60 | +@testset "A moved stack slot passed to jl_object_id_" begin |
| 61 | + base = [3.0, 4.0, 2.0, 1.0, -0.5] |
| 62 | + for rows in ([1, 2, 4], [2, 2, 5, 1], Int[]) |
| 63 | + active = [0.5, -1.0] |
| 64 | + expected = slot_expected(active, base, rows) |
| 65 | + primal = slot_sumsq([SlotPoint(base, active)[r] for r in rows]) |
| 66 | + for f in (slot_view_loss, slot_copy_loss) |
| 67 | + dactive = zero(active) |
| 68 | + b = copy(base) |
| 69 | + res = autodiff(ReverseWithPrimal, f, Active, Duplicated(active, dactive), Const(b), Const(rows)) |
| 70 | + @test res[2] ≈ primal |
| 71 | + @test dactive ≈ expected |
| 72 | + @test b == base |
| 73 | + dactive = zero(active) |
| 74 | + autodiff(set_runtime_activity(Reverse), f, Active, Duplicated(active, dactive), Const(b), Const(rows)) |
| 75 | + @test dactive ≈ expected |
| 76 | + end |
| 77 | + |
| 78 | + dactive = zero(active) |
| 79 | + args = (Duplicated(active, dactive), Const(copy(base)), Const(rows)) |
| 80 | + fwd, rev = autodiff_thunk( |
| 81 | + ReverseSplitWithPrimal, Const{typeof(slot_view_direct)}, Active, |
| 82 | + typeof.(args)..., |
| 83 | + ) |
| 84 | + tape, value, _ = fwd(Const(slot_view_direct), args...) |
| 85 | + @test value ≈ primal |
| 86 | + rev(Const(slot_view_direct), args..., 1.0, tape) |
| 87 | + @test dactive ≈ expected |
| 88 | + end |
| 89 | +end |
0 commit comments