mirror of
https://github.com/ml-explore/mlx-examples.git
synced 2025-08-11 11:48:39 +08:00
add create_dataset
This commit is contained in:
parent
c33c245c11
commit
1beefd58a0
@ -140,6 +140,7 @@ class CompletionsDataset:
|
|||||||
|
|
||||||
|
|
||||||
def create_dataset(
|
def create_dataset(
|
||||||
|
args,
|
||||||
data,
|
data,
|
||||||
tokenizer: PreTrainedTokenizer,
|
tokenizer: PreTrainedTokenizer,
|
||||||
prompt_feature: Optional[str] = None,
|
prompt_feature: Optional[str] = None,
|
||||||
@ -149,23 +150,30 @@ def create_dataset(
|
|||||||
completion_feature = completion_feature or "completion"
|
completion_feature = completion_feature or "completion"
|
||||||
sample = data[0]
|
sample = data[0]
|
||||||
|
|
||||||
# Add DPO dataset support
|
if args.training_mode == "normal":
|
||||||
if "chosen" in sample and "rejected" in sample:
|
if "messages" in sample:
|
||||||
return ORPODataset(data, tokenizer)
|
return ChatDataset(data, tokenizer)
|
||||||
elif "messages" in sample:
|
elif prompt_feature in sample and completion_feature in sample:
|
||||||
return ChatDataset(data, tokenizer)
|
return CompletionsDataset(data, tokenizer, prompt_feature, completion_feature)
|
||||||
elif prompt_feature in sample and completion_feature in sample:
|
elif "text" in sample:
|
||||||
return CompletionsDataset(data, tokenizer, prompt_feature, completion_feature)
|
return Dataset(data, tokenizer)
|
||||||
elif "text" in sample:
|
else:
|
||||||
return Dataset(data, tokenizer)
|
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."
|
||||||
|
)
|
||||||
|
elif args.training_mode == "orpo":
|
||||||
|
if "chosen" in sample and "rejected" in sample:
|
||||||
|
return ORPODataset(data, tokenizer)
|
||||||
else:
|
else:
|
||||||
raise ValueError(
|
raise ValueError(
|
||||||
"Unsupported data format, check the supported formats here:\n"
|
"Unsupported training mode, check the supported training modes and their formats here:\n"
|
||||||
"https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/LORA.md#data."
|
"https://github.com/ml-explore/mlx-examples/blob/main/llms/mlx_lm/LORA.md#training-modes."
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def load_local_dataset(
|
def load_local_dataset(
|
||||||
|
args,
|
||||||
data_path: Path,
|
data_path: Path,
|
||||||
tokenizer: PreTrainedTokenizer,
|
tokenizer: PreTrainedTokenizer,
|
||||||
prompt_feature: Optional[str] = None,
|
prompt_feature: Optional[str] = None,
|
||||||
@ -176,7 +184,7 @@ def load_local_dataset(
|
|||||||
return []
|
return []
|
||||||
with open(path, "r") as fid:
|
with open(path, "r") as fid:
|
||||||
data = [json.loads(l) for l in fid]
|
data = [json.loads(l) for l in fid]
|
||||||
return create_dataset(data, tokenizer, prompt_feature, completion_feature)
|
return create_dataset(args, data, tokenizer, prompt_feature, completion_feature)
|
||||||
|
|
||||||
names = ("train", "valid", "test")
|
names = ("train", "valid", "test")
|
||||||
train, valid, test = [load_subset(data_path / f"{n}.jsonl") for n in names]
|
train, valid, test = [load_subset(data_path / f"{n}.jsonl") for n in names]
|
||||||
@ -184,6 +192,7 @@ def load_local_dataset(
|
|||||||
|
|
||||||
|
|
||||||
def load_hf_dataset(
|
def load_hf_dataset(
|
||||||
|
args,
|
||||||
data_id: str,
|
data_id: str,
|
||||||
tokenizer: PreTrainedTokenizer,
|
tokenizer: PreTrainedTokenizer,
|
||||||
prompt_feature: Optional[str] = None,
|
prompt_feature: Optional[str] = None,
|
||||||
@ -199,7 +208,7 @@ def load_hf_dataset(
|
|||||||
train, valid, test = [
|
train, valid, test = [
|
||||||
(
|
(
|
||||||
create_dataset(
|
create_dataset(
|
||||||
dataset[n], tokenizer, prompt_feature, completion_feature
|
args, dataset[n], tokenizer, prompt_feature, completion_feature
|
||||||
)
|
)
|
||||||
if n in dataset.keys()
|
if n in dataset.keys()
|
||||||
else []
|
else []
|
||||||
@ -264,12 +273,12 @@ def load_dataset(args, tokenizer: PreTrainedTokenizer):
|
|||||||
completion_feature = getattr(args, "completion_feature", None)
|
completion_feature = getattr(args, "completion_feature", None)
|
||||||
if data_path.exists():
|
if data_path.exists():
|
||||||
train, valid, test = load_local_dataset(
|
train, valid, test = load_local_dataset(
|
||||||
data_path, tokenizer, prompt_feature, completion_feature
|
args, data_path, tokenizer, prompt_feature, completion_feature
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
print(f"Loading Hugging Face dataset {args.data}.")
|
print(f"Loading Hugging Face dataset {args.data}.")
|
||||||
train, valid, test = load_hf_dataset(
|
train, valid, test = load_hf_dataset(
|
||||||
args.data, tokenizer, prompt_feature, completion_feature
|
args, args.data, tokenizer, prompt_feature, completion_feature
|
||||||
)
|
)
|
||||||
|
|
||||||
if args.train and len(train) == 0:
|
if args.train and len(train) == 0:
|
||||||
|
Loading…
Reference in New Issue
Block a user