-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[trainer, data] feat: Dynamic Data Generation #2312
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 19 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
4a4dbc0
add support for custom datagen class that allows for adding new data …
jwong8314 9d599ce
ruff
jwong8314 d91b626
ruff-format
jwong8314 84b3815
ruff-format
jwong8314 3c1cf80
Update license
jwong8314 9c65168
update license
jwong8314 8a04aca
fix: make sure if there's not data_generatore it doesn't crash
jwong8314 87b89d0
ruff-format
jwong8314 732b184
Merge branch 'main' into main
zhaochenyang20 ffba50d
Merge branch 'main' into dynamic_dataset
jwong8314 c620bcb
undo change to import_utils
jwong8314 6b061f9
merging into dataset
jwong8314 13debde
Merge pull request #1 from jwong8314/dynamic_dataset
jwong8314 19201e2
rename variables
jwong8314 72b223e
is_train rename
jwong8314 383cf61
Merge pull request #2 from jwong8314/dynamic_dataset
jwong8314 5070088
rename
jwong8314 3250d1d
rename to Generator
jwong8314 8c48f09
Merge pull request #3 from jwong8314/dynamic_dataset
jwong8314 0a5cabf
Merge branch 'main' into main
zhaochenyang20 4aac878
add parameter for batch information
jwong8314 e3bbd57
add comments and placed files in experimental
jwong8314 122e817
move to experimental subdir
jwong8314 a74ff75
ruff
jwong8314 2e44ead
ruff
jwong8314 6126b96
Merge branch 'volcengine:main' into main
jwong8314 16647d4
patch CI
jwong8314 171c9be
Merge branch 'main' into main
jwong8314 314d350
resolve conflicts new yaml
jwong8314 4bd1452
typo
jwong8314 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,108 @@ | ||
| # Copyright 2025 Amazon.com Inc and/or its affiliates | ||
| # | ||
| # 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. | ||
| """ | ||
| FSDP PPO Trainer with Ray-based single controller. | ||
jwong8314 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| This trainer supports model-agonistic model initialization with huggingface | ||
| """ | ||
|
|
||
| import logging | ||
| from abc import ABC, abstractmethod | ||
| from typing import List, Optional, Union | ||
|
|
||
| import datasets | ||
| from omegaconf import DictConfig | ||
| from torch.utils.data import Dataset | ||
| from transformers import PreTrainedTokenizer, ProcessorMixin | ||
|
|
||
| from verl.utils.dataset import RLHFDataset | ||
| from verl.utils.import_utils import load_extern_type | ||
|
|
||
| logger = logging.getLogger(__name__) | ||
|
|
||
|
|
||
| class AbstractDataGenerator(ABC): | ||
| def __init__(self, config: DictConfig): | ||
| self.config = config | ||
|
|
||
| @abstractmethod | ||
| def generate(self, dataset: Dataset) -> datasets.Dataset: | ||
| """ | ||
| Generate method must be implemented by subclasses. | ||
| Args: | ||
| dataset: The dataset to generate from. | ||
| Returns: | ||
| Processed data or result as implemented by the subclass. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| class MockDataGenerator(AbstractDataGenerator): | ||
jwong8314 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| """ | ||
| A noop data gen class that only reappends the first datapoint. | ||
| This class is useful as a placeholder and testing. | ||
| """ | ||
|
|
||
| def __init__(self, config: DictConfig = None): | ||
| super().__init__(config) | ||
|
|
||
| def generate(self, dataset: Dataset) -> datasets.Dataset: | ||
| print("MockDataGenerator: No operation performed on the dataset.") | ||
| return dataset.dataframe.select([0]) | ||
|
|
||
|
|
||
| class DynamicGenDataset(RLHFDataset): | ||
| """ | ||
| A dataset class that uses a data generation strategy to process data. | ||
| This class extends RLHFDataset and uses an AbstractDataGen instance to generate data. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, | ||
| data_files: Union[str, List[str]], | ||
| tokenizer: PreTrainedTokenizer, | ||
| config: DictConfig, | ||
| processor: Optional[ProcessorMixin] = None, | ||
| ): | ||
| super().__init__(data_files, tokenizer, config, processor) | ||
| self.datagen: AbstractDataGenerator = config.datagen | ||
| assert "datagen" in config and config.datagen.get("path", None) is not None, ( | ||
| f"datagen path is not set in config: {config}" | ||
| ) | ||
| # Dynamically load the custom datagen class | ||
| datagen_cls = load_extern_type(config.datagen.path, config.datagen.name) | ||
|
|
||
| # Verify that the custom datagen class inherits from AbstractDataGenerator | ||
| abs_cls = AbstractDataGenerator | ||
| if not issubclass(datagen_cls, abs_cls): | ||
| raise TypeError( | ||
| f"The custom datagen class '{config.datagen.name}' from '{config.datagen.path}'" | ||
| + " must inherit from {abs_cls}" | ||
| ) | ||
|
|
||
| self.data_generator = datagen_cls(config.datagen) | ||
| self.on_batch_end() | ||
|
|
||
| def append_dataframe(self, new_dataframe: datasets.Dataset): | ||
| new_dataframe = self.maybe_filter_out_long_prompts(new_dataframe) | ||
| self.dataframe = datasets.concatenate_datasets([self.dataframe, new_dataframe]) | ||
|
|
||
| logger.info(f"new dataset len: {len(self.dataframe)}") | ||
|
|
||
| def on_batch_end(self) -> None: | ||
| """ | ||
| Generate data using the provided data generation strategy. | ||
| Note: This method is intended to change the dataset after each training batch. | ||
| """ | ||
| new_data = self.data_generator.generate(self) | ||
| self.append_dataframe(new_data) | ||
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
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.