question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Improve moving data / model to GPU using torchtext

See original GitHub issue

🚀 Feature

Improve moving data and model to GPU if torchtext is used.

Motivation

Case 1:

Batch object generated by torchtext.data.Iterator doesn’t follow the rules described here https://github.com/PyTorchLightning/pytorch-lightning/blob/45d671a4a81788b9d97fd6b47763816926e58e95/pytorch_lightning/trainer/distrib_parts.py#L420

As the result data is not moved to GPU. torchtext.data.Iterator is returned by method train_dataloader. Take in mind that torchtext.data.Iterator has a device argument that is not properly utilized by pytorch-ligthning.

    @ptl.data_loader
    def train_dataloader(self):
        ...  
        return Iterator(dataset=dataset, batch_size=self.batch_size, shuffle=False, device=DEVICE)

Partially reported here https://github.com/PyTorchLightning/pytorch-lightning/issues/226

Case 2

Using torchtext you can read pre-trained embeddings and create nn.Embedding object as follows

    def train_dataloader(self):
        ...
        self.text_field.build_vocab(
            dataset,
            vectors=Vectors("/data/embeddings/glove/glove.840B.300d.txt"),
        )

        self.embeddings = nn.Embedding(
            ...
            padding_idx=self.text_field.vocab.stoi[PAD_TOKEN],
            _weight=self.text_field.vocab.vectors.to(DEVICE),
        )

nn.Embedding is clearly dependent on self.text_field.vocab and this is in turn dependent on dataset that is used by train_dataloader. Currently any part of the model that is not created fully in __init__ of the ptl.LigthningModule is not moved to the GPU. It requires still to have a global variable that determines a device i.e. DEVICE. It makes Trainer(n_gpus=...) useless.

Pitch

I would like not to worry about moving data to GPU using torchtext combined with pytorch-lightning.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:28 (24 by maintainers)

github_iconTop GitHub Comments

3reactions
jeremyjordancommented, May 7, 2020

@awaelchli i would prefer a hook since that will be generally useful and will likely reduce the long-term maintenance burden

3reactions
williamFalconcommented, May 6, 2020
  1. let’s introduce s formal hook
  2. let’s formally support torchtext!

good ideas everyone!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Saving and loading models across devices in PyTorch
Saving and loading models across devices is relatively straightforward using PyTorch. In this recipe, we will experiment with saving and loading models ......
Read more >
Use torchtext to Load NLP Datasets — Part I | by Ceshine Lee
Since in most cases we're using GPU to train models, setting it to torch.cuda.LongTensor will save us the trouble of moving it to...
Read more >
Getting started with PyTorch - IBM
The WML CE PyTorch includes support for IBM's Distributed Deep Learning (DDL) and Large Model Support (LMS). This release of WML CE includes...
Read more >
Text classification with the torchtext library - PyTorch Tutorials
Access to the raw data as an iterator · Build data processing pipeline to convert the raw text strings into torch.Tensor that can...
Read more >
A Tutorial on Torchtext - Allen Nie
I moved on. ... I have started using PyTorch on and off during the summer. ... Train/Val/Test Split: seperate your data into a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found