Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Orca chat Dataloader #3583

Merged
merged 5 commits into from
Jul 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion model/model_training/custom_datasets/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from model_training.custom_datasets.instruction import INSTRUCTION_DATASETS, InstructionDataset
from model_training.custom_datasets.oasst_dataset import load_oasst_export
from model_training.custom_datasets.pretrain_datasets import RedPajama
from model_training.custom_datasets.prompt_dialogue import Gpt4All, load_oig_file
from model_training.custom_datasets.prompt_dialogue import Gpt4All, OrcaChat, load_oig_file
from model_training.custom_datasets.qa_datasets import (
SODA,
AlpacaGpt4,
Expand Down Expand Up @@ -172,6 +172,8 @@ def get_one_dataset(
dataset = RedPajama(cache_dir=data_path, mode=mode, **kwargs)
elif dataset_name == "gpteacher_roleplay":
dataset = GPTeacher_Roleplay(cache_dir=data_path, mode=mode, **kwargs)
elif dataset_name == "orca-chat":
dataset = OrcaChat(cache_dir=data_path, **kwargs)
else:
raise ValueError(f"Unknown dataset {dataset_name}")

Expand Down
11 changes: 10 additions & 1 deletion model/model_training/custom_datasets/formatting.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ def system_tag(
else:
fragments.append(f"{k}: {v}")

if len(fragments) == 0:
return ""

content = "\n".join(fragments)
return f"{QA_SPECIAL_TOKENS['System']}{content}\n{eos_token}"

Expand All @@ -109,6 +112,7 @@ class DatasetEntrySft(DatasetEntry):
"""Supervised fine-tuning conversation dataset entry"""

conversation: list[Utterance]
system_message: Optional[str]

def get_formatted(
self,
Expand All @@ -131,7 +135,12 @@ def get_formatted(
)
else:
system_tag = ""
output.append(f"{QA_SPECIAL_TOKENS['Question']}{m.text}{eos_token}{system_tag}")
if i == 0 and self.system_message:
output.append(
f"{QA_SPECIAL_TOKENS['System']}{self.system_message}{eos_token}{QA_SPECIAL_TOKENS['Question']}{m.text}{eos_token}{system_tag}"
)
else:
output.append(f"{QA_SPECIAL_TOKENS['Question']}{m.text}{eos_token}{system_tag}")
else:
output.append(f"{QA_SPECIAL_TOKENS['Answer']}{m.text}{eos_token}")

Expand Down
29 changes: 28 additions & 1 deletion model/model_training/custom_datasets/prompt_dialogue.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
import json
import re
from pathlib import Path
from typing import Optional
from typing import List, Optional, Union

import requests
from datasets import load_dataset
from model_training.custom_datasets.formatting import DatasetEntrySft, Role, Utterance
from model_training.custom_datasets.oasst_dataset import ListDataset
from model_training.custom_datasets.utils import _filter_by_words
from torch import Generator, randperm
Expand Down Expand Up @@ -166,3 +167,29 @@ def __getitem__(self, index: int) -> list[str] | tuple[str]:
return dialogue
elif self.mode == "rl":
return tuple(dialogue[:-1])


class OrcaChat(Dataset):
name = "orca-chat"

def __init__(self, data_files: Union[List[str], str] = "orca-chat-gpt4.json", cache_dir: str = None) -> None:
self.dataset = load_dataset("shahules786/orca-chat", split="train", data_files=data_files, cache_dir=cache_dir)

def __len__(self):
return len(self.dataset)

def __getitem__(self, idx):
conversation, instruction = [self.dataset[idx][key] for key in ("conversation", "instruction")]
conversation = [(item["input"], item["output"]) for item in conversation]
conversation = list(sum(conversation, ()))
conv_utt: list[Utterance] = [
(
Utterance(
text=conv,
role=Role.prompter if i % 2 == 0 else Role.assistant,
)
)
for i, conv in enumerate(conversation)
]

return DatasetEntrySft(conversation=conv_utt, system_message=instruction)