Converting MATLAB double to tensor continues to increases memory

sscott: <p>I need to apply a function from MATLAB to a tensor and have <a href="https://www.mathworks.com/help/matlab/matlab_external/install-the-matlab-engine-for-python.html" rel="noopener nofollow ugc">installed the MATLAB Engine API</a> so I can use MATLAB within the Python environment. Currently, I convert the tensor to a MATLAB double, perform the calculation using the MATLAB engine, and then convert the answer back to a torch tensor. The issue that I have is that doing conversion from MATLAB double to torch tensor continues to take up memory, even when I reasign the same variable. The conversion of tensor to MATLAB double doesn’t lead to the increase in memory, it is precisely converting the MATLAB double back into a tensor which leads to this.<br>
This could be some misunderstanding on my part of what torch.tensor or torch.as_tensor does, or something specific to doing this to a MATLAB double, however I have not been able to find information that will resolve my issue. Just creating a torch tensor or doing a conversion of a numpy array to a torch tensor does not see any memory increase. A minimal working example is</p>
<pre data-code-wrap="python"><code class="lang-python">import numpy as np
import torch
import matlab.engine
import psutil

import matplotlib.pyplot as plt
matlab_eng = matlab.engine.start_matlab()

num_loops = 6
n = 2000

mem_no_conversion = np.empty(num_loops)
mem_with_conversion = np.empty(num_loops)

for ind in range(num_loops):
    mem_no_conversion[ind] = psutil.virtual_memory().percent
    A = torch.eye(n,n)
    Amat = matlab.double(A.numpy().tolist())
    del A, Amat
    
for ind in range(num_loops):
    mem_with_conversion[ind] = psutil.virtual_memory().percent
    A = torch.eye(n,n)
    Amat = matlab.double(A.numpy().tolist())
    Atorch = torch.as_tensor(Amat)
    del A, Amat, Atorch
    
plt.figure()
plt.plot(mem_no_conversion,label='No conversion')
plt.plot(mem_with_conversion,label='With conversion')
plt.ylabel('Memory usage')
plt.legend()
</code></pre>
<p>which produces the following figure (I am using 16GB of RAM).<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/7/2/72854cf6790645f5463ff99ca0a58db7ee4f360c.png" alt="memory_increase" data-base62-sha1="gl61FvjB8f3jQ4sFenhpBKoouAs" width="480" height="360"></p>
<p>Is there any way to do this conversion without seeing this increase in memory?</p>

sscott: <p>While I haven’t been able to directly do the conversion from MATLAB double to torch tensor without continuously increasing the memory, a workaround is to save the MATLAB variable as a .mat file using the MATLAB engine and then load that file as a torch tensor.<br>
For example,</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
import matlab.engine
import scipy.io as io

matlab_eng = matlab.engine.start_matlab()

for _ in range(10):
    A = torch.eye(2000,2000)
    Amat = matlab.double(A.numpy().tolist())
    matlab_eng.workspace['Amat'] = Amat
    matlab_eng.save('mat.mat','Amat', nargout=0)
    Atorch = torch.tensor(io.loadmat('mat.mat')['Amat'], dtype=torch.float64)
</code></pre>
<p>does not lead to a continuous increase in memory usage. I am still curious as to why the original approach leads to repeated memory allocation.</p>