What if I load data in __init__ function of LightningDataModule #10772
-
Hi, I see the doc describing the functions of LightningDataModule. Here is my thinking. If some variable, e.g., a transform, can be defined in init function, and later shared across different GPUs. Theoretically, if we load data in init, the data should also be able to transfer to different GPUs similarly. In the case of a single machine with multiple GPUs, the data will be copied multiple times in the memory. In the case of multiple machines, the data will broadcast through the network from the main node to other nodes. Broadcasting large data through networks may have efficiency issue, which is why we had better load data in the setup function. Please let me know whether my analysis is correct or not. Basically, I am not clear about how the variables, e.g., i.e. self.something, defined in init are shared across multiple GPUs/machines. Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
The data don't get broadcasted. They are just manipulated (downloaded, loaded, processed, etc...) Loading isn't any issue, but downloading or preprocessing in a distributed setting is. Imagine the following scenario with 2 processes. A -> download the data at time t1 and B start to download the data at time t2. Both process will override each other and the data will corrupted. Same thing with preprocessing. If you don't do any data manipulation, it is entirely fine loading the data in init. However, if you do, use the |
Beta Was this translation helpful? Give feedback.
-
Thanks @tchaton,
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
Thanks @rohitgr7, Just to clarify. If I use |
Beta Was this translation helpful? Give feedback.
@zhiqiangdon
prepare_data
on the main process before the distributed process actually starts so there is no blocking happening behind the scenes.prepare_data_per_node
should be set to False.__init__
function when they initialize the DataModule, lightning just send to across devices.