fix: wrap training loop in torch.enable_grad()

ComfyUI executes all nodes inside torch.no_grad(), which prevents gradient
tracking and makes loss.backward() fail. torch.enable_grad() overrides it.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-05 22:32:00 +02:00
parent 8fade1b0e3
commit 505d445eb3
+2
View File
@@ -433,6 +433,8 @@ class SelvaLoraTrainer:
print(f"\n[LoRA Trainer] Training {remaining} steps "
f"(step {start_step + 1}{steps})\n", flush=True)
# ComfyUI runs nodes inside torch.no_grad() — re-enable gradients for training
with torch.enable_grad():
for step in range(start_step + 1, steps + 1):
x1_cpu, clip_f_cpu, sync_f_cpu, text_clip_cpu = random.choice(dataset)