How to load big dataset file for NMT task

Aiman_Mutasem-bellh: <p>Dear <span class="mention">@all</span>,</p>
<p>I have a NMT dataset in size of 199 MB for Training and 22.3 MB for dev. set. , batch size is 256, and the max-length of each sentence is 50 words. The data is loaded to GPU RAM without any problems when I start training I got <code>Out of memory error</code>.</p>
<pre><code class="lang-auto">SRC = Field(tokenize= normalizeString, init_token='&lt;sos&gt;', eos_token='&lt;eos&gt;', fix_length = 50, batch_first=True)
TRG = Field(tokenize= normalizeString, init_token='&lt;sos&gt;', eos_token='&lt;eos&gt;', fix_length = 50, batch_first=True) 

train_data, valid_data = TabularDataset.splits(path='./data/',train='SCUT_train.csv',
    validation=''SCUT_.csv' , format='csv',
    fields=[('src', SRC), ('trg', TRG)], skip_header=True)

SRC.build_vocab(train_data, min_freq = 2) 
TRG.build_vocab(train_data, min_freq = 2)

BATCH_SIZE = 128

train_iterator, vali_iterator = BucketIterator.splits((train_data, valid_data), sort_key=lambda x: len(x.src),
     batch_size = BATCH_SIZE, device = device)
</code></pre>
<p>The data is not that huge size in total 221.3 MB.</p>
<p>I have found many techniques online to overcome the challenge of loading huge data (GBs), but it is almost for image processing tasks.</p>
<p>Kindly, any suggestion to fix this issue, and what is the optimal way to slice and load big dataset file for machine translation task.</p>
<p>Regards,</p>

vdw: <p>Does the memory error occur directly when starting the training or after some little time? If the latter, maybe there’s a memory leak in the training itself.</p>
<p>Did you try with (much) smaller batch sizes?</p>

Aiman_Mutasem-bellh: <p>Yes, sir <a class="mention" href="/u/vdw">@vdw</a>  it works well when I minimize the batch and the sentence length. I believe the is a dataset size issue.</p>
<p>Currently, my dataset is small (212.3 MB) and I can’t use the whole training set, what if I use 1GB data or more?</p>

vdw: <p>When it works fine with smaller batches, then the dataset size is not an issue. I usually work with smaller batches such as 32 or 64 for text processing tasks including NMT. There are several post discussing the effect of batch size and GPU memory consumption (e.g., <a href="https://discuss.pytorch.org/t/gpu-and-batch-size/40578">this thread</a>.</p>
<p>If the dataset size really becomes an issue – 212MB should cause any – you may also consider splitting the dataset. Say your current training loads the dataset from a CSV file like in this pseudocode:</p>
<pre><code class="lang-auto">data_loader = DataLoader('dataset.csv')

for epoch in range(1, 100):
    for batch in data_loader:
        inputs, targets = batch[0], batch[1]
        outputs = model(inputs)
        loss = calc_loss(outputs, targets)
        ....
</code></pre>
<p>You can split the original CSV file into , say, 3 equally sized chunks and do the following:</p>
<pre><code class="lang-auto">    for file in ["dataset01.csv", "dataset02.csv", "dataset03.csv"]:
        data_loader = DataLoader(file)
        for batch in data_loader:
            inputs, targets = batch[0], batch[1]
            outputs = model(inputs)
            loss = calc_loss(outputs, targets)
            ....
</code></pre>

Aiman_Mutasem-bellh: <p>This is a huge support sir thank you <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>