Skip to content

Don't read complex adjoints from their parent's buffer in reductions - #221

Merged
maleadt merged 2 commits into
mainfrom
tb/adjoint-strides
Dec 3, 2026
Merged

maleadt merged 2 commits into
mainfrom
tb/adjoint-strides

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026

Copy link
Copy Markdown
Member

Reductions along dims of a complex adjoint return unconjugated results when ArrayLayouts.jl is loaded (which LazyArrays.jl, BandedMatrices.jl and others depend on):

julia> using CUDA, LazyArrays

julia> A = CuArray(ComplexF32[1+1im 2+2im; 3+3im 4+4im]);

julia> Base.mapreducedim!(identity, +, CUDA.zeros(ComplexF32, 2), A')
2-element CuArray{ComplexF32, 1, CUDACore.DeviceMemory}:
 4.0f0 + 4.0f0im   # should be 4 - 4im
 6.0f0 + 6.0f0im   # should be 6 - 6im

strided_layout treats any source that has strides as a view of its parent's buffer, and reads its elements from there. LinearAlgebra only defines strides for adjoints of real numbers, since the elements of a complex adjoint are the conjugates of the values in memory, but ArrayLayouts.jl defines it for all adjoints. Complex adjoints now take the linear layout regardless of strides, as the comment on strided_layout already intended. Naming Adjoint makes LinearAlgebra a dependency; it is a stdlib in the system image, and GPUArrays already depends on it.

This showed up as order-dependent failures of GPUArrays' mapreducedim! testsuite in CUDA.jl's CI (JuliaGPU/CUDA.jl#3639), where a test loading LazyArrays sometimes ran earlier in the same worker. The new test defines strides for a complex adjoint the way ArrayLayouts does, and fails without the fix (6 unconjugated results); it passes on CUDA with it. Includes a version bump for a patch release.

Reductions along dims read a source that has `strides` over a dense
parent straight from that parent's buffer. LinearAlgebra defines
`strides` for adjoints of real numbers only, since the adjoint of a
complex number is its conjugate, but ArrayLayouts.jl defines it for all
adjoints. With it loaded, reductions of complex adjoints summed the
unconjugated values, e.g. GPUArrays' `mapreducedim!` from `A'`.
@maleadt
maleadt merged commit 678a28b into main Dec 3, 2026
22 of 23 checks passed
@maleadt
maleadt deleted the tb/adjoint-strides branch December 3, 2026 10:48
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