|
| 1 | +# Copyright 2025 - Oumi |
| 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 re |
| 16 | +from typing import Any, Optional |
| 17 | + |
| 18 | +from oumi.core.configs.params.evaluation_params import EvaluationTaskParams |
| 19 | +from oumi.core.inference.base_inference_engine import BaseInferenceEngine |
| 20 | +from oumi.core.registry import register_evaluation_function |
| 21 | +from oumi.datasets.grpo.letter_count import LetterCountGrpoDataset |
| 22 | +from oumi.utils.logging import logger |
| 23 | + |
| 24 | + |
| 25 | +def _extract_prediction(response: str) -> Optional[int]: |
| 26 | + r"""Returns the numeric answer extracted from `\boxed{...}`, or None otherwise.""" |
| 27 | + regex_result = re.findall(r"\\boxed\{(\d+)\}", response) |
| 28 | + if not regex_result or len(regex_result) != 1: |
| 29 | + return None |
| 30 | + number_str = regex_result[0] |
| 31 | + # Except clause shouldn't trigger because the regex should only find ints. |
| 32 | + try: |
| 33 | + return int(number_str) |
| 34 | + except ValueError: |
| 35 | + return None |
| 36 | + |
| 37 | + |
| 38 | +@register_evaluation_function("count_letters") |
| 39 | +def count_letters( |
| 40 | + task_params: EvaluationTaskParams, |
| 41 | + inference_engine: BaseInferenceEngine, |
| 42 | +) -> dict[str, Any]: |
| 43 | + """Custom evaluation function registered as `count_letters`.""" |
| 44 | + dataset = LetterCountGrpoDataset(split="test") |
| 45 | + # TODO: OPE-1155: Add support for using Oumi dataset code to create the dataset. |
| 46 | + # dataset = build_dataset("oumi-ai/oumi-letter-count", tokenizer=None, sample_count=10) # noqa: E501 |
| 47 | + # dataset = build_dataset("oumi-ai/berrybench-v0.1.0", tokenizer=None, sample_count=10) # noqa: E501 |
| 48 | + num_samples = task_params.num_samples |
| 49 | + if num_samples is None: |
| 50 | + num_samples = len(dataset) |
| 51 | + input_conversations = [dataset.conversation(i) for i in range(num_samples)] |
| 52 | + conversations = inference_engine.infer(input_conversations) |
| 53 | + logger.info(f"Finished inference on {len(conversations)} conversations!") |
| 54 | + if len(conversations) > 0: |
| 55 | + logger.info(f"Sample conversation: {conversations[0]}") |
| 56 | + |
| 57 | + count = 0 # The number of examples with correct answers extracted. |
| 58 | + total = 0 # All examples. |
| 59 | + valid_count = 0 # The number of examples with valid answers extracted. |
| 60 | + for i, conversation in enumerate(conversations): |
| 61 | + total += 1 |
| 62 | + # Grab the model's response |
| 63 | + response = conversation.last_message() |
| 64 | + # Ignore cases where model didn't respond or it's a multimodal response. |
| 65 | + # For now, we focus on text-only responses. |
| 66 | + if not response or not isinstance(response.content, str): |
| 67 | + continue |
| 68 | + # Count the example as correct if the extracted prediction is correct. |
| 69 | + prediction = _extract_prediction(response.content) |
| 70 | + if prediction is None: |
| 71 | + continue |
| 72 | + valid_count += 1 |
| 73 | + if prediction == conversation.metadata["letter_count_integer"]: |
| 74 | + count += 1 |
| 75 | + |
| 76 | + return { |
| 77 | + # Accuracy across all examples. |
| 78 | + "accuracy": count / total, |
| 79 | + # Accuracy when only counting examples with properly extracted answers. |
| 80 | + "properly_extracted_accuracy": count / valid_count, |
| 81 | + "num_samples": num_samples, |
| 82 | + # These three values sum up to num_samples. |
| 83 | + "num_correct_answers": count, |
| 84 | + "num_incorrect_answers": valid_count - count, |
| 85 | + "num_invalid_answers": total - valid_count, |
| 86 | + } |
0 commit comments