-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathhelper.py
255 lines (190 loc) · 7.89 KB
/
helper.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
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
import re
import torch
from comfy.samplers import SCHEDULER_NAMES
import torch.nn.functional as F
def get_extra_options_kv(key, default, extra_options):
match = re.search(rf"{key}\s*=\s*([a-zA-Z0-9_.+-]+)", extra_options)
if match:
value = match.group(1)
else:
value = default
return value
def get_extra_options_list(key, default, extra_options):
match = re.search(rf"{key}\s*=\s*([a-zA-Z0-9_.,+-]+)", extra_options)
if match:
value = match.group(1)
else:
value = default
return value
def extra_options_flag(flag, extra_options):
return bool(re.search(rf"{flag}", extra_options))
def safe_get_nested(d, keys, default=None):
for key in keys:
if isinstance(d, dict):
d = d.get(key, default)
else:
return default
return d
def is_video_model(model):
is_video_model = False
try :
is_video_model = 'video' in model.inner_model.inner_model.model_config.unet_config['image_model'] or \
'cosmos' in model.inner_model.inner_model.model_config.unet_config['image_model']
except:
pass
return is_video_model
def is_RF_model(model):
from comfy import model_sampling
modelsampling = model.inner_model.inner_model.model_sampling
return isinstance(modelsampling, model_sampling.CONST)
def lagrange_interpolation(x_values, y_values, x_new):
if not isinstance(x_values, torch.Tensor):
x_values = torch.tensor(x_values, dtype=torch.get_default_dtype())
if x_values.ndim != 1:
raise ValueError("x_values must be a 1D tensor or a list of scalars.")
if not isinstance(x_new, torch.Tensor):
x_new = torch.tensor(x_new, dtype=x_values.dtype, device=x_values.device)
if x_new.ndim == 0:
x_new = x_new.unsqueeze(0)
if isinstance(y_values, list):
y_values = torch.stack(y_values, dim=0)
if y_values.ndim < 1:
raise ValueError("y_values must have at least one dimension (the sample dimension).")
n = x_values.shape[0]
if y_values.shape[0] != n:
raise ValueError(f"Mismatch: x_values has length {n} but y_values has {y_values.shape[0]} samples.")
m = x_new.shape[0]
result_shape = (m,) + y_values.shape[1:]
result = torch.zeros(result_shape, dtype=y_values.dtype, device=y_values.device)
for i in range(n):
Li = torch.ones_like(x_new, dtype=y_values.dtype, device=y_values.device)
xi = x_values[i]
for j in range(n):
if i == j:
continue
xj = x_values[j]
Li = Li * ((x_new - xj) / (xi - xj))
extra_dims = (1,) * (y_values.ndim - 1)
Li = Li.view(m, *extra_dims)
result = result + Li * y_values[i]
return result
def get_cosine_similarity_manual(a, b):
return (a * b).sum() / (torch.norm(a) * torch.norm(b))
def get_cosine_similarity(a, b):
if a.dim() == 5 and b.dim() == 5 and b.shape[2] == 1:
b = b.expand(-1, -1, a.shape[2], -1, -1)
return F.cosine_similarity(a.flatten(), b.flatten(), dim=0)
def get_pearson_similarity(a, b):
a = a.mean(dim=(-2,-1))
b = b.mean(dim=(-2,-1))
if a.dim() == 5 and b.dim() == 5 and b.shape[2] == 1:
b = b.expand(-1, -1, a.shape[2], -1, -1)
return F.cosine_similarity(a.flatten(), b.flatten(), dim=0)
def initialize_or_scale(tensor, value, steps):
if tensor is None:
return torch.full((steps,), value)
else:
return value * tensor
def has_nested_attr(obj, attr_path):
attrs = attr_path.split('.')
for attr in attrs:
if not hasattr(obj, attr):
return False
obj = getattr(obj, attr)
return True
def get_res4lyf_scheduler_list():
scheduler_names = SCHEDULER_NAMES.copy()
if "beta57" not in scheduler_names:
scheduler_names.append("beta57")
return scheduler_names
def conditioning_set_values(conditioning, values={}):
c = []
for t in conditioning:
n = [t[0], t[1].copy()]
for k in values:
n[1][k] = values[k]
c.append(n)
return c
def get_collinear_alt(x, y):
y_flat = y.view(y.size(0), -1).clone()
x_flat = x.view(x.size(0), -1).clone()
y_flat /= y_flat.norm(dim=-1, keepdim=True)
x_proj_y = torch.sum(x_flat * y_flat, dim=-1, keepdim=True) * y_flat
return x_proj_y.view_as(x)
def get_collinear(x, y):
y_flat = y.view(y.size(0), -1).clone()
x_flat = x.view(x.size(0), -1).clone()
y_flat /= y_flat.norm(dim=-1, keepdim=True)
x_proj_y = torch.sum(x_flat * y_flat, dim=-1, keepdim=True) * y_flat
return x_proj_y.view_as(x)
def get_orthogonal(x, y):
y_flat = y.view(y.size(0), -1).clone()
x_flat = x.view(x.size(0), -1).clone()
y_flat /= y_flat.norm(dim=-1, keepdim=True)
x_proj_y = torch.sum(x_flat * y_flat, dim=-1, keepdim=True) * y_flat
x_ortho_y = x_flat - x_proj_y
return x_ortho_y.view_as(x)
# pytorch slerp implementation from https://gist.github.com/Birch-san/230ac46f99ec411ed5907b0a3d728efa
from torch import FloatTensor, LongTensor, Tensor, Size, lerp, zeros_like
from torch.linalg import norm
# adapted to PyTorch from:
# https://gist.github.com/dvschultz/3af50c40df002da3b751efab1daddf2c
# most of the extra complexity is to support:
# - many-dimensional vectors
# - v0 or v1 with last dim all zeroes, or v0 ~colinear with v1
# - falls back to lerp()
# - conditional logic implemented with parallelism rather than Python loops
# - many-dimensional tensor for t
# - you can ask for batches of slerp outputs by making t more-dimensional than the vectors
# - slerp(
# v0: torch.Size([2,3]),
# v1: torch.Size([2,3]),
# t: torch.Size([4,1,1]),
# )
# - this makes it interface-compatible with lerp()
def slerp(v0: FloatTensor, v1: FloatTensor, t: float|FloatTensor, DOT_THRESHOLD=0.9995):
'''
Spherical linear interpolation
Args:
v0: Starting vector
v1: Final vector
t: Float value between 0.0 and 1.0
DOT_THRESHOLD: Threshold for considering the two vectors as
colinear. Not recommended to alter this.
Returns:
Interpolation vector between v0 and v1
'''
assert v0.shape == v1.shape, "shapes of v0 and v1 must match"
# Normalize the vectors to get the directions and angles
v0_norm: FloatTensor = norm(v0, dim=-1)
v1_norm: FloatTensor = norm(v1, dim=-1)
v0_normed: FloatTensor = v0 / v0_norm.unsqueeze(-1)
v1_normed: FloatTensor = v1 / v1_norm.unsqueeze(-1)
# Dot product with the normalized vectors
dot: FloatTensor = (v0_normed * v1_normed).sum(-1)
dot_mag: FloatTensor = dot.abs()
# if dp is NaN, it's because the v0 or v1 row was filled with 0s
# If absolute value of dot product is almost 1, vectors are ~colinear, so use lerp
gotta_lerp: LongTensor = dot_mag.isnan() | (dot_mag > DOT_THRESHOLD)
can_slerp: LongTensor = ~gotta_lerp
t_batch_dim_count: int = max(0, t.dim()-v0.dim()) if isinstance(t, Tensor) else 0
t_batch_dims: Size = t.shape[:t_batch_dim_count] if isinstance(t, Tensor) else Size([])
out: FloatTensor = zeros_like(v0.expand(*t_batch_dims, *[-1]*v0.dim()))
# if no elements are lerpable, our vectors become 0-dimensional, preventing broadcasting
if gotta_lerp.any():
lerped: FloatTensor = lerp(v0, v1, t)
out: FloatTensor = lerped.where(gotta_lerp.unsqueeze(-1), out)
# if no elements are slerpable, our vectors become 0-dimensional, preventing broadcasting
if can_slerp.any():
# Calculate initial angle between v0 and v1
theta_0: FloatTensor = dot.arccos().unsqueeze(-1)
sin_theta_0: FloatTensor = theta_0.sin()
# Angle at timestep t
theta_t: FloatTensor = theta_0 * t
sin_theta_t: FloatTensor = theta_t.sin()
# Finish the slerp algorithm
s0: FloatTensor = (theta_0 - theta_t).sin() / sin_theta_0
s1: FloatTensor = sin_theta_t / sin_theta_0
slerped: FloatTensor = s0 * v0 + s1 * v1
out: FloatTensor = slerped.where(can_slerp.unsqueeze(-1), out)
return out