MCPcopy Create free account
hub / github.com/OpenSparseLLMs/MoM / LM

Class LM

lm-eval-harness/lm_eval/api/model.py:19–151  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

17
18
19class LM(abc.ABC):
20 def __init__(self) -> None:
21 """Defines the interface that should be implemented by all LM subclasses.
22 LMs are assumed to take text (strings) as input and yield strings as output
23 (inputs/outputs should be tokenization-agnostic.)
24
25 """
26 # set rank and world size to a single process, by default.
27 self._rank = 0
28 self._world_size = 1
29 self.cache_hook = CacheHook(None)
30
31 @abc.abstractmethod
32 def loglikelihood(self, requests) -> List[Tuple[float, bool]]:
33 """Compute log-likelihood of generating a continuation from a context.
34 Downstream tasks should attempt to use loglikelihood instead of other
35 LM calls whenever possible.
36
37 :param requests: list[Instance]
38 A list of Instance objects, with property `args` which returns a tuple (context, continuation).
39 `context: str`
40 Context string. Implementations of LM must be able to handle an
41 empty context string.
42 `continuation: str`
43 The continuation over which log likelihood will be calculated. If
44 there is a word boundary, the space should be in the continuation.
45 For example, context="hello" continuation=" world" is correct.
46
47 :return: list[tuple[float, bool]]
48 A list of pairs (logprob, isgreedy)
49 `logprob: float`
50 The log probability of `continuation`.
51 `isgreedy`:
52 Whether `continuation` would be generated by greedy sampling from `context`.
53 """
54 pass
55
56 @abc.abstractmethod
57 def loglikelihood_rolling(self, requests) -> List[Tuple[float, bool]]:
58 """Compute full log-likelihood of a string, with no truncation, for perplexity computation
59 - We will use the full max context length of the model.
60 - For inputs that exceed the max context length, we divide the tokenized string into chunks of up to
61 the max context length.
62 - IMPORTANT: Each document's loglikelihood/perplexity is computed *separately*, unlike other implementations
63 which may simply concatenate multiple documents together.
64 - IMPORTANT: We maximize the amount of context for each prediction. Specifically, for inputs that we break into
65 multiple chunks, the last input will still a full-sized context.
66 Example:
67 Input tokens: [ 0 1 2 3 4 5 6 7 8 9 ]
68 Prefix: EOT
69 Max context length: 4
70 Resulting input/prediction pairs:
71
72 INPUT: EOT 0 1 2
73 PRED: 0 1 2 3
74
75 INPUT: 3 4 5 6
76 PRED: 4 5 6 7

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected