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

Fix stride(A, i) for 0-dim inputs #44090

Merged
merged 3 commits into from
Feb 16, 2022
Merged

Conversation

N5N3
Copy link
Member

@N5N3 N5N3 commented Feb 9, 2022

This PR recover the orignal behavior of stride for Union{DenseArray,StridedReshapedArray,StridedReinterpretArray}.
Besides, stride(a::AbstractArray{<:Any, 0}, k) now always return 1 if k >= 1 and strides is defined.

Test added. (Close #44087)

@N5N3 N5N3 added the regression Regression in behavior compared to a previous version label Feb 9, 2022
@dkarrasch dkarrasch added the arrays [a, r, r, a, y, s] label Feb 9, 2022
@mkitti
Copy link
Contributor

mkitti commented Feb 9, 2022

Do we prefer

ndims(A) == 0 ? 1 : sum(st .* size(A))

over

sum(st .* size(A); init = 1)

?

@N5N3
Copy link
Member Author

N5N3 commented Feb 9, 2022

I have no idea. For me we can return any value if k > ndims(A).
But since Strided.jl hardcoded a stride test.
I guess we'd better keep the return as before if possible?

@@ -546,7 +546,7 @@ julia> stride(A,3)
function stride(A::AbstractArray, k::Integer)
st = strides(A)
k ≤ ndims(A) && return st[k]
return sum(st .* size(A))
return ndims(A) == 0 ? 1 : sum(st .* size(A))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems like a very low-level function to be calling broadcast and reduce. Should probably be a loop.

Copy link
Contributor

@mkitti mkitti Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suggest restoring this function:

function _stride(a, i)
    if i > ndims(a)
        return length(a)
    end
    s = 1
    for n = 1:(i-1)
        s *= size(a, n)
    end
    return s
end

function _stride(a, i)
if i > ndims(a)
return length(a)
end
s = 1
for n = 1:(i-1)
s *= size(a, n)
end
return s
end

Copy link
Member Author

@N5N3 N5N3 Feb 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The orginal version doesn't thrown a error for stride(a, -1).
On 1.7, we have

julia> stride([], -1)
1
julia> stride(view([1],1:1), -1)
ERROR: BoundsError: attempt to access Tuple{Int64} at index [-1]
Stacktrace:
 [1] getindex
   @ .\tuple.jl:29 [inlined]
 [2] stride(V::SubArray{Int64, 1, Vector{Int64}, Tuple{UnitRange{Int64}}, true}, d::Int64)
   @ Base .\subarray.jl:358
 [3] top-level scope
   @ REPL[2]:1

I think it's meaningful to keep error messages' consistency. The easist way is to follow the implement in general fallback.
(And they should have no performance difference)

@N5N3 N5N3 added the backport 1.8 Change should be backported to release-1.8 label Feb 16, 2022
@JeffBezanson JeffBezanson merged commit 2d1ea3c into JuliaLang:master Feb 16, 2022
@N5N3 N5N3 deleted the 0dim-stride branch February 16, 2022 16:08
@N5N3 N5N3 restored the 0dim-stride branch February 16, 2022 16:08
@mkitti
Copy link
Contributor

mkitti commented Feb 16, 2022

@JeffBezanson , @N5N3 . I'm confused. I get a distinct result with this new implementation.

Julia 1.7.2:

julia> stride(zeros(5,3,2),1)
1

julia> stride(zeros(5,3,2),2)
5

julia> stride(zeros(5,3,2),3)
15

julia> stride(zeros(5,3,2),4)
30

This function:

julia> function new_stride(A::AbstractArray, k::Integer)
           st = strides(A)
           k  ndims(A) && return st[k]
           ndims(A) == 0 && return 1
           sz = size(A)
           s = st[1] * sz[1]
           for i in 2:ndims(A)
               s += st[i] * sz[i]
           end
           return s
       end
new_stride (generic function with 1 method)

julia> new_stride(zeros(5,3,2),1)
1

julia> new_stride(zeros(5,3,2),2)
5

julia> new_stride(zeros(5,3,2),3)
15

julia> new_stride(zeros(5,3,2),4)
50

Why are we summing?

julia> strides(A) .* size(A)
(5, 15, 30)

julia> strides(A) .* size(A) |> sum
50

@N5N3
Copy link
Member Author

N5N3 commented Feb 16, 2022

We only do sum for non-dense array. see #35929 (comment) for more details.

@musm
Copy link
Contributor

musm commented Feb 16, 2022

Upon reflection here I still do have a slight preference for return sum(st .* size(A)) here simply because using length alone is a code smell in strides computations — it just looks wrong. Indeed, it makes it clearer that we need to call strides in this branch (like Tim noted above). But as a style thing, I'll leave the final decision to you.

I'm not sure I follow the logic either for k > ndims(A). Sounds like the preference to sum is to avoid using length ?

@N5N3
Copy link
Member Author

N5N3 commented Feb 17, 2022

I guess yes. Although it's correctness is also fragile as stride could < 0.

@N5N3 N5N3 deleted the 0dim-stride branch February 17, 2022 01:32
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request Feb 17, 2022
@KristofferC KristofferC removed the backport 1.8 Change should be backported to release-1.8 label Feb 18, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Feb 22, 2022
LilithHafner pushed a commit to LilithHafner/julia that referenced this pull request Mar 8, 2022
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] regression Regression in behavior compared to a previous version
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Regression: stride errors on 0-dim array
6 participants