Repository navigation
Commit fce1acb
Add async_op to defer functional-collective waits
Summary:
`_AllReduce`, `_AllGatherShard` and `_ReduceScatterShard` issue functional
collectives and then call `.wait()` on the returned `AsyncCollectiveTensor`
immediately. That makes the compute stream wait on NCCL right after the
collective is issued, so no communication can overlap with compute in eager
mode.
DTensor solves this with an explicit opt-in: `redistribute(..., async_op=True)`
returns the `AsyncCollectiveTensor` and the first consuming op waits (a stream
wait, not a host sync). torchtitan's DTensor backend passed `async_op=True` at
its module-boundary redistributes; everything else waits immediately.
This diff adds the same opt-in to spmd_types:
- `async_op: bool = False` on `all_reduce`, `all_gather`, `reduce_scatter`,
`all_to_all`, `redistribute`, `unshard`, and (for uniformity, so
`backward_options` can be forwarded anywhere) `reinterpret`, `convert`,
`shard` and `invariant_to_replicate`, which issue no forward communication.
With `async_op=True` a functional collective's result is returned unwaited.
The default keeps today's behavior.
- `backward_options={"async_op": True}` defers the backward collective,
mirroring how `op_dtype`/`out_dtype` are already split between forward and
backward. Unlike DTensor (whose backward reuses the forward flag), the two are
independent. Gradients for leaf inputs are always waited, since they would
otherwise land in `.grad` as an unwaited `AsyncCollectiveTensor`; DTensor
avoids this because its gradients stay wrapped in a DTensor.
- Paths without functional collectives (list/uneven APIs, in-place all_reduce)
and results wrapping a tensor subclass (`LocalTensor` simulation) still wait.
Under tracing, funcol already returns waited tensors.
Also makes `_ReduceScatterShard` pass a contiguous input to
`reduce_scatter_tensor`, like `_AllGatherShard` already does. The gradient
reaching all_gather's backward (which calls reduce_scatter) can be an
expanded view, e.g. from `out.sum()`, and process groups require contiguous
collective inputs. A backward test hit this on the CI torch build.
Measured in torchtitan (Llama 3 8B, TP4, eager, 4x GB300) with forward-only
deferral at the module-boundary redistributes (prototyped as a patch on
spmd_types 0.2.5): NCCL/compute overlap goes from 0% to 17.6% (same as the
DTensor backend) and throughput from -1.7..-2.4% vs DTensor to parity
(+0.2%), with identical loss. A follow-up torchtitan change opts in.
___
landed-with-radar-review
Differential Revision: D124958791
fbshipit-source-id: a21d630d3b66149bd2e7933dff1057bdccca9d201 parent a1b86de commit fce1acb
5 files changed
Lines changed: 278 additions & 20 deletions
File tree
- spmd_types
- tests
0 commit comments