Skip to content

[JLArrays] allow mapreducedim! into wrapper - #754

Open
simeonschaub wants to merge 4 commits into
JuliaGPU:mainfrom
simeonschaub:sds/sum!_transpose
Open

[JLArrays] allow mapreducedim! into wrapper#754
simeonschaub wants to merge 4 commits into
JuliaGPU:mainfrom
simeonschaub:sds/sum!_transpose

Conversation

@simeonschaub

Copy link
Copy Markdown
Member

Previously, e.g. sum!(transpose(v), M) would error

Previously, e.g. `sum!(transpose(v), M)` would error
@simeonschaub

Copy link
Copy Markdown
Member Author

Hmm, this is annoying! Looks like all mapreducedim! implementations in GPU packages return a ReshapedArray with singleton dimensions added, which I'd argue is a bug. Looks like this will need to be fixed in each backend by hand 😢

simeonschaub added a commit to simeonschaub/CUDA.jl that referenced this pull request Jul 29, 2026
Discovered in JuliaGPU/GPUArrays.jl#754. Once
this is approved, I will open PRs for all the other backends as well.
christiangnrd pushed a commit to JuliaGPU/Metal.jl that referenced this pull request Jul 29, 2026
`mapreducedim!` reshapes the output container to match the number of
dimensions of the input, but then returned that reshaped array instead of
the one it was handed. Keep a reference to the original and return that.

Same as JuliaGPU/CUDA.jl#3219, discovered in
JuliaGPU/GPUArrays.jl#754.
luraess pushed a commit to JuliaGPU/AMDGPU.jl that referenced this pull request Jul 29, 2026
`mapreducedim!` reshapes the output container to match the number of
dimensions of the input, but then returned that reshaped array instead of
the one it was handed. Keep a reference to the original and return that.

Same as JuliaGPU/CUDA.jl#3219, discovered in
JuliaGPU/GPUArrays.jl#754.
Previously, all the GPU backends returned a reshaped array here, which
is inconsistent with base and could cause issues with wrapper types.
@christiangnrd

Copy link
Copy Markdown
Member

Can you fix this up into a JLArrays commit and a tests commit and then we won't squash

@simeonschaub

Copy link
Copy Markdown
Member Author

Ok, I can do that! Looks like Metal tests are still failing though, do you have any ideas why?

@christiangnrd

christiangnrd commented Jul 31, 2026

Copy link
Copy Markdown
Member

Ok, I can do that! Looks like Metal tests are still failing though, do you have any ideas why?

It's unrelated to this. You can work around it by using Base.mapreduce!(identity, +, R, A) instead of sum!

Good catch! JuliaGPU/Metal.jl#907

I don't think it makes sense to promote to `Int` if the result is stored
into e.g. an `Int16` array
@simeonschaub

Copy link
Copy Markdown
Member Author

I have pushed a potential fix, which avoids overpromoting in sum! and prod!. Let's see if this works, then I can clean up the commit history

christiangnrd pushed a commit to JuliaGPU/CUDA.jl that referenced this pull request Aug 5, 2026
* don't return `ReshapedArray` from `mapreducedim!`

Discovered in JuliaGPU/GPUArrays.jl#754. Once
this is approved, I will open PRs for all the other backends as well.

* change other return as well

* fix doctest
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.

2 participants