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

Class AutoSeq2SeqLM

test/general/lm_eval/models/huggingface.py:564–720  ·  view source on GitHub ↗

Seq2Seq language modeling. You can find a set of supported models in the following documentation: https://huggingface.co/docs/transformers/main/model_doc/auto#transformers.AutoModelForSeq2SeqLM

Source from the content-addressed store, hash-verified

562
563
564class AutoSeq2SeqLM(HuggingFaceAutoLM):
565 """Seq2Seq language modeling.
566 You can find a set of supported models in the following documentation:
567 https://huggingface.co/docs/transformers/main/model_doc/auto#transformers.AutoModelForSeq2SeqLM
568 """
569
570 AUTO_MODEL_CLASS = transformers.AutoModelForSeq2SeqLM
571 AUTO_PEFT_CLASS = peft.PeftModel
572
573 def loglikelihood(
574 self, requests: List[Tuple[str, str]]
575 ) -> List[Tuple[float, bool]]:
576 new_requests = []
577 for chunk in utils.chunks(requests, self.batch_size):
578 context, continuation = zip(*chunk)
579
580 # Fill empty contexts with the EOT token.
581 context = [
582 f"{self.eot_token}" if len(text) == 0 else text for text in context
583 ]
584 context_enc = self.tok_encode_batch(context)
585 for key in context_enc:
586 context_enc[key] = context_enc[key][:, -self.max_length :]
587
588 # Remove leading whitespace introduced by the default
589 # `text_target_separator` since the context and continuation
590 # will not be concatenated as a single (decoder) input.
591 continuation = [text.lstrip() for text in continuation]
592 continuation_enc = self.tok_encode_batch(list(continuation))
593 for key in continuation_enc:
594 continuation_enc[key] = continuation_enc[key][:, -self.max_length :]
595
596 new_requests.append(
597 ((context, continuation), context_enc, continuation_enc)
598 )
599 return self._loglikelihood_tokens(new_requests)
600
601 def loglikelihood_rolling(self, requests: List[Tuple[str, str]]) -> List[float]:
602 loglikelihoods = []
603 for (string,) in tqdm(requests):
604 rolling_token_windows = list(
605 map(
606 utils.make_disjoint_window,
607 utils.get_rolling_token_windows(
608 token_list=self.tok_encode(string),
609 prefix_token=self.eot_token_id,
610 max_seq_len=self.max_length,
611 context_len=1,
612 ),
613 )
614 )
615 contexts, conts = utils.split_and_pad_windows(
616 rolling_token_windows,
617 pad_token_id=self.eot_token_id,
618 max_seq_len=self.max_length,
619 )
620 # Manually create BatchEncoding tensors with attention masks as
621 # expected by `self._model_call` in `self._loglikelihood_tokens`.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected