cache.py 6.26 KB
Newer Older
1
import hashlib
Lintang Sutawika's avatar
Lintang Sutawika committed
2
import logging
3
import os
4
5
from functools import wraps
from typing import Callable, List, Optional, Union
6

Lintang Sutawika's avatar
Lintang Sutawika committed
7
8

eval_logger = logging.getLogger(__name__)
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25


MODULE_DIR = os.path.dirname(os.path.realpath(__file__))

OVERRIDE_PATH = os.getenv("LM_HARNESS_CACHE_PATH")


PATH = OVERRIDE_PATH if OVERRIDE_PATH else f"{MODULE_DIR}/.cache"

# This should be sufficient for uniqueness
HASH_INPUT = "EleutherAI-lm-evaluation-harness"

HASH_PREFIX = hashlib.sha256(HASH_INPUT.encode("utf-8")).hexdigest()

FILE_SUFFIX = f".{HASH_PREFIX}.pickle"


Baber Abbasi's avatar
Baber Abbasi committed
26
27
28
def load_from_cache(file_name: str, cache: bool = False):
    if not cache:
        return
29
    try:
30
31
        import dill

32
33
34
35
36
37
38
39
40
41
42
43
        path = f"{PATH}/{file_name}{FILE_SUFFIX}"

        with open(path, "rb") as file:
            cached_task_dict = dill.loads(file.read())
            return cached_task_dict

    except Exception:
        eval_logger.debug(f"{file_name} is not cached, generating...")
        pass


def save_to_cache(file_name, obj):
44
45
    import dill

46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
    if not os.path.exists(PATH):
        os.mkdir(PATH)

    file_path = f"{PATH}/{file_name}{FILE_SUFFIX}"

    eval_logger.debug(f"Saving {file_path} to cache...")
    with open(file_path, "wb") as file:
        file.write(dill.dumps(obj))


# NOTE the "key" param is to allow for flexibility
def delete_cache(key: str = ""):
    files = os.listdir(PATH)

    for file in files:
        if file.startswith(key) and file.endswith(FILE_SUFFIX):
            file_path = f"{PATH}/{file}"
            os.unlink(file_path)
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212


def _build_cache_key(
    task: str,
    num_fewshot: int,
    rank: int,
    world_size: int,
    apply_chat_template: bool,
    fewshot_as_multiturn: bool,
    system_instruction: Optional[str],
    tokenizer_name: str,
) -> str:
    """Build cache key from parameters"""
    cache_key = f"requests-{task}-{num_fewshot}shot-rank{rank}-world_size{world_size}"

    if apply_chat_template:
        cache_key += "-chat_template"
    if fewshot_as_multiturn:
        cache_key += "-fewshot_as_multiturn"
    if system_instruction is not None:
        # Import utils here to avoid circular imports
        import utils

        cache_key += f"-system_prompt_hash{utils.hash_string(system_instruction)}"
    cache_key += f"-tokenizer{tokenizer_name}"

    return cache_key


def cache_instances(func):
    """Decorator to handle request caching for build_all_requests"""

    @wraps(func)
    def wrapper(
        self,
        *,
        limit: Union[int, None] = None,
        samples: Optional[List[int]] = None,
        rank: int = 0,
        world_size: int = 1,
        cache_requests: bool = False,
        rewrite_requests_cache: bool = False,
        system_instruction: Optional[str] = None,
        apply_chat_template: bool = False,
        fewshot_as_multiturn: bool = False,
        chat_template: Optional[Callable] = None,
        tokenizer_name: str = "",
        **kwargs,
    ):
        # If caching is disabled, just call the original function
        # The method will handle setting self._instances
        if not cache_requests:
            return func(
                self,
                limit=limit,
                samples=samples,
                rank=rank,
                world_size=world_size,
                cache_requests=cache_requests,
                rewrite_requests_cache=rewrite_requests_cache,
                system_instruction=system_instruction,
                apply_chat_template=apply_chat_template,
                fewshot_as_multiturn=fewshot_as_multiturn,
                chat_template=chat_template,
                tokenizer_name=tokenizer_name,
                **kwargs,
            )

        # Build cache key
        cache_key = _build_cache_key(
            self._config.task,
            self.config.num_fewshot,
            rank,
            world_size,
            apply_chat_template,
            fewshot_as_multiturn,
            system_instruction,
            tokenizer_name,
        )

        # Try to load from cache
        cached_instances = load_from_cache(file_name=cache_key, cache=cache_requests)

        # Return cached instances if available and not rewriting
        if cached_instances and not rewrite_requests_cache:
            cached_instances = (
                cached_instances[:limit] if limit is not None else cached_instances
            )
            flattened_instances = [
                instance
                for instance_group in cached_instances
                for instance in instance_group
            ]
            self._instances = flattened_instances
            eval_logger.debug(
                f"Using {len(flattened_instances)}contexts for {self.config.task} on rank {rank}..."
            )
            return

        # Store original limit for later use
        original_limit = limit

        # Process all documents when caching for simplicity
        if limit is not None:
            limit = None

        # Call the original function with modified parameters
        instances = func(
            self,
            limit=limit,
            samples=samples,
            rank=rank,
            world_size=world_size,
            cache_requests=cache_requests,
            rewrite_requests_cache=rewrite_requests_cache,
            system_instruction=system_instruction,
            apply_chat_template=apply_chat_template,
            fewshot_as_multiturn=fewshot_as_multiturn,
            chat_template=chat_template,
            tokenizer_name=tokenizer_name,
            **kwargs,
        )

        # Check if method handled everything (non-cache mode returns None)
        if instances is None:
            return

        # Apply original limit if specified
        sliced_instances = (
            instances[:original_limit] if original_limit is not None else instances
        )

        # Flatten and set instances
        flattened_instances = [
            instance
            for instance_group in sliced_instances
            for instance in instance_group
        ]
        self._instances = flattened_instances

        # Validate results
        if len(self._instances) == 0:
            raise ValueError("task.build_requests() did not find any docs!")

        # Save to cache if we generated new instances
        if not cached_instances or rewrite_requests_cache:
            save_to_cache(file_name=cache_key, obj=instances)

    return wrapper