-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use stateful dataloader to checkpoint data iteration order and token …
…buffer (#279) Summary: Use the stateful_dataloader from torchdata (https://github.com/pytorch/data/tree/main/torchdata/stateful_dataloader) for storing the token buffer and iteration data order. It requires a dependency on the nightly build of torchdata >= 20240426. Also make sure the dataloader state has a different key per rank. Test Plan: Tested locally by first running 30 steps (checkpointing every 5 steps) and capturing all the loss values. Then deleting the last 3 checkpoints and then re-run the training and the loss values from step 16-30 match with what we had earlier in the first run. Note that this requires changes in the train.py to enable a deterministic run. Reviewers: @tianyu-l Subscribers: @andrewkho Tasks: Tags:
- Loading branch information
1 parent
3bd14ec
commit 99a73dd
Showing
12 changed files
with
145 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
import torch | ||
from torchtitan.datasets.hf_datasets import build_hf_data_loader | ||
from torchtitan.datasets.tokenizer import create_tokenizer | ||
|
||
|
||
class TestCheckpoint: | ||
def test_c4_resumption(self): | ||
dataset_name = "c4_mini" | ||
dataset_path = "./torchtitan/datasets/c4_mini" | ||
batch_size = 1 | ||
seq_len = 1024 | ||
world_size = 4 | ||
rank = 0 | ||
|
||
dl = self._build_dataloader( | ||
dataset_name, dataset_path, batch_size, seq_len, world_size, rank | ||
) | ||
|
||
it = iter(dl) | ||
for _ in range(250): | ||
next(it) | ||
state = dl.state_dict() | ||
expected_input_ids, expected_labels = next(it) | ||
|
||
# Create new dataloader, restore checkpoint, and check if next data yielded is the same as above | ||
dl = self._build_dataloader( | ||
dataset_name, dataset_path, batch_size, seq_len, world_size, rank | ||
) | ||
dl.load_state_dict(state) | ||
input_ids, labels = next(iter(dl)) | ||
|
||
assert torch.equal(input_ids, expected_input_ids) | ||
assert torch.equal(labels, expected_labels) | ||
|
||
def _build_dataloader( | ||
self, dataset_name, dataset_path, batch_size, seq_len, world_size, rank | ||
): | ||
tokenizer_type = "tiktoken" | ||
tokenizer = create_tokenizer("tiktoken", "./test/assets/test_tiktoken.model") | ||
return build_hf_data_loader( | ||
dataset_name=dataset_name, | ||
dataset_path=dataset_path, | ||
tokenizer=tokenizer, | ||
batch_size=1, | ||
seq_len=1024, | ||
world_size=4, | ||
rank=0, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
should torchdata be added to requirements too? or how would users know to install it other than looking at our CI yaml?