Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions ggml/src/ggml-openvino/dev_doc/COOKBOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -611,25 +611,33 @@ On the NPU, the cache holds the compiled models without weights, plus one shared
With the default settings, the GPU and CPU plugins keep their own copy of the weights next to the copy of the backend while a model compiles. On integrated GPUs, both copies are in system RAM. The settings below lower that peak. They do not change the output.

```bash
# GPU or CPU, large model: back the host buffers with a file, so the kernel can drop their pages.
# Use a directory on a local disk (not tmpfs) with free space of about the model size. Can lower prompt speed.
export GGML_OPENVINO_SPILL_DIR=$HOME/ov_spill
mkdir -p $GGML_OPENVINO_SPILL_DIR
# GPU or CPU, large model, Linux: keep at most 10% of the weights in RAM while the model loads and compiles.
# This lowers the peak the most, also when RAM is free. The first token can come later.
export GGML_OPENVINO_COMPILE_MEM_SPILL=0.1
# Or without a cap: the kernel drops the spilled pages only when RAM runs out, so the peak does not change while
# RAM is free.
# export GGML_OPENVINO_COMPILE_MEM_SPILL=1
# Both back the host buffers with files in /var/tmp and write them at each start: the load takes longer, and decoding
# can be a little slower. Needs free disk space of more than the model size, on CPU also the KV cache; with less, the
# backend warns and uses RAM.
# Another directory, on a local disk (not tmpfs):
export GGML_OPENVINO_COMPILE_MEM_SPILL_DIR=$HOME/ov_spill
mkdir -p $GGML_OPENVINO_COMPILE_MEM_SPILL_DIR

# GPU: nothing to set. The backend already releases its host copy of the weights after compilation
# (GGML_OPENVINO_RELEASE_WEIGHTS=1, the default).

# CPU, MoE model: build fewer weight nodes during compilation.
export GGML_OPENVINO_REDUCE_COMPILE_MEM=1

# Both switches at once: GGML_OPENVINO_MEMORY_OPTIMIZE sets the default of GGML_OPENVINO_REDUCE_COMPILE_MEM
# and, on GPU, of GGML_OPENVINO_RELEASE_WEIGHTS.
# All at once: GGML_OPENVINO_MEMORY_OPTIMIZE=1 sets GGML_OPENVINO_REDUCE_COMPILE_MEM=1, GGML_OPENVINO_COMPILE_MEM_SPILL=0.1
# and, on GPU, GGML_OPENVINO_RELEASE_WEIGHTS=1 (already the default). Each one set on its own wins.
export GGML_OPENVINO_MEMORY_OPTIMIZE=1
```

- The same model, started again and again: [cache-only mode](#cpu-and-gpu) skips the weight upload. `GGML_OPENVINO_SPILL_DIR` is optional in the first run.
- The same model, started again and again: [cache-only mode](#cpu-and-gpu) skips the weight upload. `GGML_OPENVINO_COMPILE_MEM_SPILL` is optional in the first run.
- NPU: `GGML_OPENVINO_SELF_CONTAINED_MMAP=1` uses the memory-mapped GGUF file as the model buffer. See [NPU: gemma-4 E2B example](#npu-gemma-4-e2b-example).
- `GGML_OPENVINO_SPILL_DIR` is ignored on Windows and in cache-only mode.
- `GGML_OPENVINO_COMPILE_MEM_SPILL` is ignored on Windows and in cache-only mode.
- `-lm none` (no mmap) does not lower the memory in use. Cache-only mode needs mmap loading and stops with an error with `-lm none`.
- The variables: [Memory optimization](OpenVINO-ENV-VARS.md#memory-optimization).

Expand Down Expand Up @@ -667,9 +675,8 @@ This needs a 64 GB system.

```bash
export GGML_OPENVINO_DEVICE=GPU
export GGML_OPENVINO_SPILL_DIR=$HOME/ov_spill # local disk, not tmpfs
export GGML_OPENVINO_COMPILE_MEM_SPILL=1
export GGML_OPENVINO_ROPE_SINCOS=device_f32 # long prompts
mkdir -p $GGML_OPENVINO_SPILL_DIR
./build/ReleaseOV/bin/llama-server -m Muse-Glimmer-30B-Q4_K_M.gguf -md <dflash-draft>.gguf \
--spec-type draft-dflash --spec-draft-n-max 4
```
Expand Down
5 changes: 3 additions & 2 deletions ggml/src/ggml-openvino/dev_doc/OpenVINO-ENV-VARS.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ These variables lower the host memory peak. They do not change the output. Comma

| Variable | Device | Type | Default | Description |
|---|---|---|---|---|
| `GGML_OPENVINO_MEMORY_OPTIMIZE` | all | Boolean | unset | When set, the default of `GGML_OPENVINO_REDUCE_COMPILE_MEM` and, on GPU, of `GGML_OPENVINO_RELEASE_WEIGHTS`. `0` also keeps the host weights on GPU. |
| `GGML_OPENVINO_MEMORY_OPTIMIZE` | all | Boolean | unset | When set, the default of `GGML_OPENVINO_REDUCE_COMPILE_MEM`, of `GGML_OPENVINO_COMPILE_MEM_SPILL` (`1` sets `0.1`) and, on GPU, of `GGML_OPENVINO_RELEASE_WEIGHTS`. `0` also keeps the host weights on GPU. |
| `GGML_OPENVINO_REDUCE_COMPILE_MEM` | all | Boolean | value of `GGML_OPENVINO_MEMORY_OPTIMIZE` | Create fewer weight nodes during compilation, to lower the host memory in use. |
| `GGML_OPENVINO_SPILL_DIR` | CPU, GPU | String | `not set` | Back the host buffers of the backend (weights, KV cache, compute) with files in this directory, so the kernel can drop their pages. Use real storage, not tmpfs (`/tmp` on many systems). NPU: only with `GGML_OPENVINO_NPU_L0_HOST_TENSORS=0`. Not part of the cache key. Ignored on Windows and in cache-only mode. |
| `GGML_OPENVINO_COMPILE_MEM_SPILL` | CPU, GPU | Float | `0.1` with `GGML_OPENVINO_MEMORY_OPTIMIZE=1`, else `0` | Back the host buffers of the backend (weights, KV cache, compute) with files in `GGML_OPENVINO_COMPILE_MEM_SPILL_DIR`, so the kernel can drop their pages. `0`: off. `1`: on, but the kernel drops the pages only when RAM runs out, so the memory peak does not change while RAM is free. A fraction in between: on, and while the model loads and compiles, keep at most this fraction of the spilled weights in RAM, also when RAM is free (`0.1` keeps 10%, Linux 5.4 or later). A fraction lowers the peak the most; the model file's pages are dropped too, and the first token can come later, while the weights are written to disk. Each start writes the buffers to disk: the load takes longer, and decoding can be a little slower. The disk space is reserved when the files are created (not for rewrites on btrfs or ZFS): more than the model size, on CPU also the KV cache. With too little space, the backend warns and uses RAM. NPU: off unless set, and only with `GGML_OPENVINO_NPU_L0_HOST_TENSORS=0`. Not part of the cache key. Ignored on Windows and in cache-only mode. |
| `GGML_OPENVINO_COMPILE_MEM_SPILL_DIR` | CPU, GPU | String | `/var/tmp` | Directory for the `GGML_OPENVINO_COMPILE_MEM_SPILL` files. Use real storage, not tmpfs (`/tmp` on many systems): on tmpfs, the backend warns and uses RAM. |
| `GGML_OPENVINO_LAZY_HOST_THRESHOLD_MB` | NPU | Integer | `2048` | With `GGML_OPENVINO_SELF_CONTAINED_MMAP=1`: buffers of at least this size get demand-zero host memory instead of Level Zero host memory. |

## NPU plugin properties
Expand Down
2 changes: 1 addition & 1 deletion ggml/src/ggml-openvino/dev_doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ Settings that must stay exactly the same between the runs, including set versus

Settings that can differ:

- `GGML_OPENVINO_SPILL_DIR`: optional on the first run and ignored in cache-only mode. Host-weight release is disabled in cache-only mode.
- `GGML_OPENVINO_COMPILE_MEM_SPILL` and `GGML_OPENVINO_COMPILE_MEM_SPILL_DIR`: optional on the first run and ignored in cache-only mode. Host-weight release is disabled in cache-only mode.
- `GGML_OPENVINO_DISABLE_REMOTE_OUTPUTS`: changes only the output bindings.
- `GGML_OPENVINO_KV_SLICE`: a runtime binding option for dynamic graphs. The same dynamic compiled graph accepts full-capacity and live-prefix attention K/V tensors. Eligibility: [KV cache binding](#kv-cache-binding).

Expand Down
9 changes: 7 additions & 2 deletions ggml/src/ggml-openvino/ggml-openvino-extra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ constexpr env_var env_vars[] = {
// Text
{"GGML_OPENVINO_DEVICE", env_type::STR},
{"GGML_OPENVINO_CACHE_DIR", env_type::STR},
{"GGML_OPENVINO_SPILL_DIR", env_type::STR},
{"GGML_OPENVINO_COMPILE_MEM_SPILL", env_type::FLOAT},
{"GGML_OPENVINO_COMPILE_MEM_SPILL_DIR", env_type::STR},
{"GGML_OPENVINO_DEBUG_NODE", env_type::STR},
{"GGML_OPENVINO_COMPILED_MODEL_CACHE_DIR", env_type::STR},
{"GGML_OPENVINO_NPU_COMPILE_CONFIG", env_type::STR},
Expand Down Expand Up @@ -271,10 +272,14 @@ std::string checked_env_value(const env_var & var, const char * value) {
return std::to_string(number);
case env_type::FLOAT: {
char * end = nullptr;
(void) std::strtof(value, &end);
const float number = std::strtof(value, &end);
if (end == value || !trim_spaces(end).empty()) {
GGML_ABORT("ggml-openvino: %s=%s is not valid, use a number", var.name, value);
}
if (std::strcmp(var.name, "GGML_OPENVINO_COMPILE_MEM_SPILL") == 0 && !(number >= 0 && number <= 1)) {
GGML_ABORT("ggml-openvino: %s=%s is not valid, use 0 (off), 1 (on) or a fraction in between (0.1)",
var.name, value);
}
break;
}
case env_type::BOOL:
Expand Down
10 changes: 10 additions & 0 deletions ggml/src/ggml-openvino/ggml-openvino-extra.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,16 @@ void ggml_openvino_unregister_weight_buffer(const void * data);
void ggml_openvino_release_weight_buffers();
bool ggml_openvino_restore_weight_buffers();

// A GGML_OPENVINO_COMPILE_MEM_SPILL fraction: the limiters trim the spilled weights while the model loads, and while
// one of these is alive and for 10 s after, and sleep otherwise. Hold one around compile_model (it reads the weights
// back) and around rewriting released weights.
struct ggml_openvino_spill_trim_scope {
ggml_openvino_spill_trim_scope();
~ggml_openvino_spill_trim_scope();
ggml_openvino_spill_trim_scope(const ggml_openvino_spill_trim_scope &) = delete;
ggml_openvino_spill_trim_scope & operator=(const ggml_openvino_spill_trim_scope &) = delete;
};

// Group size of a 2-bit type that is loaded as u2 weights, 0 for other types
size_t ggml_openvino_u2_group_size(ggml_type type);

Expand Down
Loading