Favicon of pytorch

pytorch

PyTorch: a Python library for GPU-accelerated tensors and dynamic neural networks with tape-based autograd.

PyTorch is a Python library for tensor computation and deep learning. It gives you NumPy-like tensors that run on CPU or GPU, plus a tape-based autograd system for building and training neural networks. It's aimed at researchers and engineers who want a Python-first, imperative framework rather than a static-graph compiler wrapped in Python bindings.

Key features

  • GPU-accelerated tensors: a tensor library like NumPy, with indexing, slicing, linear algebra, and reductions that run fast on CPU or GPU.
  • Dynamic autograd: reverse-mode automatic differentiation built by recording operations as you run them, so you can change network behavior at runtime with no extra overhead.
  • torch.nn: a neural network module library tightly integrated with autograd, built for flexibility rather than a fixed graph structure.
  • torch.jit / TorchScript: a compilation stack for turning PyTorch models into serializable, optimizable artifacts.
  • torch.multiprocessing: Python multiprocessing with shared-memory tensors across processes, useful for data loading and Hogwild-style training.
  • C/C++ extension API: write custom layers in Python or drop into C/C++ with minimal boilerplate when you need lower-level control.
  • Backend integration: uses Intel MKL and NVIDIA cuDNN/NCCL under the hood, plus custom GPU memory allocators for efficient memory use.
  • Straightforward debugging: code executes line by line, so stack traces point to the actual location in your Python code.

Ideal use cases

PyTorch fits deep learning research and production model development where you need to change network architecture or control flow at runtime, for example RNNs with variable-length sequences, models with conditional branching, or rapid experimentation with new layer types. It also works well as a general GPU-accelerated tensor library if you're doing numerical computing beyond NumPy's CPU limits.

It's a good match if you want Python-native debugging, direct interop with NumPy/SciPy/Cython, and the ability to write custom autograd-compatible layers without wrapper code.

It's less of a fit if you need a fully static, ahead-of-time compiled graph for constrained embedded deployment without any Python runtime (though TorchScript covers part of that gap), or if your team is standardized on another framework's ecosystem (TensorFlow, JAX) and switching cost outweighs the benefit. Building from source also demands real setup time and disk space, so if you just want a quick install, use the prebuilt binaries instead.

Installation

The simplest path is installing prebuilt binaries via pip or Conda, with exact commands generated at pytorch.org/get-started/locally based on your OS, package manager, and CUDA version.

To build from source, first clone the repo and its submodules:

git clone https://github.com/pytorch/pytorch
cd pytorch
git submodule sync
git submodule update --init --recursive

Prerequisites: Python 3.10+, a C++20-capable compiler (gcc 11.3.0+ on Linux), at least 10 GB free disk space, and 30-60 minutes for the initial build. On Linux, set up a virtual environment (Conda or a tool like uv), then install dependencies:

pip install --group dev
pip install mkl-static mkl-include
# optional CUDA LAPACK support
.ci/docker/common/install_magma_conda.sh 12.4
# optional, for torch.compile with inductor/triton
make triton

Then build and install in editable mode:

export CMAKE_PREFIX_PATH="${CONDA_PREFIX:-'$(dirname $(which conda))/../'}:${CMAKE_PREFIX_PATH}"
python -m pip install --no-build-isolation -v -e .

For CUDA support, install NVIDIA CUDA and cuDNN (v9.0+) first, or set USE_CUDA=0 to disable it. For AMD GPUs, install ROCm 4.0+ and run python tools/amd_build/build_amd.py before the install step, or set USE_ROCM=0 to disable it. Intel GPU support is enabled via USE_XPU=1 and disabled with USE_XPU=0. Docker images and NVIDIA Jetson wheels are also available for platforms where building from source isn't practical.

Frequently asked questions

Share:

Stars
101.6K
Forks
28.3K
Last commit
2 hours ago
Repository age
10 years
Self-hosted
No
Activity score
91/100
View Repository
Built with:

Similar to pytorch

Favicon

 

  
  
Favicon

 

  
  
Favicon