Data-parallel and fully sharded matrix multiply over simulated devices
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in SpaceXAI's technical phone screen, last asked Apr 2026. It tests distributed training, parallelism and python.
You are given a small simulator of P devices that run in parallel (one thread each) and can exchange arrays only through point-to-point send and recv. Compute Y = X @ W for an input X of shape (B, D) and weights W of shape (D, H). First finish a data-parallel version, where every device has the full weights and a slice of the batch. Then write a fully sharded (FSDP-style) version, where each device stores only its slice of the weights and gathers the rest from the others just before it needs them. Say what each version costs in memory and communication.
Interface
Unless you agree on a different interface with the interviewer, assume:
class Device:
rank: int # 0 .. world_size - 1
world_size: int
def send(self, dst: int, array: np.ndarray) -> None: ...
def recv(self, src: int) -> np.ndarray: ... # blocks until a message from src arrives
def run_on_devices(world_size: int, fn) -> list: # runs fn(device) on every device, returns their results
-
data_parallel_matmul(dev, x_shard, w) -> np.ndarray:x_shardis this device'sB / Prows ofX;wis the fullW. Returns this device's rows ofY. -
fsdp_matmul(dev, x_shard, w_shard) -> np.ndarray:w_shardis this device'sD / Prows ofW. Gather the fullWfrom all devices withsend/recv, compute, then drop the gathered copy. Returns this device's rows ofY. -
all_gather(dev, piece) -> list[np.ndarray]: the helper you write for the fully sharded version; returns every device's piece in rank order.
Example
P = 2, X: (4, 6), W: (6, 4)
data parallel: device r holds X rows 2r .. 2r+1 and all 24 numbers of W
returns a (2, 4) block, no communication
fully sharded: device r holds X rows 2r .. 2r+1 and W rows 3r .. 3r+2 (12 numbers)
receives the other device's 12 numbers, rebuilds W, returns a (2, 4) block
stacking both blocks in rank order equals X @ W
Constraints
-
BandDare divisible byPunless you choose to handle the remainder. -
sendmay block until the receiver callsrecv, so the order of sends and receives matters. - Use NumPy for the local multiply; the communication is what is being tested.
Clarifying questions
- Is
sendbuffered or does it block until matched? Can a device send to itself? - Must the full
Yend up on one device, on every device, or stay sharded by rows? - Should the weights be sharded by rows or by columns of
W? - Is there a backward pass and gradient sync, or only the forward multiply?
What a strong answer covers
- A correct all-gather built from
send/recvthat cannot deadlock: a ring, or ordering sends and receives by rank parity. - Data parallel: no communication in the forward pass, full
Win memory on every device. - Fully sharded: each device stores
|W| / P, receives(P - 1) / P · |W|per forward, and briefly holds the fullW; when that trade is worth it. - Results checked against
X @ Won one device for severalP, includingP = 1. - The ring all-gather taking
P - 1steps, and why its cost per device does not grow withP.
Follow-ups
- Add the backward pass: gradients of
Wneed an all-reduce for data parallel and a reduce-scatter for fully sharded. - Overlap communication with compute, for example by prefetching the next layer's shards.
- Shard
Wby columns instead (tensor parallelism): what has to be communicated now? - How would you test for deadlocks and for messages delivered out of order?
Where it's asked
- SpaceXAI · Technical phone screen · Last asked Apr 202645 min · Live applied coding on a shared platform such as CoderPad · No AI
What it tests
distributed training · parallelism · python
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
