Skip to content

Studio: set GGML_CUDA_ENABLE_UNIFIED_MEMORY only where it gains memory - #9884

Merged
danielhanchen merged 3 commits into
unslothai:mainfrom
oobabooga:worktree-apu-unified-memory-carveout
Aug 31, 2026
Merged

danielhanchen merged 3 commits into
unslothai:mainfrom
oobabooga:worktree-apu-unified-memory-carveout

Conversation

@oobabooga

Copy link
Copy Markdown
Member

Problem

Studio automatically sets GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 for AMD unified-memory APUs (gfx1150/gfx1151/gfx1152).

On HIP, this changes allocations from hipMalloc to hipMallocManaged. 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

  • Add _rocm_selected_pool_mib() to sum the selected APUs' carve-outs. It returns None unless every selected device is present, readable, and a unified-memory APU.
  • Add _unified_memory_would_help() to enable managed allocation only when available host RAM exceeds the combined carve-out.
  • Apply that decision to the initial launch, arch-gate narrowing, and arch-crash retry.
  • Recompute after device narrowing so an APU-only child can enable the setting and a discrete-only child can disable it.
  • Keep _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_MEMORY value is still preserved, while UNSLOTH_DISABLE_UNIFIED_MEMORY=1 still 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.
  • Available host RAM was 48,937 MiB.
  • _unified_memory_would_help() returned False.
  • A written 24 GiB hipMalloc allocation increased VRAM use by 24.17 GiB and barely changed available host RAM.
  • The equivalent managed allocation did not increase VRAM use and reduced available host RAM by 24.11 GiB.
  • Qwen3-Next-80B-A3B UD-Q2_K_XL loaded in 10 seconds without the variable. With it enabled, the process was killed after 338 seconds by the OOM killer.

Test results:

  • Focused unified-memory and arch-gate tests: 248 passed, including mixed-device selection and narrowing in both directions.
  • Broader llama.cpp, GPU, AMD, and ROCm tests: 5,376 passed and 57 skipped. The 12 test_gpu_init_crash_message.py failures also reproduce on the merge base.
  • ruff check and git diff --check pass.

Limitations

  • No small-carve-out Strix Halo was available for a live positive-path test.
  • No mixed APU and discrete ROCm host was available. That behavior is covered by unit and launch-level tests.
  • gfx1150 and gfx1152 were not tested live, but use the same classifier and decision path.

The Qwen3.8-Flash-Next gibberish report is unrelated. It was a separate qwen4exp issue fixed in unslothai/llama.cpp#134.

@chatgpt-codex-connector chatgpt-codex-connector Bot 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.

💡 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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge 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 👍 / 👎.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.

@oobabooga

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Delightful!

Reviewed commit: 8b95622d16

ℹ️ 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".

@danielhanchen

Copy link
Copy Markdown
Member

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.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants