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

Memoize col in conv_im2col functions to reduce allocations #635

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "NNlib"
uuid = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
version = "0.9.28"
version = "0.9.29"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Expand Down
70 changes: 68 additions & 2 deletions src/impl/conv_im2col.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@
return (kernel_w - w + 1, kernel_h - h + 1, kernel_d - d + 1)
end

# Internal. Used to memoize the `col` scratchspace used in the functions below.
_col_memo_lock = ReentrantLock()
_col_memo = Dict()
macro _memo_col(key, type, default)
# The caller gets exclusive use of the returned array and should return it to the memo once they are finished.
return quote
lock(_col_memo_lock) do
if !haskey(_col_memo, $(esc(key)))
_col_memo[$(esc(key))] = $(esc(type))[]
end
if !isempty(_col_memo[$(esc(key))])
return pop!(_col_memo[$(esc(key))])
else
return $(esc(default))
end
end
end
end
macro _return_col_to_memo(key, type, val)
return quote
lock(_col_memo_lock) do
# always need to check the key because the memo could have been emptied
if !haskey(_col_memo, $(esc(key)))
_col_memo[$(esc(key))] = $(esc(type))[]

Check warning on line 34 in src/impl/conv_im2col.jl

View check run for this annotation

Codecov / codecov/patch

src/impl/conv_im2col.jl#L34

Added line #L34 was not covered by tests
end
push!(_col_memo[$(esc(key))], $(esc(val)))
end
end
end

"""
free_scratchspace_memo!()

Empties the memo holding arrays used for scratch space. Thread safe.
"""
function free_scratchspace_memo!()
lock(_col_memo_lock) do
empty!(_col_memo)
end
return nothing
end

"""
conv_im2col!(y, x, w, cdims, col=similar(x); alpha=1, beta=0)

Expand All @@ -19,15 +61,24 @@

Note for the particularly performance-minded, you can provide a pre-allocated `col`,
which should eliminate any need for large allocations within this method.
By default, `col` will be memoized to reduce allocations between calls.
The memo can be emptied at any time using [`free_scratchspace_memo!`](@ref).
"""
function conv_im2col!(
y::AbstractArray{T,5}, x::AbstractArray{T,5},
w::AbstractArray{T,5}, cdims::DenseConvDims;
col::AbstractArray{T,3}=similar(x, im2col_dims(cdims)),
col::Union{AbstractArray{T,3},Nothing}=nothing,
alpha::T=T(1), beta::T=T(0),
ntasks::Int=nthreads()) where {T}
check_dims(size(x), size(w), size(y), cdims)

# Memoize col to reduce allocations. We get exclusive use of the returned col.
dims = im2col_dims(cdims)
if col === nothing
key = (T,dims...)
col = @_memo_col key AbstractArray{T,3} similar(x, dims)

Check warning on line 79 in src/impl/conv_im2col.jl

View check run for this annotation

Codecov / codecov/patch

src/impl/conv_im2col.jl#L79

Added line #L79 was not covered by tests
end

# COL * W -> Y
# [M x K] * [K x N] -> [M x N]
#
Expand Down Expand Up @@ -61,6 +112,10 @@
end
end
end

# Return col to the memo so another function can use it.
@_return_col_to_memo key AbstractArray{T,3} col

Check warning on line 117 in src/impl/conv_im2col.jl

View check run for this annotation

Codecov / codecov/patch

src/impl/conv_im2col.jl#L117

Added line #L117 was not covered by tests

return y
end

Expand All @@ -74,10 +129,17 @@
function ∇conv_filter_im2col!(
dw::AbstractArray{T,5}, x::AbstractArray{T,5},
dy::AbstractArray{T,5}, cdims::DenseConvDims;
col::AbstractArray{T,3} = similar(dw, ∇filter_im2col_dims(cdims)),
col::Union{AbstractArray{T,3},Nothing}=nothing,
alpha::T=T(1), beta::T=T(0)) where {T}
check_dims(size(x), size(dw), size(dy), cdims)

# Memoize col to reduce allocations. We get exclusive use of the returned col.
dims = ∇filter_im2col_dims(cdims)
if col === nothing
key = (T,dims...)
col = @_memo_col key AbstractArray{T,3} similar(dw, dims)

Check warning on line 140 in src/impl/conv_im2col.jl

View check run for this annotation

Codecov / codecov/patch

src/impl/conv_im2col.jl#L140

Added line #L140 was not covered by tests
end

# COL' * dY -> dW
# [M x K] * [K x N] -> [M x N]
#
Expand Down Expand Up @@ -114,6 +176,10 @@
# to `1.0` from this point on.
beta = T(1)
end

# Return col to the memo so another function can use it.
@_return_col_to_memo key AbstractArray{T,3} col

Check warning on line 181 in src/impl/conv_im2col.jl

View check run for this annotation

Codecov / codecov/patch

src/impl/conv_im2col.jl#L181

Added line #L181 was not covered by tests

return dw
end

Expand Down
7 changes: 7 additions & 0 deletions test/conv.jl
Original file line number Diff line number Diff line change
Expand Up @@ -997,3 +997,10 @@ end
end

end

@testset "free_scratchspace_memo" begin
NNlib._col_memo[1] = 2
@test !isempty(NNlib._col_memo)
NNlib.free_scratchspace_memo!()
@test isempty(NNlib._col_memo)
end
Loading