You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I looked at the deepspeed implementation before, which had a similar issue with shared weights. The problem was that the algorithm splits all tensors found in the optimizer state, which includes the quantization statistics. But this can lead to incorrect behavior. The workaround in deepspeed is to hide the quantization statistics by obscuring their type (putting the tensor into a list/tuple).
I am not sure if the error message that you provided is related to that or not.
It would be nice if we could get 8-bit Adam working for FSDP. Would you be able to provide a simple example for debugging and replication purposes? Since I will be pretty busy the next month, I would also be very happy to guide you on how to fix this if you create a PR and provide me with error messages / stack traces. I think it would be pretty useful since more and more people are using FSDP.
I created a gist with an example. The gist includes a process_dataset.py to prepare a dataset and a run_clm_bnb8.py script, which uses the adamw_bnb_8bit optimizer and FSDP.
Hi @TimDettmers in my latest test, it turns out that saving the model is the source of this issue.
Specifically the error pops up when I run this: optim_state = FSDP.full_optim_state_dict(model, optimizer)
What this is supposed to do is assemble the entire optimizer based on the model params. Now what I think is the problem is that the optimizer is in 8-bit but the model is not in 8-bit. The reason for my assumption is the error is thrown by
File "/share03/draj/environments/.conda/envs/yanmtt/lib/python3.9/site-packages/torch/distributed/distributed_c10d.py", line 2136, in _all_gather_base
work = group._allgather_base(output_tensor, input_tensor)
There is another issue.
When I applied FSDP cpu offload with Adam8bit, I got the following error:
Expected a cuda device, but got: cpu
Traceback (most recent call last):
File "scripts/sft/run_train.py", line 509, in <module>
main()
File "scripts/sft/run_train.py", line 503, in main
run(artifact_config, train_config, experiment_config, execution_config)
File "scripts/sft/run_train.py", line 378, in run
optimizer.step()
File "/home/kyeongpil/venv/lib/python3.8/site-packages/torch/optim/lr_scheduler.py", line 68, in wrapper
return wrapped(*args, **kwargs)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/accelerate/optimizer.py", line 140, in step
self.optimizer.step(closure)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/torch/optim/optimizer.py", line 140, in wrapper
out = func(*args, **kwargs)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/bitsandbytes/optim/optimizer.py", line 263, in step
self.update_step(group, p, gindex, pindex)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/torch/autograd/grad_mode.py", line 27, in decorate_context
return func(*args, **kwargs)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/bitsandbytes/optim/optimizer.py", line 504, in update_step
F.optimizer_update_8bit_blockwise(
File "/home/kyeongpil/venv/lib/python3.8/site-packages/bitsandbytes/functional.py", line 972, in optimizer_update_8bit_blockwise
prev_device = pre_call(g.device)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/bitsandbytes/functional.py", line 318, in pre_call
torch.cuda.set_device(device)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/torch/cuda/__init__.py", line 324, in set_device
device = _get_device_index(device)
File "/home/kyeongpil/venv/lib/python3.8/site-packages/torch/cuda/_utils.py", line 30, in _get_device_index
raise ValueError('Expected a cuda device, but got: {}'.format(device))
ValueError: Expected a cuda device, but got: cpuRank(3)
I encountered a similar issue using PEFT LoRA, load_in_8bit, and DeepSpeed 3 (optimizer and params offload) with huggingface accelerator. on a single gpu, training was fine as expected.
If anyone found a workaround to enable parallel training with PEFT LoRA and load_in_8bit, please let me know.
Reacted by Jeremy Jahn, whus6207, Roman Koshkin, Nick Korovaiko, Adib Mosharrof and Sanghyuk Choi
on the error @prajdabre was mentioning, I find that the problem does not come from a dtype mismatch, but rather a size mismatch. With printf debugging, I noticed that this seemed to first error on the absmax1 value, with
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
No unfortunately.
I was able to use FSDP2 with only data parallel, rather than sharding. But didnt give much benefit due to not Sharding.
Sharding with FSDP2 + bitsandbytes doesnt work still.
Activity
TimDettmers commentedon Jan 3, 2023
I looked at the deepspeed implementation before, which had a similar issue with shared weights. The problem was that the algorithm splits all tensors found in the optimizer state, which includes the quantization statistics. But this can lead to incorrect behavior. The workaround in deepspeed is to hide the quantization statistics by obscuring their type (putting the tensor into a list/tuple).
I am not sure if the error message that you provided is related to that or not.
It would be nice if we could get 8-bit Adam working for FSDP. Would you be able to provide a simple example for debugging and replication purposes? Since I will be pretty busy the next month, I would also be very happy to guide you on how to fix this if you create a PR and provide me with error messages / stack traces. I think it would be pretty useful since more and more people are using FSDP.
philschmid commentedon Mar 24, 2023
Hey @TimDettmers,
I created a gist with an example. The gist includes a
process_dataset.pyto prepare a dataset and arun_clm_bnb8.pyscript, which uses theadamw_bnb_8bitoptimizer andFSDP.https://gist.github.com/philschmid/99410e8bf66d34e52bb0cd5270b07989
I hope that's enough for you to test it.
philschmid commentedon Mar 28, 2023
I tested the example i shared with the
adamw_bnb_8bitandadafactorit seems that its not working if the training runs.AdamWInt8
{'loss': 2.6643, 'learning_rate': 4.847094801223242e-05, 'epoch': 0.09} {'loss': 2.752, 'learning_rate': 4.694189602446483e-05, 'epoch': 0.18} {'loss': 3.1493, 'learning_rate': 4.541284403669725e-05, 'epoch': 0.28} {'loss': 3.412, 'learning_rate': 4.3883792048929664e-05, 'epoch': 0.37} {'loss': 3.6722, 'learning_rate': 4.0825688073394495e-05, 'epoch': 0.55}Adafactor
{'loss': 2.8385, 'learning_rate': 4.847094801223242e-05, 'epoch': 0.09} {'loss': 2.6384, 'learning_rate': 4.694189602446483e-05, 'epoch': 0.18} {'loss': 2.5725, 'learning_rate': 4.541284403669725e-05, 'epoch': 0.28} {'loss': 2.5757, 'learning_rate': 4.3883792048929664e-05, 'epoch': 0.37} {'loss': 2.5297, 'learning_rate': 4.0825688073394495e-05, 'epoch': 0.55}prajdabre commentedon Apr 3, 2023
Hi @TimDettmers in my latest test, it turns out that saving the model is the source of this issue.
Specifically the error pops up when I run this: optim_state = FSDP.full_optim_state_dict(model, optimizer)
What this is supposed to do is assemble the entire optimizer based on the model params. Now what I think is the problem is that the optimizer is in 8-bit but the model is not in 8-bit. The reason for my assumption is the error is thrown by
File "/share03/draj/environments/.conda/envs/yanmtt/lib/python3.9/site-packages/torch/distributed/distributed_c10d.py", line 2136, in _all_gather_base
work = group._allgather_base(output_tensor, input_tensor)
Indeed if you look here: https://github.com/pytorch/pytorch/blob/55daa835e97a6e742cba1f0e9d2a5c78b1615e99/torch/csrc/distributed/c10d/ProcessGroupNCCL.cpp#L2779
Then there is a constraint that the dtypes of tensors should be the same and we are not able to guarantee this for a sharded 8-bit optimizer.
If we can find some way to bypass this requirement, then we are good to go.
How do we overcome this issue?
Kyeongpil commentedon Apr 19, 2023
I have the same issue. #323
Is there any solution to solve this problem? @TimDettmers @prajdabre
Kyeongpil commentedon Apr 20, 2023
There is another issue.
When I applied FSDP cpu offload with Adam8bit, I got the following error:
prajdabre commentedon Apr 28, 2023
Im not a 100% sure but this might be taken care of in pytorch 2.0.
dotsnangles commentedon May 7, 2023
I encountered a similar issue using PEFT LoRA, load_in_8bit, and DeepSpeed 3 (optimizer and params offload) with huggingface accelerator. on a single gpu, training was fine as expected.
If anyone found a workaround to enable parallel training with PEFT LoRA and load_in_8bit, please let me know.
hscspring commentedon May 16, 2023
it seems that pytorch 2 doesnot support 8bit
152334H commentedon Jul 9, 2023
anyone still working on this....?
on the error @prajdabre was mentioning, I find that the problem does not come from a dtype mismatch, but rather a size mismatch. With printf debugging, I noticed that this seemed to first error on the absmax1 value, with
HamidShojanazeri commentedon Jul 18, 2023
cc @awgu
github-actions commentedon Dec 20, 2023
This issue has been automatically marked as stale because it has not had recent activity. If you think this still needs to be addressed please comment on this thread.
152334H commentedon Dec 22, 2023
Noting that this issue, although stale, remains an issue. Although optimization can run, a functional state dict cannot be saved with 8bitadam.
I notice that there is a PR for FSDP functionality in #840. It generally does not address the state dict issue in its tests.
13 remaining items
musabgultekin commentedon Oct 28, 2024
No unfortunately.
I was able to use FSDP2 with only data parallel, rather than sharding. But didnt give much benefit due to not Sharding.
Sharding with FSDP2 + bitsandbytes doesnt work still.
musabgultekin commentedon Oct 28, 2024
Use torchtune with torch2.5 for lower memory requirements, if you're fine-tuning LLMs
nighting0le01 commentedon Oct 28, 2024
@musabgultekin did you explore torchao low-bit optims? to work with FSDP2?
musab-mk commentedon Jan 17, 2025
@nighting0le01 I have not received notification for this comment, sorry for late response.
I can confirm
torchao.prototype.low_bit_optim.AdamW8bitis working with FSDP2!lakshya-skyfall commentedon Apr 27, 2026
This issue is still not fixed. I am not able to save the 8 bit AdamW optimizer with bnb while training a fp32 model.