From a252a830d70d6ec3d1f9f6d3553457cd8be55cf9 Mon Sep 17 00:00:00 2001 From: Arthur Zucker Date: Thu, 24 Aug 2023 11:48:28 +0000 Subject: [PATCH 1/6] refactor complicated from pretrained for peft --- src/transformers/modeling_utils.py | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index 580f58a6caeade..476de7be632056 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -2369,27 +2369,20 @@ def from_pretrained( ) if is_peft_available() and _adapter_model_path is None: - maybe_adapter_model_path = find_adapter_config_file( + _adapter_model_path = find_adapter_config_file( pretrained_model_name_or_path, revision=revision, subfolder=subfolder, token=token, commit_hash=commit_hash, ) - elif is_peft_available() and _adapter_model_path is not None: - maybe_adapter_model_path = _adapter_model_path - else: - 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"] + if os.ispath(_adapter_model_path): + with open(_adapter_model_path, "r", encoding="utf-8") as f: + pretrained_model_name_or_path = json.load(f)["base_model_name_or_path"] + adapter_model_id = pretrained_model_name_or_path + elif _adapter_model_path is not None: + adapter_model_id = _adapter_model_path # change device_map into a map if we passed an int, a str or a torch.device if isinstance(device_map, torch.device): From 46aa8d1cbc1155d753f1e6c3ad7504856423bf97 Mon Sep 17 00:00:00 2001 From: Arthur Zucker Date: Thu, 24 Aug 2023 11:50:57 +0000 Subject: [PATCH 2/6] nits --- src/transformers/modeling_utils.py | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index 476de7be632056..f9a28bb1c7a565 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -2368,20 +2368,18 @@ def from_pretrained( " ignored." ) - if is_peft_available() and _adapter_model_path is None: - _adapter_model_path = find_adapter_config_file( - pretrained_model_name_or_path, - revision=revision, - subfolder=subfolder, - token=token, - commit_hash=commit_hash, - ) - - if os.ispath(_adapter_model_path): - with open(_adapter_model_path, "r", encoding="utf-8") as f: - pretrained_model_name_or_path = json.load(f)["base_model_name_or_path"] - adapter_model_id = pretrained_model_name_or_path - elif _adapter_model_path is not None: + if is_peft_available(): + if _adapter_model_path is None: + _adapter_model_path = find_adapter_config_file( + pretrained_model_name_or_path, + revision=revision, + subfolder=subfolder, + token=token, + commit_hash=commit_hash, + ) + if os.ispath(_adapter_model_path): + with open(_adapter_model_path, "r", encoding="utf-8") as f: + _adapter_model_path = json.load(f)["base_model_name_or_path"] adapter_model_id = _adapter_model_path # change device_map into a map if we passed an int, a str or a torch.device From 2dbc9adacc2520dc56bdf49f47895c2e4a64050a Mon Sep 17 00:00:00 2001 From: Arthur Zucker Date: Thu, 24 Aug 2023 11:56:15 +0000 Subject: [PATCH 3/6] more nits --- src/transformers/modeling_utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index f9a28bb1c7a565..c54446562396a7 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -2377,10 +2377,9 @@ def from_pretrained( token=token, commit_hash=commit_hash, ) - if os.ispath(_adapter_model_path): + if os.path.isfile(_adapter_model_path): with open(_adapter_model_path, "r", encoding="utf-8") as f: _adapter_model_path = json.load(f)["base_model_name_or_path"] - adapter_model_id = _adapter_model_path # change device_map into a map if we passed an int, a str or a torch.device if isinstance(device_map, torch.device): @@ -3212,9 +3211,9 @@ def from_pretrained( if quantization_method_from_config == QuantizationMethod.GPTQ: model = quantizer.post_init_model(model) - if has_adapter_config: + if _adapter_model_path: model.load_adapter( - adapter_model_id, + _adapter_model_path, adapter_name=adapter_name, revision=revision, token=token, From 86aa982ba5c603710e09ec0aeaa49a4c16eefe5e Mon Sep 17 00:00:00 2001 From: Arthur <48595927+ArthurZucker@users.noreply.github.com> Date: Thu, 24 Aug 2023 14:19:42 +0200 Subject: [PATCH 4/6] Update src/transformers/modeling_utils.py Co-authored-by: Sylvain Gugger <35901082+sgugger@users.noreply.github.com> --- src/transformers/modeling_utils.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index c54446562396a7..f7f53656cde513 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -3211,7 +3211,7 @@ def from_pretrained( if quantization_method_from_config == QuantizationMethod.GPTQ: model = quantizer.post_init_model(model) - if _adapter_model_path: + if _adapter_model_path is not None: model.load_adapter( _adapter_model_path, adapter_name=adapter_name, From 2444c4f7d1abd56b994b3fe629f6546a9a94b0b0 Mon Sep 17 00:00:00 2001 From: Arthur Zucker Date: Thu, 24 Aug 2023 12:48:40 +0000 Subject: [PATCH 5/6] make tests happy --- src/transformers/modeling_utils.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index 3c25a660275506..b24796857881e5 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -2377,9 +2377,10 @@ def from_pretrained( token=token, commit_hash=commit_hash, ) - if os.path.isfile(_adapter_model_path): + if _adapter_model_path is not None and os.path.isfile(_adapter_model_path): with open(_adapter_model_path, "r", encoding="utf-8") as f: - _adapter_model_path = json.load(f)["base_model_name_or_path"] + _adapter_model_path = 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 if isinstance(device_map, torch.device): From 0ee61be4c1948302f7f643a1399581f76d9ad4bc Mon Sep 17 00:00:00 2001 From: Arthur Zucker Date: Thu, 24 Aug 2023 12:56:48 +0000 Subject: [PATCH 6/6] fixup after merge --- src/transformers/modeling_utils.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/transformers/modeling_utils.py b/src/transformers/modeling_utils.py index b41595c2d4cbdc..fbcd136376ccf4 100644 --- a/src/transformers/modeling_utils.py +++ b/src/transformers/modeling_utils.py @@ -2394,22 +2394,21 @@ def from_pretrained( if is_peft_available(): if _adapter_model_path is None: _adapter_model_path = find_adapter_config_file( - pretrained_model_name_or_path, - cache_dir=cache_dir, - force_download=force_download, - resume_download=resume_download, - proxies=proxies, - local_files_only=local_files_only, - token=token, - revision=revision, - subfolder=subfolder, - _commit_hash=commit_hash, - ) + pretrained_model_name_or_path, + cache_dir=cache_dir, + force_download=force_download, + resume_download=resume_download, + proxies=proxies, + local_files_only=local_files_only, + token=token, + revision=revision, + subfolder=subfolder, + _commit_hash=commit_hash, + ) if _adapter_model_path is not None and os.path.isfile(_adapter_model_path): - with open(_adapter_model_path, "r", encoding="utf-8") as f: + with open(_adapter_model_path, "r", encoding="utf-8"): _adapter_model_path = pretrained_model_name_or_path - # change device_map into a map if we passed an int, a str or a torch.device if isinstance(device_map, torch.device): device_map = {"": device_map}