Files
Svetlana Karslioglu 19da3da3d3 Restructure C++ docs (#174096)
The fix-cpp-build branch is a major restructuring that:

- Drops Exhale (which auto-generated API docs but caused OOM in CI) in favor of hand-written RST pages using Breathe directives
- Reorganizes notes into a hierarchical API reference with proper categories
- Adds new content: XPU docs, stable ABI docs, DeviceGuard/Stream docs
- Always runs Doxygen instead of gating on an env var

Pull Request resolved: https://github.com/pytorch/pytorch/pull/174096
Approved by: https://github.com/janeyx99, https://github.com/seemethere, https://github.com/albanD
2026-04-07 17:24:38 +00:00

2.7 KiB

myst
myst
html_meta
description keywords
Module containers in PyTorch C++ — Sequential, ModuleList, ModuleDict for composing neural networks. PyTorch, C++, Sequential, ModuleList, ModuleDict, container, module

Containers

Container modules hold other modules and define how they are composed together. Use containers to build complex architectures from simpler building blocks.

  • Sequential: Chain modules in order, output of one feeds into the next
  • ModuleList: Store modules in a list for iteration (not auto-forwarded)
  • ModuleDict: Store modules in a dictionary for named access
  • ParameterList/ParameterDict: Store parameters directly without wrapping in modules

PyTorch's C++ API uses the PIMPL (Pointer to Implementation) pattern. You create
modules using the public class name (e.g., `torch::nn::Sequential`), which
internally wraps an implementation class (`SequentialImpl`). The documentation
below shows the implementation classes, which contain all the actual methods.

Sequential

Sequential is a container that chains modules together. Each module's output becomes the next module's input. This is the simplest way to build feed-forward networks.

:members:
:undoc-members:

Example:

torch::nn::Sequential seq(
    torch::nn::Conv2d(torch::nn::Conv2dOptions(1, 32, 3)),
    torch::nn::ReLU(),
    torch::nn::Conv2d(torch::nn::Conv2dOptions(32, 64, 3)),
    torch::nn::ReLU()
);

auto output = seq->forward(input);

ModuleList

ModuleList stores modules in a list for indexed or iterated access. Unlike Sequential, it does not have a built-in forward() method—you control how modules are called.

:members:
:undoc-members:

Example:

torch::nn::ModuleList layers;
layers->push_back(torch::nn::Linear(10, 20));
layers->push_back(torch::nn::Linear(20, 30));

torch::Tensor x = input;
for (const auto& layer : *layers) {
    x = layer->as<torch::nn::Linear>()->forward(x);
}

ModuleDict

ModuleDict stores modules in a dictionary for named access. Useful when you need to select modules by name at runtime.

:members:
:undoc-members:

ParameterList

ParameterList stores parameters directly without wrapping them in modules.

:members:
:undoc-members:
:members:
:undoc-members:

ParameterDict

ParameterDict stores parameters in a dictionary for named access.

:members:
:undoc-members:
:members:
:undoc-members: