An abstracted Huggingface model class. Enables usage with both models of `transformers.AutoModelForCausalLM` and `transformers.AutoModelForSeq2SeqLM` classes. Supports data-parallel multi-GPU with HF Accelerate.
| 68 | |
| 69 | @register_model("hf-auto", "hf", "huggingface") |
| 70 | class HFLM(LM): |
| 71 | """ |
| 72 | An abstracted Huggingface model class. Enables usage with both models of |
| 73 | `transformers.AutoModelForCausalLM` and `transformers.AutoModelForSeq2SeqLM` classes. |
| 74 | |
| 75 | Supports data-parallel multi-GPU with HF Accelerate. |
| 76 | """ |
| 77 | |
| 78 | AUTO_MODEL_CLASS = None |
| 79 | _DEFAULT_MAX_LENGTH = 2048 |
| 80 | |
| 81 | def __init__( |
| 82 | self, |
| 83 | pretrained: Optional[Union[str, transformers.PreTrainedModel]] = "gpt2", |
| 84 | backend: Optional[ |
| 85 | Literal["default", "causal", "seq2seq"] |
| 86 | ] = "default", # override whether the model should be treated as decoder-only (causal) or encoder-decoder (seq2seq) |
| 87 | revision: Optional[str] = "main", |
| 88 | subfolder: Optional[str] = None, |
| 89 | tokenizer: Optional[ |
| 90 | Union[ |
| 91 | str, |
| 92 | transformers.PreTrainedTokenizer, |
| 93 | transformers.PreTrainedTokenizerFast, |
| 94 | ] |
| 95 | ] = None, |
| 96 | truncation: Optional[bool] = False, |
| 97 | max_length: Optional[int] = None, |
| 98 | device: Optional[str] = "cuda", |
| 99 | dtype: Optional[Union[str, torch.dtype]] = "auto", |
| 100 | batch_size: Optional[Union[int, str]] = 1, |
| 101 | max_batch_size: Optional[int] = 64, |
| 102 | trust_remote_code: Optional[bool] = False, |
| 103 | use_fast_tokenizer: Optional[bool] = True, |
| 104 | # arguments used for splitting a model across GPUs naively. |
| 105 | # only used if `parallelize=True`. |
| 106 | parallelize: Optional[bool] = False, |
| 107 | device_map_option: Optional[str] = "auto", |
| 108 | max_memory_per_gpu: Optional[Union[int, str]] = None, |
| 109 | max_cpu_memory: Optional[Union[int, str]] = None, |
| 110 | offload_folder: Optional[Union[str, os.PathLike]] = "./offload", |
| 111 | # PEFT and quantization options |
| 112 | peft: Optional[str] = None, |
| 113 | autogptq: Optional[Union[bool, str]] = False, |
| 114 | **kwargs, |
| 115 | ) -> None: |
| 116 | super().__init__() |
| 117 | |
| 118 | # optionally: take in an already-initialized transformers.PreTrainedModel |
| 119 | if not isinstance(pretrained, str): |
| 120 | eval_logger.warning( |
| 121 | "`pretrained` model kwarg is not of type `str`. Many other model arguments may be ignored. Please do not launch via accelerate or use `parallelize=True` if passing an existing model this way." |
| 122 | ) |
| 123 | assert not parallelize, "`parallelize=True` is not compatible with passing pre-initialized model to `pretrained`" |
| 124 | self._model = pretrained |
| 125 | self._device = self._model.device |
| 126 | self._config = self._model.config |
| 127 | gpus = 0 |
nothing calls this directly
no outgoing calls
no test coverage detected