-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathnodes.py
More file actions
484 lines (401 loc) · 22.1 KB
/
nodes.py
File metadata and controls
484 lines (401 loc) · 22.1 KB
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#code originally taken from: https://github.com/ChenyangSi/FreeU (under MIT License)
import torch
import torch as th
import torch.fft as fft
import torch.nn as nn
import math
def normalize(latent, target_min=None, target_max=None):
"""
Normalize a tensor `latent` between `target_min` and `target_max`.
Args:
latent (torch.Tensor): The input tensor to be normalized.
target_min (float, optional): The minimum value after normalization.
- When `None` min will be tensor min range value.
target_max (float, optional): The maximum value after normalization.
- When `None` max will be tensor max range value.
Returns:
torch.Tensor: The normalized tensor
"""
min_val = latent.min()
max_val = latent.max()
if target_min is None:
target_min = min_val
if target_max is None:
target_max = max_val
normalized = (latent - min_val) / (max_val - min_val)
scaled = normalized * (target_max - target_min) + target_min
return scaled
def hslerp(a, b, t):
"""
Perform Hybrid Spherical Linear Interpolation (HSLERP) between two tensors.
This function combines two input tensors `a` and `b` using HSLERP, which is a specialized
interpolation method for smooth transitions between orientations or colors.
Args:
a (tensor): The first input tensor.
b (tensor): The second input tensor.
t (float): The blending factor, a value between 0 and 1 that controls the interpolation.
Returns:
tensor: The result of HSLERP interpolation between `a` and `b`.
Note:
HSLERP provides smooth transitions between orientations or colors, particularly useful
in applications like image processing and 3D graphics.
"""
if a.shape != b.shape:
raise ValueError("Input tensors a and b must have the same shape.")
num_channels = a.size(1)
interpolation_tensor = torch.zeros(1, num_channels, 1, 1, device=a.device, dtype=a.dtype)
interpolation_tensor[0, 0, 0, 0] = 1.0
result = (1 - t) * a + t * b
if t < 0.5:
result += (torch.norm(b - a, dim=1, keepdim=True) / 6) * interpolation_tensor
else:
result -= (torch.norm(b - a, dim=1, keepdim=True) / 6) * interpolation_tensor
return result
def stable_slerp(a, b, t: float, eps: float = 1e-6):
"""
Numerically stable spherical linear interpolation over the channel dimension.
Treat each BCHW location's C-vector as a point on a hypersphere and SLERP from a->b.
Falls back to LERP when the angle is very small or vectors are near-zero.
"""
if a.shape != b.shape:
raise ValueError("Input tensors a and b must have the same shape.")
# Norms across channel dimension
a_norm = torch.linalg.norm(a, dim=1, keepdim=True).clamp_min(eps)
b_norm = torch.linalg.norm(b, dim=1, keepdim=True).clamp_min(eps)
a_n = a / a_norm
b_n = b / b_norm
# Cosine of angle between vectors
dot = (a_n * b_n).sum(dim=1, keepdim=True).clamp(-1.0 + eps, 1.0 - eps)
theta = torch.acos(dot)
sin_theta = torch.sin(theta).clamp_min(eps)
# Scalar t is expected; keep broadcast-friendly
s0 = torch.sin((1.0 - t) * theta) / sin_theta
s1 = torch.sin(t * theta) / sin_theta
slerp_out = s0 * a + s1 * b
lerp_out = (1.0 - t) * a + t * b
# When angle is too small, prefer LERP to avoid instabilities
use_lerp = (theta < 1e-3).squeeze(1)
out = torch.where(use_lerp.unsqueeze(1), lerp_out, slerp_out)
return out
blending_modes = {
# Args:
# - a (tensor): Latent input 1
# - b (tensor): Latent input 2
# - t (float): Blending factor
# Interpolates between tensors a and b using normalized linear interpolation.
'bislerp': lambda a, b, t: normalize((1 - t) * a + t * b),
# Transfer the color from `b` to `a` by t` factor
'colorize': lambda a, b, t: a + (b - a) * t,
# Interpolates between tensors a and b using cosine interpolation.
'cosine interp': lambda a, b, t: (a + b - (a - b) * torch.cos(t * torch.tensor(math.pi))) / 2,
# Interpolates between tensors a and b using cubic interpolation.
'cuberp': lambda a, b, t: a + (b - a) * (3 * t ** 2 - 2 * t ** 3),
# Interpolates between tensors a and b using normalized linear interpolation,
# with a twist when t is greater than or equal to 0.5.
'hslerp': hslerp,
# Numerically stable SLERP over channel vectors
'stable_slerp': stable_slerp,
# Adds tensor b to tensor a, scaled by t.
'inject': lambda a, b, t: a + b * t,
# Interpolates between tensors a and b using linear interpolation.
'lerp': lambda a, b, t: (1 - t) * a + t * b,
# Simulates a brightening effect by adding tensor b to tensor a, scaled by t.
'linear dodge': lambda a, b, t: normalize(a + b * t),
}
mscales = {
"Default": None,
"Low-Pass": [
(10, 1.0), # Allows low-frequency components, suppresses high-frequency components
],
"Pass-Through": [
(10, 1.0), # Passes all frequencies unchanged, no filtering
],
"Gaussian-Blur": [
(10, 0.5), # Blurs the image by allowing a range of frequencies with a Gaussian shape
],
"Edge-Enhancement": [
(10, 2.0), # Enhances edges and high-frequency features while suppressing low-frequency details
],
"Sharpen": [
(10, 1.5), # Increases the sharpness of the image by emphasizing high-frequency components
],
"Multi-Bandpass": [
[(5, 0.0), (15, 1.0), (25, 0.0)], # Multi-scale bandpass filter
],
"Multi-Low-Pass": [
[(5, 1.0), (10, 0.5), (15, 0.2)], # Multi-scale low-pass filter
],
"Multi-High-Pass": [
[(5, 0.0), (10, 0.5), (15, 0.8)], # Multi-scale high-pass filter
],
"Multi-Pass-Through": [
[(5, 1.0), (10, 1.0), (15, 1.0)], # Pass-through at different scales
],
"Multi-Gaussian-Blur": [
[(5, 0.5), (10, 0.8), (15, 0.2)], # Multi-scale Gaussian blur
],
"Multi-Edge-Enhancement": [
[(5, 1.2), (10, 1.5), (15, 2.0)], # Multi-scale edge enhancement
],
"Multi-Sharpen": [
[(5, 1.5), (10, 2.0), (15, 2.5)], # Multi-scale sharpening
],
}
def Fourier_filter(x, threshold, scale, scales=None, strength=1.0):
# FFT
if isinstance(x, list):
x = x[0]
if isinstance(x, torch.Tensor):
x_freq = fft.fftn(x.float(), dim=(-2, -1))
x_freq = fft.fftshift(x_freq, dim=(-2, -1))
B, C, H, W = x_freq.shape
mask = torch.ones((B, C, H, W), device=x.device)
crow, ccol = H // 2, W // 2
mask[..., crow - threshold:crow + threshold, ccol - threshold:ccol + threshold] = scale
if scales is not None:
if isinstance(scales[0], tuple):
# Single-scale mode
for scale_params in scales:
if len(scale_params) == 2:
scale_threshold, scale_value = scale_params
scaled_scale_value = scale_value * strength
scale_mask = torch.ones((B, C, H, W), device=x.device)
scale_mask[..., crow - scale_threshold:crow + scale_threshold, ccol - scale_threshold:ccol + scale_threshold] = scaled_scale_value
mask = mask + (scale_mask - mask) * strength
else:
# Multi-scale mode
for scale_params in scales:
if isinstance(scale_params, list):
for scale_tuple in scale_params:
if len(scale_tuple) == 2:
scale_threshold, scale_value = scale_tuple
scaled_scale_value = scale_value * strength
scale_mask = torch.ones((B, C, H, W), device=x.device)
scale_mask[..., crow - scale_threshold:crow + scale_threshold, ccol - scale_threshold:ccol + scale_threshold] = scaled_scale_value
mask = mask + (scale_mask - mask) * strength
x_freq = x_freq * mask
# IFFT
x_freq = fft.ifftshift(x_freq, dim=(-2, -1))
x_filtered = fft.ifftn(x_freq, dim=(-2, -1)).real
return x_filtered.to(x.dtype)
return x
class WAS_FreeU:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL",),
"target_block": (["output_block", "middle_block", "input_block", "all"], {"tooltip": "Which UNet block(s) to patch."}),
"multiscale_mode": (list(mscales.keys()), {"tooltip": "Frequency shaping preset used by Fourier_filter."}),
"multiscale_strength": ("FLOAT", {"default": 1.0, "max": 1.0, "min": 0, "step": 0.001, "tooltip": "Intensity of multi-scale shaping [0-1]."}),
"slice_b1": ("INT", {"default": 640, "min": 64, "max": 1280, "step": 1, "tooltip": "Slice width (channels) affected in 1280-channel features."}),
"slice_b2": ("INT", {"default": 320, "min": 64, "max": 640, "step": 1, "tooltip": "Slice width (channels) affected in 640-channel features."}),
"b1": ("FLOAT", {"default": 1.1, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Gain multiplier applied to the b1 slice (1280-ch)."}),
"b2": ("FLOAT", {"default": 1.2, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Gain multiplier applied to the b2 slice (640-ch)."}),
"s1": ("FLOAT", {"default": 0.9, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Fourier scale at threshold for 1280-ch features."}),
"s2": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Fourier scale at threshold for 640-ch features."}),
},
"optional": {
"b1_mode": (list(blending_modes.keys()), {"tooltip": "Blending mode for b1 path."}),
"b1_blend": ("FLOAT", {"default": 1.0, "max": 100, "min": 0, "step": 0.001, "tooltip": "Blend strength for b1 path."}),
"b2_mode": (list(blending_modes.keys()), {"tooltip": "Blending mode for b2 path."}),
"b2_blend": ("FLOAT", {"default": 1.0, "max": 100, "min": 0, "step": 0.001, "tooltip": "Blend strength for b2 path."}),
"threshold": ("INT", {"default": 1.0, "max": 10, "min": 1, "step": 1, "tooltip": "Base radius for the Fourier mask."}),
"use_override_scales": (["false", "true"], {"tooltip": "Enable manual override of scale presets."}),
"override_scales": ("STRING", {"default": '''# OVERRIDE SCALES
# Sharpen
# 10, 1.5''', "multiline": True, "tooltip": "Custom scale lines: '<radius>, <scale>'. Comments with #,//,!"}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "_for_testing"
def patch(self, model, target_block, multiscale_mode, multiscale_strength, slice_b1, slice_b2, b1, b2, s1, s2, b1_mode="add", b1_blend=1.0, b2_mode="add", b2_blend=1.0, threshold=1.0, use_override_scales="false", override_scales=""):
min_slice = 64
max_slice_b1 = 1280
max_slice_b2 = 640
slice_b1 = max(min(max_slice_b1, slice_b1), min_slice)
slice_b2 = max(min(min(slice_b1, max_slice_b2), slice_b2), min_slice)
scales_list = []
if use_override_scales == "true":
if override_scales.strip() != "":
scales_str = override_scales.strip().splitlines()
for line in scales_str:
if not line.strip().startswith('#') and not line.strip().startswith('!') and not line.strip().startswith('//'):
scale_values = line.split(',')
if len(scale_values) == 2:
scales_list.append((int(scale_values[0]), float(scale_values[1])))
if use_override_scales == "true" and not scales_list:
print("No valid override scales found. Using default scale.")
scales_list = None
scales = mscales[multiscale_mode] if use_override_scales == "false" else scales_list
print(f"FreeU Plate Portions: {slice_b1} over {slice_b2}")
print(f"FreeU Multi-Scales: {scales}")
def block_patch(h, transformer_options):
if h.shape[1] == 1280:
h_t = h[:,:slice_b1]
h_r = h_t * b1
h[:,:slice_b1] = blending_modes[b1_mode](h_t, h_r, b1_blend)
if h.shape[1] == 640:
h_t = h[:,:slice_b2]
h_r = h_t * b2
h[:,:slice_b2] = blending_modes[b2_mode](h_t, h_r, b2_blend)
return h
def block_patch_hsp(h, hsp, transformer_options):
if h.shape[1] == 1280:
h = block_patch(h, transformer_options)
hsp = Fourier_filter(hsp, threshold=threshold, scale=s1, scales=scales, strength=multiscale_strength)
if h.shape[1] == 640:
h = block_patch(h, transformer_options)
hsp = Fourier_filter(hsp, threshold=threshold, scale=s2, scales=scales, strength=multiscale_strength)
return h, hsp
print(f"Patching {target_block}")
m = model.clone()
if target_block == "all" or target_block == "output_block":
m.set_model_output_block_patch(block_patch_hsp)
if target_block == "all" or target_block == "input_block":
m.set_model_input_block_patch(block_patch)
if target_block == "all" or target_block == "middle_block":
m.set_model_patch(block_patch, "middle_block_patch")
return (m, )
class WAS_PostCFGShift:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL",),
"steps": ("INT", {"default": 20, "min": 1, "max": 1000, "step": 1, "tooltip": "Number of steps to apply SHIFT."}),
"mode": (list(blending_modes.keys()), {"tooltip": "Blend strategy for denoised vs denoised*b (e.g., inject, stable_slerp)."}),
"blend": ("FLOAT", {"default": 1.0, "max": 100.0, "min": 0.0, "step": 0.001, "tooltip": "Blend amount between base and scaled tensors."}),
"b": ("FLOAT", {"default": 1.1, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Scale factor for the injected path (higher = stronger)."}),
"apply_fourier": ("BOOLEAN", {"default": False, "tooltip": "Apply frequency-domain shaping (Fourier_filter)."}),
"multiscale_mode": (list(mscales.keys()), {"tooltip": "Preset shaping curves for Fourier_filter."}),
"multiscale_strength": ("FLOAT", {"default": 1.0, "max": 1.0, "min": 0.0, "step": 0.001, "tooltip": "Intensity of multi-scale shaping [0-1]."}),
"threshold": ("INT", {"default": 1, "min": 1, "max": 10, "step": 1, "tooltip": "Base radius for frequency mask."}),
"s": ("FLOAT", {"default": 0.5, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Base scale value applied at threshold radius."}),
"force_gain": ("FLOAT", {"default": 1.0, "min": 0.0, "max": 100.0, "step": 0.01, "tooltip": "Final multiplier to boost or attenuate effect."}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "_for_testing"
def patch(self, model, steps, mode, blend, b, apply_fourier, multiscale_mode, multiscale_strength, threshold, s, force_gain):
scales = mscales[multiscale_mode]
steps = max(1, min(1000, steps))
current_step = 0
print(
"[FluxU] inputs:",
f"mode={mode}", f"blend={blend}", f"b={b}",
f"apply_fourier={apply_fourier}", f"multiscale_mode={multiscale_mode}", f"multiscale_strength={multiscale_strength}",
f"threshold={threshold}", f"s={s}", f"force_gain={force_gain}"
)
m = model.clone()
def post_cfg_function(args):
nonlocal current_step
current_step += 1
if current_step > steps:
return args.get("denoised")
denoised = args.get("denoised")
eff_blend = float(blend)
t_scaled = denoised * b
y = blending_modes[mode](denoised, t_scaled, eff_blend)
if apply_fourier:
y = Fourier_filter(y, threshold=threshold, scale=s, scales=scales, strength=multiscale_strength)
if force_gain != 1.0:
y = y * float(force_gain)
return y
try:
m.set_model_sampler_post_cfg_function(post_cfg_function)
print("[FluxU] set_model_sampler_post_cfg_function registered")
except Exception as e:
print(f"[FluxU] set_model_sampler_post_cfg_function failed: {e}")
return (m, )
class WAS_FreeU_V2:
@classmethod
def INPUT_TYPES(s):
return {"required": {
"model": ("MODEL",),
"input_block": ("BOOLEAN", {"default": False, "tooltip": "Enable patching on the input block."}),
"middle_block": ("BOOLEAN", {"default": False, "tooltip": "Enable patching on the middle block."}),
"output_block": ("BOOLEAN", {"default": False, "tooltip": "Enable patching on the output block."}),
"multiscale_mode": (list(mscales.keys()), {"tooltip": "Frequency shaping preset used by Fourier_filter."}),
"multiscale_strength": ("FLOAT", {"default": 1.0, "max": 1.0, "min": 0, "step": 0.001, "tooltip": "Intensity of multi-scale shaping [0-1]."}),
"slice_b1": ("INT", {"default": 640, "min": 64, "max": 1280, "step": 1, "tooltip": "Slice width (channels) affected in 1280-channel features."}),
"slice_b2": ("INT", {"default": 320, "min": 64, "max": 640, "step": 1, "tooltip": "Slice width (channels) affected in 640-channel features."}),
"b1": ("FLOAT", {"default": 1.1, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Gain multiplier for 1280-channel slice."}),
"b2": ("FLOAT", {"default": 1.2, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Gain multiplier for 640-channel slice."}),
"s1": ("FLOAT", {"default": 0.9, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Fourier scale at threshold for 1280-ch features."}),
"s2": ("FLOAT", {"default": 0.2, "min": 0.0, "max": 10.0, "step": 0.001, "tooltip": "Fourier scale at threshold for 640-ch features."}),
},
"optional": {
"threshold": ("INT", {"default": 1.0, "max": 10, "min": 1, "step": 1, "tooltip": "Base radius for the Fourier mask."}),
"use_override_scales": (["false", "true"], {"tooltip": "Enable manual override of scale presets."}),
"override_scales": ("STRING", {"default": '''# OVERRIDE SCALES
# Sharpen
# 10, 1.5''', "multiline": True, "tooltip": "Custom scale lines: '<radius>, <scale>'. Comments with #,//,!"}),
}
}
RETURN_TYPES = ("MODEL",)
FUNCTION = "patch"
CATEGORY = "_for_testing"
def patch(self, model, input_block, middle_block, output_block, multiscale_mode, multiscale_strength, slice_b1, slice_b2, b1, b2, s1, s2, threshold=1.0, use_override_scales="false", override_scales=""):
min_slice = 64
max_slice_b1 = 1280
max_slice_b2 = 640
slice_b1 = max(min(max_slice_b1, slice_b1), min_slice)
slice_b2 = max(min(min(slice_b1, max_slice_b2), slice_b2), min_slice)
scales_list = []
if use_override_scales == "true":
if override_scales.strip() != "":
scales_str = override_scales.strip().splitlines()
for line in scales_str:
if not line.strip().startswith('#') and not line.strip().startswith('!') and not line.strip().startswith('//'):
scale_values = line.split(',')
if len(scale_values) == 2:
scales_list.append((int(scale_values[0]), float(scale_values[1])))
if use_override_scales == "true" and not scales_list:
print("No valid override scales found. Using default scale.")
scales_list = None
scales = mscales[multiscale_mode] if use_override_scales == "false" else scales_list
def _hidden_mean(h):
hidden_mean = h.mean(1).unsqueeze(1)
B = hidden_mean.shape[0]
hidden_max, _ = torch.max(hidden_mean.view(B, -1), dim=-1, keepdim=True)
hidden_min, _ = torch.min(hidden_mean.view(B, -1), dim=-1, keepdim=True)
hidden_mean = (hidden_mean - hidden_min.unsqueeze(2).unsqueeze(3)) / (hidden_max - hidden_min).unsqueeze(2).unsqueeze(3)
return hidden_mean
def block_patch(h, transformer_options):
if h.shape[1] == 1280:
hidden_mean = _hidden_mean(h)
h[:,:slice_b1] = h[:,:slice_b1] * ((b1 - 1 ) * hidden_mean + 1)
if h.shape[1] == 640:
hidden_mean = _hidden_mean(h)
h[:,:slice_b2] = h[:,:slice_b2] * ((b2 - 1 ) * hidden_mean + 1)
return h
def block_patch_hsp(h, hsp, transformer_options):
if h.shape[1] == 1280:
h = block_patch(h, transformer_options)
hsp = Fourier_filter(hsp, threshold=threshold, scale=s1, scales=scales, strength=multiscale_strength)
if h.shape[1] == 640:
h = block_patch(h, transformer_options)
hsp = Fourier_filter(hsp, threshold=threshold, scale=s2, scales=scales, strength=multiscale_strength)
return h, hsp
m = model.clone()
if output_block:
print("Patching output block")
m.set_model_output_block_patch(block_patch_hsp)
if input_block:
print("Patching input block")
m.set_model_input_block_patch(block_patch)
if middle_block:
print("Patching middle block")
m.set_model_patch(block_patch, "middle_block_patch")
return (m, )
NODE_CLASS_MAPPINGS = {
"WAS_FreeU": WAS_FreeU,
"WAS_FreeU_V2": WAS_FreeU_V2,
"WAS_PostCFGShift": WAS_PostCFGShift,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"WAS_FreeU": "FreeU (Advanced Plus)",
"WAS_FreeU_V2": "FreeU V2 (Advanced Plus)",
"WAS_PostCFGShift": "Post-CFG SHIFT",
}