-
Notifications
You must be signed in to change notification settings - Fork 14
/
util.py
132 lines (103 loc) · 4 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import gc
import os
from contextlib import contextmanager
import torch
from torch.nn.functional import pad
# TODO: this sucks
COMFY_PATH = os.path.realpath(os.path.join(os.path.dirname(__file__), "..", ".."))
from folder_paths import (
models_dir,
get_output_directory,
get_temp_directory,
get_save_image_path,
)
def do_cleanup(cuda_cache=True):
gc.collect()
if cuda_cache:
torch.cuda.empty_cache()
def get_device():
return "cuda" if torch.cuda.is_available() else "cpu"
def tensors_to(tensors, device):
if isinstance(tensors, torch.Tensor):
return tensors.to(device)
if hasattr(tensors, "__dict__"):
return object_to(tensors, device, empty_cuda_cache=False)
if isinstance(tensors, (list, tuple)):
return [tensors_to(x, device) for x in tensors]
if isinstance(tensors, dict):
return {k: tensors_to(v, device) for k, v in tensors.items()}
if isinstance(tensors, set):
return {tensors_to(x, device) for x in tensors}
return tensors
def tensors_to_cuda(tensors):
return tensors_to(tensors, "cuda")
def tensors_to_cpu(tensors):
return tensors_to(tensors, "cpu")
def object_to(obj, device=None, exclude=None, empty_cuda_cache=True, verbose=False):
"""
recurse through an object and move any pytorch tensors/parameters/modules to the given device.
if device is None, cpu is used by default. if the device is a CUDA device and empty_cuda_cache is
enabled, this will also free unused CUDA memory cached by pytorch.
"""
if not hasattr(obj, "__dict__"):
return obj
classname = type(obj).__name__
exclude = exclude or set()
device = device or "cpu"
def _move_and_recurse(o, name=""):
child_moved = False
for k, v in vars(o).items():
moved = False
cur_name = f"{name}.{k}" if name != "" else k
if cur_name in exclude:
continue
if isinstance(v, (torch.nn.Module, torch.nn.Parameter, torch.Tensor)):
setattr(o, k, v.to(device))
moved = True
elif hasattr(v, "__dict__"):
v, moved = _move_and_recurse(v, name=cur_name)
if moved: setattr(o, k, v)
if verbose and moved:
print(f"moved {classname}.{cur_name} to {device}")
child_moved |= moved
return o, child_moved
if isinstance(obj, torch.nn.Module):
obj = obj.to(device)
obj, _ = _move_and_recurse(obj)
if "cuda" in device and empty_cuda_cache:
torch.cuda.empty_cache()
return obj
@contextmanager
def obj_on_device(model, src="cpu", dst="cuda", exclude=None, empty_cuda_cache=True, verbose_move=False):
model = object_to(model, dst, exclude=exclude, empty_cuda_cache=empty_cuda_cache, verbose=verbose_move)
yield model
model = object_to(model, src, exclude=exclude, empty_cuda_cache=empty_cuda_cache, verbose=verbose_move)
@contextmanager
def on_device(model, src="cpu", dst="cuda", empty_cuda_cache=True, **kwargs):
model = model.to(dst)
yield model
model = model.to(src)
if empty_cuda_cache:
torch.cuda.empty_cache()
def stack_audio_tensors(tensors, mode="pad"):
# assert all(len(x.shape) == 2 for x in tensors)
sizes = [x.shape[-1] for x in tensors]
if mode in {"pad_l", "pad_r", "pad"}:
# pad input tensors to be equal length
dst_size = max(sizes)
stack_tensors = (
[pad(x, pad=(0, dst_size - x.shape[-1])) for x in tensors]
if mode == "pad_r"
else [pad(x, pad=(dst_size - x.shape[-1], 0)) for x in tensors]
)
elif mode in {"trunc_l", "trunc_r", "trunc"}:
# truncate input tensors to be equal length
dst_size = min(sizes)
stack_tensors = (
[x[:, x.shape[-1] - dst_size:] for x in tensors]
if mode == "trunc_r"
else [x[:, :dst_size] for x in tensors]
)
else:
assert False, 'unknown mode "{pad}"'
return torch.stack(stack_tensors)