How can I use Pytorch multiprocessing on an Nvidia GPU that is strictly in exclusive…
- User
How can I use Pytorch multiprocessing on an Nvidia GPU that is strictly in exclusive process mode only?
Assistant
How can I use Pytorch multiprocessing on an Nvidia GPU that is strictly in exclusive process mode only?
You can't directly use PyTorch multiprocessing with an NVIDIA GPU in exclusive process mode. Here's why and potential workarounds:
Why it doesn't work:
Workarounds:
Disable Exclusive Process Mode (Recommended if possible):
Multiprocessing without GPU Sharing (CPU-based):
Distributed Data Parallel (DDP):
Inter-Process Communication (IPC):
Example (DDP with a single node and multiple GPUs):
import torch
import torch.distributed as dist
import torch.multiprocessing as mp
from torch.nn.parallel import DistributedDataParallel as DDP
def setup(rank, world_size):
os.environ['MASTER_ADDR'] = 'localhost'
os.environ['MASTER_PORT'] = '12355' # Choose a free port
# initialize the process group
dist.init_process_group("nccl", rank=rank, world_size=world_size)
def cleanup():
dist.destroy_process_group()
def main(rank, world_size):
setup(rank, world_size)
# Create model and move it to the correct device
model = ... # Your model
device = torch.device(f'cuda:{rank}')
model.to(device)
ddp_model = DDP(model, device_ids=[rank])
# ... your training loop ...
cleanup()
if __name__ == '__main__':
world_size = torch.cuda.device_count()
mp.spawn(main, args=(world_size,), nprocs=world_size, join=True)
Which approach to choose:
Remember to carefully consider your specific requirements and the trade-offs of each approach before making a decision.
Not yet included in a published release.