You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/source/advanced/training_tricks.rst
+48Lines changed: 48 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -154,3 +154,51 @@ Advanced GPU Optimizations
154
154
155
155
When training on single or multiple GPU machines, Lightning offers a host of advanced optimizations to improve throughput, memory efficiency, and model scaling.
156
156
Refer to :doc:`Advanced GPU Optimized Training for more details <../advanced/advanced_gpu>`.
157
+
158
+
----------
159
+
160
+
Sharing Datasets Across Process Boundaries
161
+
------------------------------------------
162
+
The :class:`~pytorch_lightning.DataModule` class provides an organized way to decouple data loading from training logic, with :meth:`~pytorch_lightning.DataModule.prepare_data` being used for downloading and pre-processing the dataset on a single process, and :meth:`~pytorch_lightning.DataModule.setup` loading the pre-processed data for each process individually:
163
+
164
+
.. code-block:: python
165
+
166
+
classMNISTDataModule(pl.LightningDataModule):
167
+
defprepare_data(self):
168
+
MNIST(self.data_dir, download=True)
169
+
170
+
defsetup(self, stage: Optional[str] =None):
171
+
self.mnist = MNIST(self.data_dir)
172
+
173
+
deftrain_loader(self):
174
+
return DataLoader(self.mnist, batch_size=128)
175
+
176
+
However, for in-memory datasets, that means that each process will hold a (redundant) replica of the dataset in memory, which may be impractical when using many processes while utilizing datasets that nearly fit into CPU memory, as the memory consumption will scale up linearly with the number of processes.
177
+
For example, when training Graph Neural Networks, a common strategy is to load the entire graph into CPU memory for fast access to the entire graph structure and its features, and to then perform neighbor sampling to obtain mini-batches that fit onto the GPU.
178
+
179
+
A simple way to prevent redundant dataset replicas is to rely on :obj:`torch.multiprocessing` to share the `data automatically between spawned processes via shared memory <https://pytorch.org/docs/stable/notes/multiprocessing.html>`_.
180
+
For this, all data pre-loading should be done on the main process inside :meth:`DataModule.__init__`.
181
+
As a result, all tensor-data will get automatically shared when using the :class:`~pytorch_lightning.plugins.DDPSpawnPlugin` training type plugin:
182
+
183
+
.. warning::
184
+
185
+
:obj:`torch.multiprocessing` will send a handle of each individual tensor to other processes.
186
+
In order to prevent any errors due to too many open file handles, try to reduce the number of tensors to share, *e.g.*, by stacking your data into a single tensor.
See the `graph-level <https://github.com/pyg-team/pytorch_geometric/blob/master/examples/pytorch_lightning/gin.py>`_ and `node-level <https://github.com/pyg-team/pytorch_geometric/blob/master/examples/pytorch_lightning/graph_sage.py>`_ prediction examples in PyTorch Geometric for practical use-cases.
0 commit comments