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

LinearAlgebra.norm(x::Union{Transpose, Adjoint}) should default to norm(parent(x)) #49020

Merged
merged 6 commits into from
Mar 22, 2023

Conversation

jondeuce
Copy link
Contributor

Currently, if x is the adjoint or transpose of an AbstractVector, then norm(x) will default to norm(parent(x)). This PR extends this behaviour to include AbstractMatrixs, as well.

I don't think this should be consider breaking. It is true by the definition of norm - which is invariant to permutation and/or conjugation of the underlying array elements - that norm(x) == norm(parent(x)) should hold.

For reference, I ran into this issue in the wild. When x is an adjoint of a CuMatrix, norm(x) falls back to the generic implementation and throws an error. While this could easily be patched in CUDA.jl, I think it is more correct and convenient to have this fallback in Base. Currently, it is not possible to fix this in one's own code without type piracy.

@dkarrasch dkarrasch added the domain:linear algebra Linear algebra label Mar 16, 2023
@dkarrasch
Copy link
Member

Is there any risk that this might be too generic? @andreasnoack @stevengj

@stevengj
Copy link
Member

Needs a test?

@stevengj stevengj added needs tests Unit tests are required for this change needs news A NEWS entry is required for this change labels Mar 17, 2023
@jondeuce
Copy link
Contributor Author

Added some tests for vectors and matrices of common element types, as well as for nested vectors/matrices. Let me know if checking norm(x) ≈ norm(parent(x)) seems sufficient, I can't think of anything else to verify.

@stevengj
Copy link
Member

I would check norm(x) == norm(Matrix(x))

@jondeuce
Copy link
Contributor Author

I would check norm(x) == norm(Matrix(x))

Done 👍

@jondeuce
Copy link
Contributor Author

Looks like tests sometimes fail when using ==. Possibly the iteration order is different for the transposed/adjoint arrays, leading to floating point differences?

@dkarrasch dkarrasch removed the needs tests Unit tests are required for this change label Mar 21, 2023
@dkarrasch
Copy link
Member

Perhaps a short note in the NEWS file to say that norm of AdjOrTrans always falls back to the norm of the parent, and then this is ready to go. I think this is more of a performance issue than an accuracy issue, so isapprox is fine in the tests.

@jondeuce
Copy link
Contributor Author

Great, NEWS file is updated now.

@dkarrasch dkarrasch removed the needs news A NEWS entry is required for this change label Mar 22, 2023
@dkarrasch
Copy link
Member

One failing test is clearly unrelated, the other one (a segmentation fault) occurs in \ in LinearAlgebra/qr in "old" code, so is unrelated to this PR.

@dkarrasch dkarrasch merged commit 70f6f7f into JuliaLang:master Mar 22, 2023
@dkarrasch dkarrasch changed the title LinearAlgebra.norm(x::Union{Transpose, Adjoint}) should default to norm(parent(x)) for matrices LinearAlgebra.norm(x::Union{Transpose, Adjoint}) should default to norm(parent(x)) Mar 22, 2023
@jondeuce jondeuce deleted the patch-2 branch March 22, 2023 17:11
@jondeuce
Copy link
Contributor Author

Regarding the CUDA issue in the original post, do you have any advice on how to incorporate a patch in the meantime?

@maleadt and I were discussing a patch to GPUArrays (e.g. here) along the lines of:

if VERSION < CUTOFF
    for trtype in (:Transpose, :Adjoint)
        @eval LinearAlgebra.norm(x::$trtype{<:Any, <:AbstractGPUArray}, p::Real = 2) = norm(parent(v), p)
    end
end

But I'm not sure what an appropriate cutoff version would be. For example, is there any chance this patch makes it into 1.9?

@maleadt
Copy link
Member

maleadt commented Apr 4, 2023

But I'm not sure what an appropriate cutoff version would be. For example, is there any chance this patch makes it into 1.9?

As this isn't really a bugfix, I think, backporting it to 1.9 is unlikely at this point. To find out the exact cutoff, you can use the contrib/commit-name.sh script, passing along the hash of the commit where this got merged (70f6f7f).

Xnartharax pushed a commit to Xnartharax/julia that referenced this pull request Apr 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants