-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: Expose additional data handlers as an argument in train #409
Merged
Abhishek-TAMU
merged 3 commits into
foundation-model-stack:main
from
dushyantbehl:additional_data_handlers
Dec 13, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 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 |
---|---|---|
|
@@ -15,7 +15,10 @@ | |
"""Unit Tests for SFT Trainer. | ||
""" | ||
|
||
# pylint: disable=too-many-lines | ||
|
||
# Standard | ||
from dataclasses import asdict | ||
import copy | ||
import json | ||
import os | ||
|
@@ -46,6 +49,13 @@ | |
from tuning import sft_trainer | ||
from tuning.config import configs, peft_config | ||
from tuning.config.tracker_configs import FileLoggingTrackerConfig | ||
from tuning.data.data_config import ( | ||
DataConfig, | ||
DataHandlerConfig, | ||
DataPreProcessorConfig, | ||
DataSetConfig, | ||
) | ||
from tuning.data.data_handlers import apply_dataset_formatting | ||
|
||
MODEL_ARGS = configs.ModelArguments( | ||
model_name_or_path=MODEL_NAME, use_flash_attn=False, torch_dtype="float32" | ||
|
@@ -1124,3 +1134,100 @@ def test_pretokenized_dataset_wrong_format(): | |
# is essentially swallowing a KeyError here. | ||
with pytest.raises(ValueError): | ||
sft_trainer.train(MODEL_ARGS, data_args, train_args, PEFT_PT_ARGS) | ||
|
||
|
||
########################################################################### | ||
### Tests for checking different cases for the argument additional_handlers | ||
### The argument `additional_handlers` in train::sft_trainer.py is used to pass | ||
### extra data handlers which should be a Dict[str,callable] | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"additional_handlers", | ||
[ | ||
"thisisnotokay", | ||
[], | ||
{lambda x: {"x": x}: "notokayeither"}, | ||
{"thisisfine": "thisisnot"}, | ||
], | ||
) | ||
def test_run_with_bad_additional_data_handlers(additional_handlers): | ||
"""Ensure that bad additional_handlers argument (which is not Dict[str,callable]) | ||
throws an error""" | ||
with tempfile.TemporaryDirectory() as tempdir: | ||
train_args = copy.deepcopy(TRAIN_ARGS) | ||
train_args.output_dir = tempdir | ||
|
||
with pytest.raises( | ||
ValueError, match="Handlers should be of type Dict, str to callable" | ||
): | ||
sft_trainer.train( | ||
MODEL_ARGS, | ||
DATA_ARGS, | ||
train_args, | ||
PEFT_PT_ARGS, | ||
additional_data_handlers=additional_handlers, | ||
) | ||
|
||
|
||
def test_run_with_additional_data_handlers_as_none(): | ||
"""Ensure that additional_handlers as None should work.""" | ||
with tempfile.TemporaryDirectory() as tempdir: | ||
train_args = copy.deepcopy(TRAIN_ARGS) | ||
train_args.output_dir = tempdir | ||
|
||
sft_trainer.train( | ||
MODEL_ARGS, | ||
DATA_ARGS, | ||
train_args, | ||
PEFT_PT_ARGS, | ||
additional_data_handlers=None, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can just add this line after this:
|
||
_validate_training(tempdir) | ||
|
||
|
||
def test_run_by_passing_additional_data_handlers(): | ||
"""Ensure that good additional_handlers argument can take a | ||
data handler and can successfully run a e2e training.""" | ||
# This is my test handler | ||
TEST_HANDLER = "my_test_handler" | ||
|
||
def test_handler(element, tokenizer, **kwargs): | ||
return apply_dataset_formatting(element, tokenizer, "custom_formatted_field") | ||
|
||
# This data config calls for data handler to be applied to dataset | ||
preprocessor_config = DataPreProcessorConfig() | ||
handler_config = DataHandlerConfig(name="my_test_handler", arguments=None) | ||
dataaset_config = DataSetConfig( | ||
name="test_dataset", | ||
data_paths=TWITTER_COMPLAINTS_DATA_JSON, | ||
data_handlers=[handler_config], | ||
) | ||
data_config = DataConfig( | ||
dataprocessor=preprocessor_config, datasets=[dataaset_config] | ||
) | ||
|
||
# dump the data config to a file, also test if json data config works | ||
with tempfile.NamedTemporaryFile( | ||
"w", delete=False, suffix=".json" | ||
) as temp_data_file: | ||
data_config_raw = json.dumps(asdict(data_config)) | ||
temp_data_file.write(data_config_raw) | ||
data_config_path = temp_data_file.name | ||
|
||
# now launch sft trainer after registering data handler | ||
with tempfile.TemporaryDirectory() as tempdir: | ||
train_args = copy.deepcopy(TRAIN_ARGS) | ||
train_args.output_dir = tempdir | ||
data_args = copy.deepcopy(DATA_ARGS) | ||
data_args.data_config_path = data_config_path | ||
data_args.dataset_text_field = "custom_formatted_field" | ||
|
||
sft_trainer.train( | ||
MODEL_ARGS, | ||
DATA_ARGS, | ||
train_args, | ||
PEFT_PT_ARGS, | ||
additional_data_handlers={TEST_HANDLER: test_handler}, | ||
) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here also, we can just add this line after this:
|
||
_validate_training(tempdir) |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: docstrings for all added test cases
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added comments. Thanks @willmj