Skip to content

Does AutoTP support sharding the optimizer states? #7242

@hijkzzz

Description

@hijkzzz

Previously, using ZeRO-3 (dp size = 4) to train llama3 8b with 4 A100 80G didn't cause OOM, but after switching to ZeRO-2 (dp size =2) with AutoTP(tp size = 2) or AutoTP(tp size = 4), it runs out of memory. I'm not sure why.

code: https://github.com/OpenRLHF/OpenRLHF/tree/auto_tp
script:

deepspeed --module openrlhf.cli.train_sft \
    --max_len 2048 \
    --dataset Open-Orca/OpenOrca \
    --input_key question \
    --output_key response \
    --train_batch_size 128 \
    --micro_train_batch_size 4 \
    --max_samples 1024 \
    --pretrain OpenRLHF/Llama-3-8b-sft-mixture \
    --save_path ./checkpoint/llama3-8b-sft \
    --save_steps -1 \
    --logging_steps 1 \
    --eval_steps -1 \
    --zero_stage 1 \
    --max_epochs 1 \
    --bf16 \
    --flash_attn \
    --learning_rate 5e-6 \
    --gradient_checkpointing \
    --packing_samples \
    --ds_tensor_parallel_size 4 \

logs

[rank1]: torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 1.96 GiB. GPU 1 has a total capacity of 79.14 GiB of which 1.01 GiB is free. Including non-PyTorch memory, this process has 0 bytes memory in use. Of the allocated memory 76.49 GiB is allocated by PyTorch, and 76.30 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.html#environment-variables)
[rank2]: Traceback (most recent call last):
[rank2]:   File "/usr/lib/python3.10/runpy.py", line 196, in _run_module_as_main
[rank2]:     return _run_code(code, main_globals, None,
[rank2]:   File "/usr/lib/python3.10/runpy.py", line 86, in _run_code
[rank2]:     exec(code, run_globals)
[rank2]:   File "/openrlhf/openrlhf/cli/train_sft.py", line 284, in <module>
[rank2]:     train(args)
[rank2]:   File "/openrlhf/openrlhf/cli/train_sft.py", line 141, in train
[rank2]:     trainer.fit(args, consumed_samples, num_update_steps_per_epoch)
[rank2]:   File "/openrlhf/openrlhf/trainer/sft_trainer.py", line 154, in fit
[rank2]:     self.strategy.backward(loss, self.model, self.optimizer)
[rank2]:   File "/openrlhf/openrlhf/utils/deepspeed/deepspeed.py", line 142, in backward
[rank2]:     model.backward(loss)
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/utils/nvtx.py", line 20, in wrapped_fn
[rank2]:     ret_val = func(*args, **kwargs)
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 2216, in backward
[rank2]:     self._do_optimizer_backward(loss, retain_graph)
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/runtime/engine.py", line 2175, in _do_optimizer_backward
[rank2]:     self.optimizer.backward(loss, retain_graph=retain_graph)
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/runtime/bf16_optimizer.py", line 327, in backward
[rank2]:     loss.backward(retain_graph=retain_graph, **bwd_kwargs)
[rank2]:   File "/usr/local/lib/python3.10/dist-packages/torch/_tensor.py", line 626, in backward
[rank2]:     torch.autograd.backward(
[rank2]:   File "/usr/local/lib/python3.10/dist-packages/torch/autograd/__init__.py", line 347, in backward
[rank2]:     _engine_run_backward(
[rank2]:   File "/usr/local/lib/python3.10/dist-packages/torch/autograd/graph.py", line 823, in _engine_run_backward
[rank2]:     return Variable._execution_engine.run_backward(  # Calls into the C++ engine to run the backward pass
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/runtime/bf16_optimizer.py", line 551, in accumulate_hp_grads_and_remove_lp
[rank2]:     self.accumulate_hp_grads_and_remove_lp(param, i, j)
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/runtime/bf16_optimizer.py", line 541, in accumulate_hp_grads_and_remove_lp
[rank2]:     self._update_hp_grad(lp_param, group_idx, param_idx, clear_lp_grads=False)
[rank2]:   File "/usr/local/lib/python3.10/dist-packages/torch/utils/_contextlib.py", line 116, in decorate_context
[rank2]:     return func(*args, **kwargs)
[rank2]:   File "/home/jianh/.local/lib/python3.10/site-packages/deepspeed/runtime/bf16_optimizer.py", line 341, in _update_hp_grad
[rank2]:     hp_grad.data.add_(lp.grad.data.to(hp_grad.dtype).view(hp_grad.shape))
[rank2]: torch.OutOfMemoryError: CUDA out of memory. Tried to allocate 1.96 GiB. GPU 2 has a total capacity of 79.14 GiB of which 1.01 GiB is free. Including non-PyTorch memory, this process has 0 bytes memory in use. Of the allocated memory 76.49 GiB is allocated by PyTorch, and 76.30 MiB is reserved by PyTorch but unallocated. If reserved but unallocated memory is large try setting PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True to avoid fragmentation.  See documentation for Memory Management  (https://pytorch.org/docs/stable/notes/cuda.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtraining

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions