rohitg00/ai-engineering-from-scratch · error · RuntimeError

rank failed

Error message

rank {rank} failed: {payload["error"]}

What it means

Worker-failure propagation in the distributed demo: at least one rank put an error payload on the result queue instead of a result, and the coordinator re-raises it so one bad rank fails the whole world.

Solutions

  1. Read the embedded error message to identify the failing rank and its exception
  2. Fix the root cause in the rank's worker code (often a CUDA/backend init issue) and re-run
  3. Ensure all ranks receive identical, valid init inputs
Defensive patterns

Strategy: retry

When it happens

Trigger: Thrown at phases/19-capstone-projects/48-distributed-fsdp-ddp/code/main.py:355 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/a4ff86ff2b9e8730. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/48-distributed-fsdp-ddp/code/main.py:355

            rank, payload, diff = result_queue.get(timeout=1.0)
        except Exception:
            continue
        results[rank] = payload
        if diff > max_diff:
            max_diff = diff
        collected += 1
    for p in procs:
        p.join(timeout=max(1.0, deadline - time.time()))
    if any(p.is_alive() for p in procs):
        for p in procs:
            if p.is_alive():
                p.terminate()
        raise RuntimeError("ranks did not finish in time")
    if collected < world_size:
        raise RuntimeError(f"only got {collected}/{world_size} results: {results}")
    for rank, payload in results.items():
        if "error" in payload:
            raise RuntimeError(f"rank {rank} failed: {payload['error']}")
    param_sums = {r: results[r]["post_param_sum"] for r in results}
    spread = max(param_sums.values()) - min(param_sums.values())
    losses = [results[r]["final_loss"] for r in results]
    grad_norm = results[0]["grad_norm_after_all_reduce"]
    return {
        "world_size": world_size,
        "backend": backend,
        "param_sum_per_rank": param_sums,
        "param_sum_spread": spread,
        "losses": losses,
        "grad_norm_after_all_reduce": grad_norm,
        "manual_all_reduce_max_diff_vs_single_process": max_diff,
        "fsdp_round_trip_all_ranks_ok": all(results[r]["fsdp_round_trip_ok"] for r in results),
    }


def write_demo(payload: Dict[str, object], path: Path) -> None:
    path.parent.mkdir(parents=True, exist_ok=True)

View on GitHub (pinned to 39ea8a1c6d)