Skip to content
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
14 changes: 9 additions & 5 deletions src/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,20 +342,24 @@ function rrule(::typeof(\), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:R
project_B = ProjectTo(B)

Y = A \ B
# Ever since https://github.com/JuliaLang/julia/pull/44358
# we need to use `pinv` rather than `/` to support both the cases of Y being scalar and array
# See also https://github.com/JuliaLang/julia/issues/28827 which would improve this
function backslash_pullback(ȳ)
Ȳ = unthunk(ȳ)
Ati = pinv(A')
∂A = @thunk begin
B̄ = A' \ Ȳ

B̄ = Ati * Ȳ
Ā = -B̄ * Y'
Ā = add!!(Ā, (B - A * Y) * B̄' / A')
Ā = add!!(Ā, A' \ Y * (Ȳ' - B̄'A))
Ā = add!!(Ā, ((B - A * Y) * B̄') * Ati)
Ā = add!!(Ā, Ati * Y * (Ȳ' - B̄'A))
project_A(Ā)
end
∂B = @thunk project_B(A' \ Ȳ)
∂B = @thunk project_B(Ati * Ȳ)
return NoTangent(), ∂A, ∂B
end
return Y, backslash_pullback

end

#####
Expand Down
2 changes: 1 addition & 1 deletion test/rulesets/Base/arraymath.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@testset "arraymath.jl" begin
@testset "inv(::Matrix{$T})" for T in (Float64, ComplexF64)
B = generate_well_conditioned_matrix(T, 3)
if VERSION >= v"1.7"
if v"1.7" <= VERSION < v"1.9"
@gpu test_frule(inv, B)
@gpu test_rrule(inv, B)
else
Expand Down