# Row-wise EmbeddingCollection backward

This page follows the autograd graph created by a row-wise `ShardedEmbeddingCollection` forward and maps a representative set of CUDA activities back to commit-pinned TorchRec call sites.

## Coverage

- 43 TorchRec-owned activities: 37 kernels and 6 memsets.
- Source checkout: `1d87b8f79b7bb1671cb8be3e646e99e835cce610`.
- Machine-readable detail: [`backward_kernel_source_map.csv`](backward_kernel_source_map.csv).

The public artifacts contain no application-specific names, paths, ranks or step identifiers.

## Backward order

1. `IndexSelectBackward0` accumulates gradients through `reverse_indices`, then through `unbucketize_permute_tensor`.
2. `All2All_Seq_Req_Wait.backward` optionally encodes the gradient and launches the reverse asynchronous AllToAll.
3. `All2All_Seq_Req.backward` waits, optionally decodes, applies `backward_recat_tensor`, and optionally divides the gradient by world size.
4. FBGEMM TBE groups repeated sparse rows, reduces row gradients, and applies the configured fused optimizer to the local shard.
5. The graph stops before KJT input distribution because sparse indices and offsets are integer tensors and `input_dist` runs under `torch.no_grad()`.

## Important runtime distinction

PyTorch does not call the TorchRec wrapper Modules in reverse. `GroupedEmbeddingsLookup` and `BatchedFusedEmbedding` participate in forward and create the TBE graph edge, but backward executes ATen nodes, TorchRec autograd.Function nodes, and the generated FBGEMM TBE autograd node.

## Nsight cross-validation

The representative report was exported to Parquet. CUDA runtime launches were selected inside the two `IndexSelectBackward0` nodes, `All2All_Seq_Req_WaitBackward`, `All2All_Seq_ReqBackward`, and the generated TBE backward op. GPU activities were joined by CUDA correlation ID and process identity, preventing correlation-ID reuse on neighboring processes from leaking unrelated work into the map.

The GPU timestamps can be much later than their CPU launch scopes because the default stream is already queued. Attribution therefore uses CUDA correlation and the launch path, not timestamp overlap alone.

## Configuration-dependent branches

- `reverse_indices` backward is present only when index deduplication is enabled.
- Communication encode/decode is present only when a qcomm codec is configured. The representative sample uses FP16 for backward communication.
- TBE update kernels depend on the fused optimizer. The representative sample uses exact row-wise Adagrad; other optimizers produce different generated kernels.

## Phase activity counts

| Phase | Activities |
|---|---:|
| Gradient restoration | 4 |
| Reverse AllToAll launch | 3 |
| Reverse AllToAll finalize | 9 |
| FBGEMM backward + update | 27 |
| Gradient boundary | 0 |

## Source ownership

| Participant | Runtime type | Source |
|---|---|---|
| `All2All_Seq_Req_Wait` | `autograd.Function` | [comm_ops.py:2061](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/comm_ops.py#L2061) |
| `All2All_Seq_Req` | `autograd.Function` | [comm_ops.py:1916](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/comm_ops.py#L1916) |
| `GroupedEmbeddingsLookup` | `nn.Module` forward owner | [embedding_lookup.py:408](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/embedding_lookup.py#L408) |
| `BatchedFusedEmbedding` | `nn.Module` forward owner | [batched_embedding_kernel.py:2530](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/batched_embedding_kernel.py#L2530) |
| `SplitTableBatchedEmbeddingBagsCodegen` | FBGEMM `nn.Module` and autograd owner | [batched_embedding_kernel.py:2565](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/batched_embedding_kernel.py#L2565) |
| `EmbeddingFusedOptimizer` | fused optimizer facade | [batched_embedding_kernel.py:2587](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/batched_embedding_kernel.py#L2587) |
| `RwSparseFeaturesDist` | `nn.Module`, no backward edge | [embedding.py:1559](https://github.com/pytorch/torchrec/blob/1d87b8f79b7bb1671cb8be3e646e99e835cce610/torchrec/distributed/embedding.py#L1559) |
