Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/chainrules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@ function rrule(::typeof(extract_derivative), t::TaylorScalar{T, N},
end

function rrule(::typeof(*), A::AbstractMatrix{S},
t::Vector{TaylorScalar{T, N}}) where {N, S <: Number, T}
t::AbstractVector{TaylorScalar{T, N}}) where {N, S <: Number, T}
project_A = ProjectTo(A)
function gemv_pullback(x̄)
NoTangent(), @thunk(project_A(contract.(x̄, transpose(t)))), @thunk(transpose(A)*x̄)
x̂ = reinterpret(reshape, T, x̄)
t̂ = reinterpret(reshape, T, t)
NoTangent(), @thunk(project_A(transpose(x̂) * t̂)), @thunk(transpose(A)*x̄)
end
return A * t, gemv_pullback
end
Expand Down
10 changes: 5 additions & 5 deletions src/derivative.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ function derivative end
derivative(f, x, Val{order + 1}())
end

@inline function derivative(f, x::V, l::V,
order::Int64) where {V <: AbstractArray{<:Number, 1}}
@inline function derivative(f, x::AbstractVector{T}, l::AbstractVector{S},
order::Int64) where {T <: Number, S <: Number}
derivative(f, x, l, Val{order + 1}())
end

Expand All @@ -29,10 +29,10 @@ end
end

# Need to rewrite like this to help Zygote infer types
make_taylor(t0::T, t1::T, ::Val{N}) where {T, N} = TaylorScalar{T, N}(t0, t1)
make_taylor(t0::T, t1::S, ::Val{N}) where {T, S, N} = TaylorScalar{T, N}(t0, T(t1))

@inline function derivative(f, x::V, l::V,
vN::Val{N}) where {V <: AbstractArray{<:Number, 1}, N}
@inline function derivative(f, x::AbstractVector{T}, l::AbstractVector{S},
vN::Val{N}) where {T <: Number, S <: Number, N}
t = map((t0, t1) -> make_taylor(t0, t1, vN), x, l) # i.e. map(TaylorScalar{T, N}, x, l)
return extract_derivative(f(t), N)
end