36A block allocator for GPU memory

mediumpython
45:00

36 A block allocator for GPU memory

Design Memory Allocator is on OpenAI's LeetCode company tag lists, the all-time, three-month and six-month ones (3 sources). Three OpenAI candidates report it under the name malloc: a machine learning engineer whose phone screen covered it (1 report), and two software engineers (2 reports), one of whom got it as the only question, with some tests provided (1 report). This is practice in that shape, not a leaked question or LeetCode's version.

Set a 45-minute timer. Read one level at a time, and run only that level's tests until they pass: pytest problems/36_block_allocator -k level1. Earlier levels must keep passing.

The setting. A model server takes one big arena of GPU memory when it starts and hands out pieces of it itself: weights, activations, the cache of each request it serves. An address is an offset from the start of the arena, from 0 to size - 1. A block is a run of units in a row, known by its start address. Two live blocks never share a unit. Build Allocator in solution.py.

Level 1. A small arena.

  • Allocator(size) makes an arena of size units, all free. A size below 1 raises ValueError.
  • alloc(n) hands out a block of n units and returns its start: the lowest address where n free units in a row begin. When no such run exists, it returns None and changes nothing, even if more than n units are free in total. An n below 1 raises ValueError.
  • free(addr) frees the live block that starts at addr and returns the number of units it held. Every other address raises ValueError and changes nothing: one inside a block, one that is free, one outside the arena, and the start of a block that was freed already.

On Allocator(10): alloc(3) returns 0, alloc(4) returns 3, alloc(2) returns 7, and another alloc(2) returns None, since only unit 9 is free. Then free(3) returns 4, and alloc(2) returns 3.

Level 2. Owners, and an arena of 80 GiB.

  • Level 2's tests use Allocator(80 * 2**30), one unit per byte. One slot per unit would be 85,899,345,920 slots, so keep the free space as runs. Each of these tests first builds an arena of 2**20 units, allocates and frees half of it, and fails if that took more than 64 KiB, before it builds the big one.
  • alloc(n, owner=None) records who holds the block. An owner is any hashable value other than None, like a job id, and 0 or "" count as owners. A block with no owner belongs to no one and is freed only by its address.
  • release(owner) frees every live block that owner holds and returns the total number of units freed, 0 if it holds none. A block of that owner already freed by its address is not freed again.
  • free_runs() returns the free space as a new list of (start, end) tuples in address order. A pair is half-open: it holds the units start to end - 1. Two runs never touch, so a freed block next to a free run becomes part of it. Changing the list you get back changes nothing in the allocator.

Level 3. What the follow-ups ask for.

  • alloc(n, owner=None, align=1) starts the block at a multiple of align: the lowest such address where n free units in a row begin. align must be a power of two, 1, 2, 4, 8 and so on, and anything else raises ValueError. Units skipped to reach an aligned start stay free.
  • resize(addr, n) changes the live block that starts at addr to hold n units without moving it, and returns True. Shrinking always works, and the units cut off the end become free. Growing works only when the free run that starts exactly where the block ends has enough units. Otherwise resize returns False and changes nothing. The block keeps its owner. An addr that isn't the start of a live block, or an n below 1, raises ValueError.

Things to get right: the lowest run that fits and not the smallest, a freed block that touches a free run on one side or on both, two free runs with one used unit between them, a free of the same block twice, an owner whose block was freed by its address, the units skipped for alignment, and a resize that can't grow and must leave everything as it was. Two of the random tests check every call against a slow model with one slot per unit on an arena of 64 units.

Be ready to talk about what comes next: best fit against first fit and what each does to fragmentation, what the cost of each call is with thousands of free runs, why a C allocator can't move a live block to close the gaps, what changes when several threads allocate at once, and where a real allocator keeps its bookkeeping.

solution.py
Run the tests to see which pass.