|
| 1 | +## Forward Rules |
| 2 | + |
| 3 | +# Note that this is type piracy as the derivative should be NaN for x == y. |
| 4 | +function ChainRulesCore.frule( |
| 5 | + (_, Δx, Δy), d::Distances.Euclidean, x::AbstractVector, y::AbstractVector |
| 6 | +) |
| 7 | + Δ = x - y |
| 8 | + D = sqrt(sum(abs2, Δ)) |
| 9 | + if !iszero(D) |
| 10 | + Δ ./= D |
| 11 | + end |
| 12 | + return D, dot(Δ, Δx) - dot(Δ, Δy) |
| 13 | +end |
| 14 | + |
| 15 | +## Reverse Rules Delta |
| 16 | + |
| 17 | +function ChainRulesCore.rrule(dist::Delta, x::AbstractVector, y::AbstractVector) |
| 18 | + d = dist(x, y) |
| 19 | + function evaluate_pullback(::Any) |
| 20 | + return NO_FIELDS, Zero(), Zero() |
| 21 | + end |
| 22 | + return d, evaluate_pullback |
| 23 | +end |
| 24 | + |
| 25 | +function ChainRulesCore.rrule( |
| 26 | + ::typeof(Distances.pairwise), d::Delta, X::AbstractMatrix, Y::AbstractMatrix; dims=2 |
| 27 | +) |
| 28 | + P = Distances.pairwise(d, X, Y; dims=dims) |
| 29 | + function pairwise_pullback(::AbstractMatrix) |
| 30 | + return NO_FIELDS, NO_FIELDS, Zero(), Zero() |
| 31 | + end |
| 32 | + return P, pairwise_pullback |
| 33 | +end |
| 34 | + |
| 35 | +function ChainRulesCore.rrule( |
| 36 | + ::typeof(Distances.pairwise), d::Delta, X::AbstractMatrix; dims=2 |
| 37 | +) |
| 38 | + P = Distances.pairwise(d, X; dims=dims) |
| 39 | + function pairwise_pullback(::AbstractMatrix) |
| 40 | + return NO_FIELDS, NO_FIELDS, Zero() |
| 41 | + end |
| 42 | + return P, pairwise_pullback |
| 43 | +end |
| 44 | + |
| 45 | +function ChainRulesCore.rrule( |
| 46 | + ::typeof(Distances.colwise), d::Delta, X::AbstractMatrix, Y::AbstractMatrix |
| 47 | +) |
| 48 | + C = Distances.colwise(d, X, Y) |
| 49 | + function colwise_pullback(::AbstractVector) |
| 50 | + return NO_FIELDS, NO_FIELDS, Zero(), Zero() |
| 51 | + end |
| 52 | + return C, colwise_pullback |
| 53 | +end |
| 54 | + |
| 55 | +## Reverse Rules DotProduct |
| 56 | + |
| 57 | +function ChainRulesCore.rrule(dist::DotProduct, x::AbstractVector, y::AbstractVector) |
| 58 | + d = dist(x, y) |
| 59 | + function evaluate_pullback(Δ::Any) |
| 60 | + return NO_FIELDS, Δ .* y, Δ .* x |
| 61 | + end |
| 62 | + return d, evaluate_pullback |
| 63 | +end |
| 64 | + |
| 65 | +function ChainRulesCore.rrule( |
| 66 | + ::typeof(Distances.pairwise), |
| 67 | + d::DotProduct, |
| 68 | + X::AbstractMatrix, |
| 69 | + Y::AbstractMatrix; |
| 70 | + dims=2, |
| 71 | +) |
| 72 | + P = Distances.pairwise(d, X, Y; dims=dims) |
| 73 | + function pairwise_pullback_cols(Δ::AbstractMatrix) |
| 74 | + if dims == 1 |
| 75 | + return NO_FIELDS, NO_FIELDS, Δ * Y, Δ' * X |
| 76 | + else |
| 77 | + return NO_FIELDS, NO_FIELDS, Y * Δ', X * Δ |
| 78 | + end |
| 79 | + end |
| 80 | + return P, pairwise_pullback_cols |
| 81 | +end |
| 82 | + |
| 83 | +function ChainRulesCore.rrule( |
| 84 | + ::typeof(Distances.pairwise), d::DotProduct, X::AbstractMatrix; dims=2 |
| 85 | +) |
| 86 | + P = Distances.pairwise(d, X; dims=dims) |
| 87 | + function pairwise_pullback_cols(Δ::AbstractMatrix) |
| 88 | + if dims == 1 |
| 89 | + return NO_FIELDS, NO_FIELDS, 2 * Δ * X |
| 90 | + else |
| 91 | + return NO_FIELDS, NO_FIELDS, 2 * X * Δ |
| 92 | + end |
| 93 | + end |
| 94 | + return P, pairwise_pullback_cols |
| 95 | +end |
| 96 | + |
| 97 | +function ChainRulesCore.rrule( |
| 98 | + ::typeof(Distances.colwise), d::DotProduct, X::AbstractMatrix, Y::AbstractMatrix |
| 99 | +) |
| 100 | + C = Distances.colwise(d, X, Y) |
| 101 | + function colwise_pullback(Δ::AbstractVector) |
| 102 | + return NO_FIELDS, NO_FIELDS, Δ' .* Y, Δ' .* X |
| 103 | + end |
| 104 | + return C, colwise_pullback |
| 105 | +end |
| 106 | + |
| 107 | +## Reverse Rules Sinus |
| 108 | + |
| 109 | +function ChainRulesCore.rrule(s::Sinus, x::AbstractVector, y::AbstractVector) |
| 110 | + d = x - y |
| 111 | + sind = sinpi.(d) |
| 112 | + abs2_sind_r = abs2.(sind) ./ s.r |
| 113 | + val = sum(abs2_sind_r) |
| 114 | + gradx = twoπ .* cospi.(d) .* sind ./ (s.r .^ 2) |
| 115 | + function evaluate_pullback(Δ::Any) |
| 116 | + return (r=-2Δ .* abs2_sind_r,), Δ * gradx, -Δ * gradx |
| 117 | + end |
| 118 | + return val, evaluate_pullback |
| 119 | +end |
| 120 | + |
| 121 | +## Reverse Rulse SqMahalanobis |
| 122 | + |
| 123 | +function ChainRulesCore.rrule( |
| 124 | + dist::Distances.SqMahalanobis, a::AbstractVector, b::AbstractVector |
| 125 | +) |
| 126 | + d = dist(a, b) |
| 127 | + function SqMahalanobis_pullback(Δ::Real) |
| 128 | + a_b = a - b |
| 129 | + ∂qmat = InplaceableThunk( |
| 130 | + @thunk((a_b * a_b') * Δ), X̄ -> mul!(X̄, a_b, a_b', true, Δ) |
| 131 | + ) |
| 132 | + ∂a = InplaceableThunk( |
| 133 | + @thunk((2 * Δ) * dist.qmat * a_b), X̄ -> mul!(X̄, dist.qmat, a_b, true, 2 * Δ) |
| 134 | + ) |
| 135 | + ∂b = InplaceableThunk( |
| 136 | + @thunk((-2 * Δ) * dist.qmat * a_b), X̄ -> mul!(X̄, dist.qmat, a_b, true, -2 * Δ) |
| 137 | + ) |
| 138 | + return Composite{typeof(dist)}(; qmat=∂qmat), ∂a, ∂b |
| 139 | + end |
| 140 | + return d, SqMahalanobis_pullback |
| 141 | +end |
| 142 | + |
| 143 | +## Reverse Rules for matrix wrappers |
| 144 | + |
| 145 | +function ChainRulesCore.rrule(::Type{<:ColVecs}, X::AbstractMatrix) |
| 146 | + ColVecs_pullback(Δ::Composite) = (NO_FIELDS, Δ.X) |
| 147 | + function ColVecs_pullback(::AbstractVector{<:AbstractVector{<:Real}}) |
| 148 | + return error( |
| 149 | + "Pullback on AbstractVector{<:AbstractVector}.\n" * |
| 150 | + "This might happen if you try to use gradients on the generic `kernelmatrix` or `kernelmatrix_diag`.\n" * |
| 151 | + "To solve this issue overload `kernelmatrix(_diag)` for your kernel for `ColVecs`", |
| 152 | + ) |
| 153 | + end |
| 154 | + return ColVecs(X), ColVecs_pullback |
| 155 | +end |
| 156 | + |
| 157 | +function ChainRulesCore.rrule(::Type{<:RowVecs}, X::AbstractMatrix) |
| 158 | + RowVecs_pullback(Δ::Composite) = (NO_FIELDS, Δ.X) |
| 159 | + function RowVecs_pullback(::AbstractVector{<:AbstractVector{<:Real}}) |
| 160 | + return error( |
| 161 | + "Pullback on AbstractVector{<:AbstractVector}.\n" * |
| 162 | + "This might happen if you try to use gradients on the generic `kernelmatrix` or `kernelmatrix_diag`.\n" * |
| 163 | + "To solve this issue overload `kernelmatrix(_diag)` for your kernel for `RowVecs`", |
| 164 | + ) |
| 165 | + end |
| 166 | + return RowVecs(X), RowVecs_pullback |
| 167 | +end |
0 commit comments