KatInterview

Build a Multi-GPU Test Harness in Python

System DesignModel answer
HardModel answerSystem Designpythontestingconcurrency

Last updated

Design and implement a pytest-style test harness that runs graphics validation tests in parallel across multiple GPUs, handling fixtures, retries, and process isolation.

Build a Multi-GPU Test Harness in Python

You are building a validation framework that runs graphics tests across several GPUs on the same machine. Each test verifies rendered output — comparing pixel buffers, checking shader correctness, validating memory usage — and the suite can contain hundreds of cases with different parameter combinations.

Design and implement this harness from first principles. Your answer should cover:

Architecture

  • How you split the two concerns: defining tests (registration, parametrization, fixtures) vs. executing them (process isolation, concurrency, device assignment).
  • Why you choose processes over threads for GPU work, and how you coordinate them without blocking the parent.

Python mechanics — explain and use each of these

  • Generators for lazy test collection
  • Context managers and ExitStack for fixture lifecycle
  • Dependency injection via inspect.signature
  • Type hints and frozen dataclasses for inter-process messages
  • asyncio in the parent process and ProcessPoolExecutor for workers
  • Retry logic that distinguishes transient device errors from real test failures

Framework choice

  • Compare unittest and pytest on style, fixtures, parametrization, and ecosystem. State which you would use in production and why.

Expected output

  • A working code skeleton (registration decorators, collection generator, DI resolver, worker entry point, async orchestrator).
  • Prose explaining every design decision and its trade-offs.
  • A brief note on what you would not implement from scratch in production.

Hints

Hint 1

Start by separating concerns: test *definition* (fixtures, params, retries) is pure Python bookkeeping; test *execution* (one process per GPU, driver crash isolation) is a concurrency problem. Solve them independently before wiring them together.

Hint 2

The GIL is not your main enemy here — driver crashes and blocked CUDA calls are. A crashed GPU worker should not take down the orchestrator. Think about what `ProcessPoolExecutor` gives you and what `BrokenProcessPool` means for your retry strategy.

Hint 3

Fixtures have dependencies on other fixtures, forming a DAG. Use `inspect.signature` to read parameter names and recurse. Wrap each resolved fixture in an `ExitStack` so teardown always runs in LIFO order, even when setup partially fails mid-DAG.