Skip to content

Commit 1187928

Browse files
authored
Merge pull request #855 from n-k/main
Add support for adding tensor buffer type overrides
2 parents cb5060a + 6fa8b77 commit 1187928

File tree

2 files changed

+59
-0
lines changed

2 files changed

+59
-0
lines changed

examples/simple/src/main.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ struct Args {
4848
#[cfg(any(feature = "cuda", feature = "vulkan"))]
4949
#[clap(long)]
5050
disable_gpu: bool,
51+
#[cfg(any(feature = "cuda", feature = "vulkan"))]
52+
#[arg(long, help = "Keep MoE layers on CPU")]
53+
cmoe: bool,
5154
#[arg(short = 's', long, help = "RNG seed (default: 1234)")]
5255
seed: Option<u32>,
5356
#[arg(
@@ -129,6 +132,8 @@ fn main() -> Result<()> {
129132
file,
130133
#[cfg(any(feature = "cuda", feature = "vulkan"))]
131134
disable_gpu,
135+
#[cfg(any(feature = "cuda", feature = "vulkan"))]
136+
cmoe,
132137
key_value_overrides,
133138
seed,
134139
threads,
@@ -176,6 +181,13 @@ fn main() -> Result<()> {
176181
model_params.as_mut().append_kv_override(k.as_c_str(), *v);
177182
}
178183

184+
#[cfg(any(feature = "cuda", feature = "vulkan"))]
185+
{
186+
if !disable_gpu && cmoe {
187+
model_params.as_mut().add_cpu_moe_override();
188+
}
189+
}
190+
179191
let model_path = model
180192
.get_or_load()
181193
.with_context(|| "failed to get model from args")?;

llama-cpp-2/src/model/params.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ pub mod kv_overrides;
1313
pub struct LlamaModelParams {
1414
pub(crate) params: llama_cpp_sys_2::llama_model_params,
1515
kv_overrides: Vec<llama_cpp_sys_2::llama_model_kv_override>,
16+
buft_overrides: Vec<llama_cpp_sys_2::llama_model_tensor_buft_override>,
1617
}
1718

1819
impl Debug for LlamaModelParams {
@@ -107,6 +108,48 @@ impl LlamaModelParams {
107108
}
108109
}
109110

111+
impl LlamaModelParams {
112+
/// Adds buffer type overides to move all mixture-of-experts layers to CPU.
113+
pub fn add_cpu_moe_override(self: Pin<&mut Self>) {
114+
self.add_cpu_buft_override(c"\\.ffn_(up|down|gate)_(ch|)exps");
115+
}
116+
117+
/// Appends a buffer type override to the model parameters, to move layers matching pattern to CPU.
118+
/// It must be pinned as this creates a self-referential struct.
119+
pub fn add_cpu_buft_override(mut self: Pin<&mut Self>, key: &CStr) {
120+
let buft_override = self
121+
.buft_overrides
122+
.get_mut(0)
123+
.expect("buft_overrides did not have a next allocated");
124+
125+
assert!(
126+
buft_override.pattern.is_null(),
127+
"last buft_override was not empty"
128+
);
129+
130+
// There should be some way to do this without iterating over everything.
131+
for (_i, &c) in key.to_bytes_with_nul().iter().enumerate() {
132+
c_char::try_from(c).expect("invalid character in key");
133+
}
134+
135+
buft_override.pattern = key.as_ptr();
136+
buft_override.buft = unsafe { llama_cpp_sys_2::ggml_backend_cpu_buffer_type() };
137+
138+
// set to null pointer for panic safety (as push may move the vector, invalidating the pointer)
139+
self.params.tensor_buft_overrides = null();
140+
141+
// push the next one to ensure we maintain the iterator invariant of ending with a 0
142+
self.buft_overrides
143+
.push(llama_cpp_sys_2::llama_model_tensor_buft_override {
144+
pattern: std::ptr::null(),
145+
buft: std::ptr::null_mut(),
146+
});
147+
148+
// set the pointer to the (potentially) new vector
149+
self.params.tensor_buft_overrides = self.buft_overrides.as_ptr();
150+
}
151+
}
152+
110153
impl LlamaModelParams {
111154
/// Get the number of layers to offload to the GPU.
112155
#[must_use]
@@ -199,6 +242,10 @@ impl Default for LlamaModelParams {
199242
val_i64: 0,
200243
},
201244
}],
245+
buft_overrides: vec![llama_cpp_sys_2::llama_model_tensor_buft_override {
246+
pattern: std::ptr::null(),
247+
buft: std::ptr::null_mut(),
248+
}],
202249
}
203250
}
204251
}

0 commit comments

Comments
 (0)