element 0 of tensors does not require grad and does not have a grad_fn
Skipping first sample due to config setting tqe_wan22_14b_lora_1702_prodigy: 38%|██████████████████████████████████████████████████▊ | 3031/8000 [03:13<8:03:01, 5.83s/it, lr: 6.1e-05 loss: 4.117e-02]Traceback (most recent call last): File "D:\ai-toolkit\jobs\process\BaseSDTrainProcess.py", line 2136, in run loss_dict = self.hook_train_loop(batch_list) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\ai-toolkit\extensions_built_in\sd_trainer\SDTrainer.py", line 1930, in hook_train_loop loss = self.train_single_accumulation(batch) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\ai-toolkit\extensions_built_in\sd_trainer\SDTrainer.py", line 1905, in train_single_accumulation self.accelerator.backward(loss) File "D:\ai-toolkit.venv\Lib\site-packages\accelerate\accelerator.py", line 2734, in backward loss.backward(**kwargs) File "D:\ai-toolkit.venv\Lib\site-packages\torch_tensor.py", line 648, in backward torch.autograd.backward( File "D:\ai-toolkit.venv\Lib\site-packages\torch\autograd_init_.py", line 353, in backward _engine_run_backward( File "D:\ai-toolkit.venv\Lib\site-packages\torch\autograd\graph.py", line 824, in _engine_run_backward return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn Batch Items:
- L:\tqeflux\output\IMG_0878.JPG Error running job: element 0 of tensors does not require grad and does not have a grad_fn
======================================== Result:
- 0 completed jobs
- 1 failure
========================================
Traceback (most recent call last):
File "D:\ai-toolkit\run.py", line 120, in
main() File "D:\ai-toolkit\run.py", line 108, in main raise e File "D:\ai-toolkit\run.py", line 96, in main job.run() File "D:\ai-toolkit\jobs\ExtensionJob.py", line 22, in run process.run() File "D:\ai-toolkit\jobs\process\BaseSDTrainProcess.py", line 2144, in run raise e File "D:\ai-toolkit\jobs\process\BaseSDTrainProcess.py", line 2136, in run loss_dict = self.hook_train_loop(batch_list) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\ai-toolkit\extensions_built_in\sd_trainer\SDTrainer.py", line 1930, in hook_train_loop loss = self.train_single_accumulation(batch) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\ai-toolkit\extensions_built_in\sd_trainer\SDTrainer.py", line 1905, in train_single_accumulation self.accelerator.backward(loss) File "D:\ai-toolkit.venv\Lib\site-packages\accelerate\accelerator.py", line 2734, in backward loss.backward(**kwargs) File "D:\ai-toolkit.venv\Lib\site-packages\torch_tensor.py", line 648, in backward torch.autograd.backward( File "D:\ai-toolkit.venv\Lib\site-packages\torch\autograd_init_.py", line 353, in backward _engine_run_backward( File "D:\ai-toolkit.venv\Lib\site-packages\torch\autograd\graph.py", line 824, in _engine_run_backward return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn how can i fix it?thanks
Same thing here, but it is only happening when I am using WAN2.2 (14B) training AND in the multistage, stages to train, I select ONLY low noise. I can always continue the training once it failed, but it can happen from 1 to 5-6 times per training. I checked both picture and caption, no issue at all.
KHALIFA-WAN2.2-LORA: 41%|###########6 | 1161/2800 [19:28<59:02, 2.16s/it, lr: 1.0e-04 loss: 9.846e-02]Traceback (most recent call last): File "D:\AI_TOOLKIT_5\jobs\process\BaseSDTrainProcess.py", line 2136, in run loss_dict = self.hook_train_loop(batch_list) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI_TOOLKIT_5\extensions_built_in\sd_trainer\SDTrainer.py", line 1927, in hook_train_loop loss = self.train_single_accumulation(batch) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\AI_TOOLKIT_5\extensions_built_in\sd_trainer\SDTrainer.py", line 1902, in train_single_accumulation self.accelerator.backward(loss) File "D:\AI_TOOLKIT_5\venv\Lib\site-packages\accelerate\accelerator.py", line 2734, in backward loss.backward(**kwargs) File "D:\AI_TOOLKIT_5\venv\Lib\site-packages\torch_tensor.py", line 647, in backward torch.autograd.backward( File "D:\AI_TOOLKIT_5\venv\Lib\site-packages\torch\autograd__init.py", line 354, in backward _engine_run_backward( File "D:\AI_TOOLKIT_5\venv\Lib\site-packages\torch\autograd\graph.py", line 829, in _engine_run_backward return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn Batch Items:
D:\AI_TOOLKIT_5\datasets/khalifa\KHALIFA 42_.jpg Error running job: element 0 of tensors does not require grad and does not have a grad_fn 9.846e-02]
Result:
0 completed jobs 1 failure
Traceback (most recent call last):
File "D:\AI_TOOLKIT_5\run.py", line 120, in
When I set timestep_type: 'sigmoid' in my configuration, the training always fails with the following error during the loss.backward() step: RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
The Solution:
This error is completely and consistently resolved by changing the parameter to timestep_type: 'linear'. The training then proceeds without any issues.
When using 'sigmoid', the model's output tensor (noise_pred) was being generated with requires_grad=False before it was even passed to the loss function. My hypothesis is that an operation related to the 'sigmoid' timestep sampling or weighting might be unintentionally detaching the tensor from the graph (perhaps via a .detach() call or a torch.no_grad() context that is triggered by specific timestep values generated by the sigmoid schedule).
I have no issue with sigmoid if i am training with both low noise and high noise, only when i am just selecting low noise training. I will try linear
same for sigmoid low noise
This happens from time to time since i have enabled
ema_config:
use_ema: true
ema_decay: 0.99
RuntimeErrorRuntimeError: : element 0 of tensors does not require grad and does not have a grad_fnelement 0 of tensors does not require grad and does not have a grad_fn myLora:
Same here, if I select sigmoid and only training low noise
same here with sigmoid and traning only low in wan2.2
same for sigmoid and only low wan2.2
I ran a training with sigmoid at 4bit ara and it worked but i have the same issue with sigmoid, low wan 2.2 at 6 bit
same problem here, Low + sigmoid wan 2.2, Ema true_99= "element 0 of tensors does not require grad and does not have a grad_fn" randomly which can be after 500 steps or after 1000.
I've tried googling around on this and found others having similar problems, putting set_grad_enabled to true and inference_mode to false, I tried adding this to train_piopelines.py around #start grad, but it did not work.
Did anyone else have any luck figuring this out yet?
Only with Wan 2.2 low training with sigmoid. And it does seem quite random when it occurs, as some trainings run quite well for a long time (most recently 30% of total steps) before it suddenly occurs.
Was very happy when I managed to run training for Wan 2.2 14b yesterday on my 5080. That was High Noise, same settings today low noise/sigmoid and keep getting this issue.
@jaretburkett Sorry to ping you, but are you aware of this? Do you have any idea what might be happening? I left my training running overnight, and I just noticed it stopped.
Got same/simillar error
|###6 | 908/2500 [11:18<19:50, 1.34it/s, lr: 1.8e-04 loss: 8.973e-02]Error running job: element 0 of tensors does not require grad and does not have a grad_fn
========================================
Result:
- 0 completed jobs
- 1 failure
========================================
Traceback (most recent call last):
Traceback (most recent call last):
File "/root/ai-toolkit/run.py", line 120, in <module>
File "/root/ai-toolkit/run.py", line 120, in <module>
main()main()
File "/root/ai-toolkit/run.py", line 108, in main
File "/root/ai-toolkit/run.py", line 108, in main
raise eraise e
File "/root/ai-toolkit/run.py", line 96, in main
File "/root/ai-toolkit/run.py", line 96, in main
job.run()job.run()
File "/root/ai-toolkit/jobs/ExtensionJob.py", line 22, in run
File "/root/ai-toolkit/jobs/ExtensionJob.py", line 22, in run
process.run()process.run()
File "/root/ai-toolkit/jobs/process/BaseSDTrainProcess.py", line 2209, in run
File "/root/ai-toolkit/jobs/process/BaseSDTrainProcess.py", line 2209, in run
loss_dict = self.hook_train_loop(batch_list)loss_dict = self.hook_train_loop(batch_list)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/ai-toolkit/extensions_built_in/sd_trainer/SDTrainer.py", line 2061, in hook_train_loop
File "/root/ai-toolkit/extensions_built_in/sd_trainer/SDTrainer.py", line 2061, in hook_train_loop
loss = self.train_single_accumulation(batch)loss = self.train_single_accumulation(batch)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/root/ai-toolkit/extensions_built_in/sd_trainer/SDTrainer.py", line 2036, in train_single_accumulation
File "/root/ai-toolkit/extensions_built_in/sd_trainer/SDTrainer.py", line 2036, in train_single_accumulation
self.accelerator.backward(loss)self.accelerator.backward(loss)
File "/usr/local/lib/python3.12/site-packages/accelerate/accelerator.py", line 2838, in backward
File "/usr/local/lib/python3.12/site-packages/accelerate/accelerator.py", line 2838, in backward
loss.backward(**kwargs)loss.backward(**kwargs)
File "/usr/local/lib/python3.12/site-packages/torch/_tensor.py", line 631, in backward
File "/usr/local/lib/python3.12/site-packages/torch/_tensor.py", line 631, in backward
torch.autograd.backward(torch.autograd.backward(
File "/usr/local/lib/python3.12/site-packages/torch/autograd/__init__.py", line 381, in backward
File "/usr/local/lib/python3.12/site-packages/torch/autograd/__init__.py", line 381, in backward
_engine_run_backward(_engine_run_backward(
File "/usr/local/lib/python3.12/site-packages/torch/autograd/graph.py", line 869, in _engine_run_backward
File "/usr/local/lib/python3.12/site-packages/torch/autograd/graph.py", line 869, in _engine_run_backward
return Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward passreturn Variable._execution_engine.run_backward( # Calls into the C++ engine to run the backward pass
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
RuntimeErrorRuntimeError: : element 0 of tensors does not require grad and does not have a grad_fnelement 0 of tensors does not require grad and does not have a grad_fn
wan-my_28_captioned: 36%|###6 | 909/2500 [11:27<20:03, 1.32it/s, lr: 1.8e-04 loss: 8.973e-02]
@jaretburkett second this, please take a look if possible. Only happens with sigmoid, which works way better than linear for characters IMHO. Weird thing is, just restarting and brute forcing it a couple of times makes it work at some point, super annoying though:
job: "extension" config: name: "ang_00008" process: - type: "diffusion_trainer" training_folder: "D:\AITOOLKIT\ai-toolkit\output" sqlite_db_path: "./aitk_db.db" device: "cuda" trigger_word: null performance_log_every: 10 network: type: "lora" linear: 32 linear_alpha: 32 conv: 16 conv_alpha: 16 lokr_full_rank: true lokr_factor: -1 network_kwargs: ignore_if_contains: [] save: dtype: "bf16" save_every: 200 max_step_saves_to_keep: 6 save_format: "diffusers" push_to_hub: false datasets: - folder_path: "D:\AITOOLKIT\ai-toolkit\datasets/ang" mask_path: null mask_min_value: 0.1 default_caption: "" caption_ext: "txt" caption_dropout_rate: 0.08 cache_latents_to_disk: true is_reg: false network_weight: 1 resolution: - 512 controls: [] shrink_video_to_frames: true num_frames: 1 do_i2v: true flip_x: false flip_y: false train: batch_size: 1 bypass_guidance_embedding: false steps: 2000 gradient_accumulation: 1 train_unet: true train_text_encoder: false gradient_checkpointing: true noise_scheduler: "flowmatch" optimizer: "adamw8bit" timestep_type: "sigmoid" content_or_style: "balanced" optimizer_params: weight_decay: 0 unload_text_encoder: false cache_text_embeddings: true lr: 0.00008 ema_config: use_ema: false ema_decay: 0.99 skip_first_sample: true force_first_sample: false disable_sampling: true dtype: "bf16" diff_output_preservation: false diff_output_preservation_multiplier: 1 diff_output_preservation_class: "person" switch_boundary_every: 1 loss_type: "mse" model: name_or_path: "ai-toolkit/Wan2.2-T2V-A14B-Diffusers-bf16" quantize: true qtype: "qfloat8" quantize_te: true qtype_te: "qfloat8" arch: "wan22_14b:t2v" low_vram: true model_kwargs: train_high_noise: false train_low_noise: true layer_offloading: true layer_offloading_text_encoder_percent: 0.25 layer_offloading_transformer_percent: 0.25 sample: sampler: "flowmatch" sample_every: 200 width: 512 height: 512 samples: - prompt: "A Woman smiling at the camera." neg: "" seed: 42 walk_seed: false guidance_scale: 4 sample_steps: 15 num_frames: 16 fps: 16 logging: log_every: 1 use_ui_logger: true meta: name: "[name]" version: "1.0"
I also have the same issue
Same here: Wan2.2(14B), only low-noise, sigmoid.
This issue has been automatically closed due to inactivity. It has had no activity for 3 months.
If this issue is still relevant, please feel free to reopen it with updated information or context. We apologize for any inconvenience.