-
Notifications
You must be signed in to change notification settings - Fork 297
feat: support multiple dataloader for grpo #1698
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
b7b22f0
support custom_dataloader
yuki-97 8c5e6bf
fix path and iter
yuki-97 eb69ac7
update config
RayenTian 61711ed
fix mcore sample count
yuki-97 4af6268
pyrefly
yuki-97 be8262f
check batch size
yuki-97 8ae14e9
fix rebase
yuki-97 11021a5
move wrap dataloader to setup
yuki-97 e84d94c
reuse multiple datasets functional test and update config
yuki-97 cba8f3a
add doc
yuki-97 e311709
fix type
yuki-97 a837cf8
fix unit test
yuki-97 c61553e
fix signature and add unit test
yuki-97 99b1379
update functional test
yuki-97 8eb538a
update print info
yuki-97 4b84948
update assert place
yuki-97 ff9832c
add example in doc
yuki-97 f464786
fix config
yuki-97 39272a2
add comment and warning
yuki-97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,104 @@ | ||
| # Copyright (c) 2026, NVIDIA CORPORATION. All rights reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| from typing import Iterator | ||
|
|
||
| from torchdata.stateful_dataloader import StatefulDataLoader | ||
|
|
||
| from nemo_rl.distributed.batched_data_dict import BatchedDataDict | ||
|
|
||
|
|
||
| def example_custom_dataloader( | ||
| data_iterators: dict[str, Iterator], | ||
| dataloaders: dict[str, StatefulDataLoader], | ||
| **kwargs, | ||
| ) -> tuple[BatchedDataDict, dict[str, Iterator]]: | ||
| """An example of custom dataloader function. | ||
|
|
||
| This function is used to sample data from multiple dataloaders using a custom dataloader function. | ||
| In this example, we simply sample data from each dataloader. | ||
yuki-97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| When a single dataloader is exhausted, the data iterator must be reset (as demonstrated here). | ||
| This design ensures that the MultipleDataloaderWrapper operates as an infinite iterator. | ||
|
|
||
| Args: | ||
| data_iterators: A dictionary of data iterators. | ||
| dataloaders: A dictionary of dataloaders. It is used to reset the data iterator when it is exhausted. | ||
| **kwargs: Additional arguments to pass to the custom dataloader function. | ||
|
|
||
| Returns: | ||
| Data from the dataloaders. | ||
| Updated data iterators (may update if the data iterator is exhausted). | ||
| """ | ||
| # sample data from each dataloader | ||
| result = [] | ||
| for task_name, data_iterator in data_iterators.items(): | ||
| try: | ||
| result.append(next(data_iterator)) | ||
| except StopIteration: | ||
| data_iterators[task_name] = iter(dataloaders[task_name]) | ||
| result.append(next(data_iterators[task_name])) | ||
yuki-97 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| # merge results | ||
| result = BatchedDataDict.from_batches(result) | ||
| return result, data_iterators | ||
|
|
||
|
|
||
| def example_custom_dataloader_with_chosen_task( | ||
| data_iterators: dict[str, Iterator], | ||
| dataloaders: dict[str, StatefulDataLoader], | ||
| chosen_task: list[str], | ||
| expected_num_prompts: int, | ||
| **kwargs, | ||
| ) -> tuple[BatchedDataDict, dict[str, Iterator]]: | ||
| """An example of custom dataloader function with chosen task. | ||
|
|
||
| This function is used to sample data from multiple dataloaders using a custom dataloader function. | ||
| In this example, we sample data from the chosen task. | ||
|
|
||
| This function will need to call `wrapped_dataloader.set_records({"chosen_task": ..., "expected_num_prompts": ...})` to set the records in `nemo_rl/algorithms/grpo.py`. | ||
| A usage example is shown in the test case `test_multiple_dataloader_with_records` in `tests/unit/data/test_multiple_dataloader.py`. | ||
|
|
||
| When a single dataloader is exhausted, the data iterator must be reset (as demonstrated here). | ||
| This design ensures that the MultipleDataloaderWrapper operates as an infinite iterator. | ||
|
|
||
| Args: | ||
| data_iterators: A dictionary of data iterators. | ||
| dataloaders: A dictionary of dataloaders. It is used to reset the data iterator when it is exhausted. | ||
| chosen_task: A list of task names to sample data from. | ||
| expected_num_prompts: The expected number of prompts to sample. | ||
|
|
||
| Returns: | ||
| Data from the dataloaders. | ||
| Updated data iterators (may update if the data iterator is exhausted). | ||
| """ | ||
| # sample data from the chosen task | ||
| result = [] | ||
| current_task_idx = 0 | ||
| current_num_prompts = 0 | ||
| while current_num_prompts < expected_num_prompts: | ||
| task_name = chosen_task[current_task_idx] | ||
| try: | ||
| data = next(data_iterators[task_name]) | ||
| except StopIteration: | ||
| data_iterators[task_name] = iter(dataloaders[task_name]) | ||
| data = next(data_iterators[task_name]) | ||
|
|
||
| result.append(data) | ||
| current_num_prompts += len(data["message_log"]) | ||
| current_task_idx = (current_task_idx + 1) % len(chosen_task) | ||
|
|
||
| # merge results | ||
| result = BatchedDataDict.from_batches(result) | ||
| return result, data_iterators | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.