Skip to content

Commit

Permalink
Fix multidim solve (#1754)
Browse files Browse the repository at this point in the history
  • Loading branch information
wsmoses authored Aug 25, 2024
1 parent 0f7dfcc commit 32b7aa2
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/internal_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,8 @@ end
return LinearAlgebra.qr(cache_A, ColumnNorm())
end

@inline onedimensionalize(::Type{T}) where T <: Array = Vector{eltype(T)}

# y=inv(A) B
# dA −= z y^T
# dB += z, where z = inv(A^T) dy
Expand Down Expand Up @@ -371,19 +373,19 @@ function EnzymeRules.augmented_primal(config, func::Const{typeof(\)}, ::Type{RT}

@static if VERSION < v"1.8.0"
UT = Union{
LinearAlgebra.Diagonal{eltype(AT), BT},
LinearAlgebra.Diagonal{eltype(AT), onedimensionalize(BT)},
LinearAlgebra.LowerTriangular{eltype(AT), AT},
LinearAlgebra.UpperTriangular{eltype(AT), AT},
LinearAlgebra.LU{eltype(AT), AT},
LinearAlgebra.QRCompactWY{eltype(AT), AT}
}
else
UT = Union{
LinearAlgebra.Diagonal{eltype(AT), BT},
LinearAlgebra.Diagonal{eltype(AT), onedimensionalize(BT)},
LinearAlgebra.LowerTriangular{eltype(AT), AT},
LinearAlgebra.UpperTriangular{eltype(AT), AT},
LinearAlgebra.LU{eltype(AT), AT, Vector{Int}},
LinearAlgebra.QRPivoted{eltype(AT), AT, BT, Vector{Int}}
LinearAlgebra.QRPivoted{eltype(AT), AT, onedimensionalize(BT), Vector{Int}}
}
end

Expand Down
15 changes: 15 additions & 0 deletions test/internal_rules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,21 @@ end

y = A \ b
@test dA (-z * transpose(y))

# Ensure multi dim doesn't crash
function test2!(A)
A .= A \ [1.0 0;0.0 1.0]
return nothing
end

A = rand(2,2)
dA = [1.0 0.0; 0.0 0.0]

Enzyme.autodiff(
Enzyme.Reverse,
test2!,
Enzyme.Duplicated(A,dA),
)
end

function tr_solv(A, B, uplo, trans, diag, idx)
Expand Down

0 comments on commit 32b7aa2

Please sign in to comment.