From afb3242ecadf2bd8f393d67be0c3518bb53640c1 Mon Sep 17 00:00:00 2001 From: Ethanfel Date: Sun, 5 Apr 2026 22:40:50 +0200 Subject: [PATCH] fix: disable inference_mode entirely for training via inference_mode(False) torch.enable_grad() alone is insufficient: operations on inference tensors (created inside ComfyUI's outer inference_mode context) produce inference tensors even inside enable_grad, breaking autograd. inference_mode(False) exits the inference context so the deepcopy, apply_lora, and training loop run with a fully clean autograd context. Co-Authored-By: Claude Sonnet 4.6 --- nodes/selva_lora_trainer.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nodes/selva_lora_trainer.py b/nodes/selva_lora_trainer.py index 6ebd4b3..bc443fe 100644 --- a/nodes/selva_lora_trainer.py +++ b/nodes/selva_lora_trainer.py @@ -365,10 +365,11 @@ class SelvaLoraTrainer: raise ValueError("[LoRA Trainer] No clips could be loaded.") print(f"[LoRA Trainer] {len(dataset)} clip(s) ready.", flush=True) - # Everything from here runs inside enable_grad: ComfyUI wraps nodes in - # inference_mode, and nn.Parameters created in that context are inference - # tensors that can't enter autograd even with requires_grad=True. - with torch.enable_grad(): + # ComfyUI executes nodes inside torch.inference_mode(). Inference tensors + # can't participate in autograd even with enable_grad — disable inference + # mode entirely so deepcopy, apply_lora, and the training loop all run + # with a clean autograd context. + with torch.inference_mode(False), torch.enable_grad(): return self._train_inner( model, dataset, feature_utils_orig, seq_cfg, device, dtype, variant, mode,