(self, requests: List[Instance])
| 255 | return loglikelihoods |
| 256 | |
| 257 | def generate_until(self, requests: List[Instance]) -> List[str]: |
| 258 | res = [] |
| 259 | |
| 260 | # batch tokenize contexts |
| 261 | context, all_gen_kwargs = zip(*(req.args for req in requests)) |
| 262 | context_encoding = self.tokenizer(context, add_special_tokens=False).input_ids |
| 263 | requests = [ |
| 264 | ((a, b), c) for a, b, c in zip(context, context_encoding, all_gen_kwargs) |
| 265 | ] |
| 266 | |
| 267 | def _collate_gen(_requests): |
| 268 | # the negative sign on len(toks) sorts descending - this has a few advantages: |
| 269 | # - time estimates will always be over not underestimates, which is more useful for planning |
| 270 | # - to know the size of a batch when going through the list, you know the first one is always the batch |
| 271 | # padded context length. this is useful to simplify the batching logic and more importantly to make |
| 272 | # automatic adaptive batches much much easier to implement |
| 273 | # - any OOMs will happen right away rather than near the end |
| 274 | return -len(_requests[0][1]), _requests[0][0] |
| 275 | |
| 276 | # we group requests by their generation_kwargs, |
| 277 | # so that we don't try to execute e.g. greedy sampling and temp=0.8 sampling |
| 278 | # in the same batch. |
| 279 | re_ords = Collator(requests, _collate_gen, grouping=True) |
| 280 | chunks = re_ords.get_batched( |
| 281 | n=int(self.batch_size) if self.batch_size != "auto" else 0, batch_fn=None |
| 282 | ) |
| 283 | |
| 284 | pbar = tqdm(total=len(requests), disable=(self.rank != 0)) |
| 285 | # for each different set of kwargs, we execute all requests, by batch. |
| 286 | for chunk in chunks: |
| 287 | context_and_encoding, all_gen_kwargs = zip(*chunk) |
| 288 | context, context_encoding = zip(*context_and_encoding) |
| 289 | # we assume all gen kwargs in the batch are the same |
| 290 | # this is safe to assume because the `grouper` object ensures it. |
| 291 | gen_kwargs = all_gen_kwargs[0] |
| 292 | # unpack our keyword arguments. |
| 293 | until = None |
| 294 | if isinstance(gen_kwargs, dict): |
| 295 | kwargs = copy.deepcopy(gen_kwargs) # edge case for repeats > 1 |
| 296 | if "until" in kwargs.keys(): |
| 297 | until = kwargs.pop("until") |
| 298 | if isinstance(until, str): |
| 299 | until = [until] |
| 300 | elif not isinstance(until, list): |
| 301 | raise ValueError( |
| 302 | f"Expected `kwargs['until']` to be of type Union[str,list] but got {until}" |
| 303 | ) |
| 304 | else: |
| 305 | raise ValueError( |
| 306 | f"Expected `kwargs` to be of type `dict` but got {gen_kwargs}" |
| 307 | ) |
| 308 | if not until: |
| 309 | until = [self.tokenizer.decode(self.eot_token_id)] |
| 310 | if "max_gen_toks" in kwargs.keys(): |
| 311 | max_gen_toks = kwargs.pop("max_gen_toks") |
| 312 | else: |
| 313 | max_gen_toks = self.max_gen_toks |
| 314 |
nothing calls this directly
no test coverage detected