JuliaGPU / JuliaGPU/GPUArrays.jl

Aliasing with `JLArray`s and broadcasting

Open
#716 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Julia
Stars
450
Forks
104
Avg merge
1d 4h
Merged PRs (30d)
10

Description

Bumped into a fun subtle issue where broadcasting with `JLArray`s and views behaves differently from their `Array` counterparts.

The specific case is this:

```julia
julia> using JLArrays

julia> A = -jl(ones(3, 3));

julia> A .*= sign.(view(A, 1:4:9)) # incorrect result!
3×3 JLArray{Float64, 2}:
1.0 -1.0 -1.0
1.0 1.0 -1.0
1.0 1.0 1.0

julia> B = -ones(3, 3);

julia> B .*= sign.(view(B, 1:4:9)) # correct result!
3×3 Matrix{Float64}:
1.0 1.0 1.0
1.0 1.0 1.0
1.0 1.0 1.0
```

From what I understand, the issue here is that broadcasting has some machinery to detect aliasing in its in/outputs, which ensures that the second case (`B`) works (I didn't delve deeply into this, but I think an independent copy is made of the view which then ensures writing into `B` does not alter reading from the view).
This machinery doesn't seem to propagate fully through the view of the `JLArray` though.

This came up in https://github.com/QuantumKitHub/MatrixAlgebraKit.jl/pull/219.
To be honest, I'm not sure if this is really a case of "bug" or just more of a wrong usage from my part, as I actually hadn't intended for the view and the output to share data.
I was more surprised that this wasn't noticed in our tests for `Array`s because there this is caught, while here it isn't.
I mostly just wanted to report this in case this wasn't known.

Contributor guide

No contributing guide indexed for this repository

Research direction

No source files or tests are named in the report. Reproduce the two broadcasting examples with JLArrays and ordinary Arrays, then trace the relevant broadcasting and view behavior to determine where aliasing diverges; done means the intended aliasing behavior is established and covered by a regression test.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.