Jutho / TensorOperations.jl

Julia package for tensor contractions and related operations
https://jutho.github.io/TensorOperations.jl/stable/
Other
438 stars 55 forks source link

possible memory leak with metaprogramming #139

Open ultimatile opened 1 year ago

ultimatile commented 1 year ago

I am encountering a problem in TensorOperations.jl when using eval and Meta.parse for metaprogramming. The code runs fine without metaprogramming. However, when I use eval and Meta.parse with the @tensor macro, it appears that the memory used will grow, and eventually, all available memory will be used up and killed.

This code works fine;

using TensorOperations
for i in 1:1000
    a = 16; c = 18; b = 18; d = 16; m = 16; n = 16; u = 18; v = 18; A = rand(Float64, (a, u, v, b, c, d)); B = rand(Float64, (n, m, v, u)); @tensor C[d, a, b, m, c, n] := A[a, u, v, b, c, d] * B[n, m, v, u]
end

If only one line is executed, it takes

0.577926 seconds (17.09 k allocations: 736.200 MiB, 7.40% gc time, 31.36% compilation time)

The whole loop takes

343.437409 seconds (114.93 k allocations: 359.412 GiB, 3.63% gc time, 0.07% compilation time)

My problem happens when I use Meta.parse and eval for metaprogramming.

using TensorOperations
for i in 1:1000
    eval(Meta.parse("a = 16; c = 18; b = 18; d = 16; m = 16; n = 16; u = 18; v = 18; A = rand(Float64, (a, u, v, b, c, d));B = rand(Float64, (n, m, v, u)); @tensor C[d, a, b, m, c, n] := A[a, u, v, b, c, d] * B[n, m, v, u]"))
end

If only one line is executed, it takes

7.389352 seconds (41.43 M allocations: 2.726 GiB, 5.71% gc time, 93.99% compilation time)

However, when I run the whole for loop, the process runs out of memory on the machine, is killed, and cannot finish. In my trial, the process died after the 179th execution.

Julia 1.8.5 TensorOperations v2.0.1