# PyTorch

**PyTorch** 是一个开源 Python 库，用于张量计算并具备强大的 GPU 加速能力，同时可在基于磁带的自动微分系统上构建深度神经网络。<sup>[1](https://github.com/pytorch/pytorch)</sup> 它随 Python 运行即时执行代码，记录计算梯度所需运算，已成为人工智能研究的主要平台之一。<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup>

| 关键事实 | 详情 |
|---|---|
| 核心设计 | 类似 NumPy 的张量计算并支持 GPU 加速，外加在即时执行过程中以磁带方式记录的反向模式自动微分<sup>[1](https://github.com/pytorch/pytorch)</sup><sup> • </sup><sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup> |
| 发布时间 | 2017 年 1 月，设计上受到 Chainer 和 torch-autograd 的影响<sup>[4](https://soumith.ch/blog/2022-01-19-pytorch-retro.md.html)</sup><sup> • </sup><sup>[1](https://github.com/pytorch/pytorch)</sup> |
| 研究占比 | 2023 年 NeurIPS 论文中明确指定框架的，约 80% 使用了 PyTorch；超过 70% 的人工智能研究实现使用它<sup>[5](https://arxiv.org/abs/2508.04035)</sup><sup> • </sup><sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup> |
| 训练采用率 | PyTorch 2024 年度回顾报告显示，模型训练采用率为 63%<sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup> |
| 规模 | 截至基金会成立公告时，贡献者超过 2,400 名，基于它构建的项目近 154,000 个<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup> |
| 治理 | 2023 年起成为 Linux 基金会下属顶级项目，归属 PyTorch 基金会，董事会成员来自 AMD、AWS、Google Cloud、Meta、Microsoft Azure 和 NVIDIA<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup> |
| 编译 | PyTorch 2.0 新增 torch.compile，在保留即时执行的同时对代码进行即时编译<sup>[5](https://arxiv.org/abs/2508.04035)</sup> |

## PyTorch 是什么

该软件包提供两大高级特性：类似 NumPy 的张量计算并具备强大的 GPU 加速能力，以及基于磁带式自动微分系统构建的深度神经网络。<sup>[1](https://github.com/pytorch/pytorch)</sup> 张量可驻留于 CPU 或 GPU，在 GPU 硬件上计算可获得大幅加速。<sup>[1](https://github.com/pytorch/pytorch)</sup> 对 Google TPU 的支持通过 PyTorch/XLA 实现。<sup>[7](https://www.georgeho.org/tensor-computation-libraries/)</sup>

其核心组件包括 torch（张量库）、torch.autograd（微分）、torch.nn（神经网络层）、torch.jit（[TorchScript](https://www.edgepedia.org/zh/torchscript) 编译）、torch.multiprocessing 和 torch.utils。<sup>[1](https://github.com/pytorch/pytorch)</sup> 设计上借鉴了 torch-autograd、autograd 和 Chainer 项目。<sup>[1](https://github.com/pytorch/pytorch)</sup>

## 历史

PyTorch 于 2017 年 1 月公开发布；据共同创造者 苏米斯·钦塔拉（Soumith Chintala）在五周年时的说法，其使用规模已远超创造者预期。<sup>[4](https://soumith.ch/blog/2022-01-19-pytorch-retro.md.html)</sup> 早期团队构建了一个追踪器，用于将 PyTorch 模型导出为中间表示，这便是其部署故事的起点。<sup>[4](https://soumith.ch/blog/2022-01-19-pytorch-retro.md.html)</sup>

**1.0 合并。** PyTorch 1.0 将 Caffe2 和 ONNX 面向生产的模块化能力与 PyTorch 灵活、面向研究的设计相结合，提供可在命令式与声明式执行之间切换的混合前端。<sup>[8](https://engineering.fb.com/2018/05/02/ai-research/announcing-pytorch-1-0-for-both-research-and-production/)</sup> 该公告发布时，Caffe2 运行在超过 10 亿部手机上，每天提供超过 200 万亿次预测；既定策略是研究用 PyTorch、生产用 Caffe2，两者通过 ONNX 转换模型，而 ONNX 作为导出格式被原生织入 PyTorch 1.0。<sup>[8](https://engineering.fb.com/2018/05/02/ai-research/announcing-pytorch-1-0-for-both-research-and-production/)</sup>

**离开 Meta。** 2023 年，PyTorch 迁入 [Linux 基金会](https://www.edgepedia.org/zh/linux-foundation)，成为名为 PyTorch 基金会的顶级项目。<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup> 一项比较性学术调查将这一迁移定在 2022 年；官方项目公告则定为 2023 年。<sup>[5](https://arxiv.org/abs/2508.04035)</sup>

## 工作原理

**张量与自动微分。** PyTorch 采用基于运算符重载的反向模式自动微分：每次执行计算时，该库都会构建所计算函数的表示，因为 Python 的动态运行时使得提前的源到源微分颇为繁琐。<sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup> 反向模式计算标量输出对多元输入的梯度；该设计还可通过数组级对偶数扩展至前向模式微分。<sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup> 实践中，对结果节点调用一次 backward() 即可通过图中中间节点计算梯度。<sup>[7](https://www.georgeho.org/tensor-computation-libraries/)</sup> PyTorch 依赖与 Python 自身引用计数集成的引用计数，在使用计数归零时管理张量内存。<sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup>

**即时执行及其取舍。** 动态的“边定义边运行”框架仅通过运行计算来定义待微分函数，而非预先指定静态图、符号化地提前微分并多次运行。<sup>[9](https://openreview.net/references/pdf?id=BJJsrmfCZ)</sup> 即时框架在遇到张量计算时便执行，仅记录微分该计算所需的内容。<sup>[9](https://openreview.net/references/pdf?id=BJJsrmfCZ)</sup> 即时执行允许 CPU 与 GPU 工作流水线化，但放弃了全网优化与批处理机会。<sup>[9](https://openreview.net/references/pdf?id=BJJsrmfCZ)</sup>

**torch.compile。** PyTorch 2.0 引入 torch.compile，构建于 TorchDynamo 之上，在不脱离即时执行范式的前提下对代码进行即时编译；早期基准测试显示某些模型有显著加速。<sup>[5](https://arxiv.org/abs/2508.04035)</sup> 这赋予 PyTorch 三种执行风格：用于交互式开发与调试的普通即时模式、用于性能的编译图模式，以及用于序列化的较旧 TorchScript 路径。<sup>[5](https://arxiv.org/abs/2508.04035)</sup><sup> • </sup><sup>[10](https://github.com/pytorch/pytorch/releases/tag/v1.0.0)</sup>

## 部署路径

PyTorch 1.0 通过 torch.jit.trace 和 torch.jit.script 引入 TorchScript：代码一旦标注，便可积极优化并序列化，供完全不依赖 Python 的 C++ API 使用。<sup>[10](https://github.com/pytorch/pytorch/releases/tag/v1.0.0)</sup> 随着 PyTorch 从研究扩展到生产，它还增加了用于模型服务的 TorchServe。<sup>[5](https://arxiv.org/abs/2508.04035)</sup> 自 1.0 起，ONNX 一直作为桥接研究与生产运行时的导出格式。<sup>[8](https://engineering.fb.com/2018/05/02/ai-research/announcing-pytorch-1-0-for-both-research-and-production/)</sup>

对于边缘设备，2023 年 PyTorch 大会宣布的 [ExecuTorch](https://www.edgepedia.org/zh/executorch) 提供紧凑运行时，带有覆盖 PyTorch 模型生态的轻量级算子注册表，可从高端手机移植到高度受限的嵌入系统和微控制器。<sup>[11](https://www.linuxfoundation.org/blog/-pytorch-conference-2023-news)</sup> 2024 年，ExecuTorch 与 Arm、Apple、Qualcomm 和 MediaTek 等合作伙伴发布了 alpha 和 beta 版本。<sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup>

## 与 TensorFlow 和 JAX 的比较

此前的 Caffe、CNTK、[TensorFlow](https://www.edgepedia.org/zh/tensorflow) 和 Theano 等框架使用静态数据流图，以易用性和调试性换取潜在性能；PyTorch 的“边定义边运行”即时执行避免了这一代价。<sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup> 2019 年发布的 TensorFlow 2.x 默认切换为即时执行，实质上采用了类似 PyTorch 的动态图模式，以回应开发者反馈与竞争；截至 2024 年，两个框架都支持动态与静态模式，TensorFlow 默认采用动态模式，并提供可选的 @tf.function 图追踪。<sup>[5](https://arxiv.org/abs/2508.04035)</sup>

JAX 走的是另一条路：它提供 grad() 函数，返回任意给定纯函数的梯度，没有显式计算图概念，并通过 XLA 为 GPU 和 TPU 即时编译类 NumPy 代码，API 包含 grad、jit、vmap 和 pmap 四个函数。<sup>[7](https://www.georgeho.org/tensor-computation-libraries/)</sup> Google 开发 JAX 时的设计是将大问题的工作自动分布到多个芯片上，而非为不同芯片运行单独的代码片段，此时 PyTorch 已超越 TensorFlow。<sup>[12](https://www.businessinsider.com/facebook-pytorch-beat-google-tensorflow-jax-meta-ai-2022-6)</sup>

在实测速度方面，2019 年 NeurIPS 论文将 PyTorch 与 CNTK、MXNet、TensorFlow、Chainer 和 [PaddlePaddle](https://www.edgepedia.org/zh/paddlepaddle) 进行基准比较，发现 PyTorch 在所有基准上与最快框架的性能差距均不超过 17%，并将这种接近归因于所有工具都将计算卸载到相同的 cuDNN 和 cuBLAS 库。<sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup>

## 数据一览

- 2023 年，PapersWithCode 在约 80% 明确指定框架的 NeurIPS 论文中追踪到 PyTorch。<sup>[5](https://arxiv.org/abs/2508.04035)</sup>
- PyTorch 2024 年度回顾报告显示模型训练采用率为 63%，并引用 2024 年 Linux 基金会年度报告的发现：超过 70% 的人工智能研究实现使用了 PyTorch。<sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup>
- 2019 年，296 篇 ICLR 投稿提及 PyTorch，统计方式为计算 arXiv 电子预印本中的框架提及次数。<sup>[3](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)</sup>
- 截至基金会成立公告时，PyTorch 拥有超过 2,400 名贡献者和近 154,000 个基于它构建的项目。<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup>
- 一项比较性学术研究统计了 2,792 名 PyTorch 与 3,288 名 TensorFlow 代码提交作者，以及 9,826 与 19,750 名 GitHub 议题事件参与者。<sup>[13](https://scholars.cityu.edu.hk/en/publications/understanding-the-oss-communities-of-deep-learning-frameworks-a-c/)</sup>
- Stack Overflow 2023 年开发者调查发现，8.41% 的开发者使用 TensorFlow，7.89% 使用 PyTorch，TensorFlow 在工业场景中略占优势。<sup>[5](https://arxiv.org/abs/2508.04035)</sup>
- 2024 年，贡献量同比增长 133%，贡献组织数量翻倍；使用 PyTorch 的新仓库同比增长 20%，分支和用户数增长 30%。<sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup>

研究与工业界的图景有所不同：PyTorch 在明确指定框架的论文中占主导，而一般开发者调查显示两个框架接近，这一差距部分可由 TensorFlow 在工业部署中更大的装机量解释。<sup>[5](https://arxiv.org/abs/2508.04035)</sup><sup> • </sup><sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup>

## 治理与近期发展

PyTorch 基金会治理董事会汇集了来自 AMD、Amazon Web Services、Google Cloud、Meta、Microsoft Azure 和 NVIDIA 的领导者；技术决策仍由个体维护者负责，而业务决策由董事会做出。<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup> 项目的技术治理从松散结构的社区模式演变为层级化维护者结构，PyTorch 被斯坦福、纽约大学、洛桑联邦理工学院和牛津等高校课程使用。<sup>[2](https://pytorch.org/blog/PyTorchfoundation/)</sup> 仓库列出的具名维护者为 苏米斯·钦塔拉（Soumith Chintala）、Gregory Chanan、Dmytro Dzhulgakov、Edward Yang、Alban Desmaison、Piotr Bialecki 和 Nikita Shulga，并有数百人参与贡献。<sup>[1](https://github.com/pytorch/pytorch)</sup>

**2023 年末以来的发布。** 2024 年有四个里程碑版本：2.2、2.3、2.4 和 2.5，特性包括 AOTInductor、FlashAttention-2 支持、张量并行、新的 Python 自定义算子 API 和 FlexAttention。<sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup> 同年，ExecuTorch 与硬件合作伙伴发布 alpha 和 beta 版本，TorchTune、TorchAO 和 TorchCodec 相继推出，TorchRec 1.0 成为其首个稳定版本。<sup>[6](https://pytorch.org/blog/2024-year-in-review/)</sup>

## 局限

已记录的取舍是结构性的，而非偶然的。即时执行放弃了静态图可利用的全网优化与批处理机会，这正是 torch.compile 存在的原因——在保留即时编程模型的同时恢复图级优化。<sup>[9](https://openreview.net/references/pdf?id=BJJsrmfCZ)</sup><sup> • </sup><sup>[5](https://arxiv.org/abs/2508.04035)</sup> 超越 Python 的部署历来需要单独的表示，先是 TorchScript，如今是 torch.export 时代的工具和 ExecuTorch，因为即时模型与 Python 解释器绑定。<sup>[10](https://github.com/pytorch/pytorch/releases/tag/v1.0.0)</sup><sup> • </sup><sup>[11](https://www.linuxfoundation.org/blog/-pytorch-conference-2023-news)</sup>

## References

1. [pytorch/pytorch repository README, GitHub](https://github.com/pytorch/pytorch)
2. [PyTorch strengthens its governance by joining the Linux Foundation, PyTorch blog](https://pytorch.org/blog/PyTorchfoundation/)
3. [Paszke et al. (2019). PyTorch: An Imperative Style, High-Performance Deep Learning Library, NeurIPS](https://proceedings.neurips.cc/paper/2019/file/bdbca288fee7f92f2bfa9f7012727740-Paper.pdf)
4. [Soumith Chintala (2022). Decisions and Pivots on PyTorch, a 5-year retrospective](https://soumith.ch/blog/2022-01-19-pytorch-retro.md.html)
5. [A Comparative Survey of PyTorch vs TensorFlow for Deep Learning, arXiv:2508.04035](https://arxiv.org/abs/2508.04035)
6. [PyTorch Grows as the Dominant Open Source Framework for AI and ML: 2024 Year in Review, PyTorch blog](https://pytorch.org/blog/2024-year-in-review/)
7. [George Ho. What I Wish Someone Had Told Me About Tensor Computation Libraries](https://www.georgeho.org/tensor-computation-libraries/)
8. [Announcing PyTorch 1.0 for both research and production, Meta Engineering](https://engineering.fb.com/2018/05/02/ai-research/announcing-pytorch-1-0-for-both-research-and-production/)
9. [Automatic differentiation in PyTorch, OpenReview](https://openreview.net/references/pdf?id=BJJsrmfCZ)
10. [PyTorch 1.0.0 release notes, GitHub](https://github.com/pytorch/pytorch/releases/tag/v1.0.0)
11. [News from PyTorch Conference 2023, Linux Foundation](https://www.linuxfoundation.org/blog/-pytorch-conference-2023-news)
12. [How PyTorch Beat TensorFlow, Forcing Google to Bet on JAX, Business Insider](https://www.businessinsider.com/facebook-pytorch-beat-google-tensorflow-jax-meta-ai-2022-6)
13. [Understanding the OSS Communities of Deep Learning Frameworks: A Comparative Case Study of PyTorch and TensorFlow, TOSEM](https://scholars.cityu.edu.hk/en/publications/understanding-the-oss-communities-of-deep-learning-frameworks-a-c/)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Sep 30, 2026; Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Machine learning and neural computation › Neural networks and deep learning › Deep learning software and hardware › Deep learning frameworks and libraries

Copyright 2026 Edgepedia

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

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

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