|
| 1 | +function Lux.AutoDiffInternalImpl.batched_jacobian_impl( |
| 2 | + f::F, ad::Lux.Training.ReactantBackend, x |
| 3 | +) where {F} |
| 4 | + ad = Utils.normalize_autoenzyme_mode(EnzymeCore.Forward, ad.ad) |
| 5 | + if ADTypes.mode(ad) isa ADTypes.ReverseMode |
| 6 | + return _batched_jacobian_reverse_impl(f, ad, x) |
| 7 | + else |
| 8 | + return _batched_jacobian_forward_impl(f, ad, x) |
| 9 | + end |
| 10 | +end |
| 11 | + |
| 12 | +struct ApplyWithReshape{F,SZ} |
| 13 | + f::F |
| 14 | + sz::SZ |
| 15 | +end |
| 16 | + |
| 17 | +(f::ApplyWithReshape)(x) = f.f(reshape(x, f.sz)) |
| 18 | + |
| 19 | +function (f::ApplyWithReshape)(y, x) |
| 20 | + res = f.f(reshape(x, f.sz)) |
| 21 | + copyto!(y, reshape(res, size(y))) |
| 22 | + return nothing |
| 23 | +end |
| 24 | + |
| 25 | +function _batched_jacobian_reverse_impl(f::F, ad::AutoEnzyme, x::AbstractArray) where {F} |
| 26 | + y = f(x) |
| 27 | + @assert y isa AbstractArray |
| 28 | + if ndims(y) ≤ 1 || size(y, ndims(y)) != size(x, ndims(x)) |
| 29 | + throw(AssertionError("`batched_jacobian` only supports batched outputs \ |
| 30 | + (ndims(y) > 1) && size(y, ndims(y)) == size(x, ndims(x)).")) |
| 31 | + end |
| 32 | + |
| 33 | + f′ = ApplyWithReshape(f, size(x)) |
| 34 | + |
| 35 | + y = Utils.contiguous(reshape(y, :, size(y, ndims(y)))) |
| 36 | + dy = repeat( |
| 37 | + reshape( |
| 38 | + Reactant.promote_to( |
| 39 | + TracedRArray{Reactant.unwrapped_eltype(y),2}, LinearAlgebra.I(size(y, 1)) |
| 40 | + ), |
| 41 | + size(y, 1), |
| 42 | + 1, |
| 43 | + size(y, 1), |
| 44 | + ), |
| 45 | + 1, |
| 46 | + size(y, 2), |
| 47 | + 1, |
| 48 | + ) |
| 49 | + dy = Utils.contiguous(dy) |
| 50 | + |
| 51 | + x = Utils.contiguous(reshape(x, :, size(x, ndims(x)))) |
| 52 | + dx = similar(x, size(x, 1), size(x, 2), size(y, 1)) |
| 53 | + fill!(dx, false) |
| 54 | + |
| 55 | + Enzyme.autodiff( |
| 56 | + ad.mode, |
| 57 | + Utils.annotate_enzyme_function(ad, f′), |
| 58 | + Reactant.StackedBatchDuplicated(y, dy), |
| 59 | + Reactant.StackedBatchDuplicated(x, dx), |
| 60 | + ) |
| 61 | + |
| 62 | + return permutedims(dx, (3, 1, 2)) |
| 63 | +end |
| 64 | + |
| 65 | +function _batched_jacobian_forward_impl(f::F, ad::AutoEnzyme, x::AbstractArray) where {F} |
| 66 | + f′ = ApplyWithReshape(f, size(x)) |
| 67 | + x = Utils.contiguous(reshape(x, :, size(x, ndims(x)))) |
| 68 | + |
| 69 | + bx = repeat( |
| 70 | + reshape( |
| 71 | + Reactant.promote_to( |
| 72 | + TracedRArray{Reactant.unwrapped_eltype(x),2}, LinearAlgebra.I(size(x, 1)) |
| 73 | + ), |
| 74 | + size(x, 1), |
| 75 | + 1, |
| 76 | + size(x, 1), |
| 77 | + ), |
| 78 | + 1, |
| 79 | + size(x, 2), |
| 80 | + 1, |
| 81 | + ) |
| 82 | + bx = Utils.contiguous(bx) |
| 83 | + |
| 84 | + return stack( |
| 85 | + only( |
| 86 | + Enzyme.autodiff( |
| 87 | + ad.mode, |
| 88 | + Utils.annotate_enzyme_function(ad, f′), |
| 89 | + Reactant.StackedBatchDuplicated(x, bx), |
| 90 | + ), |
| 91 | + ); |
| 92 | + dims=2, |
| 93 | + ) |
| 94 | +end |
0 commit comments