In PyTorch 2.14, what do torch.optim.AdamW's foreach and fused flags change, and which implementation runs when you set neither?
answer
- one update, three launch patterns
- horizontal versus vertical fusion
- unset flags on CUDA
- fused has to be asked for
- both True is an error
basics
~20 sAdamW can run as a per-parameter for-loop, a foreach multi-tensor version or a fused kernel. With both flags None on CUDA, PyTorch 2.14 picks foreach; fused=True must be passed explicitly and is the fastest of the three.
solid answer
~40 sThe flags choose how the same AdamW update is launched. The **for-loop** runs several kernels per parameter tensor; **foreach** (`foreach=True`) groups tensors into lists and runs each piece of the update across all of them at once — horizontal fusion; **fused** (`fused=True`) also fuses the steps of the update into one kernel — vertical on top of horizontal. With both left as `None`, PyTorch 2.14 picks foreach when every parameter is a supported tensor type on a device with foreach kernels, CUDA included, and never picks fused on its own. Passing both as `True` raises a `RuntimeError`; `fused=True` requires floating-point parameters on a supported device and rejects `differentiable=True`. foreach costs about one parameter-sized copy of extra peak memory. In a profile the for-loop step shows as thousands of tiny kernels.
go deeper
Recall the three implementations — for-loop, foreach, fused — and that fused must be requested with fused=True.
Explain horizontal against vertical fusion, what PyTorch 2.14 picks when both flags are None, and which combinations raise errors.
Spot a launch-bound optimizer step in a trace, switch to fused safely, and know when foreach's extra peak memory forces the for-loop.
Weigh adopting the fused path across a team's jobs against its constraints, and decide whether the measured gain justifies a standing default.
## Three ways to launch one update An optimizer step applies the same arithmetic to every parameter tensor. How that arithmetic is launched on the GPU decides how long the step takes, especially for a model with hundreds of parameter tensors such as a 7B transformer. `torch.optim.AdamW` in PyTorch 2.14 has three implementations of the same update rule: | Implementation | How it runs | Selected by | |---|---|---| | **for-loop** | A Python loop over parameters; each tensor's update is several separate kernels | `foreach=False` or `fused=False` | | **foreach** (multi-tensor) | Groups parameters into tensor lists and runs each piece of the update across all of them at once — **horizontal fusion** | `foreach=True`, or the default when applicable | | **fused** | Runs the whole update for a group of tensors in one kernel — **vertical fusion** on top of horizontal | `fused=True` | The documented performance order is fused, then foreach, then for-loop. The fewer kernels a step launches, the less time the GPU spends waiting for the CPU to queue the next one. ## What the defaults pick Both flags default to `None`. In 2.14: 1. If neither flag is set, PyTorch picks **foreach** when every parameter is a supported tensor type on a device that has foreach kernels — CUDA included, and PyTorch's distributed `DTensor` registers itself as supported — and falls back to the for-loop otherwise. 2. It does not pick fused on its own. The fused implementations are newer, and the maintainers are giving them more bake-in time before making them the default. 3. `fused=True` must therefore be passed explicitly. For `AdamW`, the fused implementation is listed as stable on CUDA and beta on CPU and MPS. 4. `foreach=False` or `fused=False` forces the for-loop. So a training script that never mentions either flag is already on foreach, and gets fused only by asking for it. ## Constraints and errors - `foreach=True` together with `fused=True` raises a `RuntimeError` when the optimizer is constructed: the two cannot both be `True`. - `fused=True` requires every parameter to be a floating-point tensor on a supported device; anything else raises a `RuntimeError` naming the dtype and device. `float64`, `float32`, `float16` and `bfloat16` are supported. - `fused=True` does not support `differentiable=True`. - foreach holds its intermediates as tensor lists rather than one tensor at a time, so its peak memory is about one parameter-sized copy higher than the for-loop. If that tips a tight job into an out-of-memory error, set `foreach=False` or step fewer parameters at a time. - `capturable=True` makes the optimizer safe to capture in a CUDA graph or under `torch.compile`, but slows runs that are not captured; leave it `False` otherwise. ## Seeing it in a profile The implementation is visible in a `torch.profiler` trace of the optimizer step: - A **for-loop** step shows a long run of tiny kernels — several per parameter tensor — often with gaps while the CPU launches the next one. - A **foreach** step shows far fewer, larger multi-tensor kernels. - A **fused** step collapses to a handful of kernels. When the trace shows the optimizer step as a visible slice of every iteration, switching to `fused=True` is a one-argument change. ## Choosing - Leave the default (foreach) when nothing has been measured; it is already the faster general path. - Set `fused=True` on CUDA when the profile shows the optimizer step taking a meaningful share of step time and the parameters are floating point. - Use `foreach=False` only when foreach's extra peak memory is what stands between the job and an out-of-memory error, and accept the slower step. The update rule — decoupled weight decay and the two moment estimates — is the same in all three. Only the kernel launch pattern differs, although results can differ in the last bits because operations run in a different order.
- Why can foreach run a tight job out of memory where the for-loop did not?foreach keeps its intermediates as tensor lists covering many parameters at once, instead of one tensor at a time, so its peak memory is about one parameter-sized copy higher. If that is the margin that fails, set `foreach=False` or pass fewer parameters through the optimizer per step.
- What does fused=True require of the parameters and the other flags?Every parameter must be a floating-point tensor — `float64`, `float32`, `float16` or `bfloat16` — on a device with fused kernels, or construction raises a `RuntimeError`. It cannot be combined with `foreach=True` or with `differentiable=True`.
- AdamW already uses the fused kernel on CUDA by default in 2.14.
- Passing fused=True and foreach=True picks the faster of the two.
- foreach uses no more peak memory than the for-loop.
- Fused AdamW implements a different update rule from plain AdamW.
- The optimizer's implementation never shows up in a step profile.
Assumes PyTorch 2.14: with foreach and fused unset, AdamW defaults to foreach on CUDA, and fused is opt-in.