r""" Args: Instantiate a `LoraModel` from a pretrained Lora configuration and weights. model (`transformers.PreTrainedModel`): The model to be adapted. The model should be initialized with the `from_pretrained` method. from `transformers` l
(cls, model, model_id, **kwargs)
| 116 | |
| 117 | @classmethod |
| 118 | def from_pretrained(cls, model, model_id, **kwargs): |
| 119 | r""" |
| 120 | Args: |
| 121 | Instantiate a `LoraModel` from a pretrained Lora configuration and weights. |
| 122 | model (`transformers.PreTrainedModel`): |
| 123 | The model to be adapted. The model should be initialized with the `from_pretrained` method. from |
| 124 | `transformers` library. |
| 125 | model_id (`str`): |
| 126 | The name of the Lora configuration to use. Can be either: |
| 127 | - A string, the `model id` of a Lora configuration hosted inside a model repo on |
| 128 | huggingface Hub |
| 129 | - A path to a directory containing a Lora configuration file saved using the |
| 130 | `save_pretrained` method, e.g., ``./my_lora_config_directory/``. |
| 131 | """ |
| 132 | from .mapping import MODEL_TYPE_TO_PEFT_MODEL_MAPPING, PEFT_TYPE_TO_CONFIG_MAPPING |
| 133 | |
| 134 | # load the config |
| 135 | config = PEFT_TYPE_TO_CONFIG_MAPPING[PeftConfig.from_pretrained(model_id).peft_type].from_pretrained(model_id) |
| 136 | |
| 137 | if getattr(model, "hf_device_map", None) is not None: |
| 138 | remove_hook_from_submodules(model) |
| 139 | |
| 140 | if config.task_type not in MODEL_TYPE_TO_PEFT_MODEL_MAPPING.keys(): |
| 141 | model = cls(model, config) |
| 142 | else: |
| 143 | model = MODEL_TYPE_TO_PEFT_MODEL_MAPPING[config.task_type](model, config) |
| 144 | |
| 145 | # load weights if any |
| 146 | if os.path.exists(os.path.join(model_id, WEIGHTS_NAME)): |
| 147 | filename = os.path.join(model_id, WEIGHTS_NAME) |
| 148 | else: |
| 149 | try: |
| 150 | filename = hf_hub_download(model_id, WEIGHTS_NAME) |
| 151 | except: # noqa |
| 152 | raise ValueError( |
| 153 | f"Can't find weights for {model_id} in {model_id} or in the Hugging Face Hub. " |
| 154 | f"Please check that the file {WEIGHTS_NAME} is present at {model_id}." |
| 155 | ) |
| 156 | |
| 157 | adapters_weights = torch.load( |
| 158 | filename, map_location=torch.device("cuda" if torch.cuda.is_available() else "cpu") |
| 159 | ) |
| 160 | # load the weights into the model |
| 161 | model = set_peft_model_state_dict(model, adapters_weights) |
| 162 | if getattr(model, "hf_device_map", None) is not None: |
| 163 | device_map = kwargs.get("device_map", "auto") |
| 164 | max_memory = kwargs.get("max_memory", None) |
| 165 | no_split_module_classes = model._no_split_modules |
| 166 | if device_map != "sequential": |
| 167 | max_memory = get_balanced_memory( |
| 168 | model, |
| 169 | max_memory=max_memory, |
| 170 | no_split_module_classes=no_split_module_classes, |
| 171 | low_zero=(device_map == "balanced_low_0"), |
| 172 | ) |
| 173 | if isinstance(device_map, str): |
| 174 | device_map = infer_auto_device_map( |
| 175 | model, max_memory=max_memory, no_split_module_classes=no_split_module_classes |
no test coverage detected