Repository navigation
Description
Relayed from a Discord user issue, who found the hardcode themselves. Line refs
against unsloth@52a960103 and unsloth-zoo@8bc15db2.
Summary
A DiffusionGemma GGUF larger than VRAM always OOMs, even with GPU layers set to 0 in Studio:
allocating 16013.20 MiB on device 0: cudaMalloc failed: out of memory
The diffusion runner spawns its visual-server child with NGL=99 (all layers on GPU)
hardcoded, and Studio's memory controls are dropped before reaching it. The same model in its
non-diffusion form loads fine on the same hardware, because that path honours the settings.
Environment: RTX 4050 Laptop (6 GB VRAM), 16 GB RAM, native Ubuntu Linux (unsloth 2026.7.5, unsloth_zoo 2026.7.6), Studio installed via the curl one-liner. DiffusionGemma
Gemma-4-26B-A4B Q4_K_M (~17 GB). (exact Ubuntu version TBC)
Status: the reporter has tested #7575 + unslothai/unsloth-zoo#958 on the
affected machine and confirms the manual split loads the model, with the offload warning shown.
Root cause
The reporter found the hardcode themselves. The setting is lost at four points:
load_modelaccepts it —gpu_memory_mode/gpu_layers/n_cpu_moe/
tensor_splitatllama_cpp.py:6709-6712.- The diffusion branch drops it — the early return into
_start_diffusion_server
(:5357) forwards onlymodel_path,gguf_path,hf_repo,hf_variant,
model_identifier,n_ctx,extra_args,gpu_ids. The values are then overwritten at
:5484(_gpu_memory_mode = "auto",_gpu_layers = -1), so/statusreportsauto
regardless — which is why the UI appears to accept "GPU layers = 0" and ignore it. - The shim has no knob to receive it — argparse exposes only
--gguf --host --port --gpu --maxtok(shim.py:365-370). - The child env pins every layer —
visual_engine.py:151:
env["NGL"] = "99"_build_subprocess_env takes a base_env but overwrites NGL unconditionally, and
VisualServer.__init__ has no layer parameter. No env var, flag, or UI control can lower it.
(tests/test_diffusion_visual_engine_env.py:39 pins the literal "99".)
The existing _canvas_maxtok guard doesn't help. It clamps the canvas compute buffer.
The 16013.20 MiB here is the weight buffer, within 14 MiB of the whole 16,027 MiB file — so
NGL=99 fails before context sizing is reached.
No workaround short of editing installed files: change visual_engine.py:151 to a smaller
value and restart. CPU-only is also unreachable, _diffusion_gpu_arg
(:5336) emits an empty CUDA mask only when zero GPUs are visible, so a small-VRAM card
can neither split nor fall back to CPU.
The binary already supports partial offload
From diffusion-gemma-visual-server.cpp on the PR branch
(danielhanchen/llama.cpp @ diffusion-visual-updates):
mparams.n_gpu_layers = atoi(getenv("NGL") ? getenv("NGL") : "0");A plain llama.cpp model param. Note the binary's own default is 0; Unsloth overrides a safe
default with the most aggressive value. Its OOM message even advises "Try a smaller quant,
lower NGL, or more VRAM/RAM", advice no Unsloth user can act on. The RAM-spill path exists
and is already probed. So this is a plumbing bug, not a decoder limitation.
What the split should be
Q4_K_M is 16,027 MiB over 30 layers; token_embd at Q6_K ≈ 577 MiB leaves ~500 MiB/layer
(cross-checked by params: (26B − 0.74B)/30 ≈ 828M × ~4.85 bpw ≈ 479 MiB).
On a 6 GiB card: ~5,100-5,800 MiB free (depending on whether the display sits on the dGPU or,
as is common on a hybrid-graphics Linux laptop, the iGPU), minus the compute buffer
(n_head × MAXTOK² × 4 = 256 MiB at the 2048 floor) and the GPU's KV share (~130 MiB) leaves
roughly 4,300-5,000 MiB → NGL ~ 8-11.
Still tight for the reporter: 22 CPU-side layers ≈ 11.3 GiB against ~9.5 GiB free RAM, so it
will lean on mmap paging. Q3_K_M would give real headroom on that config.
Performance tradeoff (confirmed by the reporter)
The reporter lowered NGL by hand and the model loads, so the diagnosis holds end to end.
Throughput is ~6 tok/s against ~31 tok/s for non-diffusion Gemma-4-26B-A4B on the same
machine.
That is a plausible reason the value was pinned at 99 to begin with: block diffusion is
compute-bound rather than memory-bandwidth-bound, so CPU-side layers cost far more than they do
for an autoregressive model.
It does not change the bug. Failing with a raw cudaMalloc OOM and no override is worse than
loading slowly, and the tradeoff is undiscoverable while the knob is unreachable. Both PRs keep
99 as the default so the fast path is untouched, and the Studio side logs a warning when a
manual split is requested so the cost is explicit rather than surprising.
Secondary bug: ram_budget measured against the full model
Same file, in the context probe:
const size_t weights = llama_model_size(model);
size_t ram_budget = r_free > weights ? (size_t) ((r_free - weights) * 0.7) : 0;llama_model_size is the total regardless of placement, so whenever free RAM is below the
full model size ram_budget collapses to 0 even when most layers are GPU-resident. On the
reporter's machine that is unconditional (9.5 < 15.65 GiB), so the RAM re-probe never
contributes and context always falls to the floor path. Should use the CPU-resident share,
weights × (n_layers − ngl) / n_layers. Lives in llama.cpp PR #24423, noted here for visibility.
Reproduce
- On a machine with less VRAM than the model (e.g. 6 GB), open Studio.
- Load a DiffusionGemma GGUF larger than VRAM.
- Set GPU memory mode to manual, GPU layers to 0. Apply.
Observed: the cudaMalloc OOM above; logs/diffusion-server/ shows the child spawned with
NGL=99; /status reports gpu_memory_mode: auto, gpu_layers: -1.
Expected: loads with the requested split, as the non-diffusion Gemma-4-26B-A4B does on the
same hardware.
Expected behaviour
- Honour
gpu_memory_mode/gpu_layerson the diffusion path, and/or auto-fitNGLto free
VRAM the wayMAXTOK=0already auto-sizes the canvas. - Report the values actually in effect in
/statusinstead of resetting toauto/-1.
Related
unslothai/unsloth#6347 (DiffusionGemma multi-GPU), #6273 and #6303 (both prior fixes to
this same _build_subprocess_env).
Activity
LeoBorcherding commentedon Jul 31, 2026
Confirmed fixed on the reporting machine.
adririnstalled #7575 and unslothai/unsloth-zoo#958 together on the RTX 4050 laptop (6 GB VRAM, 16 GB RAM, native Ubuntu) and reports the model loads with a manual split, and the CPU-offload warning shows as intended. Versions on the failing baseline wereunsloth 2026.7.5/unsloth_zoo 2026.7.6.The exact Ubuntu release is still outstanding; I'll amend this once he replies.
[-][Bug] DiffusionGemma: hardcoded `NGL=99` forces a full-VRAM load; Studio's GPU-layer setting is ignored[/-][+][Bug] DiffusionGemma: GPU-layer setting is ignored, hardcoded `NGL=99` forces a full-VRAM load[/+]