forked from NVIDIA-NeMo/RL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpsteer3.py
More file actions
73 lines (61 loc) · 2.46 KB
/
helpsteer3.py
File metadata and controls
73 lines (61 loc) · 2.46 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
73
# 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 absl import logging
from datasets import load_dataset
from nemo_rl.data.datasets.raw_dataset import RawDataset
class HelpSteer3Dataset(RawDataset):
"""HelpSteer3 preference dataset for DPO training.
Args:
split: Split name for the dataset, default is "train"
"""
def __init__(self, split: str = "train", **kwargs):
self.task_name = "HelpSteer3"
# load from huggingface
self.dataset = load_dataset("nvidia/HelpSteer3", "preference")[split]
# format the dataset
self.dataset = self.dataset.map(
self.format_data,
remove_columns=self.dataset.column_names,
)
def format_data(self, data: dict[str, Any]) -> dict[str, Any]:
response_1 = data["response1"]
response_2 = data["response2"]
overall_preference = data["overall_preference"]
if overall_preference < 0:
chosen = response_1
rejected = response_2
elif overall_preference == 0:
logging.log_every_n(
logging.WARNING,
"Preference is 0 for some examples! Setting chosen and rejected to response 1 since we don't know which response is better",
1000,
)
chosen = response_1
rejected = response_1
else:
chosen = response_2
rejected = response_1
if isinstance(data["context"], str):
context = [{"role": "user", "content": data["context"]}]
else:
context = data["context"]
return {
"context": context,
"completions": [
{"rank": 0, "completion": [{"role": "assistant", "content": chosen}]},
{"rank": 1, "completion": [{"role": "assistant", "content": rejected}]},
],
"task_name": self.task_name,
}