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

Class AutoCausalLM

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

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

Source from the content-addressed store, hash-verified

495
496
497class AutoCausalLM(HuggingFaceAutoLM):
498 """Causal language modeling.
499 You can find a set of supported models in the HF documentation:
500 https://huggingface.co/docs/transformers/main/model_doc/auto#transformers.AutoModelForCausalLM
501 """
502
503 AUTO_MODEL_CLASS = transformers.AutoModelForCausalLM
504 AUTO_PEFT_CLASS = peft.PeftModel
505
506 def _create_auto_tokenizer(
507 self,
508 *,
509 pretrained: str,
510 revision: str,
511 subfolder: str,
512 tokenizer: Optional[str] = None,
513 trust_remote_code: Optional[bool] = False,
514 ) -> transformers.PreTrainedTokenizer:
515 tokenizer = super()._create_auto_tokenizer(
516 pretrained=pretrained,
517 revision=revision,
518 subfolder=subfolder,
519 tokenizer=tokenizer,
520 trust_remote_code=trust_remote_code,
521 )
522 tokenizer.padding_side = "left"
523 return tokenizer
524
525 def _model_call(
526 self, inputs: TokenSequence, labels: Optional[TokenSequence] = None
527 ) -> TokenSequence:
528 return self.model(inputs)["logits"]
529
530 def _model_generate(
531 self,
532 inputs: transformers.BatchEncoding,
533 max_tokens: int,
534 stop: Optional[List[str]] = None,
535 ) -> TokenSequence:
536 # Ensure that the context does not encroach into the `space`
537 # for the generation.
538 input_ids = inputs["input_ids"][:, self.max_gen_toks - self.max_length :]
539 attention_mask = inputs["attention_mask"][
540 :, self.max_gen_toks - self.max_length :
541 ]
542 input_ids = input_ids.to(self.device)
543 attention_mask = attention_mask.to(self.device)
544
545 stopping_criteria = stop_sequences_criteria(
546 self.tokenizer, stop, input_ids.shape[1], input_ids.shape[0]
547 )
548
549 generations = self.model.generate(
550 input_ids=input_ids,
551 attention_mask=attention_mask,
552 # GPT style models require the `generate` `max_length` arg to include the
553 # context length, so we instead set `max_new_tokens` which is the number
554 # of new tokens to generate, excluding the current number of tokens.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected