-
Notifications
You must be signed in to change notification settings - Fork 316
Expand file tree
/
Copy pathhelpsteer3.py
More file actions
66 lines (56 loc) · 2.13 KB
/
helpsteer3.py
File metadata and controls
66 lines (56 loc) · 2.13 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
# 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.interfaces import TaskDataSpec
def to_preference_data_format(
data: dict[str, Any],
) -> dict[
str, list[dict[str, int | list[dict[str, str | Any]]]] | list[dict[str, str]]
]:
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
return {
"context": [{"role": "user", "content": data["context"]}]
if isinstance(data["context"], str)
else data["context"],
"completions": [
{"rank": 0, "completion": [{"role": "assistant", "content": chosen}]},
{"rank": 1, "completion": [{"role": "assistant", "content": rejected}]},
],
}
class HelpSteer3Dataset:
"""HelpSteer3 preference dataset for DPO training."""
def __init__(self) -> None:
ds = load_dataset("nvidia/HelpSteer3", "preference")
self.formatted_ds = ds.map(to_preference_data_format)
self.task_spec = TaskDataSpec(
task_name="HelpSteer3",
)