forked from NVIDIA-NeMo/RL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtulu3.py
More file actions
72 lines (57 loc) · 2.57 KB
/
Copy pathtulu3.py
File metadata and controls
72 lines (57 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved.
#
# 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.
from typing import Any
from datasets import load_dataset
from nemo_rl.data.datasets.raw_dataset import RawDataset
class Tulu3SftMixtureDataset(RawDataset):
"""Simple wrapper around the Tulu3 SFT mixture dataset with train split.
Args:
split_validation_size: Size of the validation data, default is 0.05
seed: Seed for train/validation split when split_validation_size > 0, default is 42
max_samples: Optional maximum number of samples to use from the dataset
"""
def __init__(
self,
split_validation_size: float = 0.05,
seed: int = 42,
max_samples: int | None = None,
**kwargs,
) -> None:
print(
"WARNING: For reproducible experiments, preprocess the dataset once and define your own HfDataset subclass that directly uses the preprocessed datasets."
)
self.task_name = "tulu3_sft_mixture"
# load from huggingface
self.dataset = load_dataset("allenai/tulu-3-sft-mixture")["train"]
# Optionally limit the number of samples
if max_samples is not None and max_samples > 0:
self.dataset = self.dataset.shuffle(seed=seed).select(
range(min(max_samples, len(self.dataset)))
)
# format the dataset
self.dataset = self.dataset.map(
self.format_data,
remove_columns=["id", "source"],
)
# `self.val_dataset` is used (not None) only when current dataset is used for both training and validation
self.val_dataset = None
self.split_train_validation(split_validation_size, seed)
def format_data(self, data: dict[str, Any]) -> dict[str, Any]:
messages = data["messages"]
# Ensure last message is from assistant
if not messages or messages[-1]["role"] != "assistant":
raise ValueError(
f"Expected last message to be from assistant, got: {messages}"
)
return {"task_name": self.task_name}