hellaswag.py 1.64 KB
Newer Older
Jonathan Tow's avatar
Jonathan Tow committed
1
import re
2
from lm_eval.base import MultipleChoiceTask
3
4
from . common import HFTask

Charles Foster's avatar
Charles Foster committed
5

6
class HellaSwag(HFTask, MultipleChoiceTask):
Charles Foster's avatar
Charles Foster committed
7
8
9
10
11
12
13
14
15
16
    DATASET_PATH = "hellaswag"
    DATASET_NAME = None

    def has_training_docs(self):
        return True

    def has_validation_docs(self):
        return True

    def has_test_docs(self):
Jon Tow's avatar
Jon Tow committed
17
        return False
Charles Foster's avatar
Charles Foster committed
18

19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
    @classmethod
    def preprocess(cls, text):
        text = text.strip()
        # NOTE: Brackets are artifacts of the WikiHow dataset portion of HellaSwag.
        text = text.replace(" [title]", ". ")
        text = re.sub('\\[.*?\\]', '', text)
        text = text.replace("  ", " ")
        return text

    def _convert_standard(self, doc):
        ctx = doc["ctx_a"] + " " + doc["ctx_b"].capitalize()
        out_doc = {
            "query": self.preprocess(doc['activity_label'] + ': ' + ctx),
            "choices": [self.preprocess(ending) for ending in doc['endings']],
            "gold": int(doc['label']),
        }
        return out_doc

    def _load_docs(self, docs):
        for record in docs:
            yield self._convert_standard(record)

Charles Foster's avatar
Charles Foster committed
41
    def training_docs(self):
42
43
        docs = super().training_docs()
        return self._load_docs(docs)
Charles Foster's avatar
Charles Foster committed
44
45

    def validation_docs(self):
46
47
        docs = super().validation_docs()
        return self._load_docs(docs)
Charles Foster's avatar
Charles Foster committed
48
49

    def fewshot_description(self):
50
51
52
        return "Label for the relevant action: Sentences describing the " \
            "context, with an incomplete sentence trailing\nanswer that " \
            "plausibly completes the situation."
Charles Foster's avatar
Charles Foster committed
53

54
    def doc_to_text(self, doc):
55
        return doc["query"]