|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2024 The HuggingFace Team |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +# inspired by https://github.com/EleutherAI/lm-evaluation-harness/blob/main/lm_eval/models/dummy.py |
| 24 | + |
| 25 | +import random |
| 26 | +from typing import Optional |
| 27 | + |
| 28 | +from transformers import AutoTokenizer |
| 29 | + |
| 30 | +from lighteval.models.abstract_model import LightevalModel |
| 31 | +from lighteval.models.model_config import DummyModelConfig, EnvConfig |
| 32 | +from lighteval.models.model_output import GenerateReturn, LoglikelihoodReturn, LoglikelihoodSingleTokenReturn |
| 33 | +from lighteval.tasks.requests import ( |
| 34 | + GreedyUntilRequest, |
| 35 | + LoglikelihoodRequest, |
| 36 | + LoglikelihoodRollingRequest, |
| 37 | + LoglikelihoodSingleTokenRequest, |
| 38 | +) |
| 39 | + |
| 40 | + |
| 41 | +class DummyModel(LightevalModel): |
| 42 | + """Dummy model to generate random baselines.""" |
| 43 | + |
| 44 | + def __init__( |
| 45 | + self, |
| 46 | + config: DummyModelConfig, |
| 47 | + env_config: EnvConfig, |
| 48 | + ): |
| 49 | + self.config = config |
| 50 | + self.env_config = env_config |
| 51 | + self._random = random.Random(self.config.seed) |
| 52 | + self._tokenizer = None |
| 53 | + |
| 54 | + @property |
| 55 | + def tokenizer(self): |
| 56 | + if not self._tokenizer: |
| 57 | + self._tokenizer = AutoTokenizer.from_pretrained("gpt2") |
| 58 | + return self._tokenizer |
| 59 | + |
| 60 | + @property |
| 61 | + def add_special_tokens(self): |
| 62 | + return False |
| 63 | + |
| 64 | + @property |
| 65 | + def max_length(self) -> int: |
| 66 | + return 2048 |
| 67 | + |
| 68 | + def greedy_until( |
| 69 | + self, requests: list[GreedyUntilRequest], override_bs: Optional[int] = None |
| 70 | + ) -> list[GenerateReturn]: |
| 71 | + return [GenerateReturn(result="random baseline") for _ in range(len(requests))] |
| 72 | + |
| 73 | + def loglikelihood( |
| 74 | + self, requests: list[LoglikelihoodRequest], override_bs: Optional[int] = None |
| 75 | + ) -> list[LoglikelihoodReturn]: |
| 76 | + return [LoglikelihoodReturn((-self._random.random(), False)) for _ in requests] |
| 77 | + |
| 78 | + def loglikelihood_rolling( |
| 79 | + self, requests: list[LoglikelihoodRollingRequest], override_bs: Optional[int] = None |
| 80 | + ) -> list[LoglikelihoodReturn]: |
| 81 | + return [LoglikelihoodReturn((-self._random.random(), False)) for _ in requests] |
| 82 | + |
| 83 | + def loglikelihood_single_token( |
| 84 | + self, requests: list[LoglikelihoodSingleTokenRequest], override_bs: Optional[int] = None |
| 85 | + ) -> list[LoglikelihoodSingleTokenReturn]: |
| 86 | + return [ |
| 87 | + LoglikelihoodSingleTokenReturn(result=[-self._random.random() for _ in req.tokenized_continuation]) |
| 88 | + for req in requests |
| 89 | + ] |
0 commit comments