I noticed the following issue when trying to minimize my memory allocation while using ForwardDiff.gradient!
. My example looks like:
using ForwardDiff
function run0(x, f, n)
out = similar(x);
cfg = ForwardDiff.GradientConfig(f, x);
for i = 1:n
ForwardDiff.gradient!(out, f, x, cfg);
end
end
function V(x)
result = (dot(x,x)-1)^2;
return result
end
If I now compute:
julia> x100 = rand(100);
julia> @time run0(x100, V, 10);
0.586337 seconds (350.17 k allocations: 19.781 MiB, 1.96% gc time)
julia> @time run0(x100, V, 10);
0.000659 seconds (7 allocations: 10.656 KiB)
julia> @time run0(x100, V, 100);
0.005968 seconds (7 allocations: 10.656 KiB)
julia> @time run0(x100, V, 1000);
0.026512 seconds (7 allocations: 10.656 KiB)
and you see that the memory usage is insensitive to the number of iterations.
If, however, I use a small vector,
julia> x5=rand(5);
julia> @time run0(x5, V, 10);
0.116469 seconds (68.13 k allocations: 3.728 MiB)
julia> @time run0(x5, V, 10);
0.000157 seconds (17 allocations: 1.453 KiB)
julia> @time run0(x5, V, 20);
0.000167 seconds (27 allocations: 2.078 KiB)
julia> @time run0(x5, V, 100);
0.000385 seconds (107 allocations: 7.078 KiB)
julia> @time run0(x5, V, 1000);
0.002894 seconds (1.01 k allocations: 63.328 KiB)
I see growth in memory usage with the number of iterations.