I’m diving into Flux and wanted to try out the reverse based autodiff via Flux.Tracker. I have written the following function to do a small test which seems to work and is reasonable fast. However, if I run this function multiple times it keeps increasing memory usage until I get an OutOfMemory error. How should I deallocated the memory between multiple runs of this function. From my naive impression this should happen automatically since nothing is holding on to the memory after the function is evaluated but obviously I’m wrong.
I’m on Julia 1.0 on Ubuntu 18.04 on a Linux Surface Pro 4.
using Flux
using Flux.Tracker
using BenchmarkTools
function fluxderivtest(nhidden=100_000, ninput=1_000)
W = param(rand(nhidden, ninput) .- 0.5)
b = param(rand(nhidden) .- 0.5)
x = rand(ninput)
f(x) = tanh(sum(W*x .+ b))
g = Tracker.gradient(() -> f(x), Params([W, b]))
g[W], g[b]
end
@btime fluxderivtest()
@btime fluxderivtest()
ERROR: OutOfMemoryError()
Stacktrace:
[1] Type at ./boot.jl:396 [inlined]
[2] Type at ./boot.jl:404 [inlined]
[3] Type at ./boot.jl:411 [inlined]
[4] similar at ./abstractarray.jl:618 [inlined]
[5] similar at ./abstractarray.jl:617 [inlined]
[6] similar at ./broadcast.jl:193 [inlined]
[7] copy at ./broadcast.jl:744 [inlined]
[8] materialize at ./broadcast.jl:724 [inlined]
[9] param(::Array{Float64,2}) at /home/michael/.julia/packages/Flux/UHjNa/src/tracker/Tracker.jl:105
[10] fluxderivtest(::Int64, ::Int64) at ./REPL[55]:2 (repeats 2 times)
[11] ##core#364() at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:293
[12] ##sample#365(::BenchmarkTools.Parameters) at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:301
[13] sample at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:316 [inlined]
[14] #_lineartrial#20(::Int64, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::BenchmarkTools.Benchmark{Symbol("##benchmark#363")}, ::BenchmarkTools.Parameters) at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:71
[15] _lineartrial(::BenchmarkTools.Benchmark{Symbol("##benchmark#363")}, ::BenchmarkTools.Parameters) at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:63
[16] #invokelatest#1 at ./essentials.jl:686 [inlined]
[17] invokelatest at ./essentials.jl:685 [inlined]
[18] #lineartrial#17 at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:33 [inlined]
[19] lineartrial at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:33 [inlined]
[20] #tune!#23(::Bool, ::String, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::Function, ::BenchmarkTools.Benchmark{Symbol("##benchmark#363")}, ::BenchmarkTools.Parameters) at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:135
[21] tune! at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:134 [inlined] (repeats 2 times)
[22] top-level scope at /home/michael/.julia/packages/BenchmarkTools/dtwnm/src/execution.jl:388
Most likely it’s a stupid error but I cannot see it.