LLaMA-Factory/tests/data/processors/test_unsupervised.py

59 lines
2.1 KiB
Python
Raw Normal View History

2024-06-15 09:54:33 +00:00
# Copyright 2024 the LlamaFactory team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
2024-06-07 21:20:54 +00:00
import os
2024-06-10 13:24:15 +00:00
import random
2024-06-07 21:20:54 +00:00
import pytest
from datasets import load_dataset
2024-06-19 12:03:50 +00:00
from transformers import AutoTokenizer
2024-06-07 21:20:54 +00:00
2024-07-18 17:06:27 +00:00
from llamafactory.train.test_utils import load_train_dataset
2024-06-07 21:20:54 +00:00
2024-07-18 17:06:27 +00:00
DEMO_DATA = os.environ.get("DEMO_DATA", "llamafactory/demo_data")
2024-06-10 13:24:15 +00:00
TINY_LLAMA = os.environ.get("TINY_LLAMA", "llamafactory/tiny-random-Llama-3")
2024-06-07 21:20:54 +00:00
2024-07-18 17:06:27 +00:00
TINY_DATA = os.environ.get("TINY_DATA", "llamafactory/tiny-supervised-dataset")
2024-06-10 13:24:15 +00:00
TRAIN_ARGS = {
2024-06-07 21:20:54 +00:00
"model_name_or_path": TINY_LLAMA,
"stage": "sft",
2024-07-18 17:06:27 +00:00
"do_predict": True,
2024-06-07 21:20:54 +00:00
"finetuning_type": "full",
"template": "llama3",
2024-06-10 13:24:15 +00:00
"cutoff_len": 8192,
2024-06-07 21:20:54 +00:00
"overwrite_cache": True,
"output_dir": "dummy_dir",
"overwrite_output_dir": True,
2024-07-18 17:06:27 +00:00
"predict_with_generate": True,
2024-06-07 21:20:54 +00:00
"fp16": True,
}
2024-06-15 11:51:20 +00:00
@pytest.mark.parametrize("num_samples", [16])
2024-07-18 17:06:27 +00:00
def test_unsupervised_data(num_samples: int):
train_dataset = load_train_dataset(dataset_dir="REMOTE:" + DEMO_DATA, dataset="system_chat", **TRAIN_ARGS)
2024-06-19 12:03:50 +00:00
ref_tokenizer = AutoTokenizer.from_pretrained(TINY_LLAMA)
2024-07-18 17:06:27 +00:00
original_data = load_dataset(DEMO_DATA, name="system_chat", split="train")
2024-06-10 13:24:15 +00:00
indexes = random.choices(range(len(original_data)), k=num_samples)
for index in indexes:
2024-07-18 17:06:27 +00:00
messages = original_data["messages"][index]
ref_ids = ref_tokenizer.apply_chat_template(messages)
ref_input_ids = ref_tokenizer.apply_chat_template(messages[:-1], add_generation_prompt=True)
ref_labels = ref_ids[len(ref_input_ids) :]
assert train_dataset["input_ids"][index] == ref_input_ids
assert train_dataset["labels"][index] == ref_labels