使用 setup.py
打包 HuggingFace PEFT 項目詳解
Source: https://github.com/huggingface/peft/blob/main/setup.py
1. 項目簡介
HuggingFace 的 PEFT(Parameter-Efficient Fine-Tuning)庫是一個用于高效參數微調的 Python 工具包,支持多種大語言模型(LLM)微調方法。本文將詳細解析其 setup.py
文件,并說明如何打包和發布該項目到 PyPI。
2. 打包的核心依賴文件解析
PEFT 項目采用 setuptools
進行打包配置,核心依賴文件包括:
setup.py
:配置項目名稱、版本號、依賴項等關鍵信息。LICENSE
:項目許可證說明,Apache 2.0。README.md
:項目詳細描述,作為 PyPI 上的主頁內容。src
文件夾:包含源代碼,代碼結構清晰。find_packages()
:自動尋找src
目錄中的 Python 包,簡化手動定義路徑的復雜度。
3. 關鍵代碼解析
3.1 基本信息
VERSION = "0.14.1.dev0" # 當前版本號
版本號定義為 0.14.1 開發版,后續發布新版本需要更新此處。
setup(name="peft", # 項目名稱version=VERSION, # 版本號description="Parameter-Efficient Fine-Tuning (PEFT)", # 項目描述license_files=["LICENSE"], # 許可證文件long_description=open("README.md", encoding="utf-8").read(), # 詳細說明long_description_content_type="text/markdown", # 描述內容格式keywords="deep learning", # 關鍵詞,方便搜索license="Apache", # 許可證類型author="The HuggingFace team", # 作者author_email="benjamin@huggingface.co", # 聯系郵箱url="https://github.com/huggingface/peft", # 項目主頁地址
上述代碼提供了基礎項目信息,這些信息會直接顯示在 PyPI 上。
3.2 安裝依賴項
install_requires=["numpy>=1.17","packaging>=20.0","psutil","pyyaml","torch>=1.13.0","transformers","tqdm","accelerate>=0.21.0","safetensors","huggingface_hub>=0.25.0",
],
- 核心依賴:
numpy
:矩陣和數據處理。torch
:深度學習框架。transformers
:模型加載與微調。accelerate
:優化并行訓練與推理。safetensors
:安全存儲模型參數。huggingface_hub
:集成 HuggingFace 模型和工具。
3.3 擴展依賴項
extras = {}
extras["quality"] = ["black", # doc-builder has an implicit dependency on Black, see huggingface/doc-builder#434"hf-doc-builder","ruff~=0.6.1",
]
extras["docs_specific"] = ["black", # doc-builder has an implicit dependency on Black, see huggingface/doc-builder#434"hf-doc-builder",
]
extras["dev"] = extras["quality"] + extras["docs_specific"]
extras["test"] = extras["dev"] + ["pytest","pytest-cov","pytest-xdist","parameterized","datasets","diffusers","scipy","protobuf","sentencepiece",
]
- 擴展功能分類:
quality
:代碼格式檢查和文檔生成。dev
:開發和測試環境的擴展依賴。test
:單元測試與代碼覆蓋率工具。
這些擴展依賴支持開發者在不同需求下安裝特定工具集。例如,開發者可以使用以下命令安裝開發環境:
pip install .[dev]
3.4 包管理與源代碼組織
package_dir={"": "src"},
packages=find_packages("src"),
package_data={"peft": ["py.typed", "tuners/boft/fbd/fbd_cuda.cpp", "tuners/boft/fbd/fbd_cuda_kernel.cu"]},
package_dir
:指定源代碼位于src
目錄下。find_packages()
:自動檢索并打包所有子模塊。package_data
:明確包含的額外資源文件,如.cpp
和.cu
文件。
3.5 分類標簽
classifiers=["Development Status :: 5 - Production/Stable","Intended Audience :: Developers","License :: OSI Approved :: Apache Software License","Operating System :: OS Independent","Programming Language :: Python :: 3","Topic :: Scientific/Engineering :: Artificial Intelligence",
],
這些標簽描述項目的開發狀態、適用人群和環境,為 PyPI 用戶提供篩選條件。例如:
- 開發狀態:5 - Production/Stable 表示穩定版。
- 操作系統:支持所有平臺。
4. 打包與發布過程
4.1 構建包
python setup.py sdist bdist_wheel
sdist
:生成源碼包,適用于不同平臺的源碼安裝。bdist_wheel
:生成二進制包,加速安裝過程。
生成的包將保存在 dist/
目錄下。
4.2 上傳測試 PyPI
twine upload dist/* -r pypitest
4.3 驗證安裝
pip install -i https://testpypi.python.org/pypi --extra-index-url https://pypi.org/simple peft
4.4 上傳正式 PyPI
twine upload dist/* -r pypi
5. 版本管理與更新
版本更新步驟
-
更新版本號:
VERSION = "0.14.2.dev0"
-
提交代碼并創建新標簽:
git tag -a 0.14.2 -m "Release version 0.14.2" git push --tags origin main
-
發布包到 PyPI:
python setup.py sdist bdist_wheel twine upload dist/*
對應的原文:
# Release checklist
# 1. Change the version in __init__.py and setup.py to the release version, e.g. from "0.6.0.dev0" to "0.6.0"
# 2. Check if there are any deprecations that need to be addressed for this release by searching for "# TODO" in the code
# 3. Commit these changes with the message: "Release: VERSION", create a PR and merge it.
# 4. Add a tag in git to mark the release: "git tag -a VERSION -m 'Adds tag VERSION for pypi' "
# Push the tag to git:
# git push --tags origin main
# It is necessary to work on the original repository, not on a fork.
# 5. Run the following commands in the top-level directory:
# python setup.py bdist_wheel
# python setup.py sdist
# Ensure that you are on the clean and up-to-date main branch (git status --untracked-files=no should not list any
# files and show the main branch)
# 6. Upload the package to the pypi test server first:
# twine upload dist/* -r pypitest
# 7. Check that you can install it in a virtualenv by running:
# pip install -i https://testpypi.python.org/pypi --extra-index-url https://pypi.org/simple peft
# 8. Upload the final version to actual pypi:
# twine upload dist/* -r pypi
# 9. Add release notes to the tag on https://github.com/huggingface/peft/releases once everything is looking hunky-dory.
# Check the notes here: https://docs.google.com/document/d/1k-sOIfykuKjWcOIALqjhFKz4amFEp-myeJUJEzNgjoU/edit?usp=sharing
# 10. Update the version in __init__.py, setup.py to the bumped minor version + ".dev0" (e.g. from "0.6.0" to "0.7.0.dev0")
6. 小結
通過上述分析可以看出,PEFT 項目的打包流程十分標準化,主要依賴 setuptools
和 twine
工具。開發者可以根據不同需求擴展依賴項,并輕松實現版本管理與發布。
PEFT 的 setup.py
設計符合 Python 打包規范,支持源碼和二進制包的分發,適合需要快速部署 AI 模型微調工具的用戶參考和使用。
后記
2024年12月30日19點23分于上海,在GPT4o大大模型輔助下完成。
附錄
https://github.com/huggingface/peft/blob/main/setup.py源代碼如下:可以結合源代碼閱讀本文。
# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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 setuptools import find_packages, setupVERSION = "0.14.1.dev0"extras = {}
extras["quality"] = ["black", # doc-builder has an implicit dependency on Black, see huggingface/doc-builder#434"hf-doc-builder","ruff~=0.6.1",
]
extras["docs_specific"] = ["black", # doc-builder has an implicit dependency on Black, see huggingface/doc-builder#434"hf-doc-builder",
]
extras["dev"] = extras["quality"] + extras["docs_specific"]
extras["test"] = extras["dev"] + ["pytest","pytest-cov","pytest-xdist","parameterized","datasets","diffusers","scipy","protobuf","sentencepiece",
]setup(name="peft",version=VERSION,description="Parameter-Efficient Fine-Tuning (PEFT)",license_files=["LICENSE"],long_description=open("README.md", encoding="utf-8").read(),long_description_content_type="text/markdown",keywords="deep learning",license="Apache",author="The HuggingFace team",author_email="benjamin@huggingface.co",url="https://github.com/huggingface/peft",package_dir={"": "src"},packages=find_packages("src"),package_data={"peft": ["py.typed", "tuners/boft/fbd/fbd_cuda.cpp", "tuners/boft/fbd/fbd_cuda_kernel.cu"]},entry_points={},python_requires=">=3.9.0",install_requires=["numpy>=1.17","packaging>=20.0","psutil","pyyaml","torch>=1.13.0","transformers","tqdm","accelerate>=0.21.0","safetensors","huggingface_hub>=0.25.0",],extras_require=extras,classifiers=["Development Status :: 5 - Production/Stable","Intended Audience :: Developers","Intended Audience :: Education","Intended Audience :: Science/Research","License :: OSI Approved :: Apache Software License","Operating System :: OS Independent","Programming Language :: Python :: 3","Programming Language :: Python :: 3.9","Programming Language :: Python :: 3.10","Programming Language :: Python :: 3.11","Programming Language :: Python :: 3.12","Topic :: Scientific/Engineering :: Artificial Intelligence",],
)# Release checklist
# 1. Change the version in __init__.py and setup.py to the release version, e.g. from "0.6.0.dev0" to "0.6.0"
# 2. Check if there are any deprecations that need to be addressed for this release by searching for "# TODO" in the code
# 3. Commit these changes with the message: "Release: VERSION", create a PR and merge it.
# 4. Add a tag in git to mark the release: "git tag -a VERSION -m 'Adds tag VERSION for pypi' "
# Push the tag to git:
# git push --tags origin main
# It is necessary to work on the original repository, not on a fork.
# 5. Run the following commands in the top-level directory:
# python setup.py bdist_wheel
# python setup.py sdist
# Ensure that you are on the clean and up-to-date main branch (git status --untracked-files=no should not list any
# files and show the main branch)
# 6. Upload the package to the pypi test server first:
# twine upload dist/* -r pypitest
# 7. Check that you can install it in a virtualenv by running:
# pip install -i https://testpypi.python.org/pypi --extra-index-url https://pypi.org/simple peft
# 8. Upload the final version to actual pypi:
# twine upload dist/* -r pypi
# 9. Add release notes to the tag on https://github.com/huggingface/peft/releases once everything is looking hunky-dory.
# Check the notes here: https://docs.google.com/document/d/1k-sOIfykuKjWcOIALqjhFKz4amFEp-myeJUJEzNgjoU/edit?usp=sharing
# 10. Update the version in __init__.py, setup.py to the bumped minor version + ".dev0" (e.g. from "0.6.0" to "0.7.0.dev0")