Skip to content

fix(api_server): run the front-end workers on jemalloc so their memory stops growing - #3840

Open
yhl-amd wants to merge 2 commits into
ROCm:mainfrom
yhl-amd:hyi/frontend-jemalloc
Open

yhl-amd wants to merge 2 commits into
ROCm:mainfrom
yhl-amd:hyi/frontend-jemalloc

Conversation

@yhl-amd

@yhl-amd yhl-amd commented Nov 25, 2026 •

Copy link
Copy Markdown
Collaborator

With --api-server-count > 1 every front-end worker renders and tokenizes long
prompts on many threads (the default executor and the tokenizers' rayon pool).
glibc malloc gives each thread its own arena and keeps freed memory there, so a
worker's RSS keeps growing over a long run. jemalloc hands freed pages back to the
OS and leaves the thread count, and so the tokenizer parallelism, unchanged.

_start_frontend_workers now sets LD_PRELOAD to libjemalloc.so.2 only while it
spawns the workers (an existing LD_PRELOAD is kept behind it) and restores the
environment right after. The API process and the engine processes, started before,
keep their allocator. jemalloc is looked up by soname (ctypes.util.find_library),
the same way the dynamic linker resolves LD_PRELOAD, so no distro paths are
listed. When the host has no jemalloc the workers fall back to glibc malloc with
one warning; the server starts as before. Since ld.so only warns when a preload
fails, each worker logs the allocator it actually mapped (from /proc/self/maps).

No new options or environment variables. --api-server-count 1 is unchanged.

Tests

  • tests/entrypoints/test_api_server_helpers.py (preload set only while
    spawning, an existing preload kept and restored, an existing jemalloc preload
    left alone, glibc fallback with a warning, restore on a failed spawn, the
    workers' start() runs with jemalloc in LD_PRELOAD, the mapped allocator is
    read from the process maps).
  • In the ATOM nightly image find_library("jemalloc") returns libjemalloc.so.2
    and LD_PRELOAD=libjemalloc.so.2 maps /usr/lib/x86_64-linux-gnu/libjemalloc.so.2.
  • GPU, validated on the previous head 66ea35f (on its base main, gfx950,
    DeepSeek-V4.1-Flash DEP8, --api-server-count 4): the 4 front-end workers map
    jemalloc and no other process does; GSM8K not below main; under a 60 min
    synthetic load the workers' RSS stops growing after about 20 min and settles
    about 15% below main's.

Performance

DeepSeek-V4.1-Flash DEP8, 8x MI355X, InferenceX AgentX c512, --api-server-count 4, 3600 s. Measured with the same change on an earlier main plus local patches; not re-measured on this head. jemalloc: 1 run; glibc: 2 runs on a different machine.

glibc (2 runs) jemalloc (1 run)
RSS per front-end worker after 1 h 17-18 GiB, still growing (+11.1 GB over 51 min, all four) 4.1-4.8 GiB, about flat (about +1 GB over the last 30 min, with drops)
total throughput / GPU (tok/s) 363,351 365,847 (+0.7%, cross-machine, treated as unchanged)
interactivity p90 (tok/s/user) 131.6 132.9
TTFT p50 / p90 1.19 / 4.59 s 1.09 / 3.95 s

Alternatives tried on the same setup: capping glibc at 4 arenas plus a 16-thread rayon pool kept memory flat but cost 1.4% throughput and +15% TTFT p50; 4 arenas alone cost 9.0%. Runs longer than 1 h are not done yet.

🤖 Generated with Claude Code

…y stops growing

With --api-server-count > 1 every front-end worker renders and tokenizes long
prompts on many threads (the default executor and the tokenizers' rayon pool).
glibc malloc gives each thread its own arena and keeps freed memory there, so
a worker's RSS keeps growing over a long run. jemalloc hands freed pages back
to the OS and leaves the thread count, and so the tokenizer parallelism,
unchanged.

_start_frontend_workers now sets LD_PRELOAD to libjemalloc.so.2 only while it
spawns the workers (an existing LD_PRELOAD is kept behind it) and restores the
environment right after. The API process and the engine processes, started
before, keep their allocator. When the host has no libjemalloc.so.2 the
workers fall back to glibc malloc with one warning; the server starts as
before. The allocator the workers got is logged once.

No new options or environment variables. --api-server-count 1 is unchanged.

Tests: tests/entrypoints/test_api_server_helpers.py (preload set only while
spawning, an existing preload kept and restored, an existing jemalloc preload
left alone, glibc fallback with a warning, restore on a failed spawn).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions

Copy link
Copy Markdown
Contributor

🏷️ CI Guide

Runs automatically on every eligible PR before approval:

  • ✅ Pre Checkin: Black, Ruff, catalog schema validation, non-GPU unit tests

Heavy model tests:

  • ✅ Gate checks are published on every PR so required checks do not stay missing
  • ✅ Expensive jobs run only after the PR is approved, or when a matching ci:* label is present
  • ✅ Pre Checkin must pass before approved heavy jobs continue
  • ✅ Without a matching label, expensive jobs also require a relevant path
  • ✅ A matching ci:* label is an explicit request and can open the gate even when paths would otherwise skip it

Path scope:

  • Native ATOM uses an ignore list: docs/markdown and plugin or Mesh paths are skipped
  • vLLM and SGLang use include lists: their own plugin code under atom/plugin/, plugin CI scripts, model list and release images. A core-only change does not run them; add ci:vllm / ci:sglang to ask for them
  • ATOMesh uses an include list: Mesh code, the ATOMesh accuracy workflow and its 1P1D launcher, plus the shared models_accuracy.json and accuracy_to_dashboard.py. ATOMesh benchmarks run after merge or on dispatch, not on PRs
  • Relevant path alone does not start expensive jobs; approval still opens the automatic path-scoped gate
Label Tests
ci:full Run all heavy PR model tests: native ATOM, vLLM, SGLang, and ATOMesh
ci:atom Run native ATOM model accuracy tests
ci:vllm Run ATOM vLLM OOT model accuracy tests
ci:sglang Run ATOM SGLang model accuracy tests
ci:mesh Run the ATOMesh accuracy tests, standalone and 1P1D, for Mesh-related changes

Without a matching ci:* label, heavy jobs are skipped when the path is not relevant or the PR is not approved.
Add labels via the sidebar or gh pr edit 3840 --add-label <label>

@yhl-amd
yhl-amd marked this pull request as ready for review November 25, 2026 10:24

@valar-cop valar-cop left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a few small things..

perf/rss numbers on the pr head? description says still in progress and numbers are from earlier main + local patches

ci ATOM Test Gate failed — main is already broken the same way (medium): The collected log is empty (0 bytes), so the failing step is unknown here. The same gate fails on main at 01:53, 09:15 and 09:40 the same day and is 3 success / 3 failure on other recent runs, so it predates this two-file api_server/test change.

same job on main: ['2026-11-25T09:40 failure', '2026-11-25T09:15 failure', '2026-11-25T01:53 failure'], on other recent runs: {'success': 3, 'failure': 3}
.valar/ci/job_139149834085.log: 0 bytes
next: Rely on a re-run or the next main run; nothing to fix in this PR. https://github.com/ROCm/ATOM/actions/runs/45594693078/job/139149834085

)
proc.start()
processes.append(proc)
logger.info(

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this logs the intent, not what actually loaded. fallback soname is never verified and ld.so just warns and runs glibc, so maybe log the real allocator from the worker once?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done in 73309e1: each worker now logs the allocator it actually mapped (read from /proc/self/maps, falls back to "glibc malloc"); the parent log line now only says what was put in LD_PRELOAD.

monkeypatch.delenv("LD_PRELOAD", raising=False)


def test_preload_is_set_only_while_the_workers_are_spawned(monkeypatch, no_preload):

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nothing asserts the spawn loop runs inside the with, so removing the wrapper keeps tests green. add a test that records LD_PRELOAD in fake start()?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added test_the_workers_are_started_with_jemalloc_preloaded in 73309e1: it runs _start_frontend_workers with a fake Process.start() that records LD_PRELOAD; replacing the with by a nullcontext makes it fail.

Comment thread atom/entrypoints/openai/api_server.py Outdated
_JEMALLOC_CANDIDATES = (
"/usr/lib/x86_64-linux-gnu/libjemalloc.so.2",
"/usr/lib/aarch64-linux-gnu/libjemalloc.so.2",
"/usr/lib64/libjemalloc.so.2",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

???

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, the path list was unnecessary. Removed in 73309e1: jemalloc is now found by soname with ctypes.util.find_library("jemalloc") and LD_PRELOAD=libjemalloc.so.2 is resolved by ld.so like any other library (checked in the ATOM nightly image: it maps /usr/lib/x86_64-linux-gnu/libjemalloc.so.2).

@valar-cop

Copy link
Copy Markdown

@yhl-amd

yhl-amd commented Nov 25, 2026

Copy link
Copy Markdown
Collaborator Author

GPU re-validation on this head (66ea35fbc), compared with its base main cce9f4b23 in the same image (rocm/atom-dev:nightly_202611241524), 8x MI355X (gfx950), DeepSeek-V4.1-Flash DEP8, --api-server-count 4.

main cce9f4b23 this PR
allocator log line none Front-end workers [...] use allocator /usr/lib/x86_64-linux-gnu/libjemalloc.so.2
processes mapping libjemalloc (/proc/<pid>/maps) none the 4 front-end workers only (not the API process, the 8 engine cores, the 8 ranks or the inductor workers); same in both runs
tests/entrypoints (CPU) 3307 passed, 3 failed 3312 passed, 3 failed
GSM8K 5-shot, 1319, c64 (flexible / strict), 2 runs 0.9280 / 0.9272, 0.9257 / 0.9265 0.9287 / 0.9280, 0.9227 / 0.9227
60 min load: 4 workers' RSS, start -> end 5.49 -> 6.94 GB 5.18 -> 5.87 GB
growth per 10 min window +909 / +458 / +71 / +10 / +4 / +1 MiB +470 / +248 / +1 / -21 / +16 / -25 MiB
  • The 3 tests/entrypoints failures are in test_structured_output.py and fail the same way on main.
  • Load: 64 concurrent completions, prompts of 200-12000 words, half streamed, 16-256 output tokens, six 10 min windows (about 14.5k requests each, no errors).
  • With jemalloc the workers' RSS stops growing after about 20 min and settles about 270 MiB per worker (about 15%) below main. Under this synthetic load main also levels off after about 30 min, so this run does not reproduce the multi-hour growth of the agentic run in the description. It only shows that the change holds the workers flat and lower.

Result: pass.

…orker loaded

Drop the list of distro paths: LD_PRELOAD resolves a soname like any other
library, so ctypes.util.find_library("jemalloc") is enough. ld.so only warns
when a preload fails, so each front-end worker now logs the allocator it
actually mapped. Add a test that the workers are started inside the
LD_PRELOAD window.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@yhl-amd
yhl-amd requested a review from valarLip November 25, 2026 16:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants