Skip to content

dropout may return incorrect results due to improper use of inbounds #655

@yurivish

Description

@yurivish

The dropout! function applies @inbounds to a for loop iterating over 1:length(x) where x is not guaranteed to have standard indexing. As a consequence the function can return incorrect results and silently access out-of-bounds memory.

@inbounds for i=1:length(y)
if y[i] > p
y[i] = x[i] / q
else
y[i] = 0
end
end

A concrete example is with OffsetArrays:

julia> using Knet, OffsetArrays

julia> a = Float64[n for n in 1:21];

julia> o = OffsetArray(a, -10:10);

julia> dropout(o, .5, drop=true)
21-element OffsetArray(::Array{Float64,1}, -10:10) with eltype Float64 with indices -10:10:
  0.5071590064611642
  0.8322953335779582
  0.3388610612978913
  0.9177628344956359
  0.5919343644328279
  0.6557221967728748
  0.14356432793391516
  0.596784239077887
  0.5427223931847711
  0.5626176563028811
  0.8176537879294099
  0.0
  0.0
  0.0
  0.0
  0.0
  0.0
 36.0
  0.0
  0.0
  0.0

When an array with nonstandard indexing is passed to dropout, similar(array) is passed to dropout!. similar for OffsetArrays returns another OffsetArray and dropout access indices 1:10 (valid) and 11:21 (invalid), while leaving uninitialized memory with arbitrary values at indices -10:0 as seen above.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions