I am learning pytorch and follow this tutorial.
I use this command to use a GPU.
device = torch.device("**cuda:0**" if torch.cuda.is_available() else "cpu")
But, I want to use two GPUs in jupyter, like this:
device = torch.device("**cuda:0,1**" if torch.cuda.is_available() else "cpu")
Of course, this is wrong. So, How can I do this?
If you want to run your code only on specific GPUs (e.g. only on GPU id 2 and 3), then you can specify that using the CUDA_VISIBLE_DEVICES=2,3 variable when triggering the python code from terminal.
To run multiple instances of a single-GPU application on different GPUs you could use CUDA environment variable CUDA_VISIBLE_DEVICES. The variable restricts execution to a specific set of devices. To use it, just set CUDA_VISIBLE_DEVICES to a comma-separated list of GPU IDs.
Distributed Data Parallel in PyTorch DDP uses multiprocessing instead of threading and executes propagation through the model as a different process for each GPU. DDP duplicates the model across multiple GPUs, each of which is controlled by one process. A process here can be called a script that runs on your system.
Assuming that you want to distribute the data across the available GPUs (If you have batch size of 16, and 2 GPUs, you might be looking providing the 8 samples to each of the GPUs), and not really spread out the parts of models across difference GPU's. This can be done as follows:
If you want to use all the available GPUs:
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = CreateModel() model= nn.DataParallel(model) model.to(device)
If you want to use specific GPUs: (For example, using 2 out of 4 GPUs)
device = torch.device("cuda:1,3" if torch.cuda.is_available() else "cpu") ## specify the GPU id's, GPU id's start from 0. model = CreateModel() model= nn.DataParallel(model,device_ids = [1, 3]) model.to(device)
To use the specific GPU's by setting OS environment variable:
Before executing the program, set CUDA_VISIBLE_DEVICES
variable as follows:
export CUDA_VISIBLE_DEVICES=1,3
(Assuming you want to select 2nd and 4th GPU)
Then, within program, you can just use DataParallel()
as though you want to use all the GPUs. (similar to 1st case). Here the GPUs available for the program is restricted by the OS environment variable.
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") model = CreateModel() model= nn.DataParallel(model) model.to(device)
In all of these cases, the data has to be mapped to the device.
If X
and y
are the data:
X.to(device) y.to(device)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With