-
Notifications
You must be signed in to change notification settings - Fork 404
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor datasets and tokenizer (#624)
- Loading branch information
1 parent
0a82ea4
commit 8183b42
Showing
23 changed files
with
984 additions
and
821 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
# 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. | ||
|
||
import pytest | ||
from torchtune.data import ChatMLFormat, Llama2ChatFormat, Message, MistralChatFormat | ||
|
||
# Taken from Open-Orca/SlimOrca-Dedup on HuggingFace: | ||
# https://huggingface.co/datasets/Open-Orca/SlimOrca-Dedup | ||
CHAT_SAMPLE = [ | ||
Message( | ||
role="system", | ||
content="You are an AI assistant. User will you give you a task. " | ||
"Your goal is to complete the task as faithfully as you can. " | ||
"While performing the task think step-by-step and justify your steps.", | ||
), | ||
Message( | ||
role="user", | ||
content="Please briefly summarize this news article:\n\nAOL.com Video - " | ||
"Father Lets 8-Year-Old Drive On Icy Road\n\nDescription:Would you let your " | ||
"8-year-old drive your car? How about on an icy road? Well one father in " | ||
"Russia did just that, and recorded the entire thing. To her credit, the " | ||
"child seemed to be doing a great job. (0:44)\n\nTags: 8-year-old driver , " | ||
"caught on camera , child driver , pix11\n\nSummary:", | ||
), | ||
Message( | ||
role="assistant", | ||
content="A father in Russia allowed his 8-year-old child to drive his car " | ||
"on an icy road and recorded the event. The child appeared to be handling the " | ||
"situation well, showcasing their driving skills despite the challenging conditions.", | ||
), | ||
] | ||
|
||
|
||
def _assert_dialogue_equal(actual, expected): | ||
assert len(actual) == len(expected) | ||
for i in range(len(actual)): | ||
assert actual[i].role == expected[i].role | ||
assert actual[i].content == expected[i].content | ||
|
||
|
||
class TestLlama2ChatFormat: | ||
expected_dialogue = [ | ||
Message( | ||
role="user", | ||
content="[INST] <<SYS>>\nYou are an AI assistant. User will you give you a task. " | ||
"Your goal is to complete the task as faithfully as you can. While performing " | ||
"the task think step-by-step and justify your steps.\n<</SYS>>\n\nPlease " | ||
"briefly summarize this news article:\n\nAOL.com Video - Father Lets 8-Year-Old " | ||
"Drive On Icy Road\n\nDescription:Would you let your 8-year-old drive your car? " | ||
"How about on an icy road? Well one father in Russia did just that, and recorded " | ||
"the entire thing. To her credit, the child seemed to be doing a great job. " | ||
"(0:44)\n\nTags: 8-year-old driver , caught on camera , child driver , pix11\n\n" | ||
"Summary: [/INST] ", | ||
), | ||
Message( | ||
role="assistant", | ||
content="A father in Russia allowed his 8-year-old child to drive his car on an " | ||
"icy road and recorded the event. The child appeared to be handling the situation well, " | ||
"showcasing their driving skills despite the challenging conditions.", | ||
), | ||
] | ||
|
||
def test_format(self): | ||
actual = Llama2ChatFormat.format(CHAT_SAMPLE) | ||
_assert_dialogue_equal(actual, self.expected_dialogue) | ||
|
||
|
||
class TestMistralChatFormat: | ||
expected_dialogue = [ | ||
Message( | ||
role="user", | ||
content="[INST] Please briefly summarize this news article:\n\nAOL.com Video - Father Lets 8-Year-Old " | ||
"Drive On Icy Road\n\nDescription:Would you let your 8-year-old drive your car? " | ||
"How about on an icy road? Well one father in Russia did just that, and recorded " | ||
"the entire thing. To her credit, the child seemed to be doing a great job. " | ||
"(0:44)\n\nTags: 8-year-old driver , caught on camera , child driver , pix11\n\n" | ||
"Summary: [/INST] ", | ||
), | ||
Message( | ||
role="assistant", | ||
content="A father in Russia allowed his 8-year-old child to drive his car on an " | ||
"icy road and recorded the event. The child appeared to be handling the situation well, " | ||
"showcasing their driving skills despite the challenging conditions.", | ||
), | ||
] | ||
|
||
def test_format(self): | ||
no_system_sample = CHAT_SAMPLE[1:] | ||
actual = MistralChatFormat.format(no_system_sample) | ||
_assert_dialogue_equal(actual, self.expected_dialogue) | ||
|
||
def test_format_with_system_prompt_raises(self): | ||
with pytest.raises( | ||
ValueError, match="System prompts are not supported in MistralChatFormat" | ||
): | ||
_ = MistralChatFormat.format(CHAT_SAMPLE) | ||
|
||
|
||
class TestChatMLFormat: | ||
expected_dialogue = [ | ||
Message( | ||
role="system", | ||
content="<|im_start|>system\nYou are an AI assistant. User will you give you a task. " | ||
"Your goal is to complete the task as faithfully as you can. While performing " | ||
"the task think step-by-step and justify your steps.<|im_end|>\n", | ||
), | ||
Message( | ||
role="user", | ||
content="<|im_start|>user\nPlease " | ||
"briefly summarize this news article:\n\nAOL.com Video - Father Lets 8-Year-Old " | ||
"Drive On Icy Road\n\nDescription:Would you let your 8-year-old drive your car? " | ||
"How about on an icy road? Well one father in Russia did just that, and recorded " | ||
"the entire thing. To her credit, the child seemed to be doing a great job. " | ||
"(0:44)\n\nTags: 8-year-old driver , caught on camera , child driver , pix11\n\n" | ||
"Summary:<|im_end|>\n", | ||
), | ||
Message( | ||
role="assistant", | ||
content="<|im_start|>assistant\nA father in Russia allowed his 8-year-old child to drive his car on an " | ||
"icy road and recorded the event. The child appeared to be handling the situation well, " | ||
"showcasing their driving skills despite the challenging conditions.<|im_end|>", | ||
), | ||
] | ||
|
||
def test_format(self): | ||
actual = ChatMLFormat.format(CHAT_SAMPLE) | ||
_assert_dialogue_equal(actual, self.expected_dialogue) |
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.