How to use a huge line corpus (text) with Dataset/DataLoader?

josmi9966: <p>If you have a very large corpus of text/documents, one document per line (actually it is a tsv file, so the label would be in one column, the document text without tabs in another, all fields separated by tabs), and there is no way to fit this data, or any numeric representation created from it into memory, how does one go about creating a Dataset subclass for it?</p>
<p>The PyTorch Dataset implementation requires that the <code>__getitem__(self, index)</code> method is implemented, but this requires direct access to each instance as you would get with all data loaded into memory.<br>
But some data is just so big it can only be accessed sequentially, how is this done properly in PyTorch?<br>
I would still want the dataloader to give me batches of data etc., but for data which cannot be easily accessed sequentially.</p>
<p>How do other people do this for large datasets of text?</p>
<p>I am reluctant to put every document into a file, since that would mean millions of files would have to get created which is a massive overhead on the hard disk in many ways (unused storage per file, massive disk segmentation slowing down access etc).</p>
<p>So what is the correct way to do this in PyTorch or are there any other libraries which support this?</p>

ptrblck: <p>Have a look at <a href="https://discuss.pytorch.org/t/data-processing-as-a-batch-way/14154/4?u=ptrblck">this small example</a> using a <code>pd.DataFrame</code> to read small chunks of data.<br>
Let me know, if that works for you.</p>

josmi9966: <p>Thank you for that suggestion, but this approach would use the CSV parser to read ALL the rows before the desired one every time, that means for N rows there would be N*N/2 full parses of the line, so it would be extremely slow.</p>
<p>I had been thinking that maybe it would be possible to directly skip to the correct position inside the file using seek, but this is very difficult to get right if the file can be in any encoding or even just for UTF8, I think.</p>

ptrblck: <p>Oh, I didn’t know that. I assumed <code>skiprows=index * self.chunksize + 1</code> would indeed skip the rows without loading them.</p>

josmi9966: <p>I am sorry, when I think about this again, I realize I actually only assumed this but I do not know. I could not find any documentation about this, and when I tried to look at the pandas code it turns out that this parameter gets passed to the C-code of the parser. So they may do something clever here.<br>
Thank you for the hint, maybe I can find out more!</p>

josmi9966: <p>As another update, I found that indexing a UTF-8 file is apparently actually possible when reading lines binary as bytes. So this may be one way to do it, but I am still wondering if anyone knows about a more “polished” or standard way to do this?</p>

josmi9966: <p>OK, I tested this with my file and tried to get the 10000th row (that is probably about 10G into the file)<br>
Pandas using skiprows=9999, chunksize=1 took 67.771 seconds, the approach I now implemented by indexing the start of line offsets and then directly skipping to the requested line takes (without the indexing which is only done once) 0.016 seconds.</p>

Jiho_Noh: <p>I am interested in your solution. Would you be able to post your method of indexing and reading in bytes?</p>

minhduc0711: <p>I would also appreciate if you can share the ideas of your solution.</p>

minhduc0711: <p>Also I found a pretty fast solution <a href="https://github.com/pytorch/text/issues/130#issuecomment-510412877" rel="nofollow noopener">here</a> in case anyone has the same problem.</p>

zhangguanheng66: <p>One of the solutions is to use a list of byte offsets and construct sub-datasets based on the byte offsets. An example is <a href="https://github.com/pytorch/text/blob/master/torchtext/datasets/unsupervised_learning.py" rel="nofollow noopener">here</a>.</p>
<p>Another solution is <code>IterableDataset</code> supported by pytorch <code>torch.utils.data</code>. A relevant example is <a href="https://github.com/pytorch/text/blob/a5880a3da7928dd7dd529507eec943a307204de7/examples/text_classification/iterable_train.py#L169-L214" rel="nofollow noopener">here</a>.</p>