-
Notifications
You must be signed in to change notification settings - Fork 430
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
285 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from unittest import mock | ||
|
||
import pytest | ||
from torchtune.data import AlpacaInstructTemplate | ||
from torchtune.datasets._common import CROSS_ENTROPY_IGNORE_IDX | ||
|
||
from torchtune.datasets._instruct import _get_template, InstructDataset | ||
|
||
|
||
class DummyTokenizer: | ||
def encode(self, text, **kwargs): | ||
words = text.split() | ||
return [len(word) for word in words] | ||
|
||
|
||
class DummyTemplate: | ||
def __init__(self, template): | ||
self.template = template | ||
|
||
def format(self, sample, column_map): | ||
return self.template.format(**sample) | ||
|
||
|
||
class TestChatDataset: | ||
template = DummyTemplate( | ||
"Instruction:\n{instruction}\n\nInput:\n{input}\n\nResponse: " | ||
) | ||
expected_tokenized_prompts = [ | ||
[12, 4, 2, 3, 2, 12, 10, 6, 4, 2, 3, 2, 6, 10, 9, 1, 5, 4, 4, 3, 6, 2, 4], | ||
[12, 4, 2, 2, 12, 10, 6, 4, 2, 2, 6, 10, 9, 1, 6, 4, 4, 3, 6, 2, 4], | ||
] | ||
|
||
def get_samples(self): | ||
return [ | ||
{ | ||
"instruction": "This is not an instruction.", | ||
"input": "This is not an input.", | ||
"output": "I never know what I'm doing, do you?", | ||
}, | ||
{ | ||
"instruction": "This is an instruction.", | ||
"input": "This is an input.", | ||
"output": "I always know what I'm doing, do you?", | ||
}, | ||
] | ||
|
||
@mock.patch("torchtune.datasets._instruct.load_dataset") | ||
def test_get_item_no_train_on_input(self, mock_load_dataset): | ||
mock_load_dataset.return_value = self.get_samples() | ||
prompt_lengths = (15, 13) | ||
expected_labels = [ | ||
[CROSS_ENTROPY_IGNORE_IDX] * prompt_lengths[0] + [1, 5, 4, 4, 3, 6, 2, 4], | ||
[CROSS_ENTROPY_IGNORE_IDX] * prompt_lengths[1] + [1, 6, 4, 4, 3, 6, 2, 4], | ||
] | ||
|
||
dataset = InstructDataset( | ||
tokenizer=DummyTokenizer(), | ||
source="iam/agoofy/goober", | ||
template=self.template, | ||
transform=dummy_transform, | ||
train_on_input=False, | ||
) | ||
assert len(dataset) == 2 | ||
mock_load_dataset.assert_called_once() | ||
|
||
for i in range(len(dataset)): | ||
prompt, label = dataset[i] | ||
print(prompt, label) | ||
assert prompt == self.expected_tokenized_prompts[i] | ||
assert label == expected_labels[i] | ||
|
||
@mock.patch("torchtune.datasets._instruct.load_dataset") | ||
def test_get_item_train_on_input(self, mock_load_dataset): | ||
mock_load_dataset.return_value = self.get_samples() | ||
expected_labels = self.expected_tokenized_prompts | ||
|
||
dataset = InstructDataset( | ||
tokenizer=DummyTokenizer(), | ||
source="iam/agoofy/goober", | ||
template=self.template, | ||
transform=dummy_transform, | ||
train_on_input=True, | ||
) | ||
assert len(dataset) == 2 | ||
mock_load_dataset.assert_called_once() | ||
|
||
for i in range(len(dataset)): | ||
prompt, label = dataset[i] | ||
assert prompt == self.expected_tokenized_prompts[i] | ||
assert label == expected_labels[i] |
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,39 @@ | ||
# Copyright (c) Meta Platforms, Inc. and affiliates. | ||
# All rights reserved. | ||
# | ||
# This source code is licensed under the BSD-style license found in the | ||
# LICENSE file in the root directory of this source tree. | ||
|
||
from unittest import mock | ||
|
||
import pytest | ||
from torchtune.data import AlpacaInstructTemplate | ||
from torchtune.datasets._common import CROSS_ENTROPY_IGNORE_IDX | ||
|
||
from torchtune.datasets._instruct import _get_template | ||
from torchtune.datasets._utils import tokenize_prompt_and_response | ||
|
||
|
||
class DummyTokenizer: | ||
def encode(self, text, **kwargs): | ||
words = text.split() | ||
return [len(word) for word in words] | ||
|
||
|
||
def test_tokenize_prompt_and_response(): | ||
tokenizer = DummyTokenizer() | ||
prompt = "Instruction:\nThis is an instruction.\n\nInput:\nThis is an input.\n\nResponse: " | ||
response = "I always know what I'm doing, do you?" | ||
prompt_length = 13 | ||
expected_tokenized_prompt = [ | ||
12, 4, 2, 2, 12, 10, 6, 4, 2, 2, 6, 10, 9, 1, 6, 4, 4, 3, 6, 2, 4, | ||
] | ||
expected_tokenized_label = [CROSS_ENTROPY_IGNORE_IDX] * prompt_length + [1, 6, 4, 4, 3, 6, 2, 4] | ||
|
||
tokenized_prompt, tokenized_label = tokenize_prompt_and_response(tokenizer, prompt, response) | ||
assert tokenized_prompt == expected_tokenized_prompt | ||
assert tokenized_label == expected_tokenized_label | ||
|
||
tokenized_prompt, tokenized_label = tokenize_prompt_and_response(tokenizer, prompt, response, train_on_input=True) | ||
assert tokenized_prompt == expected_tokenized_prompt | ||
assert tokenized_label == expected_tokenized_prompt |
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
Oops, something went wrong.