Growing allocated memory issue for every batch during training

username: rf6025
cooked: <p>Hello! I am using a custom Dataloader (see image below for reference) designed to access the dataset in a “sliding window” manner. The max batch size is set to 32 for training.</p>
<p>My custom Dataloader with a sliding window:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/4/b/4b43ca4c3b29ab2678658317fb94a3f0ee6951b8.png" alt="image" data-base62-sha1="aJP36Ru5FeFJgIOuDn6olN4Y8NW" width="624" height="259"></p>
<p>The discussion forum that inspired my Dataloader’s design is linked here: <a href="https://discuss.pytorch.org/t/dataloader-for-a-lstm-model-with-a-sliding-window/22235" class="inline-onebox">DataLoader for a LSTM Model with a Sliding Window</a></p>
<p>During training, the amount of memory allocated is printed for each batch. I noticed that the allocated memory amount would gradually grow for every batch (e.g. Batch 1 has ~132 Mb allocated, Batch 2 has ~137 Mb allocated, Batch 3 has ~142 Mb allocated, and so on). This memory allocation issue still occurs when the max batch size is reached, but the amount of allocated memory doesn’t grow as much as before (e.g. Batch 32 has ~304 Mb allocated, Batch 33 has ~306 Mb allocated, Batch 34 has ~308 Mb allocated, and so on).</p>
<p>Note that I am using a NVIDIA GeForce RTX 3090 GPU that has 24 Gb. Any advice or help is appreciated.</p>

username: ptrblck
cooked: <p>Could you post a minimal and executable code snippet reproducing the issue, please?</p>

username: rf6025
cooked: <p>Apologies for the late reply, I was having trouble recreating the same issue. I realized that I mistook my custom Dataset I shared in my original post as the Dataloader when it isn’t. Thus, my training results were gotten from using just my Dataset class and no Dataloader. I’m assuming the memory issue was caused by not using a Dataloader, so I am now trying to get my custom Dataset to work with the Pytorch Dataloader.</p>
<p>Below is a simple code to test the Dataset with the Pytorch Dataloader. When I tried testing my custom Dataset, it wouldn’t execute. But when I tried using the LSTM sliding window Dataset (see link to that dataset in original post), it would execute.</p>
<p><strong>Simple Execution Code for Testing (<em>Dataset</em> is the custom Dataset class)</strong></p>
<pre><code class="lang-auto">from torch.utils.data import DataLoader
import albumentations as A
import numpy as np

B, H, W    = 100, 32, 32                          # Batch, Height, Width
imgset     = np.random.rand(B,H,W,3)              # Dummy numpy datasets
labset     = np.random.rand(B,H,W,1)

transform  = A.Compose([ToTensorV2()])            # Data transformation

trainset   = Dataset(imgset, labset, transform)   # Process data into trainset   
dataloader = DataLoader(trainset, batch_size = 1, shuffle=False, pin_memory=True)  # Dataloader

# Print each batch
for i, (img, msk) in enumerate(dataloader):
    print(f"Batch {i}  \tImages: {img.shape}\t  Masks: {msk.shape}")
</code></pre>
<p>However, the LSTM sliding window Dataset didn’t make batches the way I wanted. <strong>Image A</strong> below shows what the batch outputs look like made by the LSTM sliding window Dataset. The batch outputs I am aiming to create can be seen in <strong>Image B</strong> below, which is similar to <strong>Image A</strong> but the sliding window starts with one image rather than the first <em>batch size</em> number of images. Note that for <strong>Image A</strong> and <strong>Image B</strong>, each row is a batch, each element is the index of one image, the maximum batch size is 4, and dataset size is 10.</p>
<p><strong>Image A: Type of batches made by the LSTM sliding window Dataset :</strong><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/1/3/134edf0d4cdec5af50802c9df82225ea38e8752c.png" alt="image" data-base62-sha1="2KO3houYIznLcUIfTzKy5Kpjfek" width="106" height="140"></p>
<p><strong>Image B: Type of batches I want to produce:</strong><br>
<img src="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec879d28682f64465982b07a093f001f57e42ee5.png" alt="image" data-base62-sha1="xKrexgRInfPhbOBz4UV5ozsTYAl" width="107" height="210"></p>
<p>I’m currently looking more into the Pytorch Dataloader to see if there is any way I can get the sliding window to work like in <strong>Image B</strong> (I am experimenting with a custom Batch Sampler with no success so far). If you or anyone else have any ideas or advice on achieving this, I would greatly appreciate it! Thank you!</p>

username: ptrblck
cooked: <p>A simple <code>Dataset</code> example using a sliding window can be found <a href="https://discuss.pytorch.org/t/dataloader-for-a-lstm-model-with-a-sliding-window/22235/2">here</a> but note that the sequence length would be static and would not increase (up to 4) as in your example image B.</p>