使用setup.py打包 HuggingFace PEFT 項目詳解:pip install peft的幕后流程

使用 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. 版本管理與更新

版本更新步驟

  1. 更新版本號:

    VERSION = "0.14.2.dev0"
    
  2. 提交代碼并創建新標簽:

    git tag -a 0.14.2 -m "Release version 0.14.2"
    git push --tags origin main
    
  3. 發布包到 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 項目的打包流程十分標準化,主要依賴 setuptoolstwine 工具。開發者可以根據不同需求擴展依賴項,并輕松實現版本管理與發布。

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")

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/64997.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/64997.shtml
英文地址,請注明出處:http://en.pswp.cn/web/64997.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

BP神經網絡的反向傳播算法

BP神經網絡(Backpropagation Neural Network)是一種常用的多層前饋神經網絡,通過反向傳播算法進行訓練。反向傳播算法的核心思想是通過計算損失函數對每個權重的偏導數,從而調整權重,使得網絡的預測輸出與真實輸出之間…

線程池的創建規范

第1章:引言——為什么使用線程池? 1.1 線程池的概念 線程池是一個容器,用來管理多個工作線程,它通過對線程的管理、復用來提高系統性能。線程池的核心理念是將線程的創建、銷毀、復用等操作交給線程池來管理,避免了頻…

【藍橋杯比賽-C++組-經典題目匯總】

1. 最短路 題目描述&#xff1a; 如下圖所示&#xff0c;G是一個無向圖&#xff0c;其中藍色邊的長度是1、橘色邊的長度是2、綠色邊的長度是3。 則從 A 到 S 的最短距離是多少&#xff1f; #include <iostream> #include <cstring> using namespace std; const i…

活動預告 | Microsoft 安全在線技術公開課:通過擴展檢測和響應抵御威脅

課程介紹 通過 Microsoft Learn 免費參加 Microsoft 安全在線技術公開課&#xff0c;掌握創造新機遇所需的技能&#xff0c;加快對 Microsoft Cloud 技術的了解。參加我們舉辦的“通過擴展檢測和響應抵御威脅”技術公開課活動&#xff0c;了解如何更好地在 Microsoft 365 Defen…

第八節:GLM-4v-9b模型的大語言模型源碼解讀(ChatGLMForConditionalGeneration)

文章目錄 前言一、ChatGLMForConditionalGeneration類源碼解讀1、ChatGLMForConditionalGeneration類源碼2、self.transformer方法源碼3、loss_fct = CrossEntropyLoss(ignore_index=-100)方法Demo二、ChatGLMModel類源碼解讀三、GLMTransformer結構源碼解讀四、GLMBlock結構源…

Windows onnxruntime編譯openvino

理論上來說&#xff0c;可以直接訪問 ONNXRuntime Releases 下載 dll 文件&#xff0c;然后從官方文檔中下載缺少的頭文件以直接調用&#xff0c;但我沒有嘗試過。 1. 下載 OpenVINO 包 從官網下載 OpenVINO 的安裝包并放置在 C:\Program Files (x86) 路徑下&#xff0c;例如…

Vue3 中的插槽

Vue3 中插槽的使用&#xff0c;插槽是 Vue 中的一個特別特性&#xff0c;插槽就是模版內容。例如<h1>標題 1</h1>標題 1 就是插槽&#xff0c;Vue 是無法識別模板內容的&#xff0c;只能通過屬性進行傳遞。Slot 主要包括默認、具名和作用域。Slot開發起來難度不大&…

01.02周四F34-Day43打卡

文章目錄 1. 地是濕的。昨晚估計下雨了。2. 你可能把包丟在餐廳里了吧?3. 她說他可能誤了航班。4. 我本來應該早點來的,但路上特別堵。5. 約翰可能在那次事故中受了重傷。6. 這是一個情景對話7. 我本可以走另一條路的。8. 我準是瘦了不少,你看我這褲子現在多肥。9. 錢沒了!會…

深度學習:基于MindSpore NLP的數據并行訓練

什么是數據并行&#xff1f; 數據并行&#xff08;Data Parallelism, DP&#xff09;的核心思想是將大規模的數據集分割成若干個較小的數據子集&#xff0c;并將這些子集分配到不同的 NPU 計算節點上&#xff0c;每個節點運行相同的模型副本&#xff0c;但處理不同的數據子集。…

五類推理(邏輯推理、概率推理、圖推理、基于深度學習的推理)的開源庫 (一)

在開發中&#xff0c;有一些開源庫可以實現不同類型的推理&#xff0c;包括邏輯推理、概率推理、圖推理、基于深度學習的推理等。以下是五類推理&#xff08;邏輯推理、概率推理、圖推理、基于深度學習的推理&#xff09;的現成開源庫&#xff0c;它們各自的功能、特點和適用場…

高等數學學習筆記 ? 函數的極限

1. 函數的極限定義 備注&#xff1a;已知坐標軸上一點&#xff0c;則&#xff1a; ①&#xff1a;的鄰域&#xff1a;指附近的開區間&#xff0c;記作。 ②&#xff1a;的去心鄰域&#xff1a;指附近的開區間&#xff0c;但不包含&#xff0c;記作。 ③&#xff1a;的鄰域&…

Python用K-Means均值聚類、LRFMC模型對航空公司客戶數據價值可視化分析指標應用|數據分享...

全文鏈接&#xff1a;https://tecdat.cn/?p38708 分析師&#xff1a;Yuling Fang 信息時代的來臨使得企業營銷焦點從產品中心轉向客戶中心&#xff0c;客戶關系管理成為企業的核心問題&#xff08;點擊文末“閱讀原文”獲取完整代碼數據&#xff09;。 客戶關系管理的關鍵是客…

【前端系列】優化axios響應攔截器

文章目錄 一、前言&#x1f680;&#x1f680;&#x1f680;二、axios響應攔截器&#xff1a;??????2.1 為什么前端需要響應攔截器element ui的消息組件 一、前言&#x1f680;&#x1f680;&#x1f680; ?? 回報不在行動之后&#xff0c;回報在行動之中。 這個系列可…

【 IEEE 獨立出版 · EI核心、Scopus穩定檢索 】第二屆算法、軟件工程與網絡安全國際學術會議(ASENS 2025)

ASENS 2025 第二屆算法、軟件工程與網絡安全國際學術會議 2025 2nd International Conference on Algorithms, Software Engineering and Network Security 中國 廣州 2025年3月21-23日 會議官網&#xff1a;www.ic-asens.org IEEE 獨立出版 EI核心、Scopus快速…

/ete/security/limits.conf參數詳解

/ete/security/limits.conf配置文件 內容如下&#xff1a; * soft nofile 65535 * hard nofile 65535參數詳解 *: 表示對所有用戶生效soft: 表示軟限制&#xff0c;即用戶可以通過ulimit命令自行調整該值hard: 表示硬限制&#xff0c;即用戶無法通過ulimit命令將該值調整超過…

#Vue3篇: 無感刷新token的原理JSESSIONID無感刷新和JWT接口刷新

基于這個后端是怎么更新token的 為了理解后端是如何更新 Token 的&#xff0c;我們需要考慮一個典型的基于 Token 的身份驗證流程&#xff0c;特別是涉及 JSESSIONID 和自定義 Token&#xff08;如 JWT, JSON Web Token&#xff09;的情況。 下面我將介紹兩種常見的更新 Token …

模塊化通訊管理機在物聯網系統中的應用

安科瑞劉鴻鵬 摘要 隨著能源結構轉型和智能化電網的推進&#xff0c;電力物聯網逐漸成為智能電網的重要組成部分。本文以安科瑞ANet系列智能通信管理機為例&#xff0c;探討其在電力物聯網中的應用&#xff0c;包括數據采集、規約轉換、邊緣計算、遠程控制等技術實踐&#…

Python基于Gradio可視化部署機器學習應用

Gradio 是一個用于快速創建機器學習模型和用戶界面之間交互的 Python 庫。它允許你無需編寫大量前端代碼&#xff0c;就能將機器學習模型部署為可交互的網頁應用。以下是一個基于 Gradio 可視化部署機器學習應用的基本步驟&#xff1a; 安裝 Gradio&#xff1a; 首先&#xff0…

Springboot使用RabbitMQ實現關閉超時訂單的一個簡單示例

1.maven中引入rabbitmq的依賴&#xff1a; <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-amqp</artifactId></dependency> 2.application.yml中進行rabbitmq相關配置&#xff1a; # rabbit…

AE Pinnacle 10x6 kW DeviceNet MDXL User r Manual

AE Pinnacle 10x6 kW DeviceNet MDXL User r Manual