Repository navigation
Studio: take GGML_CUDA_ENABLE_UNIFIED_MEMORY only when the weights outgrow the APU carve-out - #10351
Conversation
…tgrow the APU carve-out Studio set the variable on every AMD unified-memory APU whose carve-out was smaller than host RAM, whatever the model. On Linux ROCm that is a measured correctness risk: on a Strix Halo (gfx1151) Qwen3.8-Flash-Next UD-IQ1_S faults in k_set_rows on the first decode with the variable set and is clean with it absent or set to 0, on the b10715 and b10798 prebuilts alike, while the same builds are clean on Windows and on Vulkan. Every HF discussion 30 / #10330 / ggml-org#27797 report is that configuration. The swap is now taken only when it is needed as well as when it pays: host RAM must be the larger pool, as before, and the GPU-resident weights must outgrow the carve-out, so a launch the carve-out can hold never risks managed pages. An unpriced launch fails closed. UNSLOTH_ENABLE_UNIFIED_MEMORY=1 asks for it regardless on a selected APU, for a user who has measured their own model; UNSLOTH_DISABLE_UNIFIED_MEMORY=1 still wins when both are set. The arch-gate narrowing and the arch-crash respawn use the same decision. Tests: the pool-comparison arms now price weights that outgrow the carve-out, so each answer turns on the pool alone; new arms cover the fit / outgrow / exact / unpriced cases, the enable switch, and the launch-time decision on a discrete card. The two crash-retry fixtures give the fake APU an 8 GiB carve-out so a 10 GiB model outgrows it without moving the placement.
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
…pt-in on a mixed selection Review round one. The automatic path priced the whole GGUF, so a manual partial placement of a file larger than the carve-out took managed pages for a launch whose device share fit; it now prices what a forced full offload puts on the device, decided by _argv_offloads_every_layer on the final argv, and passes None (fail closed) when the fitter or an explicit layer count owns placement, since such a load fits by spilling. The overrun test compares bytes rather than a MiB floor. The opt-in goes through _rocm_selected_pool_mib, which fails closed unless every selected device is a unified-memory APU, the same predicate as the automatic path, so a mixed APU plus discrete selection can no longer set the process-wide variable. Tests: manual partial placement and fitter-owned placement never take it, a one-byte overrun does, the opt-in refuses a mixed selection at both the unit and the launch level. The positive fixtures are a forced full offload (manual mode above the block count) because that is the only launch the gate is for.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 53900c2835
ℹ️ 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".
| # Price only what a forced full offload puts on the device. With the | ||
| # fitter or an explicit partial placement owning the layer count, the | ||
| # load fits by spilling, so the whole-file size would overstate the | ||
| # need and take managed pages for a launch the carve-out holds. | ||
| _unified_need = model_size if self._argv_offloads_every_layer(cmd, env) else None |
There was a problem hiding this comment.
Include GPU-resident sidecar weights in the required bytes
For a forced full offload with pass-through LoRA/control-vector adapters or a GPU-offloaded separate drafter, model_size contains only the main GGUF and resolved projector. The repository's _sidecar_adapter_bytes and drafter-budget code confirm that these additional weights are resident on the GPU, so a 63 GiB model plus a 2 GiB adapter on a 64 GiB carve-out is classified as fitting and launches without managed memory, then exceeds the carve-out. Add every GPU-resident weight source to this requirement (and to the two retry recomputations), failing closed when an active sidecar cannot be sized.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not taking this one. The gate is deliberately coarse and its error direction is the clean hipErrorOutOfMemory, not the measured fault; a launch within a sidecar's size of the carve-out has no room for KV and compute either way, and UNSLOTH_ENABLE_UNIFIED_MEMORY=1 exists for a user who has measured it.
| return False | ||
| if need_bytes is None: | ||
| return False | ||
| return int(need_bytes) > int(pool_mib) * 1024 * 1024 |
There was a problem hiding this comment.
Compare against the pools that actually receive weights
On a launch selecting multiple unified-memory APUs, this compares the whole model with the sum of every selected carve-out even when the placement leaves some devices empty. For example, two 32 GiB APUs running a 40 GiB forced full offload with pass-through --split-mode none put the weights on the main GPU, but the 64 GiB aggregate makes this return false; the same problem occurs with a sufficiently skewed --tensor-split. The existing placement accounting explicitly treats these flags as invalidating pooled VRAM credit, so this gate likewise needs to evaluate the per-device weight assignment rather than the combined selected capacity or it omits managed memory and the populated APU OOMs.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not taking this one. Several unified-memory APUs in one selection with a pass-through split policy is outside what this gate prices; _rocm_selected_pool_mib sums the selected carve-outs by design, the failure direction is the clean OOM, and the opt-in covers the case.
…ll offload under the fitter Review round two. _argv_offloads_every_layer defers to an active fitter, which is right for crediting VRAM but wrong for this gate: llama.cpp's fitter refuses to lower a count the user set (common/fit.cpp throws "n_gpu_layers already set by user", downgraded to a warning), so Auto mode with a pass-through --gpu-layers above the block count, or an inherited LLAMA_ARG_N_GPU_LAYERS saying so, still attempts the full offload and needs managed pages when the file outgrows the carve-out. _launch_forces_full_offload recognises both; -1 stays the fitter's to lower and a concrete count below the block count stays a partial placement. Tests: a user count above the block count under --fit on takes it, an inherited count does too, a user -1 and a count below the block count do not.
Review round three. Auto mode's fitting path emits --fit on and no layer count; a trailing user --fit off wins the last-wins parse and leaves llama.cpp's default n_gpu_layers of -1, every layer, with nothing left to lower it. _launch_forces_full_offload read the missing count as "not full" and an oversized file lost managed memory. The no-count case now follows the fitter: full offload when it is off, the fitter's to spill when it is on. A concrete count still needs the block count to be read against. Tests: --fit off with no count takes it, --fit off with a partial count does not.
…y need Review round four. model_size is the whole file, but the trailing NextN/MTP blocks of an embedded-head GGUF are not loaded unless a draft engages (offload_layout already treats them as immovable for the same reason), so a base that fits its carve-out could take managed pages on their account, which is the unsafe direction. The price now subtracts the layout's excluded_block_bytes when no draft will engage, through the cached _tensor_spill_layout the spill planner already uses; a table that cannot be read cannot price them and does not take them. One helper computes the need at the launch and at both respawn sites, and _mtp_will_engage has a default before the fit try so a raise there cannot leave it unbound. Tests: 10 GiB of unloaded blocks in a 40 GiB file on a 32 GiB carve-out does not take it, 4 GiB still leaves an oversized base that does, an unreadable table fails closed.
…oding The unified-memory price counts the trailing MTP blocks when a draft will load them. If that draft then fails at startup, the retry launches the base alone with the same env, so a base the carve-out holds carried GGML_CUDA_ENABLE_UNIFIED_MEMORY=1 into the drafterless server. _unified_need_now now takes the argv and whether MTP engages, and the MTP fallback re-prices with the fallback command and no draft. It withdraws only what this launch set; an inherited or opted-in variable is left alone.
… retries The whole GGUF overstates what a full offload puts on the carve-out. dev_input is CPU-pinned even at -ngl 999, so token_embd, and the per-layer table that shares its name, never reach the device; on Qwen3.8-Flash-Next that table alone is 26.8 GiB of the file. A tied file duplicates the matrix onto the device instead, so only an untied one subtracts it, and a layout that cannot be read prices nothing. The projector's bytes were priced onto the device for the whole session. The CPU-projector retry and the text-only retry take them off it while reusing the launch env, so a base the carve-out holds carried the variable into the recovered server. One helper now re-prices every respawn from its own argv and withdraws only what this launch set.
|
@codex review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 503d372d3c
ℹ️ 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".
| if not _unified_env_applied: | ||
| return |
There was a problem hiding this comment.
Allow retries to add managed memory
When the initial --mmproj names a projector small enough to fit, but an inherited LLAMA_ARG_MMPROJ names a larger compatible projector, the initial argv overrides the environment and managed memory remains unset. If that command then hits a projector-format failure, the text-only retry strips --mmproj, exposing the inherited projector; this early return prevents re-pricing from enabling managed memory even though the retry's GPU footprint may now exceed the APU carve-out, causing the recovery launch to OOM. The retry path needs to size the newly effective inherited projector and support adding, not only withdrawing, the variable.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not taken: this needs an inherited projector larger than the one Studio resolved plus a projector-format crash, the retry's error is the clean OOM rather than the measured fault, and an inherited projector cannot always be sized (a URL never can), which is why the spill planner abstains on it too. The gate withdraws only what this launch set; adding the variable on a retry is a different design.
|
@codex review |
|
You have reached your Codex usage limits for security reviews. Please try again later. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 503d372d3c
ℹ️ 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".
| layout = self._tensor_spill_layout(model_path, all_shards = True) | ||
| if layout is None or not getattr(layout, "complete", True): | ||
| return None |
There was a problem hiding this comment.
Price attention-free GGUF layouts before failing closed
When a forced-full-offload model without attention KV metadata (for example, a pure Mamba GGUF) exceeds an APU carve-out, this always returns None: _layout_from_readers marks such a valid model incomplete because it requires attention.head_count_kv and key/value lengths. Consequently the new gate withholds managed memory even though the weights cannot fit, regressing these launches from using the larger host pool to a predictable HIP OOM. The unified-memory calculation should obtain the embedding/MTP weight buckets without requiring the planner's KV-completeness criteria, or explicitly support zero-attention architectures.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not taken: an attention-free GGUF is unpriced, and unpriced fails closed by design, the same as an unreadable table; the error is the clean OOM, no pure-SSM file published today outgrows an APU carve-out, and UNSLOTH_ENABLE_UNIFIED_MEMORY=1 covers the case.
| engages = mtp_engages | ||
| if model_size is None or not self._launch_forces_full_offload(run_argv, env): | ||
| return None | ||
| need = int(model_size) |
There was a problem hiding this comment.
Exclude GGUF container bytes from the device requirement
For a forced full offload whose GGUF file size is just above the carve-out but whose GPU-resident tensors fit, initializing need from model_size incorrectly counts the GGUF headers and metadata (including tokenizer metadata) as device weights. The subsequent adjustments remove selected tensors but never remove these container bytes, so the one-byte-over comparison can enable managed memory and expose the measured ROCm fault even though the actual weights fit. Compute this requirement from the tensor-layout buckets, including the projector's tensor bytes, rather than from on-disk file sizes.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Not taken: the container bytes are a few MiB against a carve-out that still has to hold the KV cache and compute buffers, so a launch inside that margin cannot load without managed memory anyway; boundary imprecision, not a reachable mispricing.
|
You have reached your Codex usage limits for security reviews. Please try again later. |
… for #10351 Two AMD-side defects, both found by running this PR on the AMD CI runners. 1. The classifier was unreachable without the training stack. _torch_unified_memory_classification_known imported _rocm_classify_unified_memory from core.training.worker, whose module body pulls in utils.wheel_utils, core.training.dataset_bounds and more. A --no-torch (GGUF-only) install has none of them, the import raises, and the whole method is wrapped in except Exception: return False -- so every ROCm device silently read "unclassifiable", the device was treated as shared, and the host-pinned discount was never applied. The direction is safe, an unclassified device keeps the old larger budget, but the optimisation was inert in exactly the configuration llama.cpp users run. MEASURED: eleven test_a_known_discrete_rocm_arch_is_proved_discrete cases fail on the AMD CI Windows runners (installed --no-torch) while passing on the Linux runner, and blocking that one import locally reproduces it exactly, 15 passed becoming 12 failed. The classifier moves to utils/rocm_topology.py, a leaf module with no package baggage, on the same reasoning as utils/gguf_archs.py. worker re-exports it so existing callers and the training-side tests are unaffected; utils/hardware/hardware.py, which had the same reach-into-training import, now uses the leaf module too. test_the_classification_seam_survives_a_gguf_only_install blocks the import and asserts a known-discrete arch still classifies. Confirmed non-vacuous: repointing the import back at core.training.worker turns it red. 2. _unified_need_now did not subtract per_layer_token_embd. #10351 makes GGML_CUDA_ENABLE_UNIFIED_MEMORY conditional on the weights outgrowing the APU carve-out, because that variable was measured faulting in k_set_rows on gfx1151 Linux ROCm. It correctly discounts token_embd, citing dev_input being CPU-pinned -- but per_layer_token_embd maps to the same LLM_TENSOR_LAYER_INPUT and main has no awareness of it at all. Unlike token_embd it is never duplicated onto the device by a tied file, so it subtracts unconditionally. Leaving it in overstates what the carve-out must hold by the whole PLE: 26.8 GiB of a 103.7 GiB Qwen3.8-Flash-Next UD-Q4_K_XL, read from its tensor tables. On a 128 GB Strix Halo with the usual 64 GB carve-out that turns a quant which fits into one that appears to outgrow it, re-enabling the managed-memory path #10351 exists to avoid, on the model family that PR names. 1261 passed, 1 skipped across the tied-output, arch-gate, OS-matrix, APU unified-memory, ROCm OOM, VRAM budget, offload planner, compute buffer and TP vision suites on the merged tree.
Resolves two conflicts from #10351 (sharded readers in layout_from_gguf; both sides appended planner tests). Pins that #5692's Windows cache tuning and #10382's shared-pool exception never coincide with the planner's --cache-ram clamp: the tuning keys on fully_gpu_offloaded, which a planner-owned launch never sets, so the argv carries exactly one --cache-ram.
Summary
Studio sets
GGML_CUDA_ENABLE_UNIFIED_MEMORY=1on every AMD unified-memory APU whose carve-out is smaller than host RAM, whatever the model. On Linux ROCm that variable is a measured correctness risk, so the swap is now taken only when it is needed as well as when it pays: the GPU-resident weights must outgrow the selected carve-out. A launch the carve-out can hold never risks managed pages.Fixes the Strix Halo reports in #10330, HF
unsloth/Qwen3.8-Flash-Next-GGUFdiscussion 30 and ggml-org/llama.cpp#27797 for any model that fits the carve-out, which is every published Qwen3.8-Flash-Next quant on a 128 GB Strix Halo with the usual 64 GB carve-out.What was measured
Qwen3.8-Flash-Next UD-IQ1_S on a Radeon 8060S (gfx1151) under Linux ROCm, the unsloth prebuilt tarballs users actually run, same prompts in every arm, a known-good sentinel model clean before and after each state:
GGML_CUDA_ENABLE_UNIFIED_MEMORY=1k_set_rowson the first decode, server dead=1=0=1The same six arms on Windows (HIP SDK) are clean throughout, so this is specific to the Linux ROCm managed-memory path. The fault is in the QSA top-k mask write, one op after the copy that unslothai/llama.cpp#157 patches, so the ROCm fixes in b10715 and later do not cover it.
The change
_unified_memory_would_helpgainsneed_bytes: after the existing pool comparison (available host RAM must exceed the carve-out total), the bytes must exceed the carve-out. The launch site prices only what a forced full offload puts on the device, decided by_argv_offloads_every_layeron the final argv; when the fitter or an explicit partial placement owns the layer count the load fits by spilling, so it passesNone, which fails closed like missing data and mixed selections._unified_memory_for_launchis the launch-time decision, used at the initial launch, the arch-gate narrowing and the arch-crash respawn, so a respawn onto an APU cannot re-add the variable for a model that fits.UNSLOTH_ENABLE_UNIFIED_MEMORY=1is a new opt-in for a user who has measured their own model on their own host. It applies only when every selected device is a unified-memory APU, since the variable is process-wide and hurts a discrete card.UNSLOTH_DISABLE_UNIFIED_MEMORY=1still wins when both are set; a user-suppliedGGML_CUDA_ENABLE_UNIFIED_MEMORYvalue passes through as before.model_sizeat the launch site already includes a GPU-loaded projector, so the price is the bytes the carve-out has to hold.What it does not change
A model larger than the carve-out still takes managed memory, because the alternative is a load that cannot fit (that is the DeepSeek-V4 case on ggml-org/llama.cpp#25436). The opt-out remains the answer there.
Tests
test_amd_apu_unified_memory.py,test_gpu_arch_gate_os_matrix_7624.py,test_gpu_arch_gate_7624.py,test_gpu_arch_gate_consumers_7624.py,test_model_memory_settings.py,test_gpu_init_crash_message.pyand the eleven other suites that reference the variable pass.test_offload_planner_seam.py::test_oversubscribed_decode_threads_decline_spill_planningfails identically onmainon this host and is unrelated.