Torch and PyTorch

Last update: 2024-03-14 19:05:08 +0800 +0800
Available translations: 日本語 / 中文(简体) / 中文(繁體)

Torch was an early machine-learning framework with a Lua interface and a C/CUDA core. PyTorch is its successor ecosystem: it retains the name and some implementation heritage, but it is an actively developed framework centered on Python and C++ APIs. LibTorch is PyTorch’s C++ distribution.

Install PyTorch #

For most Python projects, install the published package using the command generated by the PyTorch installation selector. The appropriate package depends on the operating system, Python version, and whether the application uses CPU, CUDA, or another accelerator backend.

sh
python -m pip install torch torchvision

Verify the installation and, when applicable, that CUDA is available:

python
import torch

print(torch.__version__)
print(torch.cuda.is_available())

Build LibTorch from Source #

Building from source is usually unnecessary; use a prebuilt package or LibTorch archive unless a custom build is required. A source build needs a compatible C++ compiler, CMake, Ninja, Python tooling, and any accelerator SDK selected for the build. Clone the release and its submodules, then follow the build instructions for that release:

sh
git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
git checkout <release-tag>
git submodule sync
git submodule update --init --recursive
cmake -S . -B build -GNinja -DBUILD_PYTHON=OFF
cmake --build build

Build flags and supported compilers change across releases, so do not copy a stale command line into production automation without checking the release documentation.

Tensors #

A torch.Tensor is a homogeneous multidimensional array with a shape, data type, and device. Unlike a typical NumPy array, a tensor can reside on a CPU or an accelerator and can participate in automatic differentiation.

python
import torch

x = torch.randn(3, 4, device="cpu")
y = x.mean(dim=1)

Some operations return views over existing storage rather than copying data. A view can be non-contiguous—for example, after a transpose—so operations that require contiguous storage may need tensor.contiguous().

Building Modules #

Neural networks are typically composed by subclassing torch.nn.Module. Register parameters and child modules in __init__, then describe the forward computation in forward:

python
import torch

class MyLinear(torch.nn.Module):
    def __init__(self, in_features: int, out_features: int) -> None:
        super().__init__()
        self.weight = torch.nn.Parameter(torch.randn(in_features, out_features))
        self.bias = torch.nn.Parameter(torch.zeros(out_features))

    def forward(self, inputs: torch.Tensor) -> torch.Tensor:
        return inputs @ self.weight + self.bias

Calling a module invokes framework-managed logic around forward, including hooks. Prefer model(inputs) over calling model.forward(inputs) directly.

Checkpoints and Interoperability #

Save model parameters with state_dict() and load them only from trusted sources. PyTorch serialization can use Python pickle, which can execute code during deserialization. For untrusted model weights, prefer safer formats such as safetensors where the tooling supports them.

LibTorch exposes C++ APIs for embedding PyTorch models in native applications, but Python and C++ artifacts must be produced with compatible versions and APIs. Test the exact inference path, device placement, and input preprocessing used in deployment.

Comments

Read or comment on GitHub (sign-in required to comment)