| 196 | return lm_attr |
| 197 | |
| 198 | def fn(requests): |
| 199 | res = [] |
| 200 | remaining_reqs = [] |
| 201 | warned = False |
| 202 | # figure out which ones are cached and which ones are new |
| 203 | eval_logger.info( |
| 204 | f"Loading '{attr}' responses from cache '{self.cache_db}' where possible..." |
| 205 | ) |
| 206 | for req in tqdm(requests): |
| 207 | hsh = hash_args(attr, req.args) |
| 208 | if attr == "generate_until" and req.args[1].get("do_sample", False): |
| 209 | # when we are doing non-greedy generation, don't use the cache |
| 210 | # (else every "randomly sampled" generation would be identical for repeats > 1). |
| 211 | if not warned: |
| 212 | eval_logger.warning( |
| 213 | f"Arguments to lm.generate_until() '{req.args[1]}' include non-deterministic sampling. Caching will not be performed for such requests." |
| 214 | ) |
| 215 | warned = True |
| 216 | res.append(None) |
| 217 | remaining_reqs.append(req) |
| 218 | elif hsh in self.dbdict: |
| 219 | ob = self.dbdict[hsh] |
| 220 | |
| 221 | assert ob is not None |
| 222 | |
| 223 | res.append(ob) |
| 224 | else: |
| 225 | res.append(None) |
| 226 | remaining_reqs.append(req) |
| 227 | |
| 228 | # actually run the LM on the requests that do not have cached results |
| 229 | rem_res = getattr(self.lm, attr)(remaining_reqs) |
| 230 | |
| 231 | # stick the new ones back into the list and also cache any of the new ones |
| 232 | resptr = 0 |
| 233 | for req, r in zip(remaining_reqs, rem_res): |
| 234 | while res[resptr] is not None: |
| 235 | resptr += 1 |
| 236 | |
| 237 | res[resptr] = r |
| 238 | |
| 239 | # caching |
| 240 | hsh = hash_args(attr, req.args) |
| 241 | self.dbdict[hsh] = r |
| 242 | self.dbdict.commit() |
| 243 | |
| 244 | return res |
| 245 | |
| 246 | return fn |
| 247 | |