Multiply two matrices across two GPUs
I was asked this
No reports yet. Sign in to say where you were asked it.
An AI/ML design interview question asked in Google's first round (coding), last asked Sep 2025. It tests gpu, distributed computing and parallelism.
Compute C = A × B on two GPUs, where A and B already sit in the memory of both devices. Design and sketch the implementation: how the work is split, which communication primitives move data, how compute and communication overlap, memory layout and buffer reuse, numerical precision, synchronisation, and how the full C is assembled and returned. Then discuss how it scales past two GPUs and what happens when something fails.
Requirements
- Functional: correct
Cof shape(M, N)fromA(M, K)andB(K, N), returned on one device or on both, as agreed. - Non-functional: close to twice the single-GPU throughput for large matrices; bounded extra memory; results numerically comparable to a single-GPU GEMM.
Clarifying questions
- Are
AandBfully replicated on both GPUs, or split between them? - Where must
Cend up: on one GPU, on both, or on the host? - How large are
M,K,N, and what precision (FP32, BF16, FP16)? - What links the GPUs: NVLink or PCIe? Is NCCL available?
- Is this one multiplication or many in a pipeline (as in a model layer)?
What a strong answer covers
- Partitioning choices and what each costs. With both inputs replicated: split
Cby rows (each GPU computesA[rows] × B) or by columns, so no communication is needed during the GEMM, only an all-gather ofCif both need it. Splitting alongKinstead gives partial sums that need an all-reduce (or reduce-scatter), costingM × Ntraffic. - Communication volume per scheme compared with link bandwidth, and when the gather of
Cdominates. - Tiled GEMM with cuBLAS or cuBLASLt on each device; separate CUDA streams for compute and copies, with events, so finished tiles of
Ctransfer while later tiles compute. - Memory: contiguous submatrix views via pointer offsets (no copies), reused workspaces, and the extra buffer for the received half of
C. - Precision: BF16 or FP16 inputs on tensor cores with FP32 accumulation; why a
K-split all-reduce in low precision changes results. - Scaling to many GPUs: 2D block partitioning (SUMMA-style), hierarchical collectives, and how this maps to tensor parallelism in a transformer layer (column-parallel then row-parallel).
- Failures: checking CUDA and NCCL errors, timeouts on collectives, rebuilding the communicator, and falling back to one GPU.
Follow-ups
-
Ais split by rows across the GPUs andBby columns, with no replication. What must move now? - Make this one layer of a model where the next layer consumes
Cdirectly, so the gather can be skipped. - Measure whether you are compute bound or communication bound, and what you would change in each case.
Where it's asked
- Google · First round: coding · Last asked Sep 202545 min · Coding in a Google Doc, no syntax highlighting or autocomplete · No AI
What it tests
gpu · distributed computing · parallelism
No official answer yet. Draw yours on the right; publish it and it shows under Community.
Nothing published yet. Publish your canvas and it shows here for others to open and remix.
