-
Notifications
You must be signed in to change notification settings - Fork 2.1k
ENH: Store PEFT version in PEFT config file #2782
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
BenjaminBossan
merged 3 commits into
huggingface:main
from
BenjaminBossan:enh-store-peft-version-in-adapter-config
Sep 30, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,16 +11,22 @@ | |
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import importlib.metadata | ||
| import inspect | ||
| import json | ||
| import os | ||
| import warnings | ||
| from dataclasses import asdict, dataclass, field | ||
| from typing import Optional, Union | ||
|
|
||
| import packaging.version | ||
| from huggingface_hub import hf_hub_download | ||
| from transformers.utils import PushToHubMixin, http_user_agent | ||
|
|
||
| from peft import __version__ | ||
|
|
||
| from .utils import CONFIG_NAME, PeftType, TaskType | ||
|
|
||
|
|
||
|
|
@@ -43,6 +49,30 @@ def _check_and_remove_unused_kwargs(cls, kwargs): | |
| return kwargs, unexpected_kwargs | ||
|
|
||
|
|
||
| def _is_dev_version(version: str) -> bool: | ||
| # check if the given version is a dev version | ||
| return packaging.version.Version(version).dev is not None | ||
|
|
||
|
|
||
| def _get_commit_hash(pkg_name: str) -> str | None: | ||
| # If PEFT was installed from a specific commit hash, try to get it. This works e.g. when installing PEFT with `pip | ||
| # install git+https://github.com/huggingface/peft.git@<HASH>`. This works not for other means, like editable | ||
| # installs. | ||
| try: | ||
| dist = importlib.metadata.distribution(pkg_name) | ||
| except importlib.metadata.PackageNotFoundError: | ||
| return None | ||
|
|
||
| # See: https://packaging.python.org/en/latest/specifications/direct-url/ | ||
| for path in dist.files or []: | ||
| if path.name == "direct_url.json": | ||
| direct_url = json.loads((dist.locate_file(path)).read_text()) | ||
| vcs_info = direct_url.get("vcs_info") | ||
| if vcs_info and "commit_id" in vcs_info: | ||
| return vcs_info["commit_id"] | ||
| return None | ||
|
|
||
|
|
||
| @dataclass | ||
| class PeftConfigMixin(PushToHubMixin): | ||
| r""" | ||
|
|
@@ -60,13 +90,38 @@ class PeftConfigMixin(PushToHubMixin): | |
| auto_mapping: Optional[dict] = field( | ||
| default=None, metadata={"help": "An auto mapping dict to help retrieve the base model class if needed."} | ||
| ) | ||
| peft_version: Optional[str] = field(default=None, metadata={"help": "PEFT version, leave empty to auto-fill."}) | ||
|
|
||
| def __post_init__(self): | ||
| # check for invalid task type | ||
| if (self.task_type is not None) and (self.task_type not in list(TaskType)): | ||
| raise ValueError( | ||
| f"Invalid task type: '{self.task_type}'. Must be one of the following task types: {', '.join(TaskType)}." | ||
| ) | ||
| if self.peft_version is None: | ||
| self.peft_version = self._get_peft_version() | ||
|
|
||
| @staticmethod | ||
| def _get_peft_version() -> str: | ||
| # gets the current peft version; if it's a dev version, try to get the commit hash too, as the dev version is | ||
| # ambiguous | ||
| version = __version__ | ||
| if not _is_dev_version(version): | ||
| return version | ||
|
|
||
| try: | ||
| git_hash = _get_commit_hash("peft") | ||
| if git_hash is None: | ||
| git_hash = "UNKNOWN" | ||
| except Exception: | ||
| # Broad exception: We never want to break user code just because the git_hash could not be determined | ||
| warnings.warn( | ||
| "A dev version of PEFT is used but there was an error while trying to determine the commit hash. " | ||
| "Please open an issue: https://github.com/huggingface/peft/issues" | ||
| ) | ||
|
Comment on lines
+116
to
+121
|
||
| git_hash = "UNKNOWN" | ||
| version = version + f"@{git_hash}" | ||
| return version | ||
|
|
||
| def to_dict(self) -> dict: | ||
| r""" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.