Repository navigation
llama: add default load-mode auto, which avoids mmap on iGPUs - #26081
Conversation
pwilkin
left a comment
There was a problem hiding this comment.
This is sorely needed for ROCm, where the --mmap mode is additionally bugged beyond 64GB.
|
LGTM for the virt-gpu part (@taronaeo) |
|
Confirming this is worth automating on gfx1151 (Strix Halo APU, Framework Desktop Ryzen AI Max+ 395, ROCm/HIP). Without |
…, multi-output backend sampling, pocket-tts Merges 45 upstream commits (0865990..ebb546b, b10362-16-gebb546b7e) into synori/llama-update-mtp-fit. Zero conflicts; all vendored patches carried over untouched. Primary motivation — new Meta architecture: * 62bf73d model: Muse Glimmer Support (ggml-org#26841) LLM_ARCH_MUSE_GLIMMER + src/models/muse-glimmer.cpp + the mtmd vision tower in tools/mtmd/models/muse-glimmer.cpp and conversion/muse_glimmer.py. Other notable changes that touch our public API surface: * dd1ea52 llama : support multi-output backend sampling (ggml-org#25532) llama_context_params gains n_outputs_max_per_seq; llama_sampler_i.backend_init takes it as a third argument; new backend_reset / copy_state vtable slots and llama_sampler_copy(). * 153d324 llama : default load-mode auto, avoids mmap on iGPUs (ggml-org#26081) llama_load_mode gains LLAMA_LOAD_MODE_AUTO = -1 (enum is now signed). * 6e62ba5 mtmd: support pocket-tts (ggml-org#26871) mtmd_gen_inp/mtmd_gen_out gain seed/temp/feats/is_eos; new mtmd_gen_inp_default(); mtmd_helper_gen_audio_step_gen() takes out_stop. * 157b81f model : Granite-Switch Architecture (ggml-org#25107) * 7a20b41 model: MTP support for Nemotron (ggml-org#26725) and cc078b4 Dflash support for nemotron-3.5 (ggml-org#26905) * e23e944 vendor : cpp-httplib 0.53.0, 4c6766f vendor : subprocess.h sync Vendored patches preserved: * 919fde3 feat(rpc): thread-local last_error accessor — intact, upstream touched ggml-rpc.cpp by one unrelated line. * 3679b23 Fixes (RPC) — intact. * 2aa76c7 fix(metal): drop stray kernel_pad_f32 — still applies; upstream has since refactored pad into a templated kernel_pad_impl<T>, so the duplicate definition that referenced the nonexistent kargs_pad.s0..s3 is gone on both sides and nothing had to be re-applied.
…rg#26081) * llama: add new default load-mode auto which picks mmap unless a non-Metal iGPU is used * Update ggml/src/ggml-hexagon/ggml-hexagon.cpp Co-authored-by: Max Krasnyansky <[email protected]> * set mmap_support to false on OpenCL backend * fix order of load modes * use -1 for auto * resolve load mode auto earlier to correctly pick gpu host or cpu memory * add load mode auto to llama-bench * bump virtgpu api version, regenerate docs --------- Co-authored-by: Piotr Wilkin (ilintar) <[email protected]> Co-authored-by: Max Krasnyansky <[email protected]> Co-authored-by: Georgi Gerganov <[email protected]>
…rg#26081) * llama: add new default load-mode auto which picks mmap unless a non-Metal iGPU is used * Update ggml/src/ggml-hexagon/ggml-hexagon.cpp Co-authored-by: Max Krasnyansky <[email protected]> * set mmap_support to false on OpenCL backend * fix order of load modes * use -1 for auto * resolve load mode auto earlier to correctly pick gpu host or cpu memory * add load mode auto to llama-bench * bump virtgpu api version, regenerate docs --------- Co-authored-by: Piotr Wilkin (ilintar) <[email protected]> Co-authored-by: Max Krasnyansky <[email protected]> Co-authored-by: Georgi Gerganov <[email protected]>
…rg#26081) * llama: add new default load-mode auto which picks mmap unless a non-Metal iGPU is used * Update ggml/src/ggml-hexagon/ggml-hexagon.cpp Co-authored-by: Max Krasnyansky <[email protected]> * set mmap_support to false on OpenCL backend * fix order of load modes * use -1 for auto * resolve load mode auto earlier to correctly pick gpu host or cpu memory * add load mode auto to llama-bench * bump virtgpu api version, regenerate docs --------- Co-authored-by: Piotr Wilkin (ilintar) <[email protected]> Co-authored-by: Max Krasnyansky <[email protected]> Co-authored-by: Georgi Gerganov <[email protected]>
…rg#26081) * llama: add new default load-mode auto which picks mmap unless a non-Metal iGPU is used * Update ggml/src/ggml-hexagon/ggml-hexagon.cpp Co-authored-by: Max Krasnyansky <[email protected]> * set mmap_support to false on OpenCL backend * fix order of load modes * use -1 for auto * resolve load mode auto earlier to correctly pick gpu host or cpu memory * add load mode auto to llama-bench * bump virtgpu api version, regenerate docs --------- Co-authored-by: Piotr Wilkin (ilintar) <[email protected]> Co-authored-by: Max Krasnyansky <[email protected]> Co-authored-by: Georgi Gerganov <[email protected]>
The OpenCL backend copies the weights into its own buffers. With the model file also mapped, gpt-oss-20b on Adreno X2-90 ran 7-8x slower in prefill and about 25% slower in decode (pp512 122 vs 950 t/s), while --no-mmap or -dio restored full speed. Gemma 4 E4B was not affected. b7725 never hit this because it defaults to direct I/O. Default to no mmap when an OpenCL GPU is offloaded to and mmap was not requested explicitly, as upstream does since ggml-org#26081 (load mode auto). --mmap / -mmp 1 still force it. No change under src/ or include/.
…s lookup tables from disk (unslothai#16857) On an integrated GPU, a GGUF larger than memory fails to load, while Studio's warning says the weights are memory-mapped and paged in from disk. Reported on Discord: on an RTX Spark N1X (Windows), `unsloth/DeepSeek-V4.1-Flash-1017-GGUF` `UD-IQ1_S` (216.97 GiB) failed with `unable to allocate CUDA_Host buffer` for 110,595,290,400 bytes, exactly the size of the model's Engram lookup table (`per_layer_token_embd.weight`). ## Cause llama.cpp's default `--load-mode auto` means mmap unless a device lacks mmap support, which ggml reports for CUDA, HIP and Vulkan iGPUs (ggml-org/llama.cpp#26081). On the same devices `--lazy-mode auto` turns off (ggml-org/llama.cpp#28326), so lookup tables that are streamed from disk elsewhere stay resident too. Studio already switches an oversized `none` or `mlock` load to mmap (`_page_an_oversized_unmapped_load`), but it treated an unset mode as mmap. On an iGPU nothing is set, so nothing changed. ## Fix When a load Studio already prices as oversized runs where llama.cpp's `auto` resolves to `none` (the build lists `auto`, and a model device lacks mmap support), the override now: - appends `--load-mode mmap`, including after it drops an explicit `none`, `--no-mmap` or `LLAMA_ARG_NO_MMAP`; - appends `--lazy-mode on` if no lazy mode was set and the model has a table over llama.cpp's 4 GiB auto threshold. mmap alone would still read the whole table at load, because prefetch skips only lazy ranges (`src/llama-mmap.cpp`); - on Windows, raises `--fit-target` by 15 GiB over the launch's margin, unless the fitter is off or the user set a margin. Otherwise the fitter fills the shared memory with the GPU share and the load aborts at warm-up. The load warning gains one sentence saying Unsloth turned on memory mapping, which llama.cpp leaves off by default on this integrated GPU; the lazy table and the fit headroom go to the log only. Both the first launch and the arch-crash retry apply it. Not changed: loads that fit (they keep `none`, the faster load on an iGPU), discrete GPUs, Metal, builds without `auto`, a lazy mode or fit target the user set, and "Don't reserve system RAM" on an iGPU. Limits: - The 15 GiB is empirical: on a 127 GiB Strix Halo, 8 GiB still aborted at warm-up and 16 GiB ran. - Nothing ran on a CUDA iGPU (RTX Spark, DGX Spark) or a ROCm APU build. The reporter's 217 GiB model leaves less room than the 147 GiB one tested, so it may still not fit, and will be slow if it does. ## Verification AMD DevLab Strix Halo machines (128 GB, Vulkan) loading `unsloth/Qwen3.8-Flash-Next-GGUF` `UD-Q5_K_XL` (147.4 GiB, 50.7 GiB of it lookup table) through Studio, then two chat requests: | | main | This PR | |---|---|---| | Linux | Killed by the OS (signal 9) in one API load and one chat-picker load; a third load got through in 104 s | Loads in 71 to 76 s, chats answer | | Windows | `ErrorOutOfDeviceMemory` at load | Loads on the GPU in 246 s, chats answer | Loading from the chat picker on Linux, main is killed after 80 s and this PR loads in 73 s. The "after" panel shows the warning text of the final commit, rendered locally over a mocked load status; the load itself is the DevLab run above.  llama-server run directly on the same file, which is why the override sets each flag: - Windows: without mmap the load fails at load, with or without the lazy table. mmap with the lazy table aborts at warm-up with a 1 or 8 GiB fit target, and with 16 GiB it runs at about 16 tok/s. - Linux: `--lazy-mode on` loads with or without mmap (34 and 72 s, about 23 tok/s). The 16 GiB fit target is not needed there and cut generation to 13 to 15 tok/s, so the headroom is Windows only. - A 61.8 GiB quant that fits launches identically on main and this PR. Tests in `test_llama_cpp_placement.py` and `test_llama_server_args.py` drive `load_model` on a simulated integrated CUDA SoC with the DeepSeek-V4.1 table and cover each rule and exception above; the behavior tests fail on main. The 19 related backend test files pass (4,202 tests), and every full-suite failure also fails on main or passes when rerun on its own. --------- Co-authored-by: Daniel Han <[email protected]> Co-authored-by: Daniel Han <[email protected]>
… RAM With load_mode=auto (upstream ggml-org#26081) a single device without mmap support turns mmap off for the whole model, and the Hexagon device reports mmap_support=false. Every CPU-side weight is then read into anonymous memory: loading Gemma 4 26B-A4B (14 GB, experts on the CPU) on the 12 GB SM8845 phone exhausted memory and rebooted it four times. A memory watchdog (tools/hui-lab/device/mem_guard.sh) showed RssAnon 3.3 GB + 2.4 GB swapped and climbing, ION steady at 1.8 GB, file pages 7 MB. Keep mmap when the model is larger than MemAvailable: without it such a model can only run out of memory. Models that fit keep upstream's rule. The app was not affected (it always passes --load-mode mmap). Verified on SM8845: 26B-A4B with the default load mode logs "keeping mmap", answers correctly, min MemAvailable 5.5 GB, no reboot; gated pp128 30.7 / tg32 6.1 t/s. MiniCPM-V 35B-A3B and Qwen3.8-Flash-Next 125B (app scheme C arguments) load and answer correctly on the merged engine. Co-Authored-By: Claude Opus 5.5 <[email protected]>
The generic defaults set mmap to true for every model that leaves it unset, and the llama-cpp backend then forces LLAMA_LOAD_MODE_MMAP. On integrated GPUs (APUs) such as AMD Strix Halo, an mmap load with ROCm can hang (ggml-org/llama.cpp#19482). llama.cpp has LLAMA_LOAD_MODE_AUTO for this case (ggml-org/llama.cpp#26081): it maps the file unless a device has no mmap support. Leave mmap unset in the defaults, except under XPU, where it stays false. Add ModelOptions.MMapManaged. It is true when the model config does not set mmap. MMap then goes out as true, the old default. The llama-cpp backend uses AUTO when MMapManaged is true, mlock is off and direct I/O is off. An explicit mmap: true or mmap: false, mlock, direct I/O and the SYCL override keep their current meaning. A new field keeps both sides of the wire compatible. An older backend ignores MMapManaged and loads with mmap, as before. An older server never sets the field, so a newer backend uses MMap as before. A proto3 optional on MMap would make a false from an older server look unset. prepare.sh probes llama.h for LLAMA_LOAD_MODE_AUTO, so forks that build the same grpc-server.cpp without AUTO keep MMAP. ik-llama-cpp and the Go llama backend do not read the new field and keep mmap on. Assisted-by: Claude:claude-sonnet-5-5 Signed-off-by: Ettore Di Giacinto <[email protected]>
Overview
Follow-up to #20834 and #26023
mmap is detrimental on iGPUs that have to copy the model to device-visible, but still shared memory, as that means during loading it will have the model in RAM twice, doubling the memory requirements temporarily and slowing down the process if it overflows. This is currently the default behaviour, leading to issues for users that don't know about this.
Add load-mode auto and default to it. It keeps the current behaviour for most cases, except when a device reports that it doesn't support mmap. This is up to the backends to flag, through a new capability boolean (apologies for the mass-pinging this causes..). I only disabled it for CUDA/ROCm and Vulkan iGPUs for now, because I'm not familiar enough with the other backends.
Please let me know if this implementation is feasible or if there's a better way to handle it, and also if there are more backends I should include.
In the future further features could be added, e.g. automatic use of direct-io, if we know when it can definitely be enabled.
Requirements