From 2520d03658c2055cf45b60c5a07321d947cf27b0 Mon Sep 17 00:00:00 2001 From: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com> Date: Sat, 17 Jun 2023 11:31:36 +0530 Subject: [PATCH 1/5] add logic --- src/peft/peft_model.py | 43 ++++++++++++++++++++++++++++++++++++-- src/peft/utils/__init__.py | 2 +- src/peft/utils/other.py | 2 +- 3 files changed, 43 insertions(+), 4 deletions(-) diff --git a/src/peft/peft_model.py b/src/peft/peft_model.py index cb9d3bf159..cd312a0f02 100644 --- a/src/peft/peft_model.py +++ b/src/peft/peft_model.py @@ -18,6 +18,7 @@ import warnings from contextlib import contextmanager +from . import __version__ import torch from accelerate import dispatch_model, infer_auto_device_map from accelerate.hooks import AlignDevicesHook, add_hook_to_module, remove_hook_from_submodules @@ -49,7 +50,7 @@ TaskType, _set_adapter, _set_trainable, - add_or_edit_model_card, + add_library_to_model_card, get_peft_model_state_dict, hub_file_exists, set_peft_model_state_dict, @@ -128,7 +129,7 @@ def save_pretrained(self, save_directory, safe_serialization=False, **kwargs): if os.path.isfile(save_directory): raise ValueError(f"Provided path ({save_directory}) should be a directory, not a file") os.makedirs(save_directory, exist_ok=True) - add_or_edit_model_card(save_directory) + self.create_or_update_model_card(save_directory) for adapter_name, peft_config in self.peft_config.items(): # save only the trainable weights @@ -526,6 +527,44 @@ def set_adapter(self, adapter_name): def active_peft_config(self): return self.peft_config[self.active_adapter] + def create_or_update_model_card(self, output_dir): + """ + Updates or create model card to include information about peft: + 1. Adds `peft` library tag + 2. Adds peft version + 3. Adds quantization information if it was used + """ + # Adds `peft` library tag + add_library_to_model_card(output_dir) + + with open(os.path.join(output_dir, "README.md"), "r") as f: + lines = f.readlines() + + quantization_config = self.config.quantization_config.to_dict() + training_config_text = "" + # Adds quantization information if it was used + if quantization_config is not None: + training_config_text += "\nThe following `bitsandbytes` quantization config was used during training:\n" + training_config_text += "\n".join([f"- {name}: {value}" for name, value in quantization_config.items()]) + training_config_text += "\n" + + training_procedure_heading = "## Training procedure" + if training_procedure_heading in lines: + lines.insert(lines.index(training_procedure_heading) + 2, training_config_text) + else: + lines.append(f"{training_procedure_heading}\n{training_config_text}") + + # Adds peft version + framework_block_heading = "### Framework versions" + if framework_block_heading in lines: + lines.insert(lines.index(framework_block_heading) + 2, "library_name: peft\n") + else: + lines.append(f"{framework_block_heading}\n\n- PEFT {__version__}") + + # write the lines back to README.md + with open(os.path.join(output_dir, "README.md"), "w") as f: + f.writelines(lines) + class PeftModelForSequenceClassification(PeftModel): """ diff --git a/src/peft/utils/__init__.py b/src/peft/utils/__init__.py index 90a93859c7..50e5da620b 100644 --- a/src/peft/utils/__init__.py +++ b/src/peft/utils/__init__.py @@ -27,7 +27,7 @@ WEIGHTS_NAME, SAFETENSORS_WEIGHTS_NAME, _set_trainable, - add_or_edit_model_card, + add_library_to_model_card, bloom_model_postprocess_past_key_value, prepare_model_for_int8_training, prepare_model_for_kbit_training, diff --git a/src/peft/utils/other.py b/src/peft/utils/other.py index dc531e7d9e..971689dafb 100644 --- a/src/peft/utils/other.py +++ b/src/peft/utils/other.py @@ -21,7 +21,7 @@ # Add or edit model card to have `library_name: peft` -def add_or_edit_model_card(output_dir): +def add_library_to_model_card(output_dir): if os.path.exists(os.path.join(output_dir, "README.md")): with open(os.path.join(output_dir, "README.md"), "r") as f: lines = f.readlines() From 9a9ee931c1f1d9c913f6a5da8622c6a64ecec853 Mon Sep 17 00:00:00 2001 From: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com> Date: Sat, 17 Jun 2023 11:35:15 +0530 Subject: [PATCH 2/5] Update peft_model.py --- src/peft/peft_model.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/peft/peft_model.py b/src/peft/peft_model.py index cd312a0f02..385e8e5b59 100644 --- a/src/peft/peft_model.py +++ b/src/peft/peft_model.py @@ -18,7 +18,6 @@ import warnings from contextlib import contextmanager -from . import __version__ import torch from accelerate import dispatch_model, infer_auto_device_map from accelerate.hooks import AlignDevicesHook, add_hook_to_module, remove_hook_from_submodules @@ -32,6 +31,7 @@ from transformers.modeling_outputs import SequenceClassifierOutput, TokenClassifierOutput from transformers.utils import PushToHubMixin +from . import __version__ from .tuners import ( AdaLoraModel, AdaptionPromptModel, From f0fe224869b4084cd6c156bfd34f08cb483be67b Mon Sep 17 00:00:00 2001 From: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com> Date: Sat, 17 Jun 2023 11:50:44 +0530 Subject: [PATCH 3/5] fix test failures --- src/peft/peft_model.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/peft/peft_model.py b/src/peft/peft_model.py index 385e8e5b59..8323735eb8 100644 --- a/src/peft/peft_model.py +++ b/src/peft/peft_model.py @@ -540,7 +540,9 @@ def create_or_update_model_card(self, output_dir): with open(os.path.join(output_dir, "README.md"), "r") as f: lines = f.readlines() - quantization_config = self.config.quantization_config.to_dict() + quantization_config = None + if hasattr(self.config, "quantization_config"): + quantization_config = self.config.quantization_config.to_dict() training_config_text = "" # Adds quantization information if it was used if quantization_config is not None: From 5dc14936ba58764653b7aaaf4285d4caff088ca5 Mon Sep 17 00:00:00 2001 From: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com> Date: Sat, 17 Jun 2023 11:55:02 +0530 Subject: [PATCH 4/5] fixes --- src/peft/peft_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/peft/peft_model.py b/src/peft/peft_model.py index 8323735eb8..cea880c037 100644 --- a/src/peft/peft_model.py +++ b/src/peft/peft_model.py @@ -550,14 +550,14 @@ def create_or_update_model_card(self, output_dir): training_config_text += "\n".join([f"- {name}: {value}" for name, value in quantization_config.items()]) training_config_text += "\n" - training_procedure_heading = "## Training procedure" + training_procedure_heading = "## Training procedure\n" if training_procedure_heading in lines: lines.insert(lines.index(training_procedure_heading) + 2, training_config_text) else: lines.append(f"{training_procedure_heading}\n{training_config_text}") # Adds peft version - framework_block_heading = "### Framework versions" + framework_block_heading = "### Framework versions\n" if framework_block_heading in lines: lines.insert(lines.index(framework_block_heading) + 2, "library_name: peft\n") else: From a5794354c57c2bafeb26b71437b871e036b54bc0 Mon Sep 17 00:00:00 2001 From: Sourab Mangrulkar <13534540+pacman100@users.noreply.github.com> Date: Sat, 17 Jun 2023 12:01:04 +0530 Subject: [PATCH 5/5] fix --- src/peft/peft_model.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/peft/peft_model.py b/src/peft/peft_model.py index cea880c037..c2d0275afe 100644 --- a/src/peft/peft_model.py +++ b/src/peft/peft_model.py @@ -559,9 +559,9 @@ def create_or_update_model_card(self, output_dir): # Adds peft version framework_block_heading = "### Framework versions\n" if framework_block_heading in lines: - lines.insert(lines.index(framework_block_heading) + 2, "library_name: peft\n") + lines.insert(lines.index(framework_block_heading) + 2, f"- PEFT {__version__}\n") else: - lines.append(f"{framework_block_heading}\n\n- PEFT {__version__}") + lines.append(f"{framework_block_heading}\n\n- PEFT {__version__}\n") # write the lines back to README.md with open(os.path.join(output_dir, "README.md"), "w") as f: