rohitg00/ai-engineering-from-scratch · error · ValueError
rank cannot send to itself
Error message
rank cannot send to itself
What it means
Mesh.send routes a tensor to another rank's inbound queue over the simulated point-to-point fabric; sending to your own rank would deadlock the queue topology (a rank is never receiving while sending), so self-send is rejected as a misuse of the collective API.
Solutions
- Remove the self-send from the collective algorithm — each rank already holds its own shard
- Guard with if dst != mesh.rank before calling send
- Use a local copy or skip when dst == rank in generic loop code
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at phases/19-capstone-projects/76-collective-ops-from-scratch/code/main.py:55 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/0759a02151001882.
Report an issue: GitHub.
Appendix: source
Thrown at phases/19-capstone-projects/76-collective-ops-from-scratch/code/main.py:55
@dataclass
class Mesh:
"""A point-to-point mesh wired as a fully-connected graph of queues.
Each rank holds out_queues[dst] and in_queues[src]. The ring algorithms
only use neighbour edges; the full mesh keeps the API general so future
lessons can experiment with tree topologies without rewiring.
"""
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):View on GitHub (pinned to 39ea8a1c6d)