QBoard » Artificial Intelligence & ML » AI and ML - PyTorch » How to tell PyTorch to not use the GPU?

How to tell PyTorch to not use the GPU?

  • I want to do some timing comparisons between CPU & GPU as well as some profiling and would like to know if there's a way to tell pytorch to not use the GPU and instead use the CPU only? I realize I could install another CPU-only pytorch, but hoping there's an easier way.
      September 23, 2021 11:11 PM IST
    0
  • You can just set the CUDA_VISIBLE_DEVICES variable to empty via shell before running your torch code.

    export CUDA_VISIBLE_DEVICES=""

    Should tell torch that there are no GPUs.

    export CUDA_VISIBLE_DEVICES="0" will tell it to use only one GPU (the one with id 0) and so on.
      September 24, 2021 1:34 PM IST
    0
  • simplest way is:

      os.environ["CUDA_VISIBLE_DEVICES"]=""
    
      September 25, 2021 12:03 AM IST
    0
  • This is a real world example: original function with gpu, versus new function with cpu.

    Source: https://github.com/zllrunning/face-parsing.PyTorch/blob/master/test.py

    In my case I have edited these 4 lines of code:

    #totally new line of code
    device=torch.device("cpu")
    
    
    
    #net.cuda()
    net.to(device)
    
    #net.load_state_dict(torch.load(cp))
    net.load_state_dict(torch.load(cp, map_location=torch.device('cpu')))
    
    #img = img.cuda()
    img = img.to(device)
    

     

    #original_function_with_gpu
    
    
    def evaluate(image_path='./imgs/116.jpg', cp='cp/79999_iter.pth'):
    
        n_classes = 19
        net = BiSeNet(n_classes=n_classes)
        net.cuda()
        net.load_state_dict(torch.load(cp))
        net.eval()
    
        to_tensor = transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225)),])
    
        with torch.no_grad():
            img = Image.open(image_path)
            image = img.resize((512, 512), Image.BILINEAR)
            img = to_tensor(image)
            img = torch.unsqueeze(img, 0)
            img = img.cuda()
            out = net(img)[0]
            parsing = out.squeeze(0).cpu().numpy().argmax(0)
            return parsing
      October 4, 2021 1:46 PM IST
    0