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

Indexing out-of-bounds not throwing #67

Open
korsbo opened this issue May 16, 2022 · 4 comments
Open

Indexing out-of-bounds not throwing #67

korsbo opened this issue May 16, 2022 · 4 comments

Comments

@korsbo
Copy link
Member

korsbo commented May 16, 2022

nn = SimpleChain(
  static(10),
  TurboDense(identity, 1),
)

nn(randn(10), SimpleChains.init_params(nn))[2]

This seems like a bit of a footgun. Indeed, my foot is not unscathed.

@chriselrod
Copy link
Contributor

julia> StrideArraysCore.boundscheck() = true

julia> nn(randn(10), SimpleChains.init_params(nn))[2]
ERROR: BoundsError: attempt to access 1-element PtrArray{Tuple{StaticInt{1}}, (true,), Float64, 1, 1, 0, (1,), Tuple{StaticInt{8}}, Tuple{StaticInt{1}}} with indices 1:1:1 at index [2]
Stacktrace:
 [1] throw_boundserror(A::PtrArray{Tuple{StaticInt{1}}, (true,), Float64, 1, 1, 0, (1,), Tuple{StaticInt{8}}, Tuple{StaticInt{1}}}, I::Tuple{Int64})
   @ Base ./abstractarray.jl:703
 [2] checkbounds
   @ ./abstractarray.jl:668 [inlined]
 [3] getindex(A::PtrArray{Tuple{StaticInt{1}}, (true,), Float64, 1, 1, 0, (1,), Tuple{StaticInt{8}}, Tuple{StaticInt{1}}}, i::Int64)
   @ StrideArraysCore ~/.julia/packages/StrideArraysCore/pjIfM/src/ptr_array.jl:569
 [4] top-level scope
   @ REPL[8]:1

There are a ton of functions in the Julia ecosystem that don't have @inbounds because, even though they iterate based on the size of the arrays, "there's no guarantee that the array types actually implemented those functions correctly".
Forcing me to either reimplement the world, or be much more dangerous.

@chriselrod
Copy link
Contributor

chriselrod commented May 16, 2022

Note that if you start Julia with --check-bounds=yes, you don't need to manually set boundscheck() = true...

julia> using SimpleChains

julia> nn = SimpleChain(
         static(10),
         TurboDense(identity, 1),
       )
SimpleChain with the following layers:
TurboDense static(1) with bias.

julia> nn(randn(10), SimpleChains.init_params(nn))[2]
ERROR: BoundsError: attempt to access 1-element StrideArraysCore.PtrArray{Tuple{Static.StaticInt{1}}, (true,), Float64, 1, 1, 0, (1,), Tuple{Static.StaticInt{8}}, Tuple{Static.StaticInt{1}}} with indices 1:1:1 at index [2]
Stacktrace:
 [1] throw_boundserror(A::StrideArraysCore.PtrArray{Tuple{Static.StaticInt{1}}, (true,), Float64, 1, 1, 0, (1,), Tuple{Static.StaticInt{8}}, Tuple{Static.StaticInt{1}}}, I::Tuple{Int64})
   @ Base ./abstractarray.jl:703
 [2] checkbounds
   @ ./abstractarray.jl:668 [inlined]
 [3] getindex(A::StrideArraysCore.PtrArray{Tuple{Static.StaticInt{1}}, (true,), Float64, 1, 1, 0, (1,), Tuple{Static.StaticInt{8}}, Tuple{Static.StaticInt{1}}}, i::Int64)
   @ StrideArraysCore ~/.julia/packages/StrideArraysCore/pjIfM/src/ptr_array.jl:569
 [4] top-level scope
   @ REPL[3]:1

@korsbo
Copy link
Member Author

korsbo commented May 18, 2022

Ok, so a general fix for this would impact performance too much - leaving it up to the documentation to be explicit about the danger?

@chriselrod
Copy link
Contributor

chriselrod commented May 18, 2022

Ok, so a general fix for this would impact performance too much - leaving it up to the documentation to be explicit about the danger?

I guess it isn't obvious to anyone where to look/maybe it should be plastered everywhere? https://github.com/JuliaSIMD/StrideArraysCore.jl#caution

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

No branches or pull requests

2 participants