-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathscheduled_lora_loader.py
More file actions
396 lines (310 loc) · 14.1 KB
/
Copy pathscheduled_lora_loader.py
File metadata and controls
396 lines (310 loc) · 14.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
"""
Scheduled LoRA Loader for ComfyUI
A standalone LoRA loader with strength scheduling support.
No block selection - just simple load with optional scheduling.
Strength Schedule Format: "0:.2,.5:.8,1:1.0"
- Each pair is "step_percent:strength"
- Values interpolate linearly between keyframes
"""
import os
import torch
import folder_paths
import comfy.sd
import comfy.hooks
import comfy.utils
from .quant_utils import is_quantized_model
# ============================================================================
# STRENGTH SCHEDULE PRESETS
# ============================================================================
SCHEDULE_PRESETS = {
# === No Schedule ===
"Custom": "",
"Constant 1.0 (No Change)": "0:1, 1:1",
# === Basic Fades ===
"Linear In (0→1)": "0:0, 1:1",
"Linear Out (1→0)": "0:1, 1:0",
# === Ease Curves ===
"Ease In (slow start)": "0:0, 0.3:0.1, 0.7:0.5, 1:1",
"Ease Out (slow end)": "0:1, 0.3:0.5, 0.7:0.9, 1:0",
"Ease In-Out": "0:0, 0.3:0.1, 0.7:0.9, 1:1",
# === Bell Curves ===
"Bell Curve (peak middle)": "0:0, 0.5:1, 1:0",
"Wide Bell": "0:0, 0.3:0.8, 0.7:0.8, 1:0",
"Sharp Bell": "0:0, 0.4:0.2, 0.5:1, 0.6:0.2, 1:0",
# === Structure LoRA Favorites ===
"High Start → Cut Low": "0:1, 0.3:1, 0.35:0.2, 1:0.2",
"High Start → Cut Off": "0:1, 0.3:1, 0.35:0, 1:0",
"High Early → Fade": "0:1, 0.2:0.8, 0.5:0.3, 1:0",
# === Detail LoRA Favorites ===
"Low Start → Ramp Late": "0:0, 0.6:0.1, 0.8:0.7, 1:1",
"Off → Kick In Late": "0:0, 0.7:0, 0.75:0.8, 1:1",
"Low → Boost End": "0:0.2, 0.5:0.2, 0.7:0.6, 1:1",
# === Step Functions ===
"Step Up Mid": "0:0.2, 0.45:0.2, 0.55:0.8, 1:0.8",
"Step Down Mid": "0:0.8, 0.45:0.8, 0.55:0.2, 1:0.2",
"Two Steps Up": "0:0, 0.3:0, 0.35:0.5, 0.65:0.5, 0.7:1, 1:1",
# === Pulses ===
"Pulse Early": "0:1, 0.25:1, 0.35:0.2, 1:0.2",
"Pulse Mid": "0:0.2, 0.4:0.2, 0.45:1, 0.55:1, 0.6:0.2, 1:0.2",
"Pulse Late": "0:0.2, 0.65:0.2, 0.75:1, 1:1",
# === Constant (for testing) ===
"Constant Half": "0:0.5, 1:0.5",
"Constant Low": "0:0.3, 1:0.3",
# =========================================================================
# INVERTED VERSIONS
# =========================================================================
# === Basic Fades (Inverted) ===
"INV: Linear In (1→0)": "0:1, 1:0",
"INV: Linear Out (0→1)": "0:0, 1:1",
# === Ease Curves (Inverted) ===
"INV: Ease In": "0:1, 0.3:0.9, 0.7:0.5, 1:0",
"INV: Ease Out": "0:0, 0.3:0.5, 0.7:0.1, 1:1",
"INV: Ease In-Out": "0:1, 0.3:0.9, 0.7:0.1, 1:0",
# === Bell Curves (Inverted) ===
"INV: Bell (dip middle)": "0:1, 0.5:0, 1:1",
"INV: Wide Bell": "0:1, 0.3:0.2, 0.7:0.2, 1:1",
"INV: Sharp Bell": "0:1, 0.4:0.8, 0.5:0, 0.6:0.8, 1:1",
# === Structure Inverted ===
"INV: Low Start → Boost High": "0:0, 0.3:0, 0.35:0.8, 1:0.8",
"INV: Low Start → Full On": "0:0, 0.3:0, 0.35:1, 1:1",
"INV: Low Early → Build": "0:0, 0.2:0.2, 0.5:0.7, 1:1",
# === Detail Inverted ===
"INV: High Start → Drop Late": "0:1, 0.6:0.9, 0.8:0.3, 1:0",
"INV: Full → Cut Off Late": "0:1, 0.7:1, 0.75:0.2, 1:0",
"INV: High → Drop End": "0:0.8, 0.5:0.8, 0.7:0.4, 1:0",
# === Step Functions (Inverted) ===
"INV: Step Down Mid": "0:0.8, 0.45:0.8, 0.55:0.2, 1:0.2",
"INV: Step Up Mid": "0:0.2, 0.45:0.2, 0.55:0.8, 1:0.8",
"INV: Two Steps Down": "0:1, 0.3:1, 0.35:0.5, 0.65:0.5, 0.7:0, 1:0",
# === Pulses (Inverted) ===
"INV: Dip Early": "0:0, 0.25:0, 0.35:0.8, 1:0.8",
"INV: Dip Mid": "0:0.8, 0.4:0.8, 0.45:0, 0.55:0, 0.6:0.8, 1:0.8",
"INV: Dip Late": "0:0.8, 0.65:0.8, 0.75:0, 1:0",
}
SCHEDULE_PRESET_LIST = list(SCHEDULE_PRESETS.keys())
# ============================================================================
# SCHEDULE PARSING AND INTERPOLATION
# ============================================================================
def _parse_strength_schedule(schedule_str: str) -> list:
"""
Parse a strength schedule string into keyframes.
Format: "0:.2,.2:.3,.5:.6,1:.9"
Each pair is "percent:strength" where:
- percent is 0.0 to 1.0 (proportion of steps)
- strength is the LoRA strength at that point
Returns list of (percent, strength) tuples, sorted by percent.
"""
if not schedule_str or not schedule_str.strip():
return None
schedule_str = schedule_str.strip()
# Check if it's just a float (no schedule)
try:
float(schedule_str)
return None
except ValueError:
pass
keyframes = []
pairs = schedule_str.split(',')
for pair in pairs:
pair = pair.strip()
if ':' not in pair:
continue
parts = pair.split(':')
if len(parts) != 2:
continue
try:
percent = float(parts[0].strip())
strength = float(parts[1].strip())
percent = max(0.0, min(1.0, percent))
keyframes.append((percent, strength))
except ValueError:
continue
if not keyframes:
return None
keyframes.sort(key=lambda x: x[0])
return keyframes
def _interpolate_strength(keyframes: list, percent: float) -> float:
"""Linearly interpolate strength at a given percent."""
if not keyframes:
return 1.0
if percent <= keyframes[0][0]:
return keyframes[0][1]
if percent >= keyframes[-1][0]:
return keyframes[-1][1]
for i in range(len(keyframes) - 1):
p1, s1 = keyframes[i]
p2, s2 = keyframes[i + 1]
if p1 <= percent <= p2:
if p2 == p1:
return s1
t = (percent - p1) / (p2 - p1)
return s1 + t * (s2 - s1)
return keyframes[-1][1]
def _create_hook_keyframes(keyframes: list, num_keyframes: int = 20) -> comfy.hooks.HookKeyframeGroup:
"""
Create hook keyframes with linear interpolation between user-defined points.
"""
if not keyframes:
return None
kf_group = comfy.hooks.HookKeyframeGroup()
for i in range(num_keyframes + 1):
percent = i / num_keyframes
strength = _interpolate_strength(keyframes, percent)
guarantee_steps = 1 if i == 0 else 0
kf = comfy.hooks.HookKeyframe(strength=strength, start_percent=percent, guarantee_steps=guarantee_steps)
kf_group.add(kf)
return kf_group
def _invert_schedule(schedule_str: str) -> str:
"""
Invert a schedule string by subtracting each strength from 1.0.
"0:1, 0.5:0.3, 1:0" becomes "0:0, 0.5:0.7, 1:1"
Useful for pairing two LoRAs that crossfade.
"""
if not schedule_str or not schedule_str.strip():
return ""
keyframes = _parse_strength_schedule(schedule_str)
if not keyframes:
return ""
inverted_pairs = []
for percent, strength in keyframes:
inv_strength = 1.0 - strength
inverted_pairs.append(f"{percent}:{inv_strength}")
return ", ".join(inverted_pairs)
# ============================================================================
# NODE CLASS
# ============================================================================
class ScheduledLoRALoader:
"""
Simple LoRA loader with strength scheduling.
No block selection - just straightforward loading with
strength scheduling over the generation steps.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model": ("MODEL",),
"positive": ("CONDITIONING", {"tooltip": "Positive conditioning from CLIP encode"}),
"negative": ("CONDITIONING", {"tooltip": "Negative conditioning from CLIP encode"}),
"lora_name": (folder_paths.get_filename_list("loras"), {
"tooltip": "LoRA file to load"
}),
"strength": ("FLOAT", {
"default": 1.0,
"min": -10.0,
"max": 10.0,
"step": 0.05,
"tooltip": "LoRA strength (ignored when using schedule)"
}),
"schedule_preset": (SCHEDULE_PRESET_LIST, {
"default": "Custom",
"tooltip": "Select a preset schedule (populates the text field)"
}),
"strength_schedule": ("STRING", {
"default": "",
"tooltip": "Strength schedule: 0:.2,.5:.8,1:1.0 (step:strength pairs)"
}),
},
"optional": {
"lora_path_opt": ("STRING", {
"forceInput": True,
"tooltip": "Optional: override the LoRA dropdown with a file path (e.g. from a LoRA-manager / lora-stack node that outputs a path)."
}),
"schedule_in": ("STRING", {
"forceInput": True,
"tooltip": "Schedule input from another node (overrides preset/text field)"
}),
},
}
RETURN_TYPES = ("MODEL", "CONDITIONING", "CONDITIONING", "STRING", "STRING")
RETURN_NAMES = ("model", "positive", "negative", "schedule_out", "schedule_inv")
OUTPUT_TOOLTIPS = (
"Model with LoRA applied.",
"Positive conditioning (with hooks if using schedule).",
"Negative conditioning (with hooks if using schedule).",
"Active schedule string (for chaining to other loaders).",
"Inverted schedule (1 - strength at each keyframe, for complementary LoRAs).",
)
FUNCTION = "load_lora"
CATEGORY = "loaders/lora"
DESCRIPTION = """Simple LoRA loader with strength scheduling.
Schedule format: "step:strength" pairs, comma-separated.
Example: "0:.2, .5:.8, 1:1.0"
- 0% of steps: strength 0.2
- 50% of steps: strength 0.8
- 100% of steps: strength 1.0
Values interpolate linearly between keyframes.
Chain multiple loaders using schedule_out → schedule_in.
Use schedule_inv for complementary LoRA pairs (crossfade effect)."""
def load_lora(self, model, positive, negative, lora_name, strength,
schedule_preset="Custom", strength_schedule="", schedule_in=None, lora_path_opt=None):
# Get LoRA path (an optional path override takes precedence over the dropdown)
if lora_path_opt and os.path.exists(lora_path_opt):
lora_path = lora_path_opt
else:
lora_path = folder_paths.get_full_path("loras", lora_name)
if not lora_path or not os.path.exists(lora_path):
print(f"[ScheduledLoRALoader] Error: LoRA not found: {lora_name or lora_path_opt}")
return (model, positive, negative, "", "")
print(f"[ScheduledLoRALoader] Loading: {lora_name}")
# Load LoRA file
if lora_path.endswith('.safetensors'):
lora = comfy.utils.load_torch_file(lora_path, safe_load=True)
else:
lora = torch.load(lora_path, map_location='cpu')
# Determine active schedule (priority: schedule_in > text field > preset)
if schedule_in and schedule_in.strip():
effective_schedule = schedule_in.strip()
print(f"[ScheduledLoRALoader] Using schedule from input")
else:
effective_schedule = strength_schedule.strip() if strength_schedule else ""
if not effective_schedule and schedule_preset and schedule_preset != "Custom":
effective_schedule = SCHEDULE_PRESETS.get(schedule_preset, "")
# Generate inverted schedule for output
schedule_inv = _invert_schedule(effective_schedule)
schedule = _parse_strength_schedule(effective_schedule)
# Strength scheduling uses ComfyUI's hook system, which crashes on GGUF /
# fp8 quantized models (the hook path trips over quantized Linear layers at
# sample time). Detect those and fall back to a flat-strength apply instead
# of crashing mid-sample. See issues #26, #41, #51.
if schedule:
quantized, quant_label = is_quantized_model(model)
if quantized:
print(f"[ScheduledLoRALoader] WARNING: strength scheduling is not supported on "
f"{quant_label} models (hooks crash on quantized layers) - applying the LoRA "
f"at flat strength 1.0 instead.")
model_out, _ = comfy.sd.load_lora_for_models(model, None, lora, 1.0, 0.0)
return (model_out, positive, negative, effective_schedule, schedule_inv)
if schedule:
# Use hook system for scheduling
print(f"[ScheduledLoRALoader] Using schedule: {effective_schedule}")
# Create hooks - strength_model=1.0 so keyframe values ARE the strengths
hooks = comfy.hooks.create_hook_lora(lora=lora, strength_model=1.0, strength_clip=0.0)
kf_group = _create_hook_keyframes(schedule)
if kf_group and hooks:
hooks.set_keyframes_on_hooks(kf_group)
# Clone model and register hooks
model_out = model.clone()
target_dict = comfy.hooks.create_target_dict(comfy.hooks.EnumWeightTarget.Model)
model_out.register_all_hook_patches(hooks, target_dict)
# Attach hooks to conditioning
positive_out = comfy.hooks.set_hooks_for_conditioning(positive, hooks)
negative_out = comfy.hooks.set_hooks_for_conditioning(negative, hooks)
print(f"[ScheduledLoRALoader] Schedule applied with {len(schedule)} keyframes")
return (model_out, positive_out, negative_out, effective_schedule, schedule_inv)
else:
# Standard loading without schedule
model_out, _ = comfy.sd.load_lora_for_models(
model, None, lora, strength, 0.0
)
print(f"[ScheduledLoRALoader] Loaded with strength={strength}")
return (model_out, positive, negative, effective_schedule, schedule_inv)
# ============================================================================
# NODE REGISTRATION
# ============================================================================
NODE_CLASS_MAPPINGS = {
"ScheduledLoRALoader": ScheduledLoRALoader,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"ScheduledLoRALoader": "LoRA Loader (Scheduled)",
}