RuntimeError: Cannot writeback when the parameter shape changes
I'm currently running LLaMa2-13B parameters with my custom dataset prepared the same as described in tutorials/prepare_dataset.md and suddenly i encounter this issue after the first training iteration. Could you kindly help me with this ?
Number of trainable parameters: 52,428,800
Number of non trainable parameters: 13,015,864,320
Estimated #TFLOPs: 1133.04
Measured #TFLOPs: 1010.58
iter 0 step 0: loss 0.5429, iter time: 10856.57ms
Traceback (most recent call last):
File "finetune/lora.py", line 420, in <module>
CLI(setup)
File "/usr/local/lib/python3.8/dist-packages/jsonargparse/_cli.py", line 96, in CLI
return _run_component(components, cfg_init)
File "/usr/local/lib/python3.8/dist-packages/jsonargparse/_cli.py", line 181, in _run_component
return component(**cfg)
File "finetune/lora.py", line 222, in setup
fabric.launch(main,
File "/usr/local/lib/python3.8/dist-packages/lightning/fabric/fabric.py", line 836, in launch
return self._wrap_and_launch(function, self, *args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/lightning/fabric/fabric.py", line 922, in _wrap_and_launch
return to_run(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/lightning/fabric/fabric.py", line 927, in _wrap_with_setup
return to_run(*args, **kwargs)
File "finetune/lora.py", line 298, in main
train(fabric,
File "finetune/lora.py", line 145, in train
logits = model(input_ids,
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/lightning/fabric/wrappers.py", line 118, in forward
output = self._forward_module(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1518, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py", line 1527, in _call_impl
return forward_call(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/fully_sharded_data_parallel.py", line 825, in forward
args, kwargs = _pre_forward(
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/_runtime_utils.py", line 429, in _pre_forward
unshard_fn(state, handle)
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/_runtime_utils.py", line 464, in _pre_forward_unshard
_unshard(state, handle, state._unshard_stream, state._pre_unshard_stream)
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/_runtime_utils.py", line 336, in _unshard
ran_pre_unshard = handle.pre_unshard()
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/flat_param.py", line 1194, in pre_unshard
ret = self._writeback_orig_params()
File "/usr/local/lib/python3.8/dist-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/flat_param.py", line 2159, in _writeback_orig_params
self._writeback_tensor(
File "/usr/local/lib/python3.8/dist-packages/torch/distributed/fsdp/flat_param.py", line 2262, in _writeback_tensor
raise RuntimeError(
RuntimeError: Cannot writeback when the parameter shape changes
Expects torch.Size([163840000]) but got torch.Size([32000, 5120])
It appears that this error is caused by LoRA finetuning since it works perfectly when i use fully finetuning.
Can you share exactly how you called the script? And if you made any modifications
I didn't make any modifications. My script to produce the result is torchrun --nproc_per_node 1 finetune/lora.py --data_dir data/custom_data --checkpoint_dir checkpoints/meta-llama/Llama-2-13b-chat-hf --out_dir out/lora/llama2_13b --precision "bf16-true" with the checkpoint was downloaded from huggingface.
The script uses just 1 device by default: https://github.com/Lightning-AI/lit-gpt/blob/main/finetune/lora.py#L35.
If you haven't made any modifications, FSDP shouldn't be getting enabled.
Maybe using torchrun is causing this issue. Does it work after simply doing python finetune/lora.py?
cc @awaelchli, perhaps Fabric is mistakenly picking up environment variables set by torchrun when it shouldn't
Oh i forgot i did change the number of device to run multiple devices by using this devices = torch.cuda.device_count(). So sorry for this mistake.
There is no need to use torchrun with Lightning. But if you want, you can still do it and Lightning will recognize it. The issue you are seeing is probably because there is a mismatch in the devices. If you do
torchrun --nproc_per_node X ...
make sure that the script and Fabric gets devices=X as input as well.
I did try to use python finetune/lora.py instead of torchrun but the issue is still the same
I had similar issue when try to use FSDP to pretrain a small (1B ) LLM. My issue got resolved once I set the forward_prefetch=False, although I'm only using Pytorch 2.1, not lighting or hugging face tools.
came here searching for the same problem, unrelated to PTL - it seems that this problem has been resolved in torch-nightly (and forward_prefetch=False solves it too, but we want it True to avoid the fetching overhead).
@stas00 We are still getting this error with the latest torch==2.4.0 when training with LoRA and FSDP
--fsdp hybrid_shard auto_wrap \
--fsdp_config fsdp_config.json \
--per_device_train_batch_size 1 \
--gradient_accumulation_steps 4 \
--learning_rate 5e-05 \
--optim adamw_torch_fused \
--adam_beta1 0.9 \
--adam_beta2 0.95 \
--adam_epsilon 1e-10 \
--lr_scheduler_type linear \
--peft_method lora \
-r 8 \
--target_modules c_attn \
--lora_alpha 8 \
--lora_dropout 0.1 \
--torch_dtype float16
Error
Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future version. Using `--per_device_train_batch_size` is preferred.
Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future version. Using `--per_device_train_batch_size` is preferred.
/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/transformers/tokenization_utils_base.py:2888: UserWarning: `max_length` is ignored when `padding`=`True` and there is no truncation strategy. To pad to max length, use `padding='max_length'`.
warnings.warn(
/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_init_utils.py:440: UserWarning: FSDP is switching to use `NO_SHARD` instead of ShardingStrategy.HYBRID_SHARD since the world size is 1.
warnings.warn(
Using deprecated `--per_gpu_train_batch_size` argument which will be removed in a future version. Using `--per_device_train_batch_size` is preferred.
0%| | 0/11442 [00:00
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/tuning/sft_trainer.py", line 567, in main
trainer = train(
^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/tuning/sft_trainer.py", line 369, in train
trainer.train()
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/trl/trainer/sft_trainer.py", line 451, in train
output = super().train(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/transformers/trainer.py", line 1948, in train
return inner_training_loop(
^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/transformers/trainer.py", line 2289, in _inner_training_loop
tr_loss_step = self.training_step(model, inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/transformers/trainer.py", line 3328, in training_step
loss = self.compute_loss(model, inputs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/transformers/trainer.py", line 3373, in compute_loss
outputs = model(**inputs)
^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1553, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/nn/modules/module.py", line 1562, in _call_impl
return forward_call(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/fully_sharded_data_parallel.py", line 849, in forward
args, kwargs = _pre_forward(
^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_runtime_utils.py", line 381, in _pre_forward
unshard_fn(state, handle)
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_runtime_utils.py", line 416, in _pre_forward_unshard
_unshard(state, handle, state._unshard_stream, state._pre_unshard_stream)
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_runtime_utils.py", line 289, in _unshard
ran_pre_unshard = handle.pre_unshard()
^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_flat_param.py", line 1255, in pre_unshard
ret = self._writeback_orig_params()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/utils/_contextlib.py", line 116, in decorate_context
return func(*args, **kwargs)
^^^^^^^^^^^^^^^^^^^^^
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_flat_param.py", line 2252, in _writeback_orig_params
self._writeback_tensor(
File "/u/myuser/code/my-conda-envs/mycondaenv/lib/python3.11/site-packages/torch/distributed/fsdp/_flat_param.py", line 2351, in _writeback_tensor
raise RuntimeError(
RuntimeError: Cannot writeback when the parameter shape changes
Expects torch.Size([301989888]) but got torch.Size([49152, 6144])
You'd probably have a better luck reporting this as a pytorch Issue.
Our code base doesn't have this problem with pt-2.4
I also got this error using pt-2.4, everything goes well using world_size >= 2. I think this might be some bugs for NoShard
Also getting this error in a NoShard situation