Spaces:
Running
on
Zero
Running
on
Zero
File size: 6,984 Bytes
829e08b |
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 |
from math import ceil
from pathlib import Path
import os
import re
from beartype.typing import Tuple
from einops import rearrange, repeat
from toolz import valmap
import torch
from torch import Tensor
from torch.nn import Module
import torch.nn.functional as F
import yaml
from .typing import typecheck
def count_parameters(model):
return sum(p.numel() for p in model.parameters() if p.requires_grad)
def path_mkdir(path):
path = Path(path)
path.mkdir(parents=True, exist_ok=True)
return path
def load_yaml(path, default_path=None):
path = path_exists(path)
with open(path, mode='r') as fp:
cfg_s = yaml.load(fp, Loader=yaml.FullLoader)
if default_path is not None:
default_path = path_exists(default_path)
with open(default_path, mode='r') as fp:
cfg = yaml.load(fp, Loader=yaml.FullLoader)
else:
# try current dir default
default_path = path.parent / 'default.yml'
if default_path.exists():
with open(default_path, mode='r') as fp:
cfg = yaml.load(fp, Loader=yaml.FullLoader)
else:
cfg = {}
update_recursive(cfg, cfg_s)
return cfg
def dump_yaml(cfg, path):
with open(path, mode='w') as f:
return yaml.safe_dump(cfg, f)
def update_recursive(dict1, dict2):
''' Update two config dictionaries recursively.
Args:
dict1 (dict): first dictionary to be updated
dict2 (dict): second dictionary which entries should be used
'''
for k, v in dict2.items():
if k not in dict1:
dict1[k] = dict()
if isinstance(v, dict):
update_recursive(dict1[k], v)
else:
dict1[k] = v
def load_latest_checkpoint(checkpoint_dir):
pattern = re.compile(rf".+\.ckpt\.(\d+)\.pt")
max_epoch = -1
latest_checkpoint = None
for filename in os.listdir(checkpoint_dir):
match = pattern.match(filename)
if match:
num_epoch = int(match.group(1))
if num_epoch > max_epoch:
max_epoch = num_epoch
latest_checkpoint = checkpoint_dir / filename
if not exists(latest_checkpoint):
raise FileNotFoundError(f"No checkpoint files found in {checkpoint_dir}")
checkpoint = torch.load(latest_checkpoint)
return checkpoint, latest_checkpoint
def torch_to(inp, device, non_blocking=False):
nb = non_blocking # set to True when doing distributed jobs
if isinstance(inp, torch.Tensor):
return inp.to(device, non_blocking=nb)
elif isinstance(inp, (list, tuple)):
return type(inp)(map(lambda t: t.to(device, non_blocking=nb) if isinstance(t, torch.Tensor) else t, inp))
elif isinstance(inp, dict):
return valmap(lambda t: t.to(device, non_blocking=nb) if isinstance(t, torch.Tensor) else t, inp)
else:
raise NotImplementedError
# helper functions
def exists(v):
return v is not None
def default(v, d):
return v if exists(v) else d
def first(it):
return it[0]
def identity(t, *args, **kwargs):
return t
def divisible_by(num, den):
return (num % den) == 0
def is_odd(n):
return not divisible_by(n, 2)
def is_empty(x):
return len(x) == 0
def is_tensor_empty(t: Tensor):
return t.numel() == 0
def set_module_requires_grad_(
module: Module,
requires_grad: bool
):
for param in module.parameters():
param.requires_grad = requires_grad
def l1norm(t):
return F.normalize(t, dim = -1, p = 1)
def l2norm(t):
return F.normalize(t, dim = -1, p = 2)
def safe_cat(tensors, dim):
tensors = [*filter(exists, tensors)]
if len(tensors) == 0:
return None
elif len(tensors) == 1:
return first(tensors)
return torch.cat(tensors, dim = dim)
def pad_at_dim(t, padding, dim = -1, value = 0):
ndim = t.ndim
right_dims = (ndim - dim - 1) if dim >= 0 else (-dim - 1)
zeros = (0, 0) * right_dims
return F.pad(t, (*zeros, *padding), value = value)
def pad_to_length(t, length, dim = -1, value = 0, right = True):
curr_length = t.shape[dim]
remainder = length - curr_length
if remainder <= 0:
return t
padding = (0, remainder) if right else (remainder, 0)
return pad_at_dim(t, padding, dim = dim, value = value)
def masked_mean(tensor, mask, dim = -1, eps = 1e-5):
if not exists(mask):
return tensor.mean(dim = dim)
mask = rearrange(mask, '... -> ... 1')
tensor = tensor.masked_fill(~mask, 0.)
total_el = mask.sum(dim = dim)
num = tensor.sum(dim = dim)
den = total_el.float().clamp(min = eps)
mean = num / den
mean = mean.masked_fill(total_el == 0, 0.)
return mean
def cycle(dl):
while True:
for data in dl:
yield data
def maybe_del(d: dict, *keys):
for key in keys:
if key not in d:
continue
del d[key]
# tensor helper functions
@typecheck
def discretize(
t: Tensor,
*,
continuous_range: Tuple[float, float],
num_discrete: int = 128
) -> Tensor:
lo, hi = continuous_range
assert hi > lo
t = (t - lo) / (hi - lo)
t *= num_discrete
t -= 0.5
return t.round().long().clamp(min = 0, max = num_discrete - 1)
@typecheck
def undiscretize(
t: Tensor,
*,
continuous_range = Tuple[float, float],
num_discrete: int = 128
) -> Tensor:
lo, hi = continuous_range
assert hi > lo
t = t.float()
t += 0.5
t /= num_discrete
return t * (hi - lo) + lo
@typecheck
def gaussian_blur_1d(
t: Tensor,
*,
sigma: float = 1.,
kernel_size: int = 5
) -> Tensor:
_, _, channels, device, dtype = *t.shape, t.device, t.dtype
width = int(ceil(sigma * kernel_size))
width += (width + 1) % 2
half_width = width // 2
distance = torch.arange(-half_width, half_width + 1, dtype = dtype, device = device)
gaussian = torch.exp(-(distance ** 2) / (2 * sigma ** 2))
gaussian = l1norm(gaussian)
kernel = repeat(gaussian, 'n -> c 1 n', c = channels)
t = rearrange(t, 'b n c -> b c n')
out = F.conv1d(t, kernel, padding = half_width, groups = channels)
return rearrange(out, 'b c n -> b n c')
@typecheck
def scatter_mean(
tgt: Tensor,
indices: Tensor,
src = Tensor,
*,
dim: int = -1,
eps: float = 1e-5
):
"""
todo: update to pytorch 2.1 and try https://pytorch.org/docs/stable/generated/torch.Tensor.scatter_reduce_.html#torch.Tensor.scatter_reduce_
"""
num = tgt.scatter_add(dim, indices, src)
den = torch.zeros_like(tgt).scatter_add(dim, indices, torch.ones_like(src))
return num / den.clamp(min = eps)
def prob_mask_like(shape, prob, device):
if prob == 1:
return torch.ones(shape, device = device, dtype = torch.bool)
elif prob == 0:
return torch.zeros(shape, device = device, dtype = torch.bool)
else:
return torch.zeros(shape, device = device).float().uniform_(0, 1) < prob |