-
Notifications
You must be signed in to change notification settings - Fork 441
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
24 changed files
with
497 additions
and
330 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,4 +13,4 @@ torchtune.datasets | |
alpaca_dataset | ||
grammar_dataset | ||
samsum_dataset | ||
SlimOrcaDataset | ||
slimorca_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,95 @@ | ||
# 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 torchtune.data import tokenize_prompt_and_response, truncate_if_necessary | ||
from torchtune.data._common import CROSS_ENTROPY_IGNORE_IDX | ||
|
||
|
||
class DummyTokenizer: | ||
def encode(self, text, **kwargs): | ||
words = text.split() | ||
return [len(word) for word in words] | ||
|
||
@property | ||
def eos_id(self): | ||
return -1 | ||
|
||
|
||
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 = 11 | ||
expected_tokenized_prompt = [ | ||
12, | ||
4, | ||
2, | ||
2, | ||
12, | ||
6, | ||
4, | ||
2, | ||
2, | ||
6, | ||
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 | ||
|
||
|
||
def test_truncate_if_necessary(): | ||
prompt_tokens = [1, 2, 3, 4, -1] | ||
label_tokens = [1, 2, 3, 4, -1] | ||
max_seq_len = 5 | ||
|
||
# Test no truncation | ||
truncated_prompt_tokens, truncated_label_tokens = truncate_if_necessary( | ||
tokenizer=DummyTokenizer(), | ||
prompt_tokens=prompt_tokens, | ||
label_tokens=label_tokens, | ||
max_seq_len=max_seq_len, | ||
) | ||
assert truncated_prompt_tokens == [1, 2, 3, 4, -1] | ||
assert truncated_label_tokens == [1, 2, 3, 4, -1] | ||
|
||
# Test truncated | ||
max_seq_len = 4 | ||
truncated_prompt_tokens, truncated_label_tokens = truncate_if_necessary( | ||
tokenizer=DummyTokenizer(), | ||
prompt_tokens=prompt_tokens, | ||
label_tokens=label_tokens, | ||
max_seq_len=max_seq_len, | ||
) | ||
assert truncated_prompt_tokens == [1, 2, 3, -1] | ||
assert truncated_label_tokens == [1, 2, 3, -1] |
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.