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

view fails when new axis is prepended and last axis is selected #53209

Closed
vadim-z opened this issue Feb 6, 2024 · 0 comments · Fixed by #53228
Closed

view fails when new axis is prepended and last axis is selected #53209

vadim-z opened this issue Feb 6, 2024 · 0 comments · Fixed by #53228
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior

Comments

@vadim-z
Copy link

vadim-z commented Feb 6, 2024

Hello,

In 1.10.0 view fails when:

  1. new axis is prepended with [CartesianIndex()]
  2. the last axis is chosen at some value
julia> A = rand(2,3)
2×3 Matrix{Float64}:
 0.562201  0.212601  0.193508
 0.408022  0.978427  0.823557

julia> A[[CartesianIndex()], :, 1]
1×2 Matrix{Float64}:
 0.562201  0.408022

julia> view(A, [CartesianIndex()], :, 1)
ERROR: ArgumentError: number of indices (1) must match the parent dimensionality (2)
Stacktrace:
 [1] check_parent_index_match(parent::Matrix{Float64}, #unused#::Tuple{Bool})
   @ Base .\subarray.jl:43
 [2] check_parent_index_match(parent::Matrix{Float64}, indices::Tuple{Vector{CartesianIndex{0}}, Base.Slice{Base.OneTo{Int64}}})
   @ Base .\subarray.jl:41
 [3] SubArray
   @ .\subarray.jl:21 [inlined]
 [4] SubArray
   @ .\subarray.jl:32 [inlined]
 [5] SubArray
   @ .\subarray.jl:28 [inlined]
 [6] unsafe_view
   @ .\subarray.jl:219 [inlined]
 [7] view(::Matrix{Float64}, ::Vector{CartesianIndex{0}}, ::Function, ::Int64)
   @ Base .\subarray.jl:180
 [8] top-level scope
   @ REPL[11]:1

This shouldn't happen according to the second block of example.

Analysis of code brought the following lines:

julia/base/subarray.jl

Lines 207 to 210 in 94fd312

if length(J) > ndims(A) && J[N+1:end] isa Tuple{Vararg{Int}}
# view([1,2,3], :, 1) does not need to reshape
return unsafe_view(A, J[1:N]...)
end

As we see, the condition holds because the length of J is 3 and ndims(A) is 2 and the last index is integer. Therefore view proceeds to the line 209.

However, because of [CartesianIndex()], the array really needs to be reshaped.

When I explicitly type line 211, it works as expected:

julia> A = rand(2,3)
2×3 Matrix{Float64}:
 0.784263  0.958573   0.869377
 0.619148  0.0324357  0.966875

julia> J = map(i->Base.unalias(A,i), to_indices(A, ([CartesianIndex()], :, 1)))
([CartesianIndex()], Base.Slice(Base.OneTo(2)), 1)

julia> Base.unsafe_view(Base._maybe_reshape_parent(A, Base.index_ndims(J...)), J...)
1×2 view(::Matrix{Float64}, [CartesianIndex()], :, 1) with eltype Float64:
 0.784263  0.619148

With best regards, Vadim Zborovskii

@jishnub jishnub added bug Indicates an unexpected problem or unintended behavior arrays [a, r, r, a, y, s] labels Feb 7, 2024
KristofferC pushed a commit that referenced this issue Feb 26, 2024
KristofferC pushed a commit that referenced this issue Feb 26, 2024
Drvi pushed a commit to RelationalAI/julia that referenced this issue Jun 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arrays [a, r, r, a, y, s] bug Indicates an unexpected problem or unintended behavior
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants