|
| 1 | + |
| 2 | +""" |
| 3 | +""" |
| 4 | +function autodiff_array_gradient(a,i_to_x,j_to_i=IdentityVector(length(i_to_x))) |
| 5 | + |
| 6 | + i_to_xdual = apply(i_to_x) do x |
| 7 | + cfg = ForwardDiff.GradientConfig(nothing, x, ForwardDiff.Chunk{length(x)}()) |
| 8 | + xdual = cfg.duals |
| 9 | + xdual |
| 10 | + end |
| 11 | + |
| 12 | + j_to_f = to_array_of_functions(a,i_to_xdual,j_to_i) |
| 13 | + j_to_x = reindex(i_to_x,j_to_i) |
| 14 | + |
| 15 | + k = ForwardDiffGradientKernel() |
| 16 | + apply(k,j_to_f,j_to_x) |
| 17 | + |
| 18 | +end |
| 19 | + |
| 20 | +struct ForwardDiffGradientKernel <: Kernel end |
| 21 | + |
| 22 | +function kernel_cache(k::ForwardDiffGradientKernel,f,x) |
| 23 | + cfg = ForwardDiff.GradientConfig(nothing, x, ForwardDiff.Chunk{length(x)}()) |
| 24 | + r = copy(x) |
| 25 | + (r, cfg) |
| 26 | +end |
| 27 | + |
| 28 | +@inline function apply_kernel!(cache,k::ForwardDiffGradientKernel,f,x) |
| 29 | + r, cfg = cache |
| 30 | + @notimplementedif length(r) != length(x) |
| 31 | + ForwardDiff.gradient!(r,f,x,cfg) |
| 32 | + r |
| 33 | +end |
| 34 | + |
| 35 | +""" |
| 36 | +""" |
| 37 | +function autodiff_array_jacobian(a,i_to_x,j_to_i=IdentityVector(length(i_to_x))) |
| 38 | + |
| 39 | + i_to_xdual = apply(i_to_x) do x |
| 40 | + cfg = ForwardDiff.JacobianConfig(nothing, x, ForwardDiff.Chunk{length(x)}()) |
| 41 | + xdual = cfg.duals |
| 42 | + xdual |
| 43 | + end |
| 44 | + |
| 45 | + j_to_f = to_array_of_functions(a,i_to_xdual,j_to_i) |
| 46 | + j_to_x = reindex(i_to_x,j_to_i) |
| 47 | + |
| 48 | + k = ForwardDiffJacobianKernel() |
| 49 | + apply(k,j_to_f,j_to_x) |
| 50 | + |
| 51 | +end |
| 52 | + |
| 53 | +struct ForwardDiffJacobianKernel <: Kernel end |
| 54 | + |
| 55 | +function kernel_cache(k::ForwardDiffJacobianKernel,f,x) |
| 56 | + cfg = ForwardDiff.JacobianConfig(nothing, x, ForwardDiff.Chunk{length(x)}()) |
| 57 | + n = length(x) |
| 58 | + j = zeros(eltype(x),n,n) |
| 59 | + (j, cfg) |
| 60 | +end |
| 61 | + |
| 62 | +@inline function apply_kernel!(cache,k::ForwardDiffJacobianKernel,f,x) |
| 63 | + j, cfg = cache |
| 64 | + @notimplementedif size(j,1) != length(x) |
| 65 | + @notimplementedif size(j,2) != length(x) |
| 66 | + ForwardDiff.jacobian!(j,f,x,cfg) |
| 67 | + j |
| 68 | +end |
| 69 | + |
| 70 | +""" |
| 71 | +""" |
| 72 | +function autodiff_array_hessian(a,i_to_x,j_to_i=IdentityVector(length(i_to_x))) |
| 73 | + agrad = i_to_y -> autodiff_array_gradient(a,i_to_y,j_to_i) |
| 74 | + autodiff_array_jacobian(agrad,i_to_x,j_to_i) |
| 75 | +end |
| 76 | + |
| 77 | +function to_array_of_functions(a,x,ids=IdentityVector(length(x))) |
| 78 | + k = ArrayOfFunctionsKernel(a,x) |
| 79 | + j = IdentityVector(length(ids)) |
| 80 | + apply(k,j) |
| 81 | +end |
| 82 | + |
| 83 | +struct ArrayOfFunctionsKernel{A,X} <: Kernel |
| 84 | + a::A |
| 85 | + x::X |
| 86 | +end |
| 87 | + |
| 88 | +function kernel_cache(k::ArrayOfFunctionsKernel,j) |
| 89 | + xi = testitem(k.x) |
| 90 | + l = length(k.x) |
| 91 | + x = MutableFill(xi,l) |
| 92 | + ax = k.a(x) |
| 93 | + axc = array_cache(ax) |
| 94 | + (ax, x, axc) |
| 95 | +end |
| 96 | + |
| 97 | +@inline function apply_kernel!(cache,k::ArrayOfFunctionsKernel,j) |
| 98 | + ax, x, axc = cache |
| 99 | + @inline function f(xj) |
| 100 | + x.value = xj |
| 101 | + axj = getindex!(axc,ax,j) |
| 102 | + end |
| 103 | + f |
| 104 | +end |
| 105 | + |
| 106 | +mutable struct MutableFill{T} <: AbstractVector{T} |
| 107 | + value::T |
| 108 | + length::Int |
| 109 | +end |
| 110 | + |
| 111 | +Base.size(a::MutableFill) = (a.length,) |
| 112 | + |
| 113 | +@inline Base.getindex(a::MutableFill,i::Integer) = a.value |
| 114 | + |
0 commit comments