Skip to content

Instantly share code, notes, and snippets.

@ronaldseoh
Last active August 11, 2023 21:35
Show Gist options
  • Save ronaldseoh/da4afaa1bb9eb34d32d167ba417a5199 to your computer and use it in GitHub Desktop.
Save ronaldseoh/da4afaa1bb9eb34d32d167ba417a5199 to your computer and use it in GitHub Desktop.
Google Colaboratory PyTorch GPU/TPU Setup - Automatically switch between GPUs and CPUs. I make the code here the second cell to run on all the Colab notebooks.
'''
MIT License
Copyright (c) 2020 Ronald Seoh
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
'''
# torch.device / CUDA Setup
import torch
use_cuda = True
use_colab_tpu = False
colab_tpu_available = False
if use_colab_tpu:
try:
assert os.environ['COLAB_TPU_ADDR']
colab_tpu_available = True
except:
colab_tpu_available = True
if use_cuda and torch.cuda.is_available():
torch_device = torch.device('cuda')
# Set this to True to make your output immediately reproducible
# Note: https://pytorch.org/docs/stable/notes/randomness.html
torch.backends.cudnn.deterministic = False
# Disable 'benchmark' mode: Set this False if you want to measure running times more fairly
# Note: https://discuss.pytorch.org/t/what-does-torch-backends-cudnn-benchmark-do/5936
torch.backends.cudnn.benchmark = True
# Faster Host to GPU copies with page-locked memory
use_pin_memory = True
# CUDA libraries version information
print("CUDA Version: " + str(torch.version.cuda))
print("cuDNN Version: " + str(torch.backends.cudnn.version()))
print("CUDA Device Name: " + str(torch.cuda.get_device_name()))
print("CUDA Capabilities: "+ str(torch.cuda.get_device_capability()))
elif use_colab_tpu and colab_tpu_available:
# This needs to be installed separately
# https://github.com/pytorch/xla/blob/master/contrib/colab/getting-started.ipynb
import torch_xla
import torch_xla.core.xla_model as xm
torch_device = xm.xla_device()
else:
torch_device = torch.device('cpu')
use_pin_memory = False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment