"tests/quantization/vscode:/vscode.git/clone" did not exist on "90ee9cea19bfd0e4e1e90f02367130d10ee00967"
Unverified Commit fecf0856 authored by Arthur's avatar Arthur Committed by GitHub
Browse files

[`from_pretrained`] Simpler code for peft (#25726)



* refactor complicated from pretrained for peft

* nits

* more nits

* Update src/transformers/modeling_utils.py
Co-authored-by: default avatarSylvain Gugger <35901082+sgugger@users.noreply.github.com>

* make tests happy

* fixup after merge

---------
Co-authored-by: default avatarSylvain Gugger <35901082+sgugger@users.noreply.github.com>
parent 0a365c3e
...@@ -2391,8 +2391,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -2391,8 +2391,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
else: else:
commit_hash = getattr(config, "_commit_hash", None) commit_hash = getattr(config, "_commit_hash", None)
if is_peft_available() and _adapter_model_path is None: if is_peft_available():
maybe_adapter_model_path = find_adapter_config_file( if _adapter_model_path is None:
_adapter_model_path = find_adapter_config_file(
pretrained_model_name_or_path, pretrained_model_name_or_path,
cache_dir=cache_dir, cache_dir=cache_dir,
force_download=force_download, force_download=force_download,
...@@ -2404,20 +2405,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -2404,20 +2405,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
subfolder=subfolder, subfolder=subfolder,
_commit_hash=commit_hash, _commit_hash=commit_hash,
) )
elif is_peft_available() and _adapter_model_path is not None: if _adapter_model_path is not None and os.path.isfile(_adapter_model_path):
maybe_adapter_model_path = _adapter_model_path with open(_adapter_model_path, "r", encoding="utf-8"):
else: _adapter_model_path = pretrained_model_name_or_path
maybe_adapter_model_path = None
has_adapter_config = maybe_adapter_model_path is not None
if has_adapter_config:
if _adapter_model_path is not None:
adapter_model_id = _adapter_model_path
else:
with open(maybe_adapter_model_path, "r", encoding="utf-8") as f:
adapter_model_id = pretrained_model_name_or_path
pretrained_model_name_or_path = json.load(f)["base_model_name_or_path"]
# change device_map into a map if we passed an int, a str or a torch.device # change device_map into a map if we passed an int, a str or a torch.device
if isinstance(device_map, torch.device): if isinstance(device_map, torch.device):
...@@ -3246,9 +3236,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix ...@@ -3246,9 +3236,9 @@ class PreTrainedModel(nn.Module, ModuleUtilsMixin, GenerationMixin, PushToHubMix
if quantization_method_from_config == QuantizationMethod.GPTQ: if quantization_method_from_config == QuantizationMethod.GPTQ:
model = quantizer.post_init_model(model) model = quantizer.post_init_model(model)
if has_adapter_config: if _adapter_model_path is not None:
model.load_adapter( model.load_adapter(
adapter_model_id, _adapter_model_path,
adapter_name=adapter_name, adapter_name=adapter_name,
revision=revision, revision=revision,
token=token, token=token,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment