Repository navigation
Studio: set GGML_CUDA_ENABLE_UNIFIED_MEMORY only where it gains memory - #9884
danielhanchen merged 3 commits into
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 427bcc1216
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| host_mib = LlamaCppBackend._available_system_memory_mib() | ||
| if not host_mib: | ||
| return False | ||
| return int(host_mib) > int(pool_mib) |
There was a problem hiding this comment.
Compare available memory on both allocation paths
When another process already occupies part of the APU carve-out, this compares currently available host RAM with the carve-out's total capacity. For example, with a 64 GiB carve-out that has only 24 GiB free and 48 GiB of available host RAM, managed allocations provide twice the usable capacity, but this returns false and leaves the launch on the constrained HIP pool. Compare host availability against the selected devices' available pool capacity, or otherwise use equivalent total/free measures on both sides.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Measured on a gfx1151 Strix Halo (ROCm 7.2.1, 64 GiB carve-out, 62 GiB host RAM). Holding 24 GiB of the carve-out in another process moves mem_get_info free from 65380 to 40658 MiB while props.total_memory stays 65536 MiB and MemAvailable moves only 49062 to 48314 MiB. Asking for 45.7 GiB in that state fails on hipMalloc ("HIP out of memory ... 39.71 GiB is free") and succeeds on hipMallocManaged. So the reading is total against available as you describe, and the capacity claim holds.
The asymmetry is the decision rather than an oversight, because the two pools fail differently. The same probe over-asked each one: hipMalloc 8 GiB past free raised OutOfMemoryError with the process intact, and a touched hipMallocManaged 6 GiB past MemAvailable was SIGKILLed. That kill is the failure this PR was opened for, so both deviations from a symmetric comparison lean toward the pool a miss is recoverable in. The managed allocation that did succeed above drove MemAvailable from 47961 to 1144 MiB, which is how little margin the host side leaves once it is chosen.
Pricing the device side on live free memory would also tie the decision to whatever holds the carve-out at probe time, seconds before llama-server allocates. If that occupancy clears first, a free-based comparison has moved every allocation into the smaller pool for nothing. Total on both sides is not an improvement either: MemTotal is 63936 MiB against a 65536 MiB carve-out here, so it answers the same wherever it matters while no longer accounting for host RAM that is already in use.
8b95622 records this at _unified_memory_would_help so the choice is visible where the comparison happens.
| return None | ||
| from core.training.worker import _rocm_classify_unified_memory | ||
|
|
||
| wanted = None if gpu_indices is None else set(gpu_indices) |
There was a problem hiding this comment.
Honor explicit llama.cpp device pins in the pool check
When placement is supplied through advanced --device arguments or LLAMA_ARG_DEVICE rather than gpu_ids, gpu_indices remains None, so this treats every torch-visible GPU as selected. On a mixed APU/dGPU host whose explicit llama.cpp pin targets only the APU, _rocm_selected_pool_mib() encounters the unselected discrete card and fails closed, leaving unified memory disabled even when the APU's carve-out is smaller than host RAM. Resolve the effective argv/environment device selection before deciding which pools to inspect.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
gpu_indices carries Studio's own device selection, so an advanced-args --device or an inherited LLAMA_ARG_DEVICE leaves it None by construction and the launch keeps control of its own devices, which is what the pin comment at the masking site states. Failing closed there is the policy for this variable rather than a gap: it is process-wide, so a selection that cannot be shown to be APU-only must not turn managed allocation on for a discrete card that it slows down.
Deriving the selection from argv means mapping ggml device names to ROCm physical ids through the active visibility mask, and the failure mode of getting that mapping wrong is enabling managed allocation on a launch that contains a discrete GPU, which is the outcome this change exists to prevent. Getting it right buys the opposite of a defect: the APU keeps its carve-out and the load is not moved into host RAM it did not need.
The common form of this shape is already covered. When the installed build has no kernels for the discrete card the arch gate pins the child to the APU, and the recompute added here turns the setting on for that child. What remains is a mixed host whose build covers both archs and whose user hand-pins through advanced args.
|
@codex review |
|
Codex Review: Didn't find any major issues. Delightful! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Confirmed main still turns GGML_CUDA_ENABLE_UNIFIED_MEMORY on from the APU arch alone in studio/backend/core/inference/llama_cpp.py, so a large carve-out with less host RAM ends up in the smaller pool, and the recompute after narrowing and after the arch-crash retry looks right. Will get this reviewed. |
Problem
Studio automatically sets
GGML_CUDA_ENABLE_UNIFIED_MEMORY=1for AMD unified-memory APUs (gfx1150/gfx1151/gfx1152).On HIP, this changes allocations from
hipMalloctohipMallocManaged. Managed allocations use host RAM instead of the ROCm-reported device pool; they do not combine the two pools. On a Strix Halo with a 64 GiB carve-out and less available host RAM, enabling the variable therefore reduces usable capacity. A 28 GiB model loaded in 10 seconds without it but was killed by the OOM killer with it.The useful default depends on the configured carve-out, not the GPU architecture alone.
Changes
_rocm_selected_pool_mib()to sum the selected APUs' carve-outs. It returnsNoneunless every selected device is present, readable, and a unified-memory APU._unified_memory_would_help()to enable managed allocation only when available host RAM exceeds the combined carve-out._amd_apu_wants_unified_memory()unchanged for APU RAM sizing and oversize-load warnings.The setting is process-wide, so mixed APU and discrete launches fail closed. A user-provided
GGML_CUDA_ENABLE_UNIFIED_MEMORYvalue is still preserved, whileUNSLOTH_DISABLE_UNIFIED_MEMORY=1still clears it.The decision is based on the hardware configuration rather than model size. This avoids using a model-size estimate to authorize moving every allocation into a potentially smaller pool.
Verification
Live testing used an AMD DevLab gfx1151 runner with a 64 GiB carve-out and 62 GiB of system RAM:
_rocm_selected_pool_mib()reported 65,536 MiB._unified_memory_would_help()returnedFalse.hipMallocallocation increased VRAM use by 24.17 GiB and barely changed available host RAM.Test results:
test_gpu_init_crash_message.pyfailures also reproduce on the merge base.ruff checkandgit diff --checkpass.Limitations
The Qwen3.8-Flash-Next gibberish report is unrelated. It was a separate
qwen4expissue fixed in unslothai/llama.cpp#134.