FSDP collective desync (NCCL all-gather timeout) from per-rank sample skipping in `DataFetcher`
Summary
The bug is that multi-rank FSDP training hang on an NCCL _ALLGATHER_BASE timeout because DataFetcher._should_skip_for_loss_mask drops samples per-rank. When a dispatched batch contains a sample that one rank skips (all-zero loss mask, or < min_loss_tokens supervised tokens, according to _should_skip_for_loss_mask) but the other ranks do not, the skipping rank runs one fewer micro-batch, thenone fewer FSDP all-gather, then the ranks fall out of collective lockstep and the next all-gather blocks until the 600 s watchdog aborts the job.
It is triggered by the DFlash SGLang example plus a dataset with open-perfectblend dataset.
Environment
Running configs/sglang_qwen3_8b_dflash.yaml
- Qwen3-8B target, DFlash draft, FSDP2
FULL_SHARD, 4 DP ranks,micro_batch_size=1,draft_accumulation_steps=2,max_seq_length=2048,min_loss_tokens=32 - Dataset: open-perfectblend
Error
[Rank N] Watchdog caught collective operation timeout:
WorkNCCL(SeqNum=…, OpType=_ALLGATHER_BASE, NumelIn=176556032, NumelOut=706224128,
Timeout(ms)=600000) ran for 600000+ ms before timing out.
Flight recorder shows exactly one rank one collective behind:
[Rank 1] Last enqueued NCCL work: 21936, last completed 21936
[Rank 0/2/3] Last enqueued NCCL work: 21937, last completed 21936 # waiting on the all-gather
Immediately preceding it, on the diverging rank only:
data_fetcher.py:208 WARNING Skipping sample with too few loss-masked tokens
(31 < 32, mooncake_key=perfectblend_72475_…, total_skipped=1)
Root cause
In torchspec/training/data_fetcher.py:
def _should_skip_for_loss_mask(self, data, mooncake_key, skip_count):
mask = self._compute_loss_mask(data)
if mask is None: # all-zero loss mask
return True, skip_count + 1
if self._min_loss_tokens > 0 and mask.sum() < self._min_loss_tokens:
return True, skip_count + 1 # too few supervised tokens
return False, skip_count
def __iter__(self):
while True:
item = self.ray_queue.get(...) # per-rank queue (this rank's dispatched slice)
data = self._load_from_mooncake(item)
should_skip, skip_count = self._should_skip_for_loss_mask(data, item.mooncake_key, skip_count)
if should_skip:
continue # DROPS the sample, so this rank yields one fewer micro-batch
...
yield data
Each DP rank's fetcher decides independently whether to drop a sample. The decision is data-dependent and per-rank, so two ranks processing different samples in the same dispatched step can yield different micro-batch counts. Under FSDP every rank must issue the same sequence of all-gathers, so a count mismatch desyncs the process group and the next collective hangs.
How the skip got here
| commit | PR | role |
|---|---|---|
40a4e8d |
#37 Support last_turn_loss_only in dynamic_loss_mask path | Origin: Added if self._compute_loss_mask(data) is None: continue, commented "Skips samples whose loss mask is all zeros to avoid wasted compute." The dynamic scan can legitimately produce an all-zero mask (no assistant turn, or last_turn_loss_only matches nothing), and the mask is only knowable in the fetcher (dynamic mode loads input_ids from Mooncake) — so the drop landed there. |
760a552 |
#64 DFlash | Amplifier: Added min_loss_tokens + mask.sum() < min_loss_tokens, widening the drop from "all-zero only" (rare) to "any sample under N supervised tokens" (common once max_seq_length truncation manufactures them). |
Why some configs don't hit it
The hang requires:
min_loss_tokens > 0or an all-zero-mask sample (enables the skip),- a sub-threshold sample actually reaching the trainer,
dp_size > 1FSDP (collective lockstep),- that sample landing on some but not all ranks in a dispatch.
Minimal reproduction
64-sample dataset = 4 copies of the offending sample + 60 valid fillers. SGLang DFlash config (min_loss_tokens=32, max_seq_length=2048), 4-rank FSDP, eval disabled. Hangs by ~step 4 (depends on the arrival to the pool), reproducible.
Offending sample
{"id": "perfectblend_72475", "conversations": [{"role": "user", "content": "Can you simplify the details of this SVG while retaining its main shape?\n<svg width=\"32\" height=\"32\" viewBox=\"0 0 32 32\" fill=\"none\" xmlns=\"http://www.w3.org/2000/svg\">\n<path d=\"M29.7164 17.8025L27.3964 17.3625C27.2564 17.3325 27.1864 17.1725 27.2564 17.0525L29.4164 13.5825C29.5064 13.4325 29.3964 13.2525 29.2264 13.2625L25.1564 13.6325C25.0164 13.6425 24.8964 13.5124 24.9364 13.3724L25.5764 11.1325C25.6264 10.9525 25.4264 10.8025 25.2664 10.9025L20.9164 13.6525C20.7664 13.7425 20.5764 13.6225 20.5964 13.4525L21.8764 4.12245C21.8964 3.95245 21.7164 3.8225 21.5664 3.9225L18.2764 5.9225C18.1764 5.9825 18.0364 5.94248 17.9864 5.84248L16.1264 2.1125C16.0464 1.9625 15.8364 1.9625 15.7564 2.1125L13.8964 5.84248C13.8464 5.95248 13.7064 5.99251 13.6064 5.93251L10.2064 3.9225C10.0564 3.8325 9.86642 3.96246 9.89642 4.13246L11.5264 13.4525C11.5564 13.6225 11.3664 13.7525 11.2164 13.6625L6.60642 10.8925C6.44642 10.7925 6.24641 10.9424 6.29641 11.1224L6.94642 13.3825C6.98642 13.5225 6.87641 13.6625 6.72641 13.6425L2.65641 13.2725C2.48641 13.2525 2.37642 13.4425 2.46642 13.5925L4.62642 17.0625C4.70642 17.1825 4.63642 17.3524 4.48642 17.3724L2.16642 17.8125C1.98642 17.8425 1.93642 18.0825 2.09642 18.1925L8.63642 22.4825C8.80642 22.5925 8.84642 22.8125 8.73642 22.9825L6.80642 25.7325C6.71642 25.8625 6.84642 26.0425 6.99642 25.9825L15.3764 22.9225C15.7364 22.7925 16.1364 22.7925 16.4964 22.9225L24.8764 25.9825C25.0264 26.0425 25.1664 25.8625 25.0664 25.7325L23.1364 22.9825C23.0264 22.8225 23.0664 22.5925 23.2364 22.4825L29.7764 18.1925C29.9464 18.0725 29.8964 17.8325 29.7164 17.8025Z\" fill=\"#F8312F\"/>\n<path d=\"M15.9464 11.2725C16.4987 11.2725 16.9464 11.7202 16.9464 12.2725V20.3934L22.3889 17.5933C22.88 17.3407 23.4829 17.534 23.7356 18.0251C23.9882 18.5162 23.7949 19.1191 23.3038 19.3718L16.9464 22.6426V28.9725C16.9464 29.5248 16.4987 29.9725 15.9464 29.9725C15.3941 29.9725 14.9464 29.5248 14.9464 28.9725V22.6426L8.58886 19.3718C8.09776 19.1191 7.90447 18.5162 8.15714 18.0251C8.4098 17.534 9.01275 17.3407 9.50384 17.5933L14.9464 20.3935V12.2725C14.9464 11.7202 15.3941 11.2725 15.9464 11.2725Z\" fill=\"#CA0B4A\"/>\n</svg>\n"}, {"role": "assistant", "content": "Your SVG includes a lot of small details which can be simplified while keeping its main shape. Here is the simplified version:\n\n```svg\n<svg xmlns=\"http://www.w3.org/2000/svg\" width=\"32\" height=\"32\" viewBox=\"0 0 32 32\">\n <path fill=\"#F8312F\" d=\"M26,18l-3-1l3-4l-5-1l1-3l-6,4l-1-11l-4,3L8,0L6,5L0,4l1,4l-7,4l5,1L2,14h4l-3,4l5,1L0,20l8,5l-6,4l9-4h4h4l9,4l-6-4L32,15H26L26,18z\" />\n <path fill=\"#CA0B4A\" d=\"M16,12h4v11l7-4l-7-4V12z M16,12h-4v11l-7-4l7-4V12z M16,23v7h-4v-7H16z\" />\n</svg>\n```\n\nHere's a few notes on changes I made to simplify this SVG:\n1. I rounded the numbers to make the paths simpler while keeping the overall shape.\n2. I removed some of the tiny details to make the SVG cleaner.\n3. The drawings are simplified with straight lines instead of curves, to better emphasize the overall shape and reduce complexity.\n4. I used relative commands instead of absolute ones which reduced the overall path d size. \n\nThis simplified version might not be identical to the original, but it retains the overall shape and should be quicker to load and render."}]}