How to check if pytorch is using the GPU?

后端 未结 10 1238
既然无缘
既然无缘 2020-12-04 05:01

I would like to know if pytorch is using my GPU. It\'s possible to detect with nvidia-smi if there is any activity from the GPU during the process,

相关标签:
10条回答
  • 2020-12-04 05:06

    On the office site and the get start page, check GPU for PyTorch as below:

    import torch
    torch.cuda.is_available()
    

    Reference: PyTorch|Get Start

    0 讨论(0)
  • 2020-12-04 05:07

    If you are here because your pytorch always gives False for torch.cuda.is_available() that's probably because you installed your pytorch version without GPU support. (Eg: you coded up in laptop then testing on server).

    The solution is to uninstall and install pytorch again with the right command from pytorch downloads page. Also refer this pytorch issue.

    0 讨论(0)
  • 2020-12-04 05:10

    Almost all answers here reference torch.cuda.is_available(). However, that's only one part of the coin. It tells you whether the GPU (actually CUDA) is available, not whether it's actually being used. In a typical setup, you would set your device with something like this:

    device = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    

    but in larger environments (e.g. research) it is also common to give the user more options, so based on input they can disable CUDA, specify CUDA IDs, and so on. In such case, whether or not the GPU is used is not only based on whether it is available or not. After the device has been set to a torch device, you can get its type property to verify whether it's CUDA or not.

    if device.type == 'cuda':
        # do something
    
    0 讨论(0)
  • 2020-12-04 05:12

    After you start running the training loop, if you want to manually watch it from the terminal whether your program is utilizing the GPU resources and to what extent, then you can simply use watch as in:

    $ watch -n 2 nvidia-smi
    

    This will continuously update the usage stats for every 2 seconds until you press ctrl+c


    If you need more control on more GPU stats you might need, you can use more sophisticated version of nvidia-smi with --query-gpu=.... Below is a simple illustration of this:

    $ watch -n 3 nvidia-smi --query-gpu=index,gpu_name,memory.total,memory.used,memory.free,temperature.gpu,pstate,utilization.gpu,utilization.memory --format=csv
    

    which would output the stats something like:

    Note: There should not be any space between the comma separated query names in --query-gpu=.... Else those values will be ignored and no stats are returned.


    Also, you can check whether your installation of PyTorch detects your CUDA installation correctly by doing:

    In [13]: import  torch
    
    In [14]: torch.cuda.is_available()
    Out[14]: True
    

    True status means that PyTorch is configured correctly and is using the GPU although you have to move/place the tensors with necessary statements in your code.


    If you want to do this inside Python code, then look into this module:

    https://github.com/jonsafari/nvidia-ml-py or in pypi here: https://pypi.python.org/pypi/nvidia-ml-py/

    0 讨论(0)
  • 2020-12-04 05:12

    From practical standpoint just one minor digression:

    import torch
    dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    

    This dev now knows if cuda or cpu.

    And there is a difference how you deal with model and with tensors when moving to cuda. It is a bit strange at first.

    import torch
    import torch.nn as nn
    dev = torch.device("cuda") if torch.cuda.is_available() else torch.device("cpu")
    t1 = torch.randn(1,2)
    t2 = torch.randn(1,2).to(dev)
    print(t1)  # tensor([[-0.2678,  1.9252]])
    print(t2)  # tensor([[ 0.5117, -3.6247]], device='cuda:0')
    t1.to(dev) 
    print(t1)  # tensor([[-0.2678,  1.9252]]) 
    print(t1.is_cuda) # False
    t1 = t1.to(dev)
    print(t1)  # tensor([[-0.2678,  1.9252]], device='cuda:0') 
    print(t1.is_cuda) # True
    
    class M(nn.Module):
        def __init__(self):        
            super().__init__()        
            self.l1 = nn.Linear(1,2)
    
        def forward(self, x):                      
            x = self.l1(x)
            return x
    model = M()   # not on cuda
    model.to(dev) # is on cuda (all parameters)
    print(next(model.parameters()).is_cuda) # True
    

    This all is tricky and understanding it once, helps you to deal fast with less debugging.

    0 讨论(0)
  • 2020-12-04 05:20

    Simply from command prompt or Linux environment run the following command.

    python -c 'import torch; print(torch.cuda.is_available())'
    

    The above should print True

    python -c 'import torch; print(torch.rand(2,3).cuda())'
    

    This one should print the following:

    tensor([[0.7997, 0.6170, 0.7042], [0.4174, 0.1494, 0.0516]], device='cuda:0')
    
    0 讨论(0)
提交回复
热议问题