Skip to content

Specialize 5-arg Hermitian-Adjoint multiplication #1394

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

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

jishnub
Copy link
Member

@jishnub jishnub commented Jun 28, 2025

This PR computes H::Hermitian * A::Adjoint as (A' * H)'. This often ensures that we take BLAS paths. The 2-arg multiplication was already specialized for StridedMatrix{<:BlasFloat } parents by materializing the Adjoint, but this PR removes this and specializes the 5-arg mul! instead for generic element types. Even if we don't hit BLAS, the change ensures that we iterate in a cache-friendly manner.

However, this PR changes the return type of the 2-arg product from a Matrix{T} to an Adjoint{T, Matrix{T}}

On nightly v"1.13.0-DEV.789"

julia> A = [1 2; 3 4];

julia> Hermitian(A) * A'
2×2 Matrix{Int64}:
  5  11
 10  22

This PR:

julia> Hermitian(A) * A'
2×2 adjoint(::Matrix{Int64}) with eltype Int64:
  5  11
 10  22

In principle, we may copy the result and return a materialized Matrix here.

Some performance improvements:

julia> X = randn(100,100); Y = randn(100,100); Z = copy(X);

julia> @btime Hermitian($X) * $Y';
  34.965 μs (6 allocations: 156.42 KiB) # v"1.13.0-DEV.789"
  23.457 μs (3 allocations: 78.21 KiB) # this PR

julia> @btime mul!($Z', Hermitian($X), $Y', 2.0, 1.0);
  1.311 ms (0 allocations: 0 bytes) # v"1.13.0-DEV.789"
  18.966 μs (0 allocations: 0 bytes) # this PR

julia> XInt, YInt, ZInt = rand(Int,size(X)), rand(Int,size(Y)), rand(Int, size(Z)); # non-BLAS

julia> @btime mul!($ZInt', Hermitian($XInt), $YInt', 2, 1);
  1.031 ms (0 allocations: 0 bytes) # v"1.13.0-DEV.789"
  232.422 μs (0 allocations: 0 bytes) # this PR

Copy link

codecov bot commented Jun 28, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.86%. Comparing base (98a0f15) to head (9129696).

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #1394      +/-   ##
==========================================
- Coverage   93.87%   93.86%   -0.01%     
==========================================
  Files          34       34              
  Lines       15812    15814       +2     
==========================================
+ Hits        14843    14844       +1     
- Misses        969      970       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@jishnub jishnub marked this pull request as draft June 28, 2025 11:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant