mirror of
https://github.com/Niko1221/Strata.git
synced 2026-12-03 19:03:30 +07:00
[PR #3913] [Engine] HIP/Linux: keep the GPU's host memory out of reclaim: coarse-grained GTT complement, or mlock (both opt-in) #4832
Labels
No labels
needs-format
pull-request
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Niko1221/Strata#4832
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Niko1221/Strata/pull/3913
Author: @alexanderi96
Created: 10/24/2026
Status: 🔄 Open
Base:
main← Head:pr-hip-mlock📝 Commits (5)
984f2fbHIP/Linux: mlock host memory registered or allocated for the GPU (opt-in: STRATA_HIP_MLOCK=1)15bc034HIP/Linux: allocate the expert complement coarse-grained (opt-in: STRATA_HIP_ARENA_NONCOHERENT=1)ef6a766docs (AMD_HIP): the GTT route's sizing, the start-up paths it does and does not cover, measured67b8859HIP/Linux: in GTT mode (STRATA_HIP_ARENA_NONCOHERENT=1) allocate the complement in one go when the RAM available covers it237d2cbdocs (AMD_HIP): GTT route on ROCm 10.2+ (HSA_SVM_FOR_PAGED_MEM=0), the TTM page pool, the one-go start when file cache fills the RAM📊 Changes
12 files changed (+391 additions, -18 deletions)
View changed files
📝
CMakeLists.txt(+10 -0)📝
docs/AMD_HIP.md(+107 -0)📝
docs/TROUBLESHOOTING.md(+5 -1)📝
include/strata/hip_compat/cuda_runtime.h(+6 -9)📝
include/strata/platform/hip_compat/strata_hip.h(+6 -5)➕
include/strata/platform/hip_host_lock.hpp(+109 -0)📝
serve/server.py(+27 -1)📝
serve/test_server.py(+17 -0)📝
src/core/expert_source.cpp(+44 -0)➕
src/core/hip_host_lock_test.cpp(+53 -0)📝
src/core/verify.cpp(+3 -2)📝
tests/core/file_expert_source_test.cpp(+4 -0)📄 Description
Problem and result
On Linux, ROCm maps host memory for the GPU through a KFD userptr (HMM) and does not pin the pages:
hipHostRegister,and
hipHostMallocunder ROCr's defaultHSA_USERPTR_FOR_PAGED_MEM, leave them pageable. On a GPU without XNACK (everyRDNA card) any invalidation of such a page (reclaim, swap, compaction, NUMA balancing) evicts all of the process's GPU
queues until amdgpu validates the range again. Under memory pressure that lasts seconds to minutes, and the engine
reports
no progress for 60 s ... 0 layers servedorverify: timed out(#649, #1705, #4862, #4866). The usualworkaround,
HSA_USERPTR_FOR_PAGED_MEM=0alone, makes the expert complement fine-grained GTT and reads prompts about2.4x slower on an RX 7900 XTX.
This PR adds two opt-in ways to keep that memory out of reclaim. Both are off by default; with neither set nothing
changes.
HSA_USERPTR_FOR_PAGED_MEM=0+STRATA_HIP_ARENA_NONCOHERENT=1(+HSA_SVM_FOR_PAGED_MEM=0on ROCm 10.2 and later;GPU_PINNED_MIN_XFER_SIZE=1048576recommended)STRATA_HIP_MLOCK=1cudaHostRegister/cudaHostAlloc/cudaMallocHostof the HIP build alsomlocks its rangettm.pages_limitset at boot so GTT holds the complement, the KV copies and ROCr's buffersvm.compact_unevictable_allowed=0/dev/zeromappings (short evictions remain: 10-600 ms here)Measured on one RX 7900 XTX (below): queue eviction 0 ms with route A against 10-600 ms with route B, and a stall
within two minutes with neither; prompt and decode speed the same for A and B.
Related discussion
Refs #649 (the mechanism), #1705 (closed with the
HSA_USERPTR_FOR_PAGED_MEM=0workaround), #750 and #920 (the docssection this extends), #2729 (SVM restore hangs), #4862 (userptr restore storms before each stop on 2x RX 9070 XT),
#4866 (NUMA balancing; kprobe trace of the
/dev/zeromappings; probe results for both routes on a two-socket host).#4867 (
MPOL_LOCAL) is complementary for two-socket hosts that stay on route B. The problem and a first per-caller fixwere found by @Ben1078 in their fork (https://github.com/Ben1078/Strata, branch rdna3-moe-offload-perf). The kernel
behaviour (non-XNACK queue eviction on any invalidation, ROCm/TheRock#7956) is unchanged.
Approach
Commit 1, route B (
STRATA_HIP_MLOCK=1):include/strata/platform/hip_host_lock.hpp(new, header-only):host_register/host_unregister/host_alloc/host_freecall the HIP function, thenmlock(ormunlockfirst) the range, with a map of what was locked under amutex. An
mlockrefusal (RLIMIT_MEMLOCK) prints one line naming the fix and changes nothing else. Non-Linux: thefunctions only forward.
include/strata/hip_compat/cuda_runtime.h,include/strata/platform/hip_compat/strata_hip.h(gfx906): thecudaHostRegister,cudaHostUnregister,cudaHostAlloc,cudaMallocHost,cudaFreeHostmacros point there.src/core/verify.cpp: the verify window's coherent mapped buffers go throughhost_alloc.serve/server.py:/healthreportslocked_mib(the engine's VmLck) andkfd_evicted_ms, how long amdgpu has keptthe engine's queues evicted (the largest
/sys/class/kfd/kfd/proc/<pid>/stats_<gpu_id>/evicted_ms;nullin acontainer with its own PID namespace). Tests in
serve/test_server.py.src/core/hip_host_lock_test.cpp(HIP + Linux, no GPU): the lock / unlock bookkeeping against VmLck.Commit 2, route A (
STRATA_HIP_ARENA_NONCOHERENT=1):src/core/expert_source.cpp: the complement is allocated withhipHostMallocNonCoherent. It is written once at loadand only read by the GPU afterwards, so it does not need the fine-grained coherence
strata_host_allocforces forthe flag words two sides spin on.
Commit 4, route A's start-up (same switch):
src/core/expert_source.cpp: when the RAM free is short, the #1250 rule used to take the paced path, a registration(an MMU-notifier range, SVM or userptr), so route A lost its effect exactly when the file cache filled the RAM. amdgpu
allocates GTT with
__GFP_RETRY_MAYFAIL(reclaims the cache, refuses instead of calling the OOM killer), so with theswitch the decision uses the RAM available (complement plus the reserve, the paced loop's own rule) and the complement
is allocated in GTT in one go. A refused GTT allocation still takes the paced path;
STRATA_PIN_GUARD=2and NUMAplacement still pace. Test:
tests/core/file_expert_source_test.cpp(test_pin_pacing).Docs (commits 3 and 5):
docs/AMD_HIP.md("Linux: verify timeouts while the kernel reclaims host memory": bothroutes, host settings, what is not covered, measurements;
HSA_SVM_FOR_PAGED_MEM=0from ROCm 10.2, the TTM page pool,where ROCm places the GTT complement with two cards visible),
docs/TROUBLESHOOTING.md(both routes).Host setup for route A
ttm.pages_limitonce, when amdgpu loads(
amdgpu_ttm.c,amdgpu_amdkfd_gpuvm.c). The default is half the RAM. A write to/sys/module/ttm/parameters/pages_limitafter boot changes neither, so set it on the kernel command line(
ttm.pages_limit=<4 KiB pages>), or in modprobe.d when that is in the initramfs before amdgpu loads. Check/sys/class/drm/card*/device/mem_info_gtt_totalafter the reboot. The KFD limit is one for the whole system (allGPUs, all processes).
Here, at 256K context with int8 KV, that rest is about 5.7 GiB (
mem_info_gtt_used38.2 GiB with a 32 GiB budget).Keep the limit above budget + 6 GiB or so: with a 40 GiB budget under a 44 GiB limit the complement fitted, the
adaptive tier's exchange buffers did not ("--adapt-async 1 is off (the exchange buffers could not be page-locked)"),
and the engine then crashed during start-up (a segfault inside
libamdhip64, under a pageablehipMemcpyAsyncof thestart-up self-test, so most likely clr's staging buffer not fitting either).
page-locked and mapped: the GTT allocation went through;GTT mode: ... available covers the complement and the reserve: one GTT allocation: free RAM was short but the RAMavailable covered it, so the complement still went to GTT in one go (commit 4);
page-locking refused (...); trying pageable memory: the complement itself is over the limit (it then paces);page-locked after taking the pages in steps: even the RAM available was below the complement plus a reserve, orthe GTT allocation was refused. This paced path registers the memory (an MMU-notifier range, SVM or userptr), so
route A does not apply there (measured below). Lower the budget so that it takes the first paths.
HSA_USERPTR_FOR_PAGED_MEM=0alone no longer gives GTT; host allocations goto SVM unless
HSA_SVM_FOR_PAGED_MEM=0is set too (80-164 s of queue eviction per benchmark run here without it).ttm.page_pool_size, half the RAM by default), outsideMemFreeandMemAvailable: the next start lowered the budget from 32 to 18.4 GiB here.ttm.page_pool_size=262144(1 GiB) returns them at stop.
boot.kernelParams); the way to set kernel and module parameters differs on other distributions.Validation
Machine: one RX 7900 XTX (gfx1100) on PCIe 4.0 x16, Ryzen 7 5800X, 64 GB DDR4, NixOS, Linux 6.18.53, Docker with
--ulimit memlock=-1and--pid host. Engine built with setup'sbuild_engine_hip(ROCm 7.10.0a20251120 TheRockwheels, gfx1100; the 4 November runs on ROCm 10.0.0 and on TheRock 10.2.0a20261022, see each table). Model Swift 1.5 (Qwen3.8-Flash-Next) IQ3_S native pack,
--resident-budget-gib 32 --kv int8 --kv-resident 32768 --max-context 262144 --prefill auto:32768 --spec 8, MTP.Passed:
On v0.2.8 (
870aedd9, engine 0.1.54) + commit 1: HIP build,hip_host_lock_test(memlock unlimited and 1 MiB: ok;0: skip with the one-line warning),
python -m unittest serve.test_server, a start withSTRATA_HIP_MLOCK=1.On v0.2.9 (
c62d5e3a, engine 0.1.55) + both commits: HIP build; starts and daily serving with each route, with theexpected start lines and the
/healthfields.Route B against neither, stress A/B (same image and config, only
STRATA_HIP_MLOCKchanged, B0 B B0 B, each stoppedat its first stall or after 30 min; 4 conversations of 30K-220K-token prompts, a second process holding RAM down to
~5 GiB available, an 18 GB file read in a loop;
vm.compact_unevictable_allowed=0in both arms; measured on 0.2.3.1):Route A against route B (v0.2.9, budget 32 GiB, GTT 44 GiB, 1 GB of RAM or less free, five fresh 30K-token prompts
per run, then 6 GB taken by another process with the engine idle):
Route A's start-up paths (v0.2.9,
HSA_USERPTR_FOR_PAGED_MEM=0+STRATA_HIP_ARENA_NONCOHERENT=1, two fresh24K-token prompts, then 14 GiB taken by another process for 40 s with the engine idle, then again during a prompt):
kfd_evicted_msafter load / at the endpage-locked and mapped(budget 32)STRATA_PIN_GUARD=2(budget 32)Commit 4, a start with about 55 GB of unrelated files read just before (TTM pool at 1 GiB, 0.4-0.5 GiB free after the
engine drops its own cache, budget 32 GiB, one
strata_bench.py --longrun each; before = v0.2.9 + commits 1-3 onROCm 10.0, after = v0.2.10 + these commits on ROCm 10.2):
kfd_evicted_msat the endpage-locked after taking the pages in stepsGTT mode: 52.20 GiB available ...,page-locked and mappedThe eviction counter is the result: the stalls show in the lowest runs, the medians at 32K and 63K are within 2-4%,
and part of the 16K gap is the newer build.
Two cards: @tproos, 2x RX 9070 XT layer split with route A, 37 requests of long MCP sessions without the
-15thatended every release run after 4-8 tool calls. He still saw the eviction counter grow slowly; the two causes found
since (clr's page-locks, the paced start) are covered by
GPU_PINNED_MIN_XFER_SIZEand commit 4, not yet confirmedon his host. Here, an RX 7900 XTX pair (x16 + chipset x4) layer split on v0.2.10,
kfd_evicted_ms0 infive benchmark runs.
Speed with route A,
tools/strata_bench.py --long, 2 rounds: prompt 4K ~1,560, 16K ~2,290, 32K ~2,350 tok/s; decodeunchanged.
On a two-socket host (@vivienfanghuagood, #4866, W7900D, eviction probe): NUMA balancing 48.3 s and page migration
34.3 s of queue eviction with route B's order (register, then mlock); 0 and 0 with route A, coherent or not.
Daily use (not a controlled test): route B since 23 October, route A since the evening of 2 November. From 28
October to 3 November the engine log has about 4,000 requests over 66 engine starts and 0
no progressstops orverify timeouts. The last stop in that log is from 23 October, on the official image during the stress hour above.
Failed:
none observed.
Not run:
page-locking refusedpath itself (a complement larger than the whole GTT limit); the start-up crash when thelimit runs out after the complement was traced only to that copy (
libamdhip64has no symbols here).Limits
covered; ROCr's own allocations are not routed through the wrappers.
or GTT is refused, does not use it (the evictions come back there); a limit too tight for the rest of the engine's host allocations ends in a crash at start-up, not a clean
error.
AI disclosure
The patch, the tests and this text were prepared with an AI coding assistant (Claude, Anthropic) working on my machine
at my direction. The measurements are from runs on my hardware; logs are available on request. Credit for finding the
problem and the first per-caller fix goes to Ben1078's fork.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.