2025-02-10 12:12:34 +08:00
|
|
|
import itertools
|
2024-03-20 07:45:46 +08:00
|
|
|
import json
|
2025-02-10 12:12:34 +08:00
|
|
|
import types
|
2024-03-20 07:45:46 +08:00
|
|
|
from pathlib import Path
|
2025-03-01 19:42:39 +08:00
|
|
|
from typing import Any, Dict, List, Union
|
2025-02-22 04:12:45 +08:00
|
|
|
from transformers import PreTrainedTokenizer
|
|
|
|
|
|
|
|
|
2025-01-24 23:09:22 +08:00
|
|
|
class ORPODataset:
|
2025-01-24 23:57:18 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
2025-02-22 04:12:45 +08:00
|
|
|
data: List[Dict[str, Union[str, Dict, List]]],
|
2025-01-24 23:57:18 +08:00
|
|
|
tokenizer: PreTrainedTokenizer,
|
|
|
|
prompt_key: str = "prompt",
|
|
|
|
chosen_key: str = "chosen",
|
|
|
|
rejected_key: str = "rejected",
|
|
|
|
preference_score_key: str = "preference_score",
|
|
|
|
system_key: str = None
|
|
|
|
):
|
2025-01-19 20:45:33 +08:00
|
|
|
self._chosen_data = []
|
|
|
|
self._rejected_data = []
|
|
|
|
self._scores = []
|
2025-01-24 23:57:18 +08:00
|
|
|
|
2025-01-19 20:45:33 +08:00
|
|
|
for d in data:
|
2025-02-22 04:12:45 +08:00
|
|
|
# Get prompt content, preferring 'prompt' over 'question'
|
|
|
|
prompt_content = d.get(prompt_key, d.get("question", ""))
|
|
|
|
|
2025-01-24 23:57:18 +08:00
|
|
|
if system_key and system_key in d:
|
|
|
|
base_messages = [{"role": "system", "content": d[system_key]}]
|
2025-02-22 04:12:45 +08:00
|
|
|
chosen_messages = base_messages + [{"role": "user", "content": prompt_content}]
|
|
|
|
rejected_messages = base_messages + [{"role": "user", "content": prompt_content}]
|
|
|
|
|
|
|
|
# Handle chosen messages
|
2025-01-24 23:57:18 +08:00
|
|
|
if isinstance(d[chosen_key], str):
|
|
|
|
chosen_messages.append({"role": "assistant", "content": d[chosen_key]})
|
2025-02-22 04:12:45 +08:00
|
|
|
elif isinstance(d[chosen_key], dict):
|
|
|
|
if "messages" in d[chosen_key]:
|
|
|
|
chosen_messages.extend(d[chosen_key]["messages"])
|
|
|
|
else:
|
|
|
|
chosen_messages.append({"role": "assistant", "content": d[chosen_key].get("content", "")})
|
|
|
|
elif isinstance(d[chosen_key], list):
|
|
|
|
chosen_messages.extend(d[chosen_key])
|
|
|
|
|
|
|
|
# Handle rejected messages
|
2025-01-24 23:57:18 +08:00
|
|
|
if isinstance(d[rejected_key], str):
|
|
|
|
rejected_messages.append({"role": "assistant", "content": d[rejected_key]})
|
2025-02-22 04:12:45 +08:00
|
|
|
elif isinstance(d[rejected_key], dict):
|
|
|
|
if "messages" in d[rejected_key]:
|
|
|
|
rejected_messages.extend(d[rejected_key]["messages"])
|
|
|
|
else:
|
|
|
|
rejected_messages.append({"role": "assistant", "content": d[rejected_key].get("content", "")})
|
|
|
|
elif isinstance(d[rejected_key], list):
|
|
|
|
rejected_messages.extend(d[rejected_key])
|
|
|
|
|
2025-01-24 23:57:18 +08:00
|
|
|
chosen_text = tokenizer.apply_chat_template(chosen_messages)
|
|
|
|
rejected_text = tokenizer.apply_chat_template(rejected_messages)
|
2025-02-22 04:12:45 +08:00
|
|
|
|
2025-01-24 23:57:18 +08:00
|
|
|
else:
|
2025-02-22 04:12:45 +08:00
|
|
|
# Handle non-system message cases
|
|
|
|
chosen_content = self._extract_content(d[chosen_key])
|
|
|
|
rejected_content = self._extract_content(d[rejected_key])
|
|
|
|
|
2025-01-24 23:57:18 +08:00
|
|
|
chosen_text = tokenizer.apply_chat_template([
|
2025-02-22 04:12:45 +08:00
|
|
|
{"role": "user", "content": prompt_content},
|
|
|
|
{"role": "assistant", "content": chosen_content},
|
2025-01-24 23:57:18 +08:00
|
|
|
])
|
|
|
|
rejected_text = tokenizer.apply_chat_template([
|
2025-02-22 04:12:45 +08:00
|
|
|
{"role": "user", "content": prompt_content},
|
|
|
|
{"role": "assistant", "content": rejected_content},
|
2025-01-24 23:57:18 +08:00
|
|
|
])
|
|
|
|
|
2025-01-19 20:45:33 +08:00
|
|
|
self._chosen_data.append(chosen_text)
|
|
|
|
self._rejected_data.append(rejected_text)
|
2025-01-24 23:57:18 +08:00
|
|
|
|
2025-01-24 23:09:22 +08:00
|
|
|
if preference_score_key in d:
|
|
|
|
self._scores.append(float(d[preference_score_key]))
|
2025-01-19 20:45:33 +08:00
|
|
|
else:
|
|
|
|
self._scores.append(1.0)
|
2025-02-22 04:12:45 +08:00
|
|
|
|
|
|
|
def _extract_content(self, data):
|
|
|
|
"""Helper method to extract content from various data formats."""
|
|
|
|
if isinstance(data, str):
|
|
|
|
return data
|
|
|
|
elif isinstance(data, dict):
|
|
|
|
if "messages" in data:
|
|
|
|
last_message = data["messages"][-1]
|
|
|
|
return last_message.get("content", last_message.get("messages", ""))
|
|
|
|
return data.get("content", "")
|
|
|
|
elif isinstance(data, list):
|
|
|
|
last_message = data[-1]
|
|
|
|
if isinstance(last_message, dict):
|
|
|
|
if "content" in last_message:
|
|
|
|
return last_message["content"]
|
|
|
|
elif "messages" in last_message:
|
|
|
|
return last_message["messages"]
|
|
|
|
return last_message if isinstance(last_message, str) else ""
|
|
|
|
return ""
|
2025-01-24 23:57:18 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._chosen_data)
|
|
|
|
|
|
|
|
def __getitem__(self, idx: int):
|
|
|
|
return {
|
|
|
|
"chosen": self._chosen_data[idx],
|
|
|
|
"rejected": self._rejected_data[idx],
|
|
|
|
"preference_score": self._scores[idx]
|
|
|
|
}
|
2025-01-19 07:19:36 +08:00
|
|
|
|
|
|
|
|
2024-03-20 07:45:46 +08:00
|
|
|
class Dataset:
|
|
|
|
"""
|
2024-06-27 01:20:50 +08:00
|
|
|
Light-weight wrapper to hold a dataset.
|
2024-03-20 07:45:46 +08:00
|
|
|
"""
|
|
|
|
|
2025-01-04 02:50:59 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: List[Dict[str, str]],
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
|
|
|
text_key: str = "text",
|
|
|
|
):
|
|
|
|
self._data = [tokenizer.encode(d[text_key]) for d in data]
|
|
|
|
for d in self._data:
|
|
|
|
if d[-1] != tokenizer.eos_token_id:
|
|
|
|
d.append(tokenizer.eos_token_id)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
def __getitem__(self, idx: int):
|
2025-01-04 02:50:59 +08:00
|
|
|
return self._data[idx]
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._data)
|
|
|
|
|
|
|
|
|
2025-01-04 02:50:59 +08:00
|
|
|
class ChatDataset:
|
2024-03-20 07:45:46 +08:00
|
|
|
"""
|
|
|
|
A dataset for chat data in the format of {"messages": [...]}
|
|
|
|
https://platform.openai.com/docs/guides/fine-tuning/example-format
|
|
|
|
"""
|
|
|
|
|
2025-02-10 12:12:34 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: List[Dict[str, str]],
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
|
|
|
chat_key: str = "messages",
|
|
|
|
mask_prompt: bool = False,
|
|
|
|
):
|
|
|
|
self._data = []
|
|
|
|
for d in data:
|
|
|
|
messages = d[chat_key]
|
|
|
|
tools = d.get("tools", None)
|
|
|
|
tokens = tokenizer.apply_chat_template(messages, tools=tools)
|
|
|
|
if mask_prompt:
|
|
|
|
messages = messages[:-1]
|
|
|
|
offset = len(tokenizer.apply_chat_template(messages, tools=tools))
|
|
|
|
self._data.append((tokens, offset))
|
|
|
|
else:
|
|
|
|
self._data.append(tokens)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
def __getitem__(self, idx: int):
|
2025-01-04 02:50:59 +08:00
|
|
|
return self._data[idx]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._data)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
|
2025-01-04 02:50:59 +08:00
|
|
|
class CompletionsDataset:
|
2024-03-20 07:45:46 +08:00
|
|
|
"""
|
|
|
|
A dataset for prompt-completion data in the format of {"prompt": ..., "completion": ...}
|
2024-06-27 01:20:50 +08:00
|
|
|
or using user-provided keys for prompt and completion values
|
2024-03-20 07:45:46 +08:00
|
|
|
https://platform.openai.com/docs/guides/fine-tuning/example-format
|
|
|
|
"""
|
|
|
|
|
2024-06-27 01:20:50 +08:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
data: List[Dict[str, str]],
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
2025-01-14 02:01:18 +08:00
|
|
|
prompt_key: str,
|
|
|
|
completion_key: str,
|
2025-02-10 12:12:34 +08:00
|
|
|
mask_prompt: bool,
|
2024-06-27 01:20:50 +08:00
|
|
|
):
|
2025-02-10 12:12:34 +08:00
|
|
|
self._data = []
|
|
|
|
for d in data:
|
|
|
|
tokens = tokenizer.apply_chat_template(
|
2025-01-04 02:50:59 +08:00
|
|
|
[
|
|
|
|
{"role": "user", "content": d[prompt_key]},
|
|
|
|
{"role": "assistant", "content": d[completion_key]},
|
|
|
|
],
|
|
|
|
)
|
2025-02-10 12:12:34 +08:00
|
|
|
if mask_prompt:
|
|
|
|
offset = len(
|
|
|
|
tokenizer.apply_chat_template(
|
|
|
|
[{"role": "user", "content": d[prompt_key]}]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
self._data.append((tokens, offset))
|
|
|
|
else:
|
|
|
|
self._data.append(tokens)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
def __getitem__(self, idx: int):
|
2025-01-04 02:50:59 +08:00
|
|
|
return self._data[idx]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._data)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
|
2025-02-10 12:12:34 +08:00
|
|
|
class ConcatenatedDataset:
|
|
|
|
def __init__(self, data: List[Any]):
|
|
|
|
self._data = list(itertools.chain(*data))
|
2024-03-20 07:45:46 +08:00
|
|
|
|
|
|
|
def __getitem__(self, idx: int):
|
2025-01-04 02:50:59 +08:00
|
|
|
return self._data[idx]
|
|
|
|
|
|
|
|
def __len__(self):
|
|
|
|
return len(self._data)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
2025-01-14 02:01:18 +08:00
|
|
|
def create_dataset(
|
2025-02-04 18:06:57 +08:00
|
|
|
args,
|
2025-01-14 02:01:18 +08:00
|
|
|
data,
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
2025-02-10 12:12:34 +08:00
|
|
|
config,
|
2025-01-14 02:01:18 +08:00
|
|
|
):
|
2025-02-10 12:12:34 +08:00
|
|
|
mask_prompt = getattr(config, "mask_prompt", False)
|
|
|
|
prompt_feature = getattr(config, "prompt_feature", "prompt")
|
|
|
|
text_feature = getattr(config, "text_feature", "text")
|
|
|
|
completion_feature = getattr(config, "completion_feature", "completion")
|
|
|
|
chat_feature = getattr(config, "chat_feature", "messages")
|
2024-09-30 22:36:21 +08:00
|
|
|
sample = data[0]
|
2025-02-10 17:51:01 +08:00
|
|
|
|
2025-02-04 18:06:57 +08:00
|
|
|
if args.training_mode == "normal":
|
2025-02-10 17:51:01 +08:00
|
|
|
if chat_feature in sample:
|
|
|
|
return ChatDataset(data, tokenizer, chat_key=chat_feature, mask_prompt=mask_prompt)
|
2025-02-04 18:06:57 +08:00
|
|
|
elif prompt_feature in sample and completion_feature in sample:
|
2025-02-10 17:51:01 +08:00
|
|
|
return CompletionsDataset(data, tokenizer, prompt_feature, completion_feature, mask_prompt)
|
|
|
|
elif text_feature in sample:
|
|
|
|
if mask_prompt:
|
|
|
|
raise ValueError("Prompt masking not supported for text dataset.")
|
|
|
|
return Dataset(data, tokenizer, text_key=text_feature)
|
2025-02-04 18:06:57 +08:00
|
|
|
else:
|
|
|
|
raise ValueError(
|
|
|
|
"Unsupported data format, check the supported formats here:\n"
|
|
|
|
"https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/LORA.md#data."
|
|
|
|
)
|
2024-03-20 07:45:46 +08:00
|
|
|
else:
|
2025-02-04 18:06:57 +08:00
|
|
|
if "chosen" in sample and "rejected" in sample:
|
|
|
|
return ORPODataset(data, tokenizer)
|
2024-03-20 07:45:46 +08:00
|
|
|
|
2025-01-14 02:01:18 +08:00
|
|
|
def load_local_dataset(
|
2025-02-04 18:06:57 +08:00
|
|
|
args,
|
2025-01-14 02:01:18 +08:00
|
|
|
data_path: Path,
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
2025-02-10 12:12:34 +08:00
|
|
|
config,
|
2025-01-14 02:01:18 +08:00
|
|
|
):
|
2024-09-30 22:36:21 +08:00
|
|
|
def load_subset(path):
|
|
|
|
if not path.exists():
|
|
|
|
return []
|
|
|
|
with open(path, "r") as fid:
|
|
|
|
data = [json.loads(l) for l in fid]
|
2025-02-10 17:51:01 +08:00
|
|
|
|
2025-02-22 04:12:45 +08:00
|
|
|
return create_dataset(args, data, tokenizer, config)
|
2024-09-30 22:36:21 +08:00
|
|
|
|
|
|
|
names = ("train", "valid", "test")
|
|
|
|
train, valid, test = [load_subset(data_path / f"{n}.jsonl") for n in names]
|
|
|
|
return train, valid, test
|
|
|
|
|
|
|
|
|
2025-01-14 02:01:18 +08:00
|
|
|
def load_hf_dataset(
|
2025-02-04 18:06:57 +08:00
|
|
|
args,
|
2025-01-14 02:01:18 +08:00
|
|
|
data_id: str,
|
|
|
|
tokenizer: PreTrainedTokenizer,
|
2025-02-10 12:12:34 +08:00
|
|
|
config,
|
2025-01-14 02:01:18 +08:00
|
|
|
):
|
2024-09-30 22:36:21 +08:00
|
|
|
from datasets import exceptions, load_dataset
|
|
|
|
|
|
|
|
try:
|
|
|
|
dataset = load_dataset(data_id)
|
2024-06-27 01:20:50 +08:00
|
|
|
|
|
|
|
names = ("train", "valid", "test")
|
|
|
|
|
|
|
|
train, valid, test = [
|
2025-01-14 02:01:18 +08:00
|
|
|
(
|
2025-02-10 17:51:01 +08:00
|
|
|
create_dataset(args, dataset[n], tokenizer, config)
|
2025-01-14 02:01:18 +08:00
|
|
|
if n in dataset.keys()
|
|
|
|
else []
|
|
|
|
)
|
2024-09-30 22:36:21 +08:00
|
|
|
for n in names
|
2024-06-27 01:20:50 +08:00
|
|
|
]
|
2024-09-30 22:36:21 +08:00
|
|
|
|
|
|
|
except exceptions.DatasetNotFoundError:
|
|
|
|
raise ValueError(f"Not found Hugging Face dataset: {data_id} .")
|
|
|
|
|
|
|
|
return train, valid, test
|
|
|
|
|
|
|
|
|
|
|
|
def load_custom_hf_dataset(args, tokenizer: PreTrainedTokenizer):
|
|
|
|
import datasets
|
|
|
|
|
2025-02-10 12:12:34 +08:00
|
|
|
def create_hf_dataset(dataset_name, config, split, hf_config):
|
2024-09-30 22:36:21 +08:00
|
|
|
ds = datasets.load_dataset(
|
|
|
|
dataset_name,
|
|
|
|
split=split,
|
2025-02-10 12:12:34 +08:00
|
|
|
**hf_config,
|
2024-09-30 22:36:21 +08:00
|
|
|
)
|
2025-02-10 12:12:34 +08:00
|
|
|
return create_dataset(ds, tokenizer, config)
|
|
|
|
|
|
|
|
dataset_collection = args.hf_dataset
|
|
|
|
if isinstance(dataset_collection, dict):
|
|
|
|
dataset_collection = [dataset_collection]
|
|
|
|
|
|
|
|
collection = []
|
|
|
|
for ds in dataset_collection:
|
|
|
|
ds_name = ds["name"]
|
|
|
|
print(f"Loading Hugging Face dataset {ds_name}.")
|
|
|
|
ds["mask_prompt"] = getattr(args, "mask_prompt", False)
|
|
|
|
config = types.SimpleNamespace(**ds)
|
|
|
|
hf_config = ds.get("config", {})
|
|
|
|
if args.train:
|
|
|
|
train_split = ds.get("train_split", "train[:80%]")
|
|
|
|
valid_split = ds.get("valid_split", "train[-10%:]")
|
|
|
|
train = create_hf_dataset(
|
|
|
|
ds_name,
|
|
|
|
config,
|
|
|
|
train_split,
|
|
|
|
hf_config,
|
|
|
|
)
|
|
|
|
valid = create_hf_dataset(
|
|
|
|
ds_name,
|
|
|
|
config,
|
|
|
|
valid_split,
|
|
|
|
hf_config,
|
|
|
|
)
|
2024-09-30 22:36:21 +08:00
|
|
|
else:
|
2025-02-10 12:12:34 +08:00
|
|
|
train, valid = [], []
|
|
|
|
|
|
|
|
if args.test:
|
|
|
|
test_split = ds.get("test_split")
|
|
|
|
test = create_hf_dataset(
|
|
|
|
ds_name,
|
|
|
|
config,
|
|
|
|
test_split,
|
|
|
|
hf_config,
|
2024-09-30 22:36:21 +08:00
|
|
|
)
|
2025-02-10 12:12:34 +08:00
|
|
|
else:
|
|
|
|
test = []
|
2024-09-30 22:36:21 +08:00
|
|
|
|
2025-02-10 12:12:34 +08:00
|
|
|
collection.append((train, valid, test))
|
2024-09-30 22:36:21 +08:00
|
|
|
|
2025-02-10 12:12:34 +08:00
|
|
|
if len(collection) == 1:
|
|
|
|
return collection[0]
|
|
|
|
|
|
|
|
# Otherwise concatenate them
|
|
|
|
return tuple(map(ConcatenatedDataset, zip(*collection)))
|
2024-09-30 22:36:21 +08:00
|
|
|
|
|
|
|
|
|
|
|
def load_dataset(args, tokenizer: PreTrainedTokenizer):
|
2025-01-04 02:50:59 +08:00
|
|
|
if getattr(args, "hf_dataset", False):
|
2024-09-30 22:36:21 +08:00
|
|
|
train, valid, test = load_custom_hf_dataset(args, tokenizer)
|
|
|
|
else:
|
|
|
|
data_path = Path(args.data)
|
|
|
|
if data_path.exists():
|
2025-02-10 17:51:01 +08:00
|
|
|
train, valid, test = load_local_dataset(args, data_path, tokenizer, args)
|
2024-09-30 22:36:21 +08:00
|
|
|
else:
|
|
|
|
print(f"Loading Hugging Face dataset {args.data}.")
|
2025-03-01 19:42:39 +08:00
|
|
|
train, valid, test = load_hf_dataset(args, args.data, tokenizer, args)
|
2024-09-30 22:36:21 +08:00
|
|
|
|
2024-03-20 07:45:46 +08:00
|
|
|
if args.train and len(train) == 0:
|
|
|
|
raise ValueError(
|
|
|
|
"Training set not found or empty. Must provide training set for fine-tuning."
|
|
|
|
)
|
|
|
|
if args.train and len(valid) == 0:
|
|
|
|
raise ValueError(
|
|
|
|
"Validation set not found or empty. Must provide validation set for fine-tuning."
|
|
|
|
)
|
|
|
|
if args.test and len(test) == 0:
|
|
|
|
raise ValueError(
|
|
|
|
"Test set not found or empty. Must provide test set for evaluation."
|
|
|
|
)
|
|
|
|
return train, valid, test
|