Skip to content

Commit cdc2ba2

Browse files
Fix GPU OOM on models larger than VRAM by reducing layer count on load failure
When a large model (e.g. Gemma 4 26B-A4B at 18 GB) fails to load because llama.cpp tries to allocate all layers on a small GPU (3.7 GB VRAM), the load itself throws before the probe loop can shed layers. Reduce the layer count until the loading is succesfull.
1 parent e4311c4 commit cdc2ba2

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

ltengine/src/llm.rs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,23 @@ impl LLM {
9393
let (model, gpu_layers) = if use_gpu {
9494
let mut n_gpu = 9999u32;
9595
let model = loop {
96-
let model = LlamaModel::load_from_file(
96+
let model = match LlamaModel::load_from_file(
9797
&backend, &model_path,
9898
&LlamaModelParams::default().with_n_gpu_layers(n_gpu),
99-
).with_context(|| "Unable to load model")?;
99+
) {
100+
Ok(m) => m,
101+
Err(_) => {
102+
// Load failed (likely GPU OOM before probe). On the first failure
103+
// jump to 64 (covers most models); after that halve to converge fast.
104+
let next = if n_gpu >= 9999 { 64 } else { n_gpu / 2 };
105+
eprintln!("ltengine: model load failed at {} GPU layers, retrying with {}", n_gpu, next);
106+
n_gpu = next;
107+
if n_gpu == 0 {
108+
return Err(anyhow::anyhow!("Unable to load model even with 0 GPU layers"));
109+
}
110+
continue;
111+
}
112+
};
100113

101114
// Probe: create a minimal context and decode one token to confirm
102115
// the GPU has enough VRAM for compute scratch buffers.

0 commit comments

Comments
 (0)