2023-02-18 00:29:03 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from contextlib import nullcontext
|
|
|
|
|
2022-08-31 04:33:23 +00:00
|
|
|
import torch
|
2022-09-06 00:40:10 +00:00
|
|
|
from torch import autocast
|
2023-07-03 14:55:04 +00:00
|
|
|
from typing import Union
|
2023-05-26 00:41:26 +00:00
|
|
|
from invokeai.app.services.config import InvokeAIAppConfig
|
2022-08-31 04:33:23 +00:00
|
|
|
|
2023-02-18 00:29:03 +00:00
|
|
|
CPU_DEVICE = torch.device("cpu")
|
2023-03-03 05:02:15 +00:00
|
|
|
CUDA_DEVICE = torch.device("cuda")
|
|
|
|
MPS_DEVICE = torch.device("mps")
|
2023-05-26 00:41:26 +00:00
|
|
|
config = InvokeAIAppConfig.get_config()
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-02-18 00:29:03 +00:00
|
|
|
def choose_torch_device() -> torch.device:
|
2023-03-03 06:02:00 +00:00
|
|
|
"""Convenience routine for guessing which GPU device to run model on"""
|
2023-05-04 03:36:51 +00:00
|
|
|
if config.always_use_cpu:
|
2023-02-18 00:29:03 +00:00
|
|
|
return CPU_DEVICE
|
2022-08-31 04:33:23 +00:00
|
|
|
if torch.cuda.is_available():
|
2023-03-03 06:02:00 +00:00
|
|
|
return torch.device("cuda")
|
|
|
|
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
|
|
|
|
return torch.device("mps")
|
2023-02-18 00:29:03 +00:00
|
|
|
return CPU_DEVICE
|
2022-08-31 04:33:23 +00:00
|
|
|
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-02-18 00:29:03 +00:00
|
|
|
def choose_precision(device: torch.device) -> str:
|
2023-03-03 06:02:00 +00:00
|
|
|
"""Returns an appropriate precision for the given torch device"""
|
|
|
|
if device.type == "cuda":
|
2022-09-17 17:56:25 +00:00
|
|
|
device_name = torch.cuda.get_device_name(device)
|
2023-03-03 06:02:00 +00:00
|
|
|
if not ("GeForce GTX 1660" in device_name or "GeForce GTX 1650" in device_name):
|
|
|
|
return "float16"
|
2023-07-04 22:05:01 +00:00
|
|
|
elif device.type == "mps":
|
|
|
|
return "float16"
|
2023-03-03 06:02:00 +00:00
|
|
|
return "float32"
|
|
|
|
|
2022-09-17 17:56:25 +00:00
|
|
|
|
2023-02-18 00:29:03 +00:00
|
|
|
def torch_dtype(device: torch.device) -> torch.dtype:
|
2023-05-04 03:36:51 +00:00
|
|
|
if config.full_precision:
|
2023-01-17 00:32:06 +00:00
|
|
|
return torch.float32
|
2023-03-03 06:02:00 +00:00
|
|
|
if choose_precision(device) == "float16":
|
2023-01-17 00:32:06 +00:00
|
|
|
return torch.float16
|
|
|
|
else:
|
|
|
|
return torch.float32
|
|
|
|
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2022-09-17 17:56:25 +00:00
|
|
|
def choose_autocast(precision):
|
2023-03-03 06:02:00 +00:00
|
|
|
"""Returns an autocast context or nullcontext for the given precision string"""
|
2022-09-17 17:56:25 +00:00
|
|
|
# float16 currently requires autocast to avoid errors like:
|
|
|
|
# 'expected scalar type Half but found Float'
|
2023-03-03 06:02:00 +00:00
|
|
|
if precision == "autocast" or precision == "float16":
|
2022-09-17 17:56:25 +00:00
|
|
|
return autocast
|
|
|
|
return nullcontext
|
2023-02-18 00:29:03 +00:00
|
|
|
|
2023-03-03 06:02:00 +00:00
|
|
|
|
2023-07-03 14:55:04 +00:00
|
|
|
def normalize_device(device: Union[str, torch.device]) -> torch.device:
|
2023-02-18 00:29:03 +00:00
|
|
|
"""Ensure device has a device index defined, if appropriate."""
|
|
|
|
device = torch.device(device)
|
|
|
|
if device.index is None:
|
|
|
|
# cuda might be the only torch backend that currently uses the device index?
|
|
|
|
# I don't see anything like `current_device` for cpu or mps.
|
2023-03-03 06:02:00 +00:00
|
|
|
if device.type == "cuda":
|
2023-02-18 00:29:03 +00:00
|
|
|
device = torch.device(device.type, torch.cuda.current_device())
|
|
|
|
return device
|