使用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.mzph.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