2022-08-31 04:33:23 +00:00
|
|
|
import torch
|
2022-09-06 00:40:10 +00:00
|
|
|
from torch import autocast
|
2022-09-17 17:56:25 +00:00
|
|
|
from contextlib import nullcontext
|
2022-08-31 04:33:23 +00:00
|
|
|
|
|
|
|
def choose_torch_device() -> str:
|
|
|
|
'''Convenience routine for guessing which GPU device to run model on'''
|
|
|
|
if torch.cuda.is_available():
|
|
|
|
return 'cuda'
|
2022-08-31 14:56:38 +00:00
|
|
|
if hasattr(torch.backends, 'mps') and torch.backends.mps.is_available():
|
2022-08-31 04:33:23 +00:00
|
|
|
return 'mps'
|
|
|
|
return 'cpu'
|
|
|
|
|
2022-09-17 17:56:25 +00:00
|
|
|
def choose_precision(device) -> str:
|
|
|
|
'''Returns an appropriate precision for the given torch device'''
|
|
|
|
if device.type == 'cuda':
|
|
|
|
device_name = torch.cuda.get_device_name(device)
|
|
|
|
if not ('GeForce GTX 1660' in device_name or 'GeForce GTX 1650' in device_name):
|
|
|
|
return 'float16'
|
|
|
|
return 'float32'
|
|
|
|
|
|
|
|
def choose_autocast(precision):
|
|
|
|
'''Returns an autocast context or nullcontext for the given precision string'''
|
|
|
|
# float16 currently requires autocast to avoid errors like:
|
|
|
|
# 'expected scalar type Half but found Float'
|
|
|
|
if precision == 'autocast' or precision == 'float16':
|
|
|
|
return autocast
|
|
|
|
return nullcontext
|