Skip to content

Studio: give GGML_CUDA_ENABLE_UNIFIED_MEMORY a real off switch (#8651) - #8680

Merged
danielhanchen merged 3 commits into
mainfrom
unified-memory-escape-hatch-8651
Aug 13, 2026
Merged

danielhanchen merged 3 commits into
mainfrom
unified-memory-escape-hatch-8651

Conversation

@danielhanchen

Copy link
Copy Markdown
Member

Closes #8651.

What happens today

ggml/src/ggml-cuda/ggml-cuda.cu has exactly one use site for the variable, in ggml_cuda_device_malloc:

if (getenv("GGML_CUDA_ENABLE_UNIFIED_MEMORY") != nullptr) {
    err = cudaMallocManaged(ptr, size);
} else {
    err = cudaMalloc(ptr, size);
}

Only the variable's PRESENCE is tested, never its value. GGML_CUDA_ENABLE_UNIFIED_MEMORY=0 therefore ENABLES managed memory. Studio set it with env.setdefault(..., "1") and the comment said "setdefault so a user value wins", which was true only in the sense that Studio would not overwrite a value you set. There was no value anyone could set that turned it off, so the reporter's only route was patching Studio's source.

That is a defect independent of whether the default is right for gfx1150/gfx1151, so it is what this PR fixes. The default is unchanged: #5301 added the variable for exactly this hardware, and I do not want to trade one host's bug for another's while the memory-split question is still open on the issue.

What happens after

Two ways to opt out, both of which end with the name absent from the child env, because absence is the only off state ggml understands.

  • UNSLOTH_DISABLE_UNIFIED_MEMORY=1, mirroring the existing UNSLOTH_DISABLE_DC_TUNING=1. Exact 1, same as that switch.
  • A falsy GGML_CUDA_ENABLE_UNIFIED_MEMORY (0, false, no, off, empty, trimmed and case-folded), which is what users reasonably expect to mean off. Studio now strips it rather than passing it through to be read as on.

Any other value is passed through untouched, ownership tracking included.

Blast radius

The decision lives in a new _unified_memory_opted_out helper used only at the three env sites. _amd_apu_wants_unified_memory is deliberately left alone: two other consumers gate RAM admission on it (_offloads_every_layer and the APU RAM refusal at load time), and an env-var opt-out must not silently change what Studio believes about the hardware.

The arch-crash retry re-adds the variable when it lands on an APU, so that branch is gated too, otherwise the opt-out would not survive a respawn. The two withdrawal branches already key off _unified_env_applied, which is False under an opt-out, so they needed no change.

Fails open: a hostile or unreadable env answers False, which is the pre-PR behaviour.

Tests

studio/backend/tests/test_amd_apu_unified_memory.py: 15 new cases over the helper, including a fail-open case.

studio/backend/tests/test_gpu_arch_gate_os_matrix_7624.py: 16 new launch-level cases on the reported Strix Halo shape, covering the unchanged default, every off spelling, the truthy pass-through, the switch beating an inherited value, and the opt-out surviving an arch-crash retry onto an APU.

Serial run, -p no:randomly:

  • the two files above: 214 passed (baseline 183, +31)
  • with test_gpu_arch_gate_consumers_7624.py, test_datacenter_gpu_tuning.py, test_llama_cpp_placement.py, test_gpu_init_crash_message.py, test_model_memory_settings.py: 886 passed, 0 failed

Eight mutations, no survivors, both directions on the guard:

mutation result
opt-out branch removed 9 failed
helper always True 19 failed
helper always False (pre-PR behaviour) 18 failed
retry gate removed 1 failed
pop changed to setdefault 10 failed
strip().lower() dropped 3 failed
disable switch loosened to any truthy value 3 failed
empty string no longer counts as off 2 failed

ruff check clean, scripts/run_ruff_format.py applied.

Still open on the issue

I have asked the reporter for their BIOS carve-out, rocminfo pool info and whether they are on WSL2 or native, so the default itself can be sized to both a large-pool Strix Halo and the small-carve-out host #5301 was solving. That is a separate change; this one is the escape hatch, which is needed either way.

@danielhanchen

Copy link
Copy Markdown
Member Author

@codex review

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. You're on a roll.

Reviewed commit: 7b765974a6

ℹ️ 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
danielhanchen merged commit 7822b7c into main Aug 13, 2026
46 of 54 checks passed
@danielhanchen
danielhanchen deleted the unified-memory-escape-hatch-8651 branch August 13, 2026 13:19
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.

[Bug] Unsloth Studio on Strix Halo: GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 prevents GPU memory offload

1 participant