transformers作为模型定义框架,覆盖文本、视觉、音频、视频多模态,是训练和推理的枢纽。兼容axolotl、unsloth、vLLM、SGLang等主流框架。
Transformers 是面向自然语言处理、计算机视觉、音频、视频及多模态模型进行推理和训练的 SOTA 预训练模型框架。
它将模型定义集中化,使整个生态系统中对模型定义达成一致。transformers 是跨框架的枢纽:只要某种模型定义被支持,它就能兼容大多数训练框架(Axolotl、Unsloth、DeepSpeed、FSDP、PyTorch-Lightning 等)、推理引擎(vLLM、SGLang、TGI 等)以及相邻的建模库(llama.cpp、mlx 等)—— 这些库都从 transformers 获取模型定义。
我们承诺,通过让模型定义保持简洁、可定制且高效,来帮助支持新的 SOTA 模型并推动其使用民主化。
Hugging Face Hub 上有超过 100 万个 Transformers 模型检查点可供使用。
立即探索 Hub,找到合适的模型并用 Transformers 快速开始使用。
Transformers 支持 Python 3.10+ 和 PyTorch 2.5+。
使用 venv 或 uv(一个基于 Rust 的快速 Python 包和项目管理器)创建并激活虚拟环境。
# venv
python -m venv .my-env
source .my-env/bin/activate
# uv
uv venv .my-env
source .my-env/bin/activate
在虚拟环境中安装 Transformers。
# pip
pip install "transformers[torch]"
# uv
uv pip install "transformers[torch]"
如果你想使用库中最新的变动或者想参与贡献,可以从源码安装。不过最新版本可能不稳定,遇到错误请随时提交 issue。
git clone https://github.com/huggingface/transformers.git
cd transformers
# pip
pip install '.[torch]'
# uv
uv pip install '.[torch]'
通过 Pipeline API 立即开始使用 Transformers。Pipeline 是一个高级推理类,支持文本、音频、视觉和多模态任务。它处理输入的预处理并返回相应的输出。
实例化一个 pipeline 并指定用于文本生成的模型。模型会被下载并缓存,以便你可以轻松地重复使用它。最后,传入一些文本作为模型的提示。
from transformers import pipeline
pipeline = pipeline(task="text-generation", model="Qwen/Qwen2.5-1.5B")
pipeline("the secret to baking a really good cake is ")
[{'generated_text': 'the secret to baking a really good cake is 1) to use the right ingredients and 2) to follow the recipe exactly. the recipe for the cake is as follows: 1 cup of sugar, 1 cup of flour, 1 cup of milk, 1 cup of butter, 1 cup of eggs, 1 cup of chocolate chips. if you want to make 2 cakes, how much sugar do you need? To make 2 cakes, you will need 2 cups of sugar.'}]
与模型对话的用法完全相同。唯一的区别是你需要在你和系统之间构建一个聊天历史(作为 Pipeline 的输入)。
只要 transformers serve 在运行,你也可以直接从命令行与模型对话。
transformers chat Qwen/Qwen2.5-0.5B-Instruct
import torch
from transformers import pipeline
chat = [
{"role": "system", "content": "You are a sassy, wise-cracking robot as imagined by Hollywood circa 1986."},
{"role": "user", "content": "Hey, can you tell me any fun things to do in New York?"}
]
pipeline = pipeline(task="text-generation", model="meta-llama/Meta-Llama-3-8B-Instruct", dtype=torch.bfloat16, device_map="auto")
response = pipeline(chat, max_new_tokens=512)
print(response[0]["generated_text"][-1]["content"])
展开以下示例,查看 Pipeline 如何处理不同模态和任务。
from transformers import pipeline
pipeline = pipeline(task="automatic-speech-recognition", model="openai/whisper-large-v3")
pipeline("https://huggingface.co/datasets/Narsil/asr_dummy/resolve/main/mlk.flac")
{'text': ' I have a dream that one day this nation will rise up and live out the true meaning of its creed.'}

from transformers import pipeline
pipeline = pipeline(task="image-classification", model="facebook/dinov2-small-imagenet1k-1-layer")
pipeline("https://huggingface.co/datasets/Narsil/image_dummy/raw/main/parrots.png")
[{'label': 'macaw', 'score': 0.997848391532898},
{'label': 'sulphur-crested cockatoo, Kakatoe galerita, Cacatua galerita',
'score': 0.0016551691805943847},
{'label': 'lorikeet', 'score': 0.00018523589824326336},
{'label': 'African grey, African gray, Psittacus erithacus',
'score': 7.85409429227002e-05},
{'label': 'quail', 'score': 5.502637941390276e-05}]

from transformers import pipeline
pipeline = pipeline(task="visual-question-answering", model="Salesforce/blip-vqa-base")
pipeline(
image="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/transformers/tasks/idefics-few-shot.jpg",
question="What is in the image?",
)
[{'answer': 'statue of liberty'}]
易于使用的 SOTA 模型:在自然语言理解与生成、计算机视觉、音频、视频和多模态任务上具有高性能。研究人员、工程师和开发者的入门门槛低。只需学习三个类即可掌握,接口简洁。使用统一 API 调用所有预训练模型。
易于使用的 SOTA 模型:
更低的计算成本,更小的碳足迹:共享训练好的模型,无需从头训练。减少计算时间和生产成本。数百种模型架构,覆盖所有模态的 100 万 + 预训练检查点。
更低的计算成本,更小的碳足迹:
为模型的整个生命周期选择合适的框架:用三行代码训练 SOTA 模型。可以随时在 PyTorch / JAX / TF2.0 框架之间迁移单个模型。为训练、评估和生产选择合适的框架。
为模型的整个生命周期选择合适的框架:
轻松根据需求自定义模型或示例:我们为每种架构提供示例,以复现原作者发布的结果。模型内部尽可能保持一致地暴露。模型文件可以独立于库使用,以便快速实验。
轻松根据需求自定义模型或示例:
这个库不是神经网络的模块化工具箱。模型文件中的代码故意没有通过额外抽象进行重构,以便研究人员能够快速迭代每个模型,而无需深入额外的抽象层或文件。
训练 API 针对与 Transformers 提供的 PyTorch 模型协同工作进行了优化。对于通用的机器学习循环,你应该使用其他库,如 Accelerate。
示例脚本仅作为示例。它们不一定开箱即用地适用于你的特定用例,你需要自行调整代码才能使其正常工作。
Transformers 不仅仅是一个使用预训练模型的工具包,它是一个围绕它和 Hugging Face Hub 构建的项目社区。我们希望 Transformers 能够帮助开发者、研究人员、学生、教授、工程师以及任何想要构建自己梦想项目的人。
为了庆祝 Transformers 达到 10 万星,我们希望在 awesome-transformers 页面上展示社区的力量,列出了 100 个使用 Transformers 构建的令人惊叹的项目。
如果你拥有或使用一个你认为应该被列入名单的项目,请提交 PR 来添加它!
你可以在 Hub 的模型页面上直接测试我们的大多数模型。
展开以下每个模态,查看各种用例的一些示例模型。
现在有一篇可以引用的关于 🤗 Transformers 库的论文:
@inproceedings{wolf-etal-2020-transformers,
title = "Transformers: State-of-the-Art Natural Language Processing",
author = "Thomas Wolf and Lysandre Debut and Victor Sanh and Julien Chaumond and Clement Delangue and Anthony Moi and Pierric Cistac and Tim Rault and Rémi Louf and Morgan Funtowicz and Joe Davison and Sam Shleifer and Patrick von Platen and Clara Ma and Yacine Jernite and Julien Plu and Canwen Xu and Teven Le Scao and Sylvain Gugger and Mariama Drame and Quentin Lhoest and Alexander M. Rush",
booktitle = "Proceedings of the 2020 Conference on Empirical Methods in Natural Language Processing: System Demonstrations",
month = oct,
year = "2020",
address = "Online",
publisher = "Association for Computational Linguistics",
url = "https://aclanthology.org/2020.emnlp-demos.6/",
pages = "38--45"
}