Repository navigation
Fast dataset resume - #1082
Fast dataset resume#1082
Conversation
tianyu-l
left a comment
There was a problem hiding this comment.
Thank you for making resuming dataset checkpoint efficient! I had some inline comments. Please see if they make sense to you.
Regarding
On the other hand, for iterable-style datasets, calling
.skipaftersplit_dataset_by_nodesplits the number of elements to skip across the ranks (e.g. calling.skip(10)aftersplit_dataset_by_node(<rank>, 2)effectively skips 5 (10 // 2 = 5) elements on each rank), which isn'r what we want/expect, so removing.skipwas justified there.
I'm a bit confused by your comment. It sounds that the behavior of skip for IterableDataset is oblivious of whether it has gone through split_dataset_by_node or not, which is not intuitive?
| assert torch.equal(input_ids["input"], expected_input_ids["input"]) | ||
| assert torch.equal(labels, expected_labels) | ||
| for dataset_name in ["c4_test", "c4_test_streaming"]: | ||
| dataset_name = "c4_test" |
| "c4_test_streaming": DatasetConfig( | ||
| path="tests/assets/c4_test", | ||
| loader=lambda path: load_dataset(path, split="train", streaming=True), | ||
| text_processor=_process_c4_text, | ||
| ), |
There was a problem hiding this comment.
Since this is only used in unit test, can we only register it in the test file?
| # Variables for checkpointing | ||
| self._sample_idx = 0 | ||
| self._all_tokens: list[int] = [] | ||
| self._data_state_dict_loaded = False |
There was a problem hiding this comment.
Thanks for considering backward compatibility. At this moment, I think we should just move with the right and efficient behavior, without worrying too much about it.
| } | ||
|
|
||
| if isinstance(self._data, IterableDataset): | ||
| _state_dict["data"] = self._data.state_dict() |
There was a problem hiding this comment.
Is this supported in datasets 2.21.0, or newer version? If so, we need to update requirements.txt.
There was a problem hiding this comment.
Also let's add some comment on the efficiency of this solution with reference to https://huggingface.co/docs/datasets/v3.5.0/en/stream#save-a-dataset-checkpoint-and-resume-iteration
| self._all_tokens = state_dict["token_buffer"] | ||
|
|
||
| if isinstance(self._data, IterableDataset): | ||
| if "data" in state_dict: # backward compatibility |
There was a problem hiding this comment.
By not worrying about BC breaking, let's assert the existence of "data"
| elif not self._data_state_dict_loaded: # backward compatibility | ||
| it = iter(self._data) | ||
| for _ in range(self._sample_idx): | ||
| next(it) | ||
| return it |
There was a problem hiding this comment.
let's remove, for simplicity
| self._sample_idx = state_dict["sample_idx"] | ||
| self._all_tokens = state_dict["token_buffer"] | ||
|
|
||
| if isinstance(self._data, IterableDataset): |
There was a problem hiding this comment.
One thing I'm curious is -- why don't map-style datasets provide the same API to make API consistent? It could be as simple as what we do here using self._data.skip(self._sample_idx).
There was a problem hiding this comment.
Dunno, I'm no longer involved in the datasets project, so I don't know exactly, but I assume the reasoning is that it's not too hard to resume map-style datasets manually, as explained in huggingface/datasets#5454 (comment)
| def state_dict(self): | ||
| return {"token_buffer": self._all_tokens, "sample_idx": self._sample_idx} | ||
| _state_dict = { | ||
| "token_buffer": self._all_tokens, |
There was a problem hiding this comment.
Could you do torchtitan a favor and rename self._all_tokens to self._token_buffer, to make things consistent?
| return {"token_buffer": self._all_tokens, "sample_idx": self._sample_idx} | ||
| _state_dict = { | ||
| "token_buffer": self._all_tokens, | ||
| "sample_idx": self._sample_idx, |
There was a problem hiding this comment.
can we save this in state dict only when it's map-style dataset?
|
Can you we have an accuracy verification for this PR? I believe llama3 8B can reproduce the loss issue if the dataset doesn't resume correctly. |
|
Sorry for the delay, I addressed the comments and made the test much more robust.
It's hard to explain because the
I don't have access to A100 / H100 GPUs right now, so it would be great if someone else could do the run. I improved the test significantly (e.g. now it re-loops the test datasets), so I'm not sure if this is really needed, though. |
|
Can you also fix the linter error and integration test error? I will try if I can verify with llama3. |
|
Unfortunately, one more bug needs to be fixed, this time directly in EDIT: |
|
Okay, I think this is ready for the verification test. |
| path="tests/assets/c4_test", | ||
| loader=lambda path: load_dataset(path, split="train"), | ||
| text_processor=_process_c4_text, | ||
| text_processor=lambda sample: sample["text"], |
| loader=lambda path: load_dataset(path, split="train").to_iterable_dataset( | ||
| num_shards=4 | ||
| ), |
There was a problem hiding this comment.
curious what's the difference between this and setting streaming=True in load_dataset
There was a problem hiding this comment.
With streaming=True in load_dataset we cannot control the number of underlying shards, but this way we can.
| def _get_data_iter(self): | ||
| if isinstance(self._data, Dataset) and self._sample_idx == len(self._data): | ||
| return iter([]) | ||
| if isinstance(self._data, Dataset): |
There was a problem hiding this comment.
I think it's worth commenting here that map-style Dataset and IterableDataset have different ways of resuming from checkpoint -- Dataset uses the _sample_idx in state dict to relocate self._data whereas for IterableDataset self._data has been updated to the correct index during state dict loading.
There was a problem hiding this comment.
also a side note:
It seems we have quite different treatment for map-style and iterable, which causes several if-else conditions. In the future it may be worth a slight refactoring into two subclasses of the same parent sharing common interfaces -- although I still think it's better for datasets to provide consistent interface (e.g. removing the need for explicitly storing _sample_idx), lol
fegin
left a comment
There was a problem hiding this comment.
The loss curve looks okay locally. Please fix the linter. The 8GPU integration test is not related.
This PR makes resuming dataset iteration from a checkpoint fast again. This performance regression comes from pytorch#838. In that PR, `.skip` is removed for both map-style and iterable-style datasets for correctness reasons. However, `.skip` works as expected for map-style datasets, so the change can be reverted for that case. On the other hand, for iterable-style datasets, calling `.skip` after `split_dataset_by_node` splits the number of elements to skip **across the ranks** (e.g. calling `.skip(10)` after `split_dataset_by_node(<rank>, 2)` effectively skips 5 (`10 // 2 = 5`) elements on each rank), which isn'r what we want/expect, so removing `.skip` was justified there. Still, we can make the whole thing much faster using the [`state_dict` API](https://huggingface.co/docs/datasets/v3.5.0/en/stream#save-a-dataset-checkpoint-and-resume-iteration) for iterable-style datasets, which avoids re-iterating past shards/files when resuming.
This PR makes resuming dataset iteration from a checkpoint fast again. This performance regression comes from pytorch#838. In that PR, `.skip` is removed for both map-style and iterable-style datasets for correctness reasons. However, `.skip` works as expected for map-style datasets, so the change can be reverted for that case. On the other hand, for iterable-style datasets, calling `.skip` after `split_dataset_by_node` splits the number of elements to skip **across the ranks** (e.g. calling `.skip(10)` after `split_dataset_by_node(<rank>, 2)` effectively skips 5 (`10 // 2 = 5`) elements on each rank), which isn'r what we want/expect, so removing `.skip` was justified there. Still, we can make the whole thing much faster using the [`state_dict` API](https://huggingface.co/docs/datasets/v3.5.0/en/stream#save-a-dataset-checkpoint-and-resume-iteration) for iterable-style datasets, which avoids re-iterating past shards/files when resuming.
This PR makes resuming dataset iteration from a checkpoint fast again.
This performance regression comes from #838. In that PR,
.skipis removed for both map-style and iterable-style datasets for correctness reasons. However,.skipworks as expected for map-style datasets, so the change can be reverted for that case. On the other hand, for iterable-style datasets, calling.skipaftersplit_dataset_by_nodesplits the number of elements to skip across the ranks (e.g. calling.skip(10)aftersplit_dataset_by_node(<rank>, 2)effectively skips 5 (10 // 2 = 5) elements on each rank), which isn'r what we want/expect, so removing.skipwas justified there. Still, we can make the whole thing much faster using thestate_dictAPI for iterable-style datasets, which avoids re-iterating past shards/files when resuming.