mirror of
https://github.com/wassname/alignment-handbook.git
synced 2026-08-12 11:50:32 +08:00
Add model utils tests
This commit is contained in:
+70
-1
@@ -15,8 +15,9 @@
|
||||
import unittest
|
||||
|
||||
import pytest
|
||||
from datasets import Dataset
|
||||
|
||||
from alignment import DataArguments, get_datasets
|
||||
from alignment import DataArguments, ModelArguments, apply_chat_template, get_datasets, get_tokenizer
|
||||
|
||||
|
||||
class GetDatasetsTest(unittest.TestCase):
|
||||
@@ -77,3 +78,71 @@ class GetDatasetsTest(unittest.TestCase):
|
||||
datasets = get_datasets(dataset_mixer, splits=["test"])
|
||||
self.assertEqual(len(datasets["test"]), 100)
|
||||
self.assertRaises(KeyError, lambda: datasets["train"])
|
||||
|
||||
|
||||
class ApplyChatTemplateTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
model_args = ModelArguments(model_name_or_path="HuggingFaceH4/zephyr-7b-alpha")
|
||||
data_args = DataArguments()
|
||||
self.tokenizer = get_tokenizer(model_args, data_args)
|
||||
self.dataset = Dataset.from_dict(
|
||||
{
|
||||
"prompt": ["Hello!"],
|
||||
"messages": [[{"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Bonjour!"}]],
|
||||
"chosen": [[{"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Bonjour!"}]],
|
||||
"rejected": [[{"role": "user", "content": "Hello!"}, {"role": "assistant", "content": "Hola!"}]],
|
||||
}
|
||||
)
|
||||
|
||||
def test_sft(self):
|
||||
dataset = self.dataset.map(
|
||||
apply_chat_template,
|
||||
fn_kwargs={"tokenizer": self.tokenizer, "task": "sft"},
|
||||
remove_columns=self.dataset.column_names,
|
||||
)
|
||||
self.assertDictEqual(
|
||||
dataset[0],
|
||||
{"text": "<|system|>\n</s>\n<|user|>\nHello!</s>\n<|assistant|>\nBonjour!</s>\n"},
|
||||
)
|
||||
|
||||
def test_generation(self):
|
||||
# Remove last turn from messages
|
||||
dataset = self.dataset.map(lambda x: {"messages": x["messages"][:-1]})
|
||||
dataset = dataset.map(
|
||||
apply_chat_template,
|
||||
fn_kwargs={"tokenizer": self.tokenizer, "task": "generation"},
|
||||
remove_columns=self.dataset.column_names,
|
||||
)
|
||||
self.assertDictEqual(
|
||||
dataset[0],
|
||||
{"text": "<|system|>\n</s>\n<|user|>\nHello!</s>\n<|assistant|>\n"},
|
||||
)
|
||||
|
||||
def test_rm(self):
|
||||
dataset = self.dataset.map(
|
||||
apply_chat_template,
|
||||
fn_kwargs={"tokenizer": self.tokenizer, "task": "rm"},
|
||||
remove_columns=self.dataset.column_names,
|
||||
)
|
||||
self.assertDictEqual(
|
||||
dataset[0],
|
||||
{
|
||||
"text_chosen": "<|system|>\n</s>\n<|user|>\nHello!</s>\n<|assistant|>\nBonjour!</s>\n",
|
||||
"text_rejected": "<|system|>\n</s>\n<|user|>\nHello!</s>\n<|assistant|>\nHola!</s>\n",
|
||||
},
|
||||
)
|
||||
|
||||
def test_dpo(self):
|
||||
dataset = self.dataset.map(
|
||||
apply_chat_template,
|
||||
fn_kwargs={"tokenizer": self.tokenizer, "task": "dpo"},
|
||||
remove_columns=self.dataset.column_names,
|
||||
)
|
||||
self.assertDictEqual(
|
||||
dataset[0],
|
||||
{
|
||||
"text_prompt": "<|system|>\n</s>\n<|user|>\nHello!</s>\n<|assistant|>\n",
|
||||
"text_chosen": "Bonjour!</s>\n",
|
||||
"text_rejected": "Hola!</s>\n",
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user