RuntimeError: function CheckpointFunctionBackward returned an incorrect number of gradients (expected 24, got 6)

username: Programer_Salar
cooked: <p>Traceback (most recent call last):<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/runpy.py”, line 196, in _run_module_as_main<br>
return _run_code(code, main_globals, None,<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/runpy.py”, line 86, in _run_code<br>
exec(code, run_globals)<br>
File “/home/manish/Desktop/stable-diffusion/stable-diffusion-from-scratch/Diffusion/main.py”, line 124, in <br>
trainer.fit(model,<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py”, line 561, in fit<br>
call._call_and_handle_interrupt(<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/call.py”, line 48, in _call_and_handle_interrupt<br>
return trainer_fn(*args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py”, line 599, in _fit_impl<br>
self._run(model, ckpt_path=ckpt_path)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py”, line 1012, in _run<br>
results = self._run_stage()<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/trainer.py”, line 1056, in _run_stage<br>
self.fit_loop.run()<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/fit_loop.py”, line 216, in run<br>
self.advance()<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/fit_loop.py”, line 455, in advance<br>
self.epoch_loop.run(self._data_fetcher)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/training_epoch_loop.py”, line 150, in run<br>
self.advance(data_fetcher)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/training_epoch_loop.py”, line 320, in advance<br>
batch_output = self.automatic_optimization.run(trainer.optimizers[0], batch_idx, kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/optimization/automatic.py”, line 192, in run<br>
self._optimizer_step(batch_idx, closure)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/optimization/automatic.py”, line 270, in _optimizer_step<br>
call._call_lightning_module_hook(<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/call.py”, line 176, in _call_lightning_module_hook<br>
output = fn(*args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/core/module.py”, line 1302, in optimizer_step<br>
optimizer.step(closure=optimizer_closure)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/core/optimizer.py”, line 154, in step<br>
step_output = self._strategy.optimizer_step(self._optimizer, closure, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/strategies/strategy.py”, line 239, in optimizer_step<br>
return self.precision_plugin.optimizer_step(optimizer, model=model, closure=closure, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/plugins/precision/amp.py”, line 79, in optimizer_step<br>
closure_result = closure()<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/optimization/automatic.py”, line 146, in <strong>call</strong><br>
self._result = self.closure(*args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/torch/utils/_contextlib.py”, line 116, in decorate_context<br>
return func(*args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/optimization/automatic.py”, line 140, in closure<br>
self._backward_fn(step_output.closure_loss)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/loops/optimization/automatic.py”, line 241, in backward_fn<br>
call._call_strategy_hook(self.trainer, “backward”, loss, optimizer)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/trainer/call.py”, line 328, in _call_strategy_hook<br>
output = fn(*args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/strategies/strategy.py”, line 213, in backward<br>
self.precision_plugin.backward(closure_loss, self.lightning_module, optimizer, *args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/plugins/precision/precision.py”, line 73, in backward<br>
model.backward(tensor, *args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/pytorch_lightning/core/module.py”, line 1097, in backward<br>
loss.backward(*args, **kwargs)<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/torch/_tensor.py”, line 581, in backward<br>
torch.autograd.backward(<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/torch/autograd/<strong>init</strong>.py”, line 347, in backward<br>
_engine_run_backward(<br>
File “/home/manish/anaconda3/envs/cuda121/lib/python3.10/site-packages/torch/autograd/graph.py”, line 825, in _engine_run_backward<br>
return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass<br>
RuntimeError: function CheckpointFunctionBackward returned an incorrect number of gradients (expected 24, got 6)</p>