MCPcopy Create free account
hub / github.com/OpenBitSys/BitDistiller / greedy_until

Method greedy_until

test/general/lm_eval/base.py:407–467  ·  view source on GitHub ↗
(self, requests)

Source from the content-addressed store, hash-verified

405 return re_ord.get_original(res)
406
407 def greedy_until(self, requests):
408 # TODO: implement fully general `until` that handles until that are
409 # multiple tokens or that span multiple tokens correctly
410
411 # TODO: extract to TokenizedLM?
412 res = []
413
414 def _collate(x):
415 # the negative sign on len(toks) sorts descending - this has a few advantages:
416 # - time estimates will always be over not underestimates, which is more useful for planning
417 # - to know the size of a batch when going through the list, you know the first one is always the batch
418 # padded context length. this is useful to simplify the batching logic and more importantly to make
419 # automatic adaptive batches much much easier to implement
420 # - any OOMs will happen right away rather than near the end
421
422 toks = self.tok_encode(x[0])
423 return -len(toks), x[0]
424
425 re_ord = utils.Reorderer(requests, _collate)
426
427 warn_stop_seq = False
428 for context, request_args in tqdm(re_ord.get_reordered()):
429 until = request_args["until"]
430 if isinstance(until, str):
431 until = [until]
432
433 if until:
434 try:
435 (primary_until,) = self.tok_encode(until[0])
436 except ValueError:
437 if not warn_stop_seq:
438 print(
439 "Warning: a primary stop sequence is multi-token! Will default to EOS token for this tokenizer. Consider using `hf-causal-experimental` for multi-token stop sequence support for the time being."
440 )
441 warn_stop_seq = True
442 primary_until = self.eot_token_id
443 else:
444 primary_until = None
445
446 context_enc = torch.tensor(
447 [self.tok_encode(context)[self.max_gen_toks - self.max_length :]]
448 ).to(self.device)
449
450 max_gen_tokens = min(
451 self.max_gen_toks, request_args.get("max_length", self.max_gen_toks)
452 )
453 cont = self._model_generate(
454 context_enc, context_enc.shape[1] + max_gen_tokens, primary_until
455 )
456
457 s = self.tok_decode(cont[0].tolist()[context_enc.shape[1] :])
458
459 for term in until:
460 s = s.split(term)[0]
461
462 # partial caching
463 self.cache_hook.add_partial("greedy_until", (context, until), s)
464

Callers

nothing calls this directly

Calls 6

get_reorderedMethod · 0.95
tok_encodeMethod · 0.95
_model_generateMethod · 0.95
tok_decodeMethod · 0.95
get_originalMethod · 0.95
add_partialMethod · 0.80

Tested by

no test coverage detected