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

rank cannot recv from itself

Error message

rank cannot recv from itself

What it means

Mesh.recv pulls a tensor pushed by another rank; receiving from yourself would block forever on an empty queue because the rank cannot have sent to itself while also waiting to receive, so self-recv is rejected as a protocol error.

Solutions

  1. Fix the source-rank arithmetic in the collective loop (usually an off-by-one in next_rank/prev_rank)
  2. Skip the recv when src == mesh.rank and use the local tensor
  3. Add ring-neighbor assertions: prev = (rank - 1) % world_size, next = (rank + 1) % world_size
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at phases/19-capstone-projects/76-collective-ops-from-scratch/code/main.py:65 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/19fd466946348373. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/76-collective-ops-from-scratch/code/main.py:65

    rank: int
    world_size: int
    out_queues: list
    in_queues: list
    byte_counter: object = None

    def send(self, dst: int, tensor: torch.Tensor) -> None:
        if dst == self.rank:
            raise ValueError("rank cannot send to itself")
        payload = tensor.detach().clone().contiguous()
        nbytes = payload.numel() * payload.element_size()
        if self.byte_counter is not None:
            with self.byte_counter.get_lock():
                self.byte_counter.value += nbytes
        self.out_queues[dst].put(payload)

    def recv(self, src: int) -> torch.Tensor:
        if src == self.rank:
            raise ValueError("rank cannot recv from itself")
        return self.in_queues[src].get(timeout=RECV_TIMEOUT_S)


def build_queue_grid(ctx, world_size: int):
    """Allocate a (world_size, world_size) grid of queues using the given context."""
    grid = [[None] * world_size for _ in range(world_size)]
    for src in range(world_size):
        for dst in range(world_size):
            if src != dst:
                grid[src][dst] = ctx.Queue()
    return grid


def mesh_from_grid(rank: int, world_size: int, grid, byte_counter) -> Mesh:
    out_qs = [grid[rank][d] for d in range(world_size)]
    in_qs = [grid[s][rank] for s in range(world_size)]
    return Mesh(rank=rank, world_size=world_size,
                out_queues=out_qs, in_queues=in_qs,

View on GitHub (pinned to 39ea8a1c6d)