# fairseq

**fairseq** 是一个开源的序列建模工具包，最初由 Facebook 人工智能研究院（FAIR）开发，让研究人员和开发者能够为翻译、摘要、语言建模及其他文本生成任务训练自定义模型。当前的 Python 实现基于 PyTorch 构建，支持跨多个 GPU 和机器的分布式训练，并可在现代 GPU 上进行快速混合精度训练与推理。<sup>[1](https://aclanthology.org/N19-4009/)</sup>

| 关键事实 | 详情 |
|---|---|
| 是什么 | 用于翻译、摘要、语言建模及其他文本生成任务的开源序列建模工具包<sup>[1](https://aclanthology.org/N19-4009/)</sup> |
| 首次发布 | 2017 年 5 月，作为面向卷积神经机器翻译模型的 Lua/Torch 工具包发布<sup>[2](https://engineering.fb.com/2017/05/09/ml-applications/a-novel-approach-to-neural-machine-translation/)</sup> |
| 参考论文 | "fairseq: A Fast, Extensible Toolkit for Sequence Modeling"，Ott 等人，NAACL 2019（演示环节），第 48–53 页<sup>[1](https://aclanthology.org/N19-4009/)</sup> |
| 分布式训练 | 基于 torch.distributed 和 NCCL2 库构建；同步优化，每个 GPU 持有一份模型副本<sup>[3](https://aclanthology.org/N19-4009.pdf)</sup> |
| 精度 | 通过 --fp16 标志进行 FP16（半精度）训练，需要 Volta GPU 及 CUDA 9.1 或更高版本<sup>[4](https://fairseq.readthedocs.io/en/latest/getting_started.html)</sup> |
| 许可证 | BSD 许可，包括预训练模型，并附加专利授权<sup>[5](https://github.com/facebookresearch/fairseq-lua)</sup> |
| 状态 | facebookresearch/fairseq 仓库已于 2026 年 3 月 20 日归档，现为只读；开发工作转至 fairseq2 继续<sup>[6](https://github.com/facebookresearch/fairseq)</sup><sup> • </sup><sup>[7](https://github.com/facebookresearch/fairseq2)</sup> |

## 历史

该工具包最初是一个为神经机器翻译量身定制的 Lua/[Torch7](https://www.edgepedia.org/zh/torch-machine-learning) 项目。2017 年 5 月，FAIR 团队发布了用于语言翻译的卷积神经网络（CNN）模型，其准确率达到当时最佳水平，速度是循环神经网络系统的九倍，并以开源许可证在 GitHub 上发布了 fairseq 源代码和训练好的系统。<sup>[2](https://engineering.fb.com/2017/05/09/ml-applications/a-novel-approach-to-neural-machine-translation/)</sup> 最初的工具包实现了 Gehring 等人（2017）的卷积序列到序列模型和一个卷积编码器模型，外加一个标准的基于 LSTM 的模型，支持单机多 GPU 训练以及在 CPU 和 GPU 上的快速束搜索（生成译文时保留若干最佳部分输出）生成；预训练模型涵盖英语到法语、德语和罗马尼亚语。<sup>[5](https://github.com/facebookresearch/fairseq-lua)</sup> 该 Lua 版本已归档，不再开发，用户被引导使用 Python 实现。<sup>[5](https://github.com/facebookresearch/fairseq-lua)</sup>

该工具包用 PyTorch 重写（常被称为 fairseq-py），并在 Myle Ott、Sergey Edunov、Alexei Baevski、Angela Fan、Sam Gross、Nathan Ng、David Grangier 和 Michael Auli 于 2019 年在明尼苏达州[明尼阿波利斯](https://www.edgepedia.org/zh/minneapolis)举行的 NAACL 2019 上发表的一篇演示论文中进行了描述。<sup>[1](https://aclanthology.org/N19-4009/)</sup> 论文列出了四项主要特性：跨模型和任务的通用接口，可通过用户提供的插件进行扩展；高效的分布式和混合精度训练，使得在数亿句子的数据集上训练成为可能；机器翻译、摘要和语言建模的最先进实现与预训练模型；以及支持束搜索、多样化束搜索和 top-k 采样的优化推理。<sup>[3](https://aclanthology.org/N19-4009.pdf)</sup>

## 架构与训练特性

**可扩展性。** 模型定义神经网络架构并封装所有可学习参数，继承 BaseFairseqModel 类，而该类又继承 torch.nn.Module，因此任何 fairseq 模型都可以作为独立模块在其他 PyTorch 代码中使用。该工具包支持五种用户提供的插件类型，以便在复用现有组件的同时试验新想法。<sup>[3](https://aclanthology.org/N19-4009.pdf)</sup>

**多 GPU 与多机训练。** fairseq 使用 NCCL2 库和 torch.distributed 进行 GPU 间通信。训练采用同步优化设置，每个 GPU 持有一份模型副本并处理一个子批次的数据，随后在各 GPU 之间同步梯度；所有子批次共同构成一个小批次。<sup>[3](https://aclanthology.org/N19-4009.pdf)</sup> 由于令牌数相近的子批次在处理时间上仍存在较大差异，该工具包将反向传播与梯度同步重叠进行，并支持累积梯度更新以减少空闲时间和通信开销。<sup>[3](https://aclanthology.org/N19-4009.pdf)</sup> --update-freq 选项在更新前累积多个小批次的梯度，从而形成更大的有效批次大小；在单个 GPU 上使用 --update-freq 8 进行训练近似于八 GPU 批次。<sup>[4](https://fairseq.readthedocs.io/en/latest/getting_started.html)</sup>

**混合精度。** 通过 --fp16 标志启用 FP16 训练，需要 Volta 级 GPU 和 CUDA 9.1 或更高版本，利用 Nvidia Tensor Core 进行高效的半精度计算。<sup>[4](https://fairseq.readthedocs.io/en/latest/getting_started.html)</sup>

**大型数据集。** 大多数任务支持在分片数据集上训练，数据被预处理为互不重叠的块（data-bin1、data-bin2 等），训练时逐一遍历，从而减少系统内存占用。<sup>[4](https://fairseq.readthedocs.io/en/latest/getting_started.html)</sup> [检查点](https://www.edgepedia.org/zh/checkpointing)机制的设计使得中断的训练在恢复时可复现，而用旧版本工具包训练的模型通过自动检查点升级可在最新版本上继续运行。<sup>[3](https://aclanthology.org/N19-4009.pdf)</sup>

## 模型与应用

fairseq 附带的实现涵盖卷积网络（包括卷积序列到序列学习和门控卷积网络语言建模）、Transformer，以及语音模型如用于语音识别无监督预训练的 wav2vec。<sup>[6](https://github.com/facebookresearch/fairseq)</sup> 预训练的翻译和语言建模模型可通过 torch.hub 接口获取；例如，加载 transformer.wmt19.en-de.single_model 并调用 translate('Hello world', beam=5) 返回 'Hallo Welt'。<sup>[6](https://github.com/facebookresearch/fairseq)</sup> fairseq 团队使用回译数据进行半监督训练所产生的集成模型赢得了 WMT'18 英德新闻翻译竞赛，而噪声信道重排序集成模型赢得了 WMT'19 赛事。<sup>[8](https://pytorch.org/hub/pytorch_fairseq_translation/)</sup>

两个扩展将工具包的应用范围拓展到了文本之外。fairseq S2T 于 2020 年推出，增加了用于端到端语音识别和语音到文本翻译的语音到文本建模，实现了基于 RNN、基于 Transformer 和基于 Conformer 的模型，并提供从预处理到训练再到推理的端到端工作流；fairseq 的机器翻译和语言模型可以集成到 S2T 工作流中，用于多任务或迁移学习。<sup>[9](https://arxiv.org/abs/2010.05171)</sup> wav2vec 2.0 是由 Alexei Baevski、Henry Zhou、Abdelrahman Mohamed 和 Michael Auli 提出的语音表征自监督学习框架，于 2020 年 8 月通过 fairseq 发布了其模型和代码。<sup>[10](https://arxiv.org/abs/2006.11477)</sup><sup> • </sup><sup>[6](https://github.com/facebookresearch/fairseq)</sup> fairseq 还被广泛用于共享任务系统；例如在 WMT 2020 上，Facebook 和 OPPO 的新闻翻译提交均使用它构建。<sup>[11](https://www.statmt.org/wmt20/book.pdf)</sup>

## 维护状态与 fairseq2

2022 年 6 月，维护者宣布了 fairseq v2 项目，目标包括：利用当前 PyTorch 特性的干净、经过充分测试的代码库；保留原有的性能优化；通过定义良好的 API 将用户代码与库代码分离；以及用于大规模训练和推理的新容错工具；在此期间，原仓库仅接受关键错误修复和小型功能改进。<sup>[12](https://github.com/facebookresearch/fairseq/issues/4493)</sup> 继任者 fairseq2 是面向相同任务族的序列建模工具包，可通过 pip 在 Linux x86-64 和 Apple 芯片 Mac 上安装，不支持原生 Windows（建议改用 WSL）。<sup>[7](https://github.com/facebookresearch/fairseq2)</sup> fairseq2 依赖 PyTorch 的 C++ API，而该 API 在不同版本之间没有 API/ABI 兼容性，因此安装的 fairseq2 变体必须与 PyTorch 版本完全匹配。<sup>[7](https://github.com/facebookresearch/fairseq2)</sup> 它包含一个 LLaMA 模块，为 [Meta AI](https://www.edgepedia.org/zh/meta-ai) 的 LLaMA 语言模型提供模型配置、分词器和 hub 访问。<sup>[13](https://facebookresearch.github.io/fairseq2/stable/reference/fairseq2.models.llama.html)</sup> 原始的 facebookresearch/fairseq 仓库拥有 2,330 次提交，发布版本直至 v0.12.2，已于 2026 年 3 月 20 日归档，现为只读。<sup>[6](https://github.com/facebookresearch/fairseq)</sup><sup> • </sup><sup>[14](https://github.com/facebookresearch/fairseq/releases)</sup>

## References

1. [Myle Ott, Sergey Edunov, Alexei Baevski, Angela Fan, Sam Gross, Nathan Ng, David Grangier, and Michael Auli (2019). fairseq: A Fast, Extensible Toolkit for Sequence Modeling. NAACL 2019 (Demonstrations), pages 48–53.](https://aclanthology.org/N19-4009/)
2. [A novel approach to neural machine translation, Engineering at Meta (May 9, 2017)](https://engineering.fb.com/2017/05/09/ml-applications/a-novel-approach-to-neural-machine-translation/)
3. [fairseq: A Fast, Extensible Toolkit for Sequence Modeling (PDF)](https://aclanthology.org/N19-4009.pdf)
4. [Evaluating Pre-trained Models, fairseq 0.12.2 documentation](https://fairseq.readthedocs.io/en/latest/getting_started.html)
5. [facebookresearch/fairseq-lua, GitHub](https://github.com/facebookresearch/fairseq-lua)
6. [facebookresearch/fairseq, GitHub](https://github.com/facebookresearch/fairseq)
7. [facebookresearch/fairseq2, GitHub](https://github.com/facebookresearch/fairseq2)
8. [Transformer (NMT), PyTorch Hub](https://pytorch.org/hub/pytorch_fairseq_translation/)
9. [fairseq S2T: Fast Speech-to-Text Modeling with fairseq, arXiv:2010.05171](https://arxiv.org/abs/2010.05171)
10. [wav2vec 2.0: A Framework for Self-Supervised Learning of Speech Representations, arXiv:2006.11477](https://arxiv.org/abs/2006.11477)
11. [Proceedings of the Fifth Conference on Machine Translation (WMT20)](https://www.statmt.org/wmt20/book.pdf)
12. [fairseq v2, Issue #4493, facebookresearch/fairseq](https://github.com/facebookresearch/fairseq/issues/4493)
13. [fairseq2.models.llama, fairseq2 Documentation](https://facebookresearch.github.io/fairseq2/stable/reference/fairseq2.models.llama.html)
14. [Releases, facebookresearch/fairseq](https://github.com/facebookresearch/fairseq/releases)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Language and vision AI › Natural language processing › NLP software, people, and community › NLP toolkits and libraries

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/fairseq

English original: https://www.edgepedia.org/fairseq
