Skip to content

modeling: an OOM's shortfall is exact and the memory percentage counts reserved bytes - #397

Draft
JadenFiotto-Kaufman wants to merge 2 commits into
devfrom
fix/memory-reporting
Draft

JadenFiotto-Kaufman wants to merge 2 commits into
devfrom
fix/memory-reporting

Conversation

@JadenFiotto-Kaufman

Copy link
Copy Markdown
Member

The problem

Two figures in the meta of a terminal response are wrong.

  1. On an out-of-memory failure, alloc_shortfall_by_gpu is requested - (budget - torch.cuda.max_memory_reserved(device)). The peak reserved includes cache the allocator released before it refused. gpt2 in bf16 under a 2 GiB cap, running a seeded mix of 240 requests through the actor's request path, raised 24 OOMs: the reported median was 609.6 MiB against an exact 178.0 MiB, and every one was 432 to 581 MiB too high. The requested size and the card are parsed out of the message. A sandbox runner's OOM arrives as text, matches is_oom, and is compared against this actor's budget and reserved bytes, which describe another process: in the same run it reported {"0": 0}.
  2. max_mem_pct_by_gpu is (peak allocated - baseline) / headroom, but set_per_process_memory_fraction caps reserved bytes. The 19 successful requests above 1.4 GiB showed 69 to 78% while their reserved bytes filled 82 to 95% of the headroom the cap left them. The gpu_mem metric has no reserved figure, so fragmentation does not show in telemetry.

The change

One commit per finding; either can be dropped.

  • Shortfall: BaseModelDeployment.__init__ attaches torch's out-of-memory observer once after loading (watch_refusals). torch calls it once per OOM it raises, after it has released its cache and retried, with the card, the requested bytes and the allowed bytes; the observer reads memory_reserved there. gpu_baselines clears what it recorded, and alloc_shortfall returns requested - (allowed - reserved) from it. An OOM raised in another process (the sandbox runner, a tensor-parallel shard) records nothing in the actor, so it reports None. The regexes are deleted.
  • Percentage: gpu_peaks also returns max_memory_reserved, GPUMemMetric writes it as peak_reserved_bytes, and request_meta computes max_mem_pct_by_gpu from it. meta gets no new key: max_mem_by_gpu stays allocated bytes above the weights, and the reserved bytes only feed the percentage, whose meaning (100 means the request filled what was left for it) is now true.
  • Docs: the percentage in docs/reference/schemas.md and docs/runbooks/model-oom-on-deploy.md, and the field list in docs/developing/telemetry-internals.md.

Why the observer rather than the figures in the message: the message gives the right number too, but only by parsing four figures that torch rounds to two decimals in changing units (and "allowed" appears only when a fraction is set), and a runner's OOM text would still parse. The observer passes exact integers and fires only in this process, so the shortfall is three lines and the runner case needs no code. It is a private call, torch._C._cuda_attach_out_of_memory_observer: present from torch 2.0 through main, tested by torch itself (test_notifies_oom in test/test_cuda.py), and since 2.1 torch releases the allocator lock before calling observers. If a torch release removes it, the actor fails to start, and test_reads_torchs_figures_at_the_refusal fails first.

Tests

tests/test_utils.py:

  • test_reads_torchs_figures_at_the_refusal (skips without a GPU): attaches the observer, leaves a cached block, asks for 1 TiB, and checks the shortfall against torch's own allowed and reserved bytes.
  • test_forgets_a_refusal_from_before_the_request: fails without the clear in gpu_baselines.
  • test_an_oom_in_another_process_reports_no_shortfall: a RunnerError carrying OOM text reports None; dev reports a number.
  • test_an_oom_adds_the_shortfall and test_floors_at_zero now feed the recorded figures. The tests of the message parsing are deleted.
  • test_percent_is_against_the_headroom_the_request_had: 40% from reserved bytes, where allocated bytes give 20%.

Measured with the review's request mix (gpt2 bf16, 2 GiB cap applied by set_process_limits, 3 seeds of 80 requests) run through gpu_baselines, the block on a thread of its own, gpu_peaks, request_meta and the actor's cleanup. "Exact" is a second observer in the script; the message's own figures (allocated plus reserved-but-unallocated) agree with it to within -0.4 to +7.6 MiB, the rounding.

dev this branch
shortfall over 24 OOMs, reported median (exact 178.0 MiB) 609.6 MiB 178.0 MiB
reported minus exact, per OOM +432 to +581 MiB 0 on all 24
a runner's OOM {"0": 0} None
request (batch, tokens, saves) dev percentage this branch reserved bytes over headroom
12, 512, layers 51.91 67.92 67.92
16, 512, logits 61.83 84.41 84.41
16, 512, layers 69.18 89.75 89.75
12, 768, logits 69.76 95.10 95.10

Full suite (python -m pytest tests): 426 passed, 95 skipped, 2 failed. The two failures are tests/test_sandbox_shared_memory.py::test_a_tensor_made_on_the_cpu_crosses_by_value and ::test_a_runner_killed_mid_request_leaves_the_host_as_it_was, the same two that fail in a full run of unmodified dev; that file passes 12 of 12 run alone. tests/test_docs.py passes.

Not run against a live stack: TestRemoteMeta and the OOM tests in tests/test_nnsight_remote.py skip without a server. A comment in that file said the observer fires on OOMs torch recovers from; it does not, and the comment now says why the test is there. nnsight's suite was not run; nnsight is unchanged.

Left as it is

nnsight's docs/remote/ndif-overview.md ("What the job cost") has two sentences this makes false: the max_mem_pct_by_gpu row ("max_mem_by_gpu against the headroom the job had") and "It is approximate, and None when the server could not tell which card refused."

🤖 Generated with Claude Code

JadenFiotto-Kaufman and others added 2 commits December 2, 2026 22:00
alloc_shortfall_by_gpu was requested - (budget - max_memory_reserved). The
peak reserved includes cache the allocator released before it refused, so
over 24 OOMs on gpt2 under a 2 GiB cap it reported a median of 610 MiB
against an exact 178 MiB. The requested size and the card came out of the
message by regex, and a sandbox runner's OOM, which arrives as text, was
compared against this actor's budget and reserved bytes.

torch's out-of-memory observer passes the card, the requested bytes and the
allowed bytes, once per OOM it raises, after its cache release and retry;
reading memory_reserved in the callback gives the reserved bytes the
refusal was measured against. The actor attaches it once after loading,
gpu_baselines clears what it recorded, and alloc_shortfall reads it. An
OOM raised in another process records nothing here, so it reports None.
The regexes go.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
max_mem_pct_by_gpu was (peak allocated - baseline) / headroom, but the
per-process cap (set_per_process_memory_fraction) is enforced on reserved
bytes. On gpt2 under a 2 GiB cap, requests shown at 70% had used 90% of
what the cap allowed. The gpu_mem metric had no reserved figure either, so
fragmentation could only be read out of OOM text.

gpu_peaks now also reads max_memory_reserved, GPUMemMetric writes it as
peak_reserved_bytes, and request_meta computes the percentage from it.
meta keeps its keys and units: max_mem_by_gpu stays allocated bytes above
the weights, and the reserved bytes only feed the percentage.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant