|
| 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 | + |
| 15 | +import argparse |
| 16 | +import os |
| 17 | +import pprint |
| 18 | +import sys |
| 19 | + |
| 20 | +sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))) |
| 21 | + |
| 22 | +from datasets import load_dataset |
| 23 | +from omegaconf import OmegaConf |
| 24 | +from transformers import AutoTokenizer |
| 25 | + |
| 26 | +from examples.run_grpo_math import math_data_processor |
| 27 | +from nemo_reinforcer.data import MathDataConfig |
| 28 | +from nemo_reinforcer.data.datasets import AllTaskProcessedDataset |
| 29 | +from nemo_reinforcer.data.interfaces import TaskDataSpec |
| 30 | +from nemo_reinforcer.data.llm_message_utils import remap_dataset_keys |
| 31 | +from nemo_reinforcer.distributed.virtual_cluster import init_ray |
| 32 | +from nemo_reinforcer.environments.math_environment import MathEnvironment |
| 33 | +from nemo_reinforcer.evals.eval import MasterConfig, run_env_eval, setup |
| 34 | +from nemo_reinforcer.models.generation.interfaces import GenerationConfig |
| 35 | + |
| 36 | + |
| 37 | +def parse_args(): |
| 38 | + """Parse command line arguments.""" |
| 39 | + parser = argparse.ArgumentParser(description="Run Evaluation with configuration") |
| 40 | + parser.add_argument( |
| 41 | + "--config", type=str, default=None, help="Path to YAML config file" |
| 42 | + ) |
| 43 | + |
| 44 | + # Parse known args for the script |
| 45 | + args, remaining = parser.parse_known_args() |
| 46 | + |
| 47 | + # Convert remaining args to OmegaConf format |
| 48 | + overrides = OmegaConf.from_dotlist(remaining) |
| 49 | + |
| 50 | + return args, overrides |
| 51 | + |
| 52 | + |
| 53 | +def setup_data( |
| 54 | + data_config: MathDataConfig, generation_config: GenerationConfig, env_configs |
| 55 | +): |
| 56 | + print("\n▶ Setting up data...") |
| 57 | + math_task_spec = TaskDataSpec( |
| 58 | + task_name="math", |
| 59 | + prompt_file=data_config["prompt_file"], |
| 60 | + system_prompt_file=data_config["system_prompt_file"], |
| 61 | + ) |
| 62 | + |
| 63 | + # load dataset |
| 64 | + base_dataset = load_dataset(data_config["dataset_name"]) |
| 65 | + if data_config["dataset_key"] is not None: |
| 66 | + base_dataset = base_dataset[data_config["dataset_key"]] |
| 67 | + # remap problem and solution keys |
| 68 | + remapped_dataset = remap_dataset_keys( |
| 69 | + base_dataset, |
| 70 | + mapping_dict={ |
| 71 | + data_config["problem_key"]: "problem", |
| 72 | + data_config["solution_key"]: "expected_answer", |
| 73 | + }, |
| 74 | + ) |
| 75 | + |
| 76 | + tokenizer = AutoTokenizer.from_pretrained(generation_config["model_name"]) |
| 77 | + if tokenizer.pad_token is None: |
| 78 | + tokenizer.pad_token = tokenizer.eos_token |
| 79 | + |
| 80 | + math_env = MathEnvironment.options( |
| 81 | + runtime_env={"py_executable": MathEnvironment.DEFAULT_PY_EXECUTABLE} |
| 82 | + ).remote(env_configs["math"]) |
| 83 | + |
| 84 | + dataset = AllTaskProcessedDataset( |
| 85 | + dataset=remapped_dataset, |
| 86 | + tokenizer=tokenizer, |
| 87 | + default_task_data_spec=math_task_spec, |
| 88 | + task_data_processors=math_data_processor, |
| 89 | + max_seq_length=data_config["max_input_seq_length"], |
| 90 | + ) |
| 91 | + |
| 92 | + return dataset, math_env, tokenizer |
| 93 | + |
| 94 | + |
| 95 | +def main(): |
| 96 | + """Main entry point.""" |
| 97 | + # Parse arguments |
| 98 | + args, overrides = parse_args() |
| 99 | + |
| 100 | + if not args.config: |
| 101 | + args.config = os.path.join(os.path.dirname(__file__), "configs", "eval.yaml") |
| 102 | + |
| 103 | + config = OmegaConf.load(args.config) |
| 104 | + print(f"Loaded configuration from: {args.config}") |
| 105 | + |
| 106 | + if overrides: |
| 107 | + override_conf = OmegaConf.from_cli() |
| 108 | + print(f"Overrides: {override_conf}") |
| 109 | + config = OmegaConf.merge(config, override_conf) |
| 110 | + |
| 111 | + config: MasterConfig = OmegaConf.to_container(config, resolve=True) |
| 112 | + print("Applied CLI overrides") |
| 113 | + |
| 114 | + # Print config |
| 115 | + print("Final config:") |
| 116 | + pprint.pprint(config) |
| 117 | + |
| 118 | + # Init ray |
| 119 | + init_ray() |
| 120 | + |
| 121 | + # Setup data |
| 122 | + ( |
| 123 | + dataset, |
| 124 | + math_env, |
| 125 | + tokenizer, |
| 126 | + ) = setup_data(config["data"], config["generation"], config["env"]) |
| 127 | + |
| 128 | + # Setup |
| 129 | + ( |
| 130 | + vllm_generation, |
| 131 | + dataloader, |
| 132 | + master_config, |
| 133 | + ) = setup(config, tokenizer, dataset) |
| 134 | + |
| 135 | + # Run evaluation |
| 136 | + run_env_eval( |
| 137 | + vllm_generation, |
| 138 | + dataloader, |
| 139 | + math_env, |
| 140 | + master_config, |
| 141 | + ) |
| 142 | + |
| 143 | + |
| 144 | +if __name__ == "__main__": |
| 145 | + main() |
0 commit comments