|
| 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) = reshape(f.f(reshape(x, f.sz)), :, size(x, ndims(x))) |
| 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 _check_validity_for_batched_jacobian(f::F, x::AbstractArray) where {F} |
| 26 | + y = f(x) |
| 27 | + @assert y isa AbstractArray |
| 28 | + B = size(y, ndims(y)) |
| 29 | + if ndims(y) ≤ 1 || B != size(x, ndims(x)) |
| 30 | + throw(AssertionError("`batched_jacobian` only supports batched outputs \ |
| 31 | + (ndims(y) > 1) && size(y, ndims(y)) == size(x, ndims(x)).")) |
| 32 | + end |
| 33 | + return y, B |
| 34 | +end |
| 35 | + |
| 36 | +function _batched_jacobian_reverse_impl(f::F, ad::AutoEnzyme, x::AbstractArray) where {F} |
| 37 | + y, B = _check_validity_for_batched_jacobian(f, x) |
| 38 | + f′ = ApplyWithReshape(f, size(x)) |
| 39 | + |
| 40 | + y = Utils.contiguous(reshape(y, :, B)) |
| 41 | + dy = Utils.contiguous( |
| 42 | + repeat( |
| 43 | + reshape( |
| 44 | + Reactant.promote_to( |
| 45 | + TracedRArray{Reactant.unwrapped_eltype(y),2}, |
| 46 | + LinearAlgebra.I(size(y, 1)), |
| 47 | + ), |
| 48 | + size(y, 1), |
| 49 | + 1, |
| 50 | + size(y, 1), |
| 51 | + ), |
| 52 | + 1, |
| 53 | + size(y, 2), |
| 54 | + 1, |
| 55 | + ), |
| 56 | + ) |
| 57 | + |
| 58 | + x = Utils.contiguous(reshape(x, :, B)) |
| 59 | + |
| 60 | + # TODO: replace once https://github.com/LuxDL/Lux.jl/issues/1523 is fixed |
| 61 | + #= |
| 62 | + dx = similar(x, size(x, 1), size(x, 2), size(y, 1)) |
| 63 | + fill!(dx, false) |
| 64 | +
|
| 65 | + Enzyme.autodiff( |
| 66 | + ad.mode, |
| 67 | + Utils.annotate_enzyme_function(ad, f′), |
| 68 | + Reactant.StackedBatchDuplicated(y, dy), |
| 69 | + Reactant.StackedBatchDuplicated(x, dx), |
| 70 | + ) |
| 71 | +
|
| 72 | + return permutedims(dx, (3, 1, 2)) |
| 73 | + =# |
| 74 | + |
| 75 | + # Our loop to batch pass should automatically batch this loop and current has better |
| 76 | + # coverage than the above. Though we should fix the above to ensure we never have a |
| 77 | + # loop in the final result. |
| 78 | + dx = similar(x, size(y, 1), size(x, 1), size(x, 2)) |
| 79 | + @trace track_numbers = false for i in 1:size(y, 1) |
| 80 | + dxᵢ = Enzyme.make_zero(x) |
| 81 | + Enzyme.autodiff( |
| 82 | + ad.mode, |
| 83 | + Utils.annotate_enzyme_function(ad, f′), |
| 84 | + Duplicated, |
| 85 | + Duplicated(y, dy[:, :, i]), |
| 86 | + Duplicated(x, dxᵢ), |
| 87 | + ) |
| 88 | + dx[i, :, :] = dxᵢ |
| 89 | + end |
| 90 | + return dx |
| 91 | +end |
| 92 | + |
| 93 | +function _batched_jacobian_forward_impl(f::F, ad::AutoEnzyme, x::AbstractArray) where {F} |
| 94 | + y, B = _check_validity_for_batched_jacobian(f, x) |
| 95 | + y = Utils.contiguous(reshape(y, :, B)) # will be DCEd away |
| 96 | + |
| 97 | + f′ = ApplyWithReshape(f, size(x)) |
| 98 | + x = Utils.contiguous(reshape(x, :, size(x, ndims(x)))) |
| 99 | + |
| 100 | + bx = Utils.contiguous( |
| 101 | + repeat( |
| 102 | + reshape( |
| 103 | + Reactant.promote_to( |
| 104 | + TracedRArray{Reactant.unwrapped_eltype(x),2}, |
| 105 | + LinearAlgebra.I(size(x, 1)), |
| 106 | + ), |
| 107 | + size(x, 1), |
| 108 | + 1, |
| 109 | + size(x, 1), |
| 110 | + ), |
| 111 | + 1, |
| 112 | + size(x, 2), |
| 113 | + 1, |
| 114 | + ), |
| 115 | + ) |
| 116 | + |
| 117 | + # TODO: replace once https://github.com/LuxDL/Lux.jl/issues/1523 is fixed |
| 118 | + # return stack( |
| 119 | + # only( |
| 120 | + # Enzyme.autodiff( |
| 121 | + # ad.mode, |
| 122 | + # Utils.annotate_enzyme_function(ad, f′), |
| 123 | + # Reactant.StackedBatchDuplicated(x, bx), |
| 124 | + # ), |
| 125 | + # ); |
| 126 | + # dims=2, |
| 127 | + # ) |
| 128 | + |
| 129 | + dy = similar(y, size(y, 1), size(x, 1), size(x, 2)) |
| 130 | + @trace track_numbers = false for i in 1:size(x, 1) |
| 131 | + dy[:, i, :] = only( |
| 132 | + Enzyme.autodiff( |
| 133 | + ad.mode, |
| 134 | + Utils.annotate_enzyme_function(ad, f′), |
| 135 | + Duplicated, |
| 136 | + Duplicated(x, bx[:, :, i]), |
| 137 | + ), |
| 138 | + ) |
| 139 | + end |
| 140 | + return dy |
| 141 | +end |
0 commit comments