36A block allocator for GPU memory
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 ofsizeunits, all free. Asizebelow 1 raisesValueError.alloc(n)hands out a block ofnunits and returns its start: the lowest address wherenfree units in a row begin. When no such run exists, it returnsNoneand changes nothing, even if more thannunits are free in total. Annbelow 1 raisesValueError.free(addr)frees the live block that starts ataddrand returns the number of units it held. Every other address raisesValueErrorand 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 thanNone, like a job id, and0or""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 thatownerholds 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 unitsstarttoend - 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 ofalign: the lowest such address wherenfree units in a row begin.alignmust be a power of two, 1, 2, 4, 8 and so on, and anything else raisesValueError. Units skipped to reach an aligned start stay free.resize(addr, n)changes the live block that starts ataddrto holdnunits without moving it, and returnsTrue. 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. OtherwiseresizereturnsFalseand changes nothing. The block keeps its owner. Anaddrthat isn't the start of a live block, or annbelow 1, raisesValueError.
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.