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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import cv2
|
3 |
+
import json
|
4 |
+
import random
|
5 |
+
import glob
|
6 |
+
import torch
|
7 |
+
import einops
|
8 |
+
import numpy as np
|
9 |
+
import datetime
|
10 |
+
import torchvision
|
11 |
+
|
12 |
+
import safetensors.torch as sf
|
13 |
+
from PIL import Image
|
14 |
+
|
15 |
+
|
16 |
+
def min_resize(x, m):
|
17 |
+
if x.shape[0] < x.shape[1]:
|
18 |
+
s0 = m
|
19 |
+
s1 = int(float(m) / float(x.shape[0]) * float(x.shape[1]))
|
20 |
+
else:
|
21 |
+
s0 = int(float(m) / float(x.shape[1]) * float(x.shape[0]))
|
22 |
+
s1 = m
|
23 |
+
new_max = max(s1, s0)
|
24 |
+
raw_max = max(x.shape[0], x.shape[1])
|
25 |
+
if new_max < raw_max:
|
26 |
+
interpolation = cv2.INTER_AREA
|
27 |
+
else:
|
28 |
+
interpolation = cv2.INTER_LANCZOS4
|
29 |
+
y = cv2.resize(x, (s1, s0), interpolation=interpolation)
|
30 |
+
return y
|
31 |
+
|
32 |
+
|
33 |
+
def d_resize(x, y):
|
34 |
+
H, W, C = y.shape
|
35 |
+
new_min = min(H, W)
|
36 |
+
raw_min = min(x.shape[0], x.shape[1])
|
37 |
+
if new_min < raw_min:
|
38 |
+
interpolation = cv2.INTER_AREA
|
39 |
+
else:
|
40 |
+
interpolation = cv2.INTER_LANCZOS4
|
41 |
+
y = cv2.resize(x, (W, H), interpolation=interpolation)
|
42 |
+
return y
|
43 |
+
|
44 |
+
|
45 |
+
def resize_and_center_crop(image, target_width, target_height):
|
46 |
+
if target_height == image.shape[0] and target_width == image.shape[1]:
|
47 |
+
return image
|
48 |
+
|
49 |
+
pil_image = Image.fromarray(image)
|
50 |
+
original_width, original_height = pil_image.size
|
51 |
+
scale_factor = max(target_width / original_width, target_height / original_height)
|
52 |
+
resized_width = int(round(original_width * scale_factor))
|
53 |
+
resized_height = int(round(original_height * scale_factor))
|
54 |
+
resized_image = pil_image.resize((resized_width, resized_height), Image.LANCZOS)
|
55 |
+
left = (resized_width - target_width) / 2
|
56 |
+
top = (resized_height - target_height) / 2
|
57 |
+
right = (resized_width + target_width) / 2
|
58 |
+
bottom = (resized_height + target_height) / 2
|
59 |
+
cropped_image = resized_image.crop((left, top, right, bottom))
|
60 |
+
return np.array(cropped_image)
|
61 |
+
|
62 |
+
|
63 |
+
def resize_and_center_crop_pytorch(image, target_width, target_height):
|
64 |
+
B, C, H, W = image.shape
|
65 |
+
|
66 |
+
if H == target_height and W == target_width:
|
67 |
+
return image
|
68 |
+
|
69 |
+
scale_factor = max(target_width / W, target_height / H)
|
70 |
+
resized_width = int(round(W * scale_factor))
|
71 |
+
resized_height = int(round(H * scale_factor))
|
72 |
+
|
73 |
+
resized = torch.nn.functional.interpolate(image, size=(resized_height, resized_width), mode='bilinear', align_corners=False)
|
74 |
+
|
75 |
+
top = (resized_height - target_height) // 2
|
76 |
+
left = (resized_width - target_width) // 2
|
77 |
+
cropped = resized[:, :, top:top + target_height, left:left + target_width]
|
78 |
+
|
79 |
+
return cropped
|
80 |
+
|
81 |
+
|
82 |
+
def resize_without_crop(image, target_width, target_height):
|
83 |
+
if target_height == image.shape[0] and target_width == image.shape[1]:
|
84 |
+
return image
|
85 |
+
|
86 |
+
pil_image = Image.fromarray(image)
|
87 |
+
resized_image = pil_image.resize((target_width, target_height), Image.LANCZOS)
|
88 |
+
return np.array(resized_image)
|
89 |
+
|
90 |
+
|
91 |
+
def just_crop(image, w, h):
|
92 |
+
if h == image.shape[0] and w == image.shape[1]:
|
93 |
+
return image
|
94 |
+
|
95 |
+
original_height, original_width = image.shape[:2]
|
96 |
+
k = min(original_height / h, original_width / w)
|
97 |
+
new_width = int(round(w * k))
|
98 |
+
new_height = int(round(h * k))
|
99 |
+
x_start = (original_width - new_width) // 2
|
100 |
+
y_start = (original_height - new_height) // 2
|
101 |
+
cropped_image = image[y_start:y_start + new_height, x_start:x_start + new_width]
|
102 |
+
return cropped_image
|
103 |
+
|
104 |
+
|
105 |
+
def write_to_json(data, file_path):
|
106 |
+
temp_file_path = file_path + ".tmp"
|
107 |
+
with open(temp_file_path, 'wt', encoding='utf-8') as temp_file:
|
108 |
+
json.dump(data, temp_file, indent=4)
|
109 |
+
os.replace(temp_file_path, file_path)
|
110 |
+
return
|
111 |
+
|
112 |
+
|
113 |
+
def read_from_json(file_path):
|
114 |
+
with open(file_path, 'rt', encoding='utf-8') as file:
|
115 |
+
data = json.load(file)
|
116 |
+
return data
|
117 |
+
|
118 |
+
|
119 |
+
def get_active_parameters(m):
|
120 |
+
return {k: v for k, v in m.named_parameters() if v.requires_grad}
|
121 |
+
|
122 |
+
|
123 |
+
def cast_training_params(m, dtype=torch.float32):
|
124 |
+
result = {}
|
125 |
+
for n, param in m.named_parameters():
|
126 |
+
if param.requires_grad:
|
127 |
+
param.data = param.to(dtype)
|
128 |
+
result[n] = param
|
129 |
+
return result
|
130 |
+
|
131 |
+
|
132 |
+
def separate_lora_AB(parameters, B_patterns=None):
|
133 |
+
parameters_normal = {}
|
134 |
+
parameters_B = {}
|
135 |
+
|
136 |
+
if B_patterns is None:
|
137 |
+
B_patterns = ['.lora_B.', '__zero__']
|
138 |
+
|
139 |
+
for k, v in parameters.items():
|
140 |
+
if any(B_pattern in k for B_pattern in B_patterns):
|
141 |
+
parameters_B[k] = v
|
142 |
+
else:
|
143 |
+
parameters_normal[k] = v
|
144 |
+
|
145 |
+
return parameters_normal, parameters_B
|
146 |
+
|
147 |
+
|
148 |
+
def set_attr_recursive(obj, attr, value):
|
149 |
+
attrs = attr.split(".")
|
150 |
+
for name in attrs[:-1]:
|
151 |
+
obj = getattr(obj, name)
|
152 |
+
setattr(obj, attrs[-1], value)
|
153 |
+
return
|
154 |
+
|
155 |
+
|
156 |
+
def print_tensor_list_size(tensors):
|
157 |
+
total_size = 0
|
158 |
+
total_elements = 0
|
159 |
+
|
160 |
+
if isinstance(tensors, dict):
|
161 |
+
tensors = tensors.values()
|
162 |
+
|
163 |
+
for tensor in tensors:
|
164 |
+
total_size += tensor.nelement() * tensor.element_size()
|
165 |
+
total_elements += tensor.nelement()
|
166 |
+
|
167 |
+
total_size_MB = total_size / (1024 ** 2)
|
168 |
+
total_elements_B = total_elements / 1e9
|
169 |
+
|
170 |
+
print(f"Total number of tensors: {len(tensors)}")
|
171 |
+
print(f"Total size of tensors: {total_size_MB:.2f} MB")
|
172 |
+
print(f"Total number of parameters: {total_elements_B:.3f} billion")
|
173 |
+
return
|
174 |
+
|
175 |
+
|
176 |
+
@torch.no_grad()
|
177 |
+
def batch_mixture(a, b=None, probability_a=0.5, mask_a=None):
|
178 |
+
batch_size = a.size(0)
|
179 |
+
|
180 |
+
if b is None:
|
181 |
+
b = torch.zeros_like(a)
|
182 |
+
|
183 |
+
if mask_a is None:
|
184 |
+
mask_a = torch.rand(batch_size) < probability_a
|
185 |
+
|
186 |
+
mask_a = mask_a.to(a.device)
|
187 |
+
mask_a = mask_a.reshape((batch_size,) + (1,) * (a.dim() - 1))
|
188 |
+
result = torch.where(mask_a, a, b)
|
189 |
+
return result
|
190 |
+
|
191 |
+
|
192 |
+
@torch.no_grad()
|
193 |
+
def zero_module(module):
|
194 |
+
for p in module.parameters():
|
195 |
+
p.detach().zero_()
|
196 |
+
return module
|
197 |
+
|
198 |
+
|
199 |
+
@torch.no_grad()
|
200 |
+
def supress_lower_channels(m, k, alpha=0.01):
|
201 |
+
data = m.weight.data.clone()
|
202 |
+
|
203 |
+
assert int(data.shape[1]) >= k
|
204 |
+
|
205 |
+
data[:, :k] = data[:, :k] * alpha
|
206 |
+
m.weight.data = data.contiguous().clone()
|
207 |
+
return m
|
208 |
+
|
209 |
+
|
210 |
+
def freeze_module(m):
|
211 |
+
if not hasattr(m, '_forward_inside_frozen_module'):
|
212 |
+
m._forward_inside_frozen_module = m.forward
|
213 |
+
m.requires_grad_(False)
|
214 |
+
m.forward = torch.no_grad()(m.forward)
|
215 |
+
return m
|
216 |
+
|
217 |
+
|
218 |
+
def get_latest_safetensors(folder_path):
|
219 |
+
safetensors_files = glob.glob(os.path.join(folder_path, '*.safetensors'))
|
220 |
+
|
221 |
+
if not safetensors_files:
|
222 |
+
raise ValueError('No file to resume!')
|
223 |
+
|
224 |
+
latest_file = max(safetensors_files, key=os.path.getmtime)
|
225 |
+
latest_file = os.path.abspath(os.path.realpath(latest_file))
|
226 |
+
return latest_file
|
227 |
+
|
228 |
+
|
229 |
+
def generate_random_prompt_from_tags(tags_str, min_length=3, max_length=32):
|
230 |
+
tags = tags_str.split(', ')
|
231 |
+
tags = random.sample(tags, k=min(random.randint(min_length, max_length), len(tags)))
|
232 |
+
prompt = ', '.join(tags)
|
233 |
+
return prompt
|
234 |
+
|
235 |
+
|
236 |
+
def interpolate_numbers(a, b, n, round_to_int=False, gamma=1.0):
|
237 |
+
numbers = a + (b - a) * (np.linspace(0, 1, n) ** gamma)
|
238 |
+
if round_to_int:
|
239 |
+
numbers = np.round(numbers).astype(int)
|
240 |
+
return numbers.tolist()
|
241 |
+
|
242 |
+
|
243 |
+
def uniform_random_by_intervals(inclusive, exclusive, n, round_to_int=False):
|
244 |
+
edges = np.linspace(0, 1, n + 1)
|
245 |
+
points = np.random.uniform(edges[:-1], edges[1:])
|
246 |
+
numbers = inclusive + (exclusive - inclusive) * points
|
247 |
+
if round_to_int:
|
248 |
+
numbers = np.round(numbers).astype(int)
|
249 |
+
return numbers.tolist()
|
250 |
+
|
251 |
+
|
252 |
+
def soft_append_bcthw(history, current, overlap=0):
|
253 |
+
if overlap <= 0:
|
254 |
+
return torch.cat([history, current], dim=2)
|
255 |
+
|
256 |
+
assert history.shape[2] >= overlap, f"History length ({history.shape[2]}) must be >= overlap ({overlap})"
|
257 |
+
assert current.shape[2] >= overlap, f"Current length ({current.shape[2]}) must be >= overlap ({overlap})"
|
258 |
+
|
259 |
+
weights = torch.linspace(1, 0, overlap, dtype=history.dtype, device=history.device).view(1, 1, -1, 1, 1)
|
260 |
+
blended = weights * history[:, :, -overlap:] + (1 - weights) * current[:, :, :overlap]
|
261 |
+
output = torch.cat([history[:, :, :-overlap], blended, current[:, :, overlap:]], dim=2)
|
262 |
+
|
263 |
+
return output.to(history)
|
264 |
+
|
265 |
+
|
266 |
+
def save_bcthw_as_mp4(x, output_filename, fps=10, crf=0):
|
267 |
+
b, c, t, h, w = x.shape
|
268 |
+
|
269 |
+
per_row = b
|
270 |
+
for p in [6, 5, 4, 3, 2]:
|
271 |
+
if b % p == 0:
|
272 |
+
per_row = p
|
273 |
+
break
|
274 |
+
|
275 |
+
os.makedirs(os.path.dirname(os.path.abspath(os.path.realpath(output_filename))), exist_ok=True)
|
276 |
+
x = torch.clamp(x.float(), -1., 1.) * 127.5 + 127.5
|
277 |
+
x = x.detach().cpu().to(torch.uint8)
|
278 |
+
x = einops.rearrange(x, '(m n) c t h w -> t (m h) (n w) c', n=per_row)
|
279 |
+
torchvision.io.write_video(output_filename, x, fps=fps, video_codec='libx264', options={'crf': str(int(crf))})
|
280 |
+
return x
|
281 |
+
|
282 |
+
|
283 |
+
def save_bcthw_as_png(x, output_filename):
|
284 |
+
os.makedirs(os.path.dirname(os.path.abspath(os.path.realpath(output_filename))), exist_ok=True)
|
285 |
+
x = torch.clamp(x.float(), -1., 1.) * 127.5 + 127.5
|
286 |
+
x = x.detach().cpu().to(torch.uint8)
|
287 |
+
x = einops.rearrange(x, 'b c t h w -> c (b h) (t w)')
|
288 |
+
torchvision.io.write_png(x, output_filename)
|
289 |
+
return output_filename
|
290 |
+
|
291 |
+
|
292 |
+
def save_bchw_as_png(x, output_filename):
|
293 |
+
os.makedirs(os.path.dirname(os.path.abspath(os.path.realpath(output_filename))), exist_ok=True)
|
294 |
+
x = torch.clamp(x.float(), -1., 1.) * 127.5 + 127.5
|
295 |
+
x = x.detach().cpu().to(torch.uint8)
|
296 |
+
x = einops.rearrange(x, 'b c h w -> c h (b w)')
|
297 |
+
torchvision.io.write_png(x, output_filename)
|
298 |
+
return output_filename
|
299 |
+
|
300 |
+
|
301 |
+
def add_tensors_with_padding(tensor1, tensor2):
|
302 |
+
if tensor1.shape == tensor2.shape:
|
303 |
+
return tensor1 + tensor2
|
304 |
+
|
305 |
+
shape1 = tensor1.shape
|
306 |
+
shape2 = tensor2.shape
|
307 |
+
|
308 |
+
new_shape = tuple(max(s1, s2) for s1, s2 in zip(shape1, shape2))
|
309 |
+
|
310 |
+
padded_tensor1 = torch.zeros(new_shape)
|
311 |
+
padded_tensor2 = torch.zeros(new_shape)
|
312 |
+
|
313 |
+
padded_tensor1[tuple(slice(0, s) for s in shape1)] = tensor1
|
314 |
+
padded_tensor2[tuple(slice(0, s) for s in shape2)] = tensor2
|
315 |
+
|
316 |
+
result = padded_tensor1 + padded_tensor2
|
317 |
+
return result
|
318 |
+
|
319 |
+
|
320 |
+
def print_free_mem():
|
321 |
+
torch.cuda.empty_cache()
|
322 |
+
free_mem, total_mem = torch.cuda.mem_get_info(0)
|
323 |
+
free_mem_mb = free_mem / (1024 ** 2)
|
324 |
+
total_mem_mb = total_mem / (1024 ** 2)
|
325 |
+
print(f"Free memory: {free_mem_mb:.2f} MB")
|
326 |
+
print(f"Total memory: {total_mem_mb:.2f} MB")
|
327 |
+
return
|
328 |
+
|
329 |
+
|
330 |
+
def print_gpu_parameters(device, state_dict, log_count=1):
|
331 |
+
summary = {"device": device, "keys_count": len(state_dict)}
|
332 |
+
|
333 |
+
logged_params = {}
|
334 |
+
for i, (key, tensor) in enumerate(state_dict.items()):
|
335 |
+
if i >= log_count:
|
336 |
+
break
|
337 |
+
logged_params[key] = tensor.flatten()[:3].tolist()
|
338 |
+
|
339 |
+
summary["params"] = logged_params
|
340 |
+
|
341 |
+
print(str(summary))
|
342 |
+
return
|
343 |
+
|
344 |
+
|
345 |
+
def visualize_txt_as_img(width, height, text, font_path='font/DejaVuSans.ttf', size=18):
|
346 |
+
from PIL import Image, ImageDraw, ImageFont
|
347 |
+
|
348 |
+
txt = Image.new("RGB", (width, height), color="white")
|
349 |
+
draw = ImageDraw.Draw(txt)
|
350 |
+
font = ImageFont.truetype(font_path, size=size)
|
351 |
+
|
352 |
+
if text == '':
|
353 |
+
return np.array(txt)
|
354 |
+
|
355 |
+
# Split text into lines that fit within the image width
|
356 |
+
lines = []
|
357 |
+
words = text.split()
|
358 |
+
current_line = words[0]
|
359 |
+
|
360 |
+
for word in words[1:]:
|
361 |
+
line_with_word = f"{current_line} {word}"
|
362 |
+
if draw.textbbox((0, 0), line_with_word, font=font)[2] <= width:
|
363 |
+
current_line = line_with_word
|
364 |
+
else:
|
365 |
+
lines.append(current_line)
|
366 |
+
current_line = word
|
367 |
+
|
368 |
+
lines.append(current_line)
|
369 |
+
|
370 |
+
# Draw the text line by line
|
371 |
+
y = 0
|
372 |
+
line_height = draw.textbbox((0, 0), "A", font=font)[3]
|
373 |
+
|
374 |
+
for line in lines:
|
375 |
+
if y + line_height > height:
|
376 |
+
break # stop drawing if the next line will be outside the image
|
377 |
+
draw.text((0, y), line, fill="black", font=font)
|
378 |
+
y += line_height
|
379 |
+
|
380 |
+
return np.array(txt)
|
381 |
+
|
382 |
+
|
383 |
+
def blue_mark(x):
|
384 |
+
x = x.copy()
|
385 |
+
c = x[:, :, 2]
|
386 |
+
b = cv2.blur(c, (9, 9))
|
387 |
+
x[:, :, 2] = ((c - b) * 16.0 + b).clip(-1, 1)
|
388 |
+
return x
|
389 |
+
|
390 |
+
|
391 |
+
def green_mark(x):
|
392 |
+
x = x.copy()
|
393 |
+
x[:, :, 2] = -1
|
394 |
+
x[:, :, 0] = -1
|
395 |
+
return x
|
396 |
+
|
397 |
+
|
398 |
+
def frame_mark(x):
|
399 |
+
x = x.copy()
|
400 |
+
x[:64] = -1
|
401 |
+
x[-64:] = -1
|
402 |
+
x[:, :8] = 1
|
403 |
+
x[:, -8:] = 1
|
404 |
+
return x
|
405 |
+
|
406 |
+
|
407 |
+
@torch.inference_mode()
|
408 |
+
def pytorch2numpy(imgs):
|
409 |
+
results = []
|
410 |
+
for x in imgs:
|
411 |
+
y = x.movedim(0, -1)
|
412 |
+
y = y * 127.5 + 127.5
|
413 |
+
y = y.detach().float().cpu().numpy().clip(0, 255).astype(np.uint8)
|
414 |
+
results.append(y)
|
415 |
+
return results
|
416 |
+
|
417 |
+
|
418 |
+
@torch.inference_mode()
|
419 |
+
def numpy2pytorch(imgs):
|
420 |
+
h = torch.from_numpy(np.stack(imgs, axis=0)).float() / 127.5 - 1.0
|
421 |
+
h = h.movedim(-1, 1)
|
422 |
+
return h
|
423 |
+
|
424 |
+
|
425 |
+
@torch.no_grad()
|
426 |
+
def duplicate_prefix_to_suffix(x, count, zero_out=False):
|
427 |
+
if zero_out:
|
428 |
+
return torch.cat([x, torch.zeros_like(x[:count])], dim=0)
|
429 |
+
else:
|
430 |
+
return torch.cat([x, x[:count]], dim=0)
|
431 |
+
|
432 |
+
|
433 |
+
def weighted_mse(a, b, weight):
|
434 |
+
return torch.mean(weight.float() * (a.float() - b.float()) ** 2)
|
435 |
+
|
436 |
+
|
437 |
+
def clamped_linear_interpolation(x, x_min, y_min, x_max, y_max, sigma=1.0):
|
438 |
+
x = (x - x_min) / (x_max - x_min)
|
439 |
+
x = max(0.0, min(x, 1.0))
|
440 |
+
x = x ** sigma
|
441 |
+
return y_min + x * (y_max - y_min)
|
442 |
+
|
443 |
+
|
444 |
+
def expand_to_dims(x, target_dims):
|
445 |
+
return x.view(*x.shape, *([1] * max(0, target_dims - x.dim())))
|
446 |
+
|
447 |
+
|
448 |
+
def repeat_to_batch_size(tensor: torch.Tensor, batch_size: int):
|
449 |
+
if tensor is None:
|
450 |
+
return None
|
451 |
+
|
452 |
+
first_dim = tensor.shape[0]
|
453 |
+
|
454 |
+
if first_dim == batch_size:
|
455 |
+
return tensor
|
456 |
+
|
457 |
+
if batch_size % first_dim != 0:
|
458 |
+
raise ValueError(f"Cannot evenly repeat first dim {first_dim} to match batch_size {batch_size}.")
|
459 |
+
|
460 |
+
repeat_times = batch_size // first_dim
|
461 |
+
|
462 |
+
return tensor.repeat(repeat_times, *[1] * (tensor.dim() - 1))
|
463 |
+
|
464 |
+
|
465 |
+
def dim5(x):
|
466 |
+
return expand_to_dims(x, 5)
|
467 |
+
|
468 |
+
|
469 |
+
def dim4(x):
|
470 |
+
return expand_to_dims(x, 4)
|
471 |
+
|
472 |
+
|
473 |
+
def dim3(x):
|
474 |
+
return expand_to_dims(x, 3)
|
475 |
+
|
476 |
+
|
477 |
+
def crop_or_pad_yield_mask(x, length):
|
478 |
+
B, F, C = x.shape
|
479 |
+
device = x.device
|
480 |
+
dtype = x.dtype
|
481 |
+
|
482 |
+
if F < length:
|
483 |
+
y = torch.zeros((B, length, C), dtype=dtype, device=device)
|
484 |
+
mask = torch.zeros((B, length), dtype=torch.bool, device=device)
|
485 |
+
y[:, :F, :] = x
|
486 |
+
mask[:, :F] = True
|
487 |
+
return y, mask
|
488 |
+
|
489 |
+
return x[:, :length, :], torch.ones((B, length), dtype=torch.bool, device=device)
|
490 |
+
|
491 |
+
|
492 |
+
def extend_dim(x, dim, minimal_length, zero_pad=False):
|
493 |
+
original_length = int(x.shape[dim])
|
494 |
+
|
495 |
+
if original_length >= minimal_length:
|
496 |
+
return x
|
497 |
+
|
498 |
+
if zero_pad:
|
499 |
+
padding_shape = list(x.shape)
|
500 |
+
padding_shape[dim] = minimal_length - original_length
|
501 |
+
padding = torch.zeros(padding_shape, dtype=x.dtype, device=x.device)
|
502 |
+
else:
|
503 |
+
idx = (slice(None),) * dim + (slice(-1, None),) + (slice(None),) * (len(x.shape) - dim - 1)
|
504 |
+
last_element = x[idx]
|
505 |
+
padding = last_element.repeat_interleave(minimal_length - original_length, dim=dim)
|
506 |
+
|
507 |
+
return torch.cat([x, padding], dim=dim)
|
508 |
+
|
509 |
+
|
510 |
+
def lazy_positional_encoding(t, repeats=None):
|
511 |
+
if not isinstance(t, list):
|
512 |
+
t = [t]
|
513 |
+
|
514 |
+
from diffusers.models.embeddings import get_timestep_embedding
|
515 |
+
|
516 |
+
te = torch.tensor(t)
|
517 |
+
te = get_timestep_embedding(timesteps=te, embedding_dim=256, flip_sin_to_cos=True, downscale_freq_shift=0.0, scale=1.0)
|
518 |
+
|
519 |
+
if repeats is None:
|
520 |
+
return te
|
521 |
+
|
522 |
+
te = te[:, None, :].expand(-1, repeats, -1)
|
523 |
+
|
524 |
+
return te
|
525 |
+
|
526 |
+
|
527 |
+
def state_dict_offset_merge(A, B, C=None):
|
528 |
+
result = {}
|
529 |
+
keys = A.keys()
|
530 |
+
|
531 |
+
for key in keys:
|
532 |
+
A_value = A[key]
|
533 |
+
B_value = B[key].to(A_value)
|
534 |
+
|
535 |
+
if C is None:
|
536 |
+
result[key] = A_value + B_value
|
537 |
+
else:
|
538 |
+
C_value = C[key].to(A_value)
|
539 |
+
result[key] = A_value + B_value - C_value
|
540 |
+
|
541 |
+
return result
|
542 |
+
|
543 |
+
|
544 |
+
def state_dict_weighted_merge(state_dicts, weights):
|
545 |
+
if len(state_dicts) != len(weights):
|
546 |
+
raise ValueError("Number of state dictionaries must match number of weights")
|
547 |
+
|
548 |
+
if not state_dicts:
|
549 |
+
return {}
|
550 |
+
|
551 |
+
total_weight = sum(weights)
|
552 |
+
|
553 |
+
if total_weight == 0:
|
554 |
+
raise ValueError("Sum of weights cannot be zero")
|
555 |
+
|
556 |
+
normalized_weights = [w / total_weight for w in weights]
|
557 |
+
|
558 |
+
keys = state_dicts[0].keys()
|
559 |
+
result = {}
|
560 |
+
|
561 |
+
for key in keys:
|
562 |
+
result[key] = state_dicts[0][key] * normalized_weights[0]
|
563 |
+
|
564 |
+
for i in range(1, len(state_dicts)):
|
565 |
+
state_dict_value = state_dicts[i][key].to(result[key])
|
566 |
+
result[key] += state_dict_value * normalized_weights[i]
|
567 |
+
|
568 |
+
return result
|
569 |
+
|
570 |
+
|
571 |
+
def group_files_by_folder(all_files):
|
572 |
+
grouped_files = {}
|
573 |
+
|
574 |
+
for file in all_files:
|
575 |
+
folder_name = os.path.basename(os.path.dirname(file))
|
576 |
+
if folder_name not in grouped_files:
|
577 |
+
grouped_files[folder_name] = []
|
578 |
+
grouped_files[folder_name].append(file)
|
579 |
+
|
580 |
+
list_of_lists = list(grouped_files.values())
|
581 |
+
return list_of_lists
|
582 |
+
|
583 |
+
|
584 |
+
def generate_timestamp():
|
585 |
+
now = datetime.datetime.now()
|
586 |
+
timestamp = now.strftime('%y%m%d_%H%M%S')
|
587 |
+
milliseconds = f"{int(now.microsecond / 1000):03d}"
|
588 |
+
random_number = random.randint(0, 9999)
|
589 |
+
return f"{timestamp}_{milliseconds}_{random_number}"
|
590 |
+
|
591 |
+
|
592 |
+
def write_PIL_image_with_png_info(image, metadata, path):
|
593 |
+
from PIL.PngImagePlugin import PngInfo
|
594 |
+
|
595 |
+
png_info = PngInfo()
|
596 |
+
for key, value in metadata.items():
|
597 |
+
png_info.add_text(key, value)
|
598 |
+
|
599 |
+
image.save(path, "PNG", pnginfo=png_info)
|
600 |
+
return image
|
601 |
+
|
602 |
+
|
603 |
+
def torch_safe_save(content, path):
|
604 |
+
torch.save(content, path + '_tmp')
|
605 |
+
os.replace(path + '_tmp', path)
|
606 |
+
return path
|
607 |
+
|
608 |
+
|
609 |
+
def move_optimizer_to_device(optimizer, device):
|
610 |
+
for state in optimizer.state.values():
|
611 |
+
for k, v in state.items():
|
612 |
+
if isinstance(v, torch.Tensor):
|
613 |
+
state[k] = v.to(device)
|