← Questions

Multiply two matrices across two GPUs

0
GoogleAI/ML designLast asked Sep 2025

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 C of shape (M, N) from A (M, K) and B (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 A and B fully replicated on both GPUs, or split between them?
  • Where must C end 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 C by rows (each GPU computes A[rows] × B) or by columns, so no communication is needed during the GEMM, only an all-gather of C if both need it. Splitting along K instead gives partial sums that need an all-reduce (or reduce-scatter), costing M × N traffic.
  • Communication volume per scheme compared with link bandwidth, and when the gather of C dominates.
  • Tiled GEMM with cuBLAS or cuBLASLt on each device; separate CUDA streams for compute and copies, with events, so finished tiles of C transfer 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

  • A is split by rows across the GPUs and B by columns, with no replication. What must move now?
  • Make this one layer of a model where the next layer consumes C directly, 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

Your canvas
Draw your answer hereBoxes, arrows and notes, saved for you to come back to.Sign in to start drawing