Recently I found my code had an unexpected memory cost while working with Julia.
See the following example:
using OMEinsum
len = 20
tensor1 = rand(len,len,len,len)
tensor2 = rand(len,len,len)
tensor3 = rand(len,len)
@time @ein tensorA[a,l,b,k,j] := tensor1[i, j, k, l] * tensor2[a,b,i]
@time @ein tensorB[b, d, k] := tensor3[a, b] * tensor2[a, d, k]
@time @ein tensorA[a,l,b,k,j] := tensor1[i, j, k, l] * tensor2[a,b,i]
@time @ein tensorB[b, d, k] := tensor3[a, b] * tensor2[a, d, k]
output:
2.807763 seconds (9.26 M allocations: 526.600 MiB, 9.24% gc time, 98.71% compilation time)
0.377838 seconds (1.04 M allocations: 52.680 MiB, 2.59% gc time, 99.94% compilation time)
0.034800 seconds (125 allocations: 50.117 MiB, 39.26% gc time)
0.000097 seconds (78 allocations: 70.391 KiB)
The actual cost of the operation in my example would be:
using OMEinsum
using BenchmarkTools
len = 20
tensor1 = rand(len,len,len,len)
tensor2 = rand(len,len,len)
tensor3 = rand(len,len)
@btime @ein tensorA[a,l,b,k,j] := tensor1[i, j, k, l] * tensor2[a,b,i]
@btime @ein tensorB[b, d, k] := tensor3[a, b] * tensor2[a, d, k]
output:
15.992 ms (126 allocations: 50.12 MiB)
16.452 μs (78 allocations: 70.39 KiB)
I knew Julia would actually be a little slower when called the function in the package the first time, but I didn’t think it would cost 10 times more memory than expected, sometimes causing outofMemory errors.
I guess it has something to do with Julia precompilation and not the OMEinsum package. But I don’t quite understand, can someone give me some guidance on how to reduce memory overhead?