[FSDP] Move the flattened tensors back to GPU to prevent CPU OOM #124008
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I encountered a CPU OOM issue when resuming from a checkpoint with
FSDP.optim_state_dict_to_load
. See https://github.com/huggingface/accelerate/blob/5ca095a34fede7c988af8c193eb0c0d199750845/src/accelerate/utils/fsdp_utils.py#L208 for details.I notice that
_flatten_tensor_optim_state
and_flatten_zero_dim_tensor_optim_state
will create tensors on CPU, and results in the OOM issue. I have tried creating tensors on GPU directly, but it caused OOM on GPU.My solution is to move the flattened tensors back to the device where FSDP is running on. This works well for me with PyTorch 2.1.1. The current main branch doesn't seem to have fixed this issue yet.
cc @mrshenli @pritamdamania87 @zhaojuanmao @satgera @gqchen @aazzolini @osalpekar @jiayisuse @H-Huang @kwen2501 @awgu @penguinwu @fegin @XilunWu @wanchaol @fduwjj @wz337 @tianyu-l @wconstab @yf225 @chauhang @d4l3k @rohan-varma