|
| 1 | +# Copyright (c) 2025, NVIDIA CORPORATION. All rights reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +import json |
| 15 | +from pathlib import Path |
| 16 | +from datasets import load_dataset, concatenate_datasets |
| 17 | +import numpy as np |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +if __name__ == "__main__": |
| 22 | + |
| 23 | + dataset = load_dataset("allenai/reward-bench-2", split='test') |
| 24 | + # select some samples from Ties |
| 25 | + #dataset = dataset.filter(lambda x: x["subset"] == "Ties") |
| 26 | + |
| 27 | + # select some samples from Ties and NonTies |
| 28 | + #dataset = concatenate_datasets([dataset.filter(lambda x: x["subset"] == "Ties").select(range(30)), |
| 29 | + # dataset.filter(lambda x: x["subset"] != "Ties").select(range(10))]) |
| 30 | + print(f"Prepared dataset with {len(dataset)} samples") |
| 31 | + |
| 32 | + # dumping the data as test.jsonl, note that the shuffling logic is not ideal, but it matches the one in reward-bench-2 |
| 33 | + np.random.seed(42) |
| 34 | + output_path = Path(__file__).parent / "preference" / "test.jsonl" |
| 35 | + ties_path = Path(__file__).parent / "ties" / "test.jsonl" |
| 36 | + ratings_path = Path(__file__).parent / "ratings" / "test.jsonl" |
| 37 | + with open(output_path, "w") as test, open(ties_path, "w") as ties, open(ratings_path, "w") as ratings: |
| 38 | + for sample in dataset: |
| 39 | + for answer in sample["chosen"]: |
| 40 | + prepared = { |
| 41 | + "id": sample["id"], |
| 42 | + "subset": sample["subset"], |
| 43 | + "question": sample["prompt"], |
| 44 | + "answer": answer, |
| 45 | + "chosen": 1, |
| 46 | + "num_correct": sample["num_correct"], |
| 47 | + "num_incorrect": sample["num_incorrect"], |
| 48 | + } |
| 49 | + ratings.write(json.dumps(prepared) + "\n") |
| 50 | + if sample["subset"] == "Ties": |
| 51 | + ties.write(json.dumps(prepared) + "\n") |
| 52 | + |
| 53 | + for answer in sample["rejected"]: |
| 54 | + prepared = { |
| 55 | + "id": sample["id"], |
| 56 | + "subset": sample["subset"], |
| 57 | + "question": sample["prompt"], |
| 58 | + "answer": answer, |
| 59 | + "chosen": 0, |
| 60 | + "num_correct": sample["num_correct"], |
| 61 | + "num_incorrect": sample["num_incorrect"], |
| 62 | + } |
| 63 | + ratings.write(json.dumps(prepared) + "\n") |
| 64 | + if sample["subset"] == "Ties": |
| 65 | + ties.write(json.dumps(prepared) + "\n") |
| 66 | + |
| 67 | + if sample["subset"] != "Ties": |
| 68 | + assert len(sample["chosen"]) == 1 |
| 69 | + answer_a = sample["chosen"][0] |
| 70 | + answer_b,answer_c, answer_d = sample["rejected"][:3] |
| 71 | + |
| 72 | + # shuffle, this uses the same logic as run_generative_v2.py |
| 73 | + chosen, shuffle_option = "[[A]]", np.random.randint(0,4) |
| 74 | + #if shuffle_option == 1: answer_a, answer_b, chosen = answer_b, answer_a, "[[B]]" |
| 75 | + #elif shuffle_option == 2: answer_a, answer_c, chosen = answer_c, answer_a, "[[C]]" |
| 76 | + #elif shuffle_option == 3: answer_a, answer_d, chosen = answer_d, answer_a, "[[D]]" |
| 77 | + |
| 78 | + prepared = { |
| 79 | + "id": sample["id"], |
| 80 | + "subset": sample["subset"], |
| 81 | + "question": sample["prompt"], |
| 82 | + "answer_a": answer_a, |
| 83 | + "answer_b": answer_b, |
| 84 | + "answer_c": answer_c, |
| 85 | + "answer_d": answer_d, |
| 86 | + "expected_answer": chosen |
| 87 | + } |
| 88 | + |
| 89 | + test.write(json.dumps(prepared) + "\n") |
| 90 | + |
0 commit comments