Transformers 的 v4.0.0rc-1 版本(即第 4 版的初始候选版本)于 2020 年 11 月 19 日发布。五年后的今天,我们发布了 v5.0.0rc-0。如今,随着 v5 的推出,Transformers 每天通过 pip 的安装量已超过 300 万次——而 v4 时期仅为每天 2 万次 🤯。截至目前,其总安装量已突破 12 亿次!
生态系统已从 v4 时期的 40 种模型架构扩展到如今的 400 多种,社区在 Hub 上贡献了超过 75 万个与 Transformers 兼容的模型检查点,而 v4 时期这一数字约为 1000 个。
这一增长得益于该领域的发展以及 AI 如今已成为主流。作为生态系统中领先的模型定义库,我们需要不断演进和调整,以保持其相关性。在 AI 领域,自我革新是长久发展的关键。
我们很幸运能与许多基于 Transformers 构建的库和应用展开合作,包括(排名不分先后):llama.cpp、MLX、onnxruntime、Jan、LMStudio、vLLM、SGLang、Unsloth、LlamaFactory、dLLM、MaxText、TensorRT、Argmax 等众多伙伴。
对于 v5,我们希望着力于几个重要方面:简洁性、训练、推理和生产部署。我们将在本文中详细介绍为此所做的工作。
简洁性
团队的首要关注点是简洁性。在开发 Transformers 的过程中,我们将代码视为产品。我们希望模型集成代码保持清晰,以便生态系统能够依赖我们的模型定义,理解底层真正发生的事情、模型之间的差异,以及每个新模型的关键特性。简洁性带来了更广泛的标准性、通用性和更全面的支持。
模型新增
Transformers 是包括 Unsloth 在内的数十万个项目的基石。我们基于 Transformers 帮助用户高效地微调和训练模型,无论是 BERT、文本转语音(TTS)还是其他模型;即使其他库尚未支持某些模型,我们也能为其运行快速的强化学习(RL)推理。我们对 Transformers v5 感到兴奋,并且非常高兴能与 Hugging Face 团队合作!
—— Unsloth 的 Michael Han
Transformers 本质上仍然是一个模型架构工具包。我们的目标是囊括所有最新架构,并成为模型定义的“事实标准”。五年来,我们每周都会新增 1 到 3 个新模型,如下方时间线所示:
我们一直在努力改进模型添加流程。
模块化方法
在过去一年中,我们大力推行模块化设计,将其视为一项重大进步。这有助于简化维护、加快集成速度,并促进社区内的更好协作。
我们在《维护不可维护之物》这篇博文中提供了更深入的概述。简而言之,我们的目标是实现更简便的模型贡献流程,同时降低维护负担。我们可以强调的一个指标是:使用模块化方法后,贡献(和审查)所需的代码行数显著下降:
虽然我们尊重“一个模型,一个文件”的理念,但我们仍在持续引入一些抽象机制,以简化通用辅助工具的管理。最典型的例子是引入了 AttentionInterface,它为注意力机制提供了集中化的抽象。Eager 方法将保留在模型文件中;而其他方法,如 FA1/2/3、FlexAttention 或 SDPA,则被移至该接口中。
过去几年中,对新模型架构的 0 天支持数量不断增加,以及注意力处理机制的标准化,都帮助我们简化了对训练后现代大语言模型的支持。
—— Wing Lian,Axolotl
模型转换工具
我们正在构建工具,以帮助我们识别新模型与哪种现有模型架构相似。此功能利用机器学习来查找独立模型文件之间的代码相似性。更进一步,我们的目标是通过为新模型创建一个集成到我们 Transformers 格式的草稿拉取请求来自动化转换过程。这一流程减少了人工操作,并确保了一致性。
代码精简
精简建模与分词/处理文件
我们对建模和分词文件进行了大幅重构。得益于上述模块化方法以及跨模型的标准化,建模文件得到了极大改进。标准化有助于抽象化那些不构成模型核心的大部分工具,从而使建模代码仅包含模型前向/反向传播的相关部分。
与此同时,我们正在简化分词和处理文件:未来,我们将只专注于分词器后端,取消“快速”和“慢速”分词器的概念。
我们将使用 tokenizers 作为主要的分词后端,就像我们对基于 PyTorch 的模型所做的那样。我们将为基于 Sentencepiece 或 MistralCommon 后端的分词器提供替代方案,这些方案将不是默认选项,但会得到支持。图像处理器现在将仅保留其快速变体,该变体依赖于 torchvision 后端。
最后,我们将逐步停止对 Flax/TensorFlow 的支持,转而专注于 PyTorch 作为唯一后端;不过,我们也在与 Jax 生态系统的合作伙伴合作,以确保我们的模型与该生态系统之间的兼容性。
随着 v5 版本的发布,transformers 将全力押注 PyTorch。Transformers 充当着整个领域建模的真相来源和基础;我们一直与团队合作,以确保整个技术栈的良好性能。
我们很高兴能在未来继续在训练、推理和部署方面推动这一发展。
—— Matt White,PyTorch 基金会执行董事,Linux 基金会 AI 总经理
训练
随着我们迈向 v5 版本,训练仍然是团队的重点关注领域:过去我们主要侧重于微调,而非大规模的预训练/完整训练,但最近我们做了大量工作来改进对后者的支持。
大规模预训练
支持预训练意味着需要重新设计我们模型的初始化方式,确保它们能在不同并行范式下大规模运行,并为前向和反向传播提供优化的内核支持。
展望未来,我们很高兴能够扩展与 torchtitan、megatron、nanotron 以及其他任何有兴趣与我们合作的预训练工具的兼容性。
微调与后训练
我们继续与 Python 生态系统中的所有微调工具保持紧密合作。我们致力于持续提供与 Unsloth、Axolotl、LlamaFactory、TRL 以及 PyTorch 生态系统中其他工具兼容的模型实现;同时,我们也在与 JAX 生态系统中的 MaxText 等工具合作,以实现其框架与 transformers 之间的良好互操作性。
所有微调与后训练工具现在都可以依赖 transformers 进行模型定义;并通过 OpenEnv 或 Prime Environment Hub 进一步支持智能体应用场景。
推理
我们在 v5 版本中将重点放在推理上,并进行了多项范式变革:引入专用内核、更简洁的默认设置、新的 API,以及对优化推理引擎的支持。
与训练类似,我们在内核打包方面也投入了精力,以便在您的硬件和软件条件允许的情况下自动使用这些内核。如果您之前不了解内核,建议查阅此文档。
与此同时,我们推出了两个专用于推理的新 API:
- 我们提供了对连续批处理和分页注意力机制的支持。这些功能已在内部使用了一段时间,我们正在努力完善细节并编写使用指南。
- 我们引入了 transformers serve,作为 transformers 专属的新服务系统,用于部署兼容 OpenAI API 的服务器。
我们认为这对于评估等需要同时处理大量推理请求的应用场景来说是一个重大进步。我们的目标并非像专用推理引擎(vLLM、SGLang、TensorRT LLM)那样进行专门的优化,而是如下一节所述,力求与这些引擎实现完美的互兼容。
vLLM 中的 Transformers 后端极大地推动了更多架构(如 BERT 及其他编码器)的普及,让更多用户能够使用这些模型。我们一直与 Transformers 团队合作,确保众多模型能在不同模态下以最佳性能运行。这仅仅是我们合作的开始:我们很高兴看到 Transformers 团队将在第 5 版中将此作为重点方向。
—— Simon Mo、Harry Mellor,vLLM
标准化是加速 AI 创新的关键。Transformers v5 让 SGLang 团队能够将更少时间花在模型重实现上,而将更多时间投入到内核优化中。我们期待共同构建一个更高效、更统一的 AI 生态系统!
—— Chenyang Zhao,SGLang
生产环境与本地部署
近期,我们与最流行的推理引擎紧密合作,使它们能够将 transformers 作为后端。其价值十分显著:一旦某个模型被添加到 transformers 中,它就能在这些推理引擎中可用,同时充分利用各引擎的优势:推理优化、专用内核、动态批处理等。
我们还与 ONNXRuntime、llama.cpp 和 MLX 展开了非常密切的合作,以确保 transformers 与这些建模库之间的实现具有良好的互操作性。例如,得益于社区的巨大努力,现在可以非常轻松地在 transformers 中加载 GGUF 文件以进行进一步的微调。反之,transformers 模型也可以轻松转换为 GGUF 文件,以便与 llama.cpp 配合使用。
Transformers 框架是参考性 AI 模型实现的首选之地。该框架在整个技术栈中发挥着关键作用,为现代 AI 赋能。该项目的团队和社区真正理解并拥抱了开源开发与协作的精神。
—— Georgi Gerganov,ggml-org
MLX 也是如此,transformers 的 safetensors 文件与 MLX 的模型直接兼容。
无论怎样强调 Transformers(以及 datasets、tokenizers 等)对开源及整个 AI 生态系统的重要性都不为过。我个人已记不清有多少次将 Transformers 作为事实标准来使用了。
—— Awni Hannun,MLX
最后,我们正在突破本地推理的边界,并与 executorch 团队紧密合作,让 Transformers 模型能够在设备端运行。我们正通过 optimum 将覆盖范围扩展到多模态模型(视觉、音频)。
量化
量化正迅速成为最先进模型开发的标准。如今,许多 SOTA 模型都以低精度格式发布,例如 8-bit 和 4-bit(如 gpt-oss、Kimi-K2、Deepseek-r1),硬件也越来越多地针对低精度工作负载进行优化,社区也在积极共享高质量的量化检查点。在 v5 中,我们将量化作为 Transformers 支持的核心焦点,确保与所有主要功能的完全兼容,并提供一个可靠的训练和推理框架。
我们引入了模型权重加载方式的重大变革;借此,量化成为了一等公民。
我们与 Transformers 团队的合作非常高效,他们主动进行代码审查、提供反馈并贡献技术专长。他们的支持对于在 V5 中集成 TorchAO、扩展量化功能以及改进文档以促进更广泛采用至关重要。
—— Jerry Zhang,TorchAO
我们很高兴 v5 将量化提升为一等公民。这为 bitsandbytes 更好地支持 TP 和 MoE 等关键特性奠定了基础,也使得集成新的量化方法更加容易。
—— Matthew Douglas 与 Titus von Koeller,bitsandbytes
结论
这个第 5 版发布的总体主题是“互操作性”。所有重构、性能改进和标准化都围绕这一主题展开。v5 与不断发展的生态系统实现了良好的端到端协作:使用 Unsloth/Axolotl/LlamaFactory/MaxText 训练模型,用 vLLM/SGLang 部署它,并将其导出到 llama.cpp/executorch/MLX 以在本地运行!
第 5 版无疑是过去五年中我们社区众多人士共同努力的成果。我们也将其视为一份承诺,以及指引我们前进方向的灯塔。
我们借此机会清理了工具包,并分离出真正重要的部分;如今我们拥有了一块干净的基础,可以在此基础上进行构建。得益于社区和团队的诸多改进,性能、易用性和可读性方面的提升将更容易实现。
既然 v5.0.0 的第一个候选版本已经发布,我们将热切期待您的反馈。请查阅我们的发布说明以了解所有技术细节,我们会在 GitHub 的 issue 中等待您的反馈!
Transformers' version v4.0.0rc-1, the initial release candidate for version 4, was released on November 19th, 2020. Five years later, we now release v5.0.0rc-0. Today, as we launch v5, Transformers is installed more than 3 million times each day via pip - up from 20,000/day in v4 🤯. Altogether, it has now surpassed 1.2 billion installs!
The ecosystem has expanded from 40 model architectures in v4 to over 400 today, and the community has contributed more than 750,000 model checkpoints on the Hub compatible with Transformers, up from roughly 1,000 at the time of v4.
This growth is powered by the evolution of the field and the now mainstream access to AI. As a leading model-definition library in the ecosystem, we need to continuously evolve and adapt the library to continue being relevant. Reinvention is key for longevity in AI.
We’re fortunate to collaborate with many libraries and apps built on transformers, in no specific order: llama.cpp, MLX, onnxruntime, Jan, LMStudio, vLLM, SGLang, Unsloth, LlamaFactory, dLLM, MaxText, TensorRT, Argmax, among many other friends.
For v5, we wanted to work on several notable aspects: simplicity, training, inference, and production. We detail the work that went into them in this post.
Simplicity
The first focus of the team was on simplicity. Working on transformers, we see the code as the product. We want our model integrations to be clean, so that the ecosystem may depend on our model definitions and understand what’s really happening under the hood, how models differ from each other, and the key features of each new model. Simplicity results in wider standardization, generality, and wider support.
Model Additions
Transformers is the backbone of hundreds of thousands of projects, Unsloth included. We build on Transformers to help people fine-tune and train models efficiently, whether that’s BERT, text-to-speech (TTS), or others; to run fast inference for reinforcement learning (RL) even when models aren’t yet supported in other libraries. We're excited for Transformers v5 and are super happy to be working with the Hugging Face team!
-- Michael Han at Unsloth
Transformers, at the core, remains a model architecture toolkit. We aim to have all recent architectures and to be the “source of truth” for model definitions. We’ve been adding between 1 - 3 new models every week for 5 years, shown in the timeline below:
We’ve worked on improving that model-addition process.
Modular Approach
Over the past year, we’ve heavily pushed our modular design as a significant step forward. This allows for easier maintenance, faster integration, and better collaboration across the community.
We give a deeper overview in our Maintain the Unmaintainable blog post. For brevity, we aim to achieve a much easier model contribution process, as well as a lower maintenance burden. One metric we can highlight is that the number of lines of code to contribute (and review), drop significantly when modular is used:
While we respect the “One model, one file” philosophy, we continue introducing some abstractions making the management of common helpers simpler. The prime example of this is the introduction of the AttentionInterface, which offers a centralized abstraction for attention methods. The eager method will remain in the modeling file; others, such as FA1/2/3, FlexAttention, or SDPA, are moved to the interface.
Over the past couple of years, the increasing amount of 0-day support for new model architectures and standardization of attention handling has helped to simplify our support for post-training modern LLMs.
-- Wing Lian, Axolotl
Tooling for Model Conversion
We’re building tooling to help us identify which existing model architecture a new model resembles. This feature uses machine learning to find code similarities between independent modeling files. Going further, we aim to automate the conversion process by opening a draft PR for the model to be integrated into our transformers format. This process reduces manual effort and ensures consistency.
Code Reduction
Streamlining Modeling & Tokenization/Processing Files
We’ve significantly refactored the modeling and tokenization files. Modeling files have been greatly improved thanks to the modular approach mentioned above, on top of standardization across models. Standardization contributes to abstracting most of the tools that don’t make up a model, so that the modeling code only contains the relevant parts for a model’s forward/backward passes.
Alongside this work, we’re simplifying the tokenization and processing files: going forward, we’ll only focus on the tokenizers backend, removing the concept of “Fast” and “Slow” tokenizers.
We'll use tokenizers as our main tokenization backend, just as we do for PyTorch-based models. We’ll offer alternatives for Sentencepiece or MistralCommon backed tokenizers, which will be non-default but will be supported. Image processors will now only exist with their fast variant, which depends on the torchvision backend.
Finally, we’re sunsetting our Flax/TensorFlow support in favor of focusing on PyTorch as the sole backend; however, we're also working with partners in the Jax ecosystem to ensure we have compatibility between our models and this ecosystem.
With its v5 release, transformers is going all in on PyTorch. Transformers acts as a source of truth and foundation for modeling across the field; we've been working with the team to ensure good performance across the stack.
We're excited to continue pushing for this in the future across training, inference, and deployment.
-- Matt White, Executive Director, PyTorch Foundation. GM of AI, Linux Foundation
Training
Training remains a big focus of the team as we head into v5: whereas previously we would focus heavily on fine-tuning rather than pre-training/full-training at scale, we’ve recently done significant work to improve our support for the latter as well.
Pre-training at scale
Supporting pre-training meant reworking the initialization of our models, ensuring that they worked at scale with different parallelism paradigms, and shipping support for optimized kernels for both the forward and backward passes.
Going forward, we’re excited to have extended compatibility with torchtitan, megatron, nanotron, as well as any other pre-training tool that is interested in collaborating with us.
Fine-tuning & Post-training
We continue collaborating closely with all fine-tuning tools in the Python ecosystem. We aim to continue providing model implementations compatible with Unsloth, Axolotl, LlamaFactory, TRL and others in the PyTorch ecosystem; but we are also working with tools such as MaxText, in the JAX ecosystem, to have good interoperability between their frameworks and transformers.
All fine-tuning and post-training tools can now rely on transformers for model definitions; further enabling Agentic use-cases through OpenEnv or the Prime Environment Hub.
Inference
We’re putting a significant focus on inference for v5, with several paradigm changes: the introduction of specialized kernels, cleaner defaults, new APIs, support for optimized inference engines.
Similarly to training, we’ve been putting some effort in packaging kernels so that they’re automatically used in case your hardware and software permits it. If you haven’t heard of kernels before, we recommend taking a look at this doc.
Alongside this effort, we ship two new APIs dedicated to inference:
- We ship support for continuous batching and paged attention mechanisms. This has now been used internally for some time, and we’re working on finalizing the rough edges and writing usage guides.
- We introduce
transformers serveas the new transformers-specific serving system, which deploys an OpenAI API-compatible server.
We see this as a major step forward for use-cases such as evaluation, where a great number of inference requests are done simultaneously. We don’t aim to do specialized optimizations like the dedicated inference engines (vLLM, SGLang, TensorRT LLM). Instead, we aim to be perfectly inter-compatible with these, as detailed in the next section.
The Transformers backend in vLLM has been very enabling to get more architectures, like BERT and other encoders, available to more users. We've been working with the Transformers team to ensure many models are available across modalities with the best performance possible. This is just the start of our collaboration: we're happy to see the Transformers team will have this as a focus going into version 5.
-- Simon Mo, Harry Mellor at vLLM
Standardization is key to accelerating AI innovation. Transformers v5 empowers the SGLang team to spend less time on model reimplementation and more time on kernel optimization. We look forward to building a more efficient and unified AI ecosystem together!
-- Chenyang Zhao at SGLang
Production & Local
Recently, we've been working hand in hand with the most popular inference engines for them to use transformers as a backend. The value added is significant: as soon as a model is added to transformers, it becomes available in these inference engines, while taking advantage of the strengths each engine provides: inference optimizations, specialized kernels, dynamic batching, etc.
We've also been working very closely with ONNXRuntime, llama.cpp and MLX so that the implementations between transformers and these modeling libraries have great interoperability. For example, thanks to a significant community effort, it's now very easy to load GGUF files in transformers for further fine-tuning. Conversely, transformers models can be easily converted to GGUF files for use with llama.cpp.
The Transformers framework is the go-to place for reference AI model implementations. The framework plays a crucial role in enabling modern AI across the entire stack. The team and the community behind the project truly understand and embrace the spirit of the open-source development and collaboration.
-- Georgi Gerganov, ggml-org
The same is true for MLX, where the transformers' safetensors files are directly compatible with MLX's models.
It’s hard to overstate the importance of Transformers (and datasets, tokenizers, etc) to the open-source and overall AI ecosystem. I can’t count the number of times I’ve personally used Transformers as a source-of-truth.
-- Awni Hannun, MLX
Finally, we’re pushing the boundaries of local inference and are working hand-in-hand with the executorch team to get the transformers models to be available on-device. We’re expanding the coverage to multimodal models (vision, audio) through optimum.
Quantization
Quantization is quickly emerging as the standard for state-of-the-art model development. Many SOTA models are now released in low-precision formats such as 8-bit and 4-bit (e.g., gpt-oss, Kimi-K2, Deepseek-r1), hardware is increasingly optimized for low-precision workloads, and the community is actively sharing high-quality quantized checkpoints. In v5, we're making quantization a central focus of Transformers support, ensuring full compatibility with all major features, and delivering a reliable framework for training and inference.
We introduce a significant change to the way we load weights in our models; and with this, we move to quantization being a first-class citizen.
Our collaboration with the Transformers team was highly productive, marked by their proactive code reviews, feedback, and technical expertise. Their support was crucial in integrating TorchAO, expanding quantization features, and improving documentation for broader adoption in the V5.
-- Jerry Zhang at TorchAO
We're excited that v5 has made quantization a first-class citizen. It provides the foundation for bitsandbytes to better support key features like TP and MoEs, and also makes it easier to integrate new quantization methods.
-- Matthew Douglas & Titus von Koeller, bitsandbytes
Conclusion
The overarching theme of this version 5 release is “interoperability”. All refactors, performance improvements, and standardization are aligned with this theme. v5 plays nicely and end-to-end with the growing ecosystem: train a model with Unsloth/Axolotl/LlamaFactory/MaxText deploy it with vLLM/SGLang, and export it to llama.cpp/executorch/MLX to run locally!
Version 5 is undeniably an accomplishment of the past five years by a very large number of people in our community. We also see it as a promise, and as a beacon of the direction we want to go.
We took it as an opportunity to clean up the toolkit and isolate what mattered; we now have a clean slate on top of which to build. Thanks to the many changes from the community and team, improvements in performance, usability, and readability, will be simpler to ship.
Now that v5.0.0's first RC is out there, we'll be eagerly awaiting your feedback. Please check our release notes for all the technical details, and we'll be awaiting your feedback in our GitHub issues!