Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gradient for dot(x,A,y) #261

Merged
merged 8 commits into from
Oct 23, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
17 changes: 17 additions & 0 deletions src/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,23 @@ function rrule(::typeof(dot), x, y)
return dot(x, y), dot_pullback
end

function frule((_, Δx, ΔA, Δy), ::typeof(dot), x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
return dot(x, A, y), dot(Δx, A, y) + dot(x, ΔA, y) + dot(x, A, Δy)
end

function rrule(::typeof(dot), x::AbstractVector, A::AbstractMatrix, y::AbstractVector)
willtebbutt marked this conversation as resolved.
Show resolved Hide resolved
Ay = A * y
z = adjoint(x) * Ay
function dot_pullback(ΔΩ)
dx = @thunk conj(ΔΩ) .* Ay
dA = @thunk ΔΩ .* x .* adjoint(y)
dy = @thunk ΔΩ .* (adjoint(A) * x)
return (NO_FIELDS, dx, dA, dy)
end
dot_pullback(::Zero) = (NO_FIELDS, Zero(), Zero(), Zero())
return z, dot_pullback
end

#####
##### `cross`
#####
Expand Down
8 changes: 8 additions & 0 deletions test/rulesets/LinearAlgebra/dense.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@
frule_test(dot, (x, ẋ), (y, ẏ))
rrule_test(dot, randn(T), (x, x̄), (y, ȳ))
end
@testset "3-arg dot, Array{$T}" for T in (Float64, ComplexF64)
M, N = 3, 4
x, A, y = randn(T, M), randn(T, M,N), randn(T, N)
ẋ, Adot, ẏ = randn(T, M), randn(T, M,N), randn(T, N)
x̄, Abar, ȳ = randn(T, M), randn(T, M,N), randn(T, N)
mcabbott marked this conversation as resolved.
Show resolved Hide resolved
frule_test(dot, (x, ẋ), (A, Adot), (y, ẏ))
rrule_test(dot, randn(T), (x, x̄), (A, Abar), (y, ȳ))
end
end
@testset "cross" begin
@testset "frule" begin
Expand Down