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

Fix type inference failure in norm on structural matrix #816

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions src/rulesets/LinearAlgebra/norm.jl
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ function rrule(::typeof(norm), x::AbstractArray{<:Number}, p::Real)
end,
# out-of-place versions
@thunk(if isempty(x) || p == 0
zero.(x) .* (zero(y) * zero(real(Δy)))
# Note: post-julia-1.11 the zero.(Diagonal(Float64[;])) .* 0.0)
# only infers down to Union(Diagonal{Float64}, Matrix{Float64})
# rather than Diagonal{Float64}, so we cast it back.
maybe_withsomezeros_rewrap(x, zero.(x) .* (zero(y) * zero(real(Δy))))
elseif p == 2
_norm2_back(x, y, Δy)
elseif p == 1
Expand Down Expand Up @@ -72,7 +75,10 @@ function rrule(::typeof(norm), x::AbstractArray{<:Number})
end
,
@thunk(if isempty(x)
zero.(x) .* (zero(y) * zero(real(Δy)))
# Note: post-julia-1.11 the zero.(Diagonal(Float64[;])) .* 0.0)
# only infers down to Union(Diagonal{Float64}, Matrix{Float64})
# rather than Diagonal{Float64}, so we cast it back.
maybe_withsomezeros_rewrap(x, zero.(x) .* (zero(y) * zero(real(Δy))))
else
_norm2_back(x, y, Δy)
end)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
2 changes: 2 additions & 0 deletions src/rulesets/LinearAlgebra/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ for S in [
:UnitLowerTriangular,
]
@eval withsomezeros_rewrap(::$S, x) = $S(x)
@eval maybe_withsomezeros_rewrap(::$S, x) = $S(x)
end
maybe_withsomezeros_rewrap(::AbstractArray, x) = x

# Bidiagonal, Tridiagonal have more complicated storage.
# AdjOrTransUpperOrUnitUpperTriangular would need adjoint(parent(parent()))
Loading