NVIDIA recsys-examples · _rewrite_model · BaseForward
TorchRec 默认在 ShardedModule.forward() 中执行两段通信:
原生串行路径必须等 input_dist 的 NCCL 完成后才能启动后续 GPU 计算,因此 AllToAll 延迟直接进入关键路径。
拆分方式:将 batch_i 的 input_dist 提前到 batch_(i-1) 的 fwd/bwd 期间,并放到一条独立 CUDA stream 上。这样通信可以与 GPU 计算重叠。
TorchRec runs two NCCL collectives inside the default ShardedModule.forward():
The native path runs them serially. Downstream GPU work cannot start until input_dist finishes, so the AllToAll latency sits on the critical path.
Split: move batch_i's input_dist into batch_(i-1)'s fwd/bwd window on an independent CUDA stream. Communication can then overlap with GPU computation.
self.shared_ebc(batch.feats_sparse) 的参数表达式会被 evaluate 一遍但结果被忽略:真实输入从 context 取。self.shared_ebc(batch.feats_sparse) still evaluates the arg expression, but the result is thrown away: the real input comes from context.model.eval() without restoring is unsafe: the patched forward always expects a populated context.每个 class box 的三条元数据:pop 来源、__call__ 里 wait 去留、用哪个 Pipeline:对应 §4 三种不同的 wait 时机策略。
Each class box's three metadata lines: pop source, wait inside __call__, which Pipeline uses it: correspond to the three wait-timing strategies in §4.
ArgInfo 是 Step 4 的产物:一条"从 batch 到 sharded 模块参数"的访问路径。简化示例:
ArgInfo is Step 4's output: a recorded access path from batch to the sharded-module arg. Stripped example:
# User wrote: self.shared_ebc(batch.sparse_features) # FX trace recovers this access path into ArgInfo ↓ ArgInfo( input_attrs = ["", "sparse_features"], # getattr chain from batch is_getitems = [False, False], postproc_modules = [None, None], constants = [None, None], name = None, # positional arg ) # Later, _start_data_dist replays this on batch_i+1: # batch_i+1 -> getattr "sparse_features" -> ShardedModule.input_dist(...)
| Context 字段Context field | 写入方Written by | 读取方Read by |
|---|---|---|
input_dist_splits_requests |
_start_data_dist |
_fuse_input_dist_splits |
fused_splits_awaitables |
_fuse_input_dist_splits |
wait_sparse_data_dist |
input_dist_tensors_requests |
wait_sparse_data_dist |
PipelinedForward.__call__ |
module_contexts |
_start_data_dist |
PipelinedForward.__call__ |
module_input_post_prefetch |
_prefetch (Prefetch variant) |
PrefetchPipelinedForward.__call__ |
module_contexts_post_prefetch |
_prefetch (Prefetch variant) |
PrefetchPipelinedForward.__call__ |
ShardedModule 的所有非-leaf 祖先:途中任何一段动态 Python 逻辑({**proxy}、len(proxy)、Python-level if/for 依赖 tensor 值)都会让 trace 失败。要么把那段代码改 FX-friendly,要么用 @torch.fx.wrap 把它包成黑盒。
The FX trace walks through every non-leaf ancestor of a ShardedModule. Any dynamic Python along the way: {**proxy}, len(proxy), value-dependent if/for: breaks the trace. Either rewrite that code to be FX-friendly, or wrap it with @torch.fx.wrap to hide it behind an opaque call_function node.
# Native (torchrec/distributed/types.py:1245): both waits chained: def forward(self, *input, **kwargs): ctx = self.create_context() dist_input = self.input_dist(ctx, *input, **kwargs).wait().wait() # ← wait #1 . wait #2 return self.compute_and_output_dist(ctx, dist_input) # Pipelined: wait #1 happens in wait_sparse_data_dist: def wait_sparse_data_dist(self, context): for names, awaitable in context.fused_splits_awaitables: for name, request in zip(names, awaitable.wait()): # ← wait #1 context.input_dist_tensors_requests[name] = request # ... and wait #2 happens inside PipelinedForward.__call__: def __call__(self, *input, **kwargs): request = self._context.input_dist_tensors_requests.pop(self._name) with torch.cuda.stream(self._stream): data = request.wait() # ← wait #2 return self._module.compute_and_output_dist(ctx, data)
data_dist_stream,wait #2 使用 default_stream。两者都可以与相邻 batch 的 fwd/bwd 重叠。§2 的 class 图标出了三种 forward 变体各自放置 wait 的位置。
wait #1 uses data_dist_stream, while wait #2 uses default_stream. Both can overlap with nearby batch fwd/bwd. The class diagram in §2 marks the wait placement for each forward variant.
| Stage 1 (splits)Stage 1 (splits) | Stage 2 (tensors)Stage 2 (tensors) | |
|---|---|---|
| 单次数据量Payload per call | 每 feature 少量 int64(数十字节)a few int64 per feature (~tens of bytes) | 真实 KJT 数据(MB-百 MB)real KJT data (MB - hundreds of MB) |
| Launch 开销占比Launch-overhead share | 压倒性(~20 μs 远大于传输时间)dominant (~20 μs ≫ transfer time) | 可忽略negligible |
| Fuse 收益Fuse ROI | 高:N 次 launch → 1 次high: N launches → 1 | 低;还会打乱 per-module pipelinelow; also serialises per-module pipelining |
| 是否 fuseIs fused? | ✓ _fuse_input_dist_splits |
✗ 按模块各自异步left per-module async |
input.variable_stride_per_key() 判断走哪条路。KJTAllToAllSplitsAwaitable.__init__ 有短路;override 没短路,但 FusedKJTListSplitsAwaitable 的 pg is None 分支会跳过 NCCL。_fuse_input_dist_splits 按 pg 分组,每个 PG 一个 fused awaitable:不跨 PG。input.variable_stride_per_key().KJTAllToAllSplitsAwaitable.__init__ short-circuits; the override does not, but FusedKJTListSplitsAwaitable skips NCCL when pg is None._fuse_input_dist_splits groups by pg; one fused awaitable per PG: no fusing across PGs.