Manual collective bucketing can replace wait outputs with graph nodes that appear later than their existing consumers. The previous repair moved each consumer separately and rebuilt the full node-position map twice per move. Long dependent chains therefore caused quadratic work and made large sharded graphs appear to hang.
Run the stable topological sort once when a replacement introduces a backward data edge. Preserve its ordering semantics while making dependency traversal linear by resuming each blocked node's reverse dependency iterator instead of rescanning all inputs after every wakeup. This also detects cycles rather than leaving an invalid graph behind.
Tests cover transitive chains, multiple replacements, stable unrelated-node ordering, idempotence, reverse high fan-in, bounded traversal, and cycles.
Test Plan:
```
PYTHONDONTWRITEBYTECODE=1 python test/distributed/test_overlap_bucketing_unit.py -v
python test/dynamo/test_graph_deduplication.py
PYTHONDONTWRITEBYTECODE=1 python test/distributed/test_aten_comm_compute_reordering.py TestManualOverlapBucketing -v
```
Authored with assistance from an AI coding assistant.
Pull Request resolved: https://github.com/pytorch/pytorch/pull/195616
Approved by: https://github.com/IvanKobzarev