forked from meta-llama/llama-recipes
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow easier use of custom datasets (meta-llama#178)
- Loading branch information
Showing
9 changed files
with
215 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. | ||
|
||
# For dataset details visit: https://huggingface.co/datasets/samsum | ||
|
||
import datasets | ||
|
||
from llama_recipes.datasets.utils import Concatenator | ||
|
||
def get_custom_dataset(dataset_config, tokenizer, split): | ||
dataset = datasets.load_dataset("samsum", split=split) | ||
|
||
prompt = ( | ||
f"Summarize this dialog:\n{{dialog}}\n---\nSummary:\n{{summary}}{{eos_token}}" | ||
) | ||
|
||
def apply_prompt_template(sample): | ||
return { | ||
"text": prompt.format( | ||
dialog=sample["dialogue"], | ||
summary=sample["summary"], | ||
eos_token=tokenizer.eos_token, | ||
) | ||
} | ||
|
||
dataset = dataset.map(apply_prompt_template, remove_columns=list(dataset.features)) | ||
|
||
dataset = dataset.map( | ||
lambda sample: tokenizer(sample["text"]), | ||
batched=True, | ||
remove_columns=list(dataset.features), | ||
).map(Concatenator(), batched=True) | ||
return dataset |
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,58 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. | ||
|
||
import pytest | ||
from unittest.mock import patch | ||
|
||
|
||
@patch('llama_recipes.finetuning.train') | ||
@patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained') | ||
@patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained') | ||
@patch('llama_recipes.finetuning.optim.AdamW') | ||
@patch('llama_recipes.finetuning.StepLR') | ||
def test_custom_dataset(step_lr, optimizer, tokenizer, get_model, train, mocker): | ||
from llama_recipes.finetuning import main | ||
|
||
tokenizer.return_value = mocker.MagicMock(side_effect=lambda x: {"input_ids":[len(x)*[0,]], "attention_mask": [len(x)*[0,]]}) | ||
|
||
kwargs = { | ||
"dataset": "custom_dataset", | ||
"custom_dataset.file": "examples/custom_dataset.py", | ||
"batch_size_training": 1, | ||
"use_peft": False, | ||
} | ||
|
||
main(**kwargs) | ||
|
||
assert train.call_count == 1 | ||
|
||
args, kwargs = train.call_args | ||
train_dataloader = args[1] | ||
eval_dataloader = args[2] | ||
|
||
VAL_SAMPLES = 818 | ||
TRAIN_SAMPLES = 14732 | ||
CONCAT_SIZE = 2048 | ||
|
||
assert len(train_dataloader) == TRAIN_SAMPLES // CONCAT_SIZE | ||
assert len(eval_dataloader) == VAL_SAMPLES | ||
|
||
|
||
@patch('llama_recipes.finetuning.train') | ||
@patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained') | ||
@patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained') | ||
@patch('llama_recipes.finetuning.optim.AdamW') | ||
@patch('llama_recipes.finetuning.StepLR') | ||
def test_unknown_dataset_error(step_lr, optimizer, tokenizer, get_model, train, mocker): | ||
from llama_recipes.finetuning import main | ||
|
||
tokenizer.return_value = mocker.MagicMock(side_effect=lambda x: {"input_ids":[len(x)*[0,]], "attention_mask": [len(x)*[0,]]}) | ||
|
||
kwargs = { | ||
"dataset": "custom_dataset", | ||
"custom_dataset.file": "examples/custom_dataset.py:get_unknown_dataset", | ||
"batch_size_training": 1, | ||
"use_peft": False, | ||
} | ||
with pytest.raises(AttributeError): | ||
main(**kwargs) |
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,37 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# This software may be used and distributed according to the terms of the Llama 2 Community License Agreement. | ||
|
||
from unittest.mock import patch | ||
|
||
|
||
@patch('llama_recipes.finetuning.train') | ||
@patch('llama_recipes.finetuning.LlamaForCausalLM.from_pretrained') | ||
@patch('llama_recipes.finetuning.LlamaTokenizer.from_pretrained') | ||
@patch('llama_recipes.finetuning.optim.AdamW') | ||
@patch('llama_recipes.finetuning.StepLR') | ||
def test_custom_dataset(step_lr, optimizer, tokenizer, get_model, train, mocker): | ||
from llama_recipes.finetuning import main | ||
|
||
tokenizer.return_value = mocker.MagicMock(side_effect=lambda x: {"input_ids":[len(x)*[0,]], "attention_mask": [len(x)*[0,]]}) | ||
|
||
|
||
kwargs = { | ||
"batch_size_training": 1, | ||
"use_peft": False, | ||
"dataset": "samsum_dataset", | ||
} | ||
|
||
main(**kwargs) | ||
|
||
assert train.call_count == 1 | ||
|
||
args, kwargs = train.call_args | ||
train_dataloader = args[1] | ||
eval_dataloader = args[2] | ||
|
||
VAL_SAMPLES = 818 | ||
TRAIN_SAMPLES = 14732 | ||
CONCAT_SIZE = 2048 | ||
assert len(train_dataloader) == TRAIN_SAMPLES // CONCAT_SIZE | ||
assert len(eval_dataloader) == VAL_SAMPLES | ||
|