Skip to content

Commit 9f5b40f

Browse files
committed
updated to latest
1 parent 61e4980 commit 9f5b40f

File tree

3 files changed

+46
-46
lines changed

3 files changed

+46
-46
lines changed

llama-cpp-2/src/model.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! A safe wrapper around `llama_model`.
22
use std::ffi::CString;
3+
use std::num::NonZeroU16;
34
use std::os::raw::c_int;
45
use std::path::Path;
56
use std::ptr::NonNull;
@@ -131,7 +132,7 @@ impl LlamaModel {
131132
token: LlamaToken,
132133
special: Special,
133134
) -> Result<Vec<u8>, TokenToStringError> {
134-
self.token_to_bytes_with_size(token, 32, special)
135+
self.token_to_bytes_with_size(token, 32, special, None)
135136
}
136137

137138
/// Convert a vector of tokens to a single string.
@@ -264,7 +265,7 @@ impl LlamaModel {
264265
buffer_size: usize,
265266
special: Special,
266267
) -> Result<String, TokenToStringError> {
267-
let bytes = self.token_to_bytes_with_size(token, buffer_size, special)?;
268+
let bytes = self.token_to_bytes_with_size(token, buffer_size, special, None)?;
268269
Ok(String::from_utf8(bytes)?)
269270
}
270271

@@ -287,6 +288,7 @@ impl LlamaModel {
287288
token: LlamaToken,
288289
buffer_size: usize,
289290
special: Special,
291+
lstrip: Option<NonZeroU16>
290292
) -> Result<Vec<u8>, TokenToStringError> {
291293
if token == self.token_nl() {
292294
return Ok(String::from("\n").into_bytes());
@@ -314,8 +316,9 @@ impl LlamaModel {
314316
let len = string.as_bytes().len();
315317
let len = c_int::try_from(len).expect("length fits into c_int");
316318
let buf = string.into_raw();
319+
let lstrip = lstrip.map(|it| i32::from(it.get())).unwrap_or(0);
317320
let size = unsafe {
318-
llama_cpp_sys_2::llama_token_to_piece(self.model.as_ptr(), token.0, buf, len, special)
321+
llama_cpp_sys_2::llama_token_to_piece(self.model.as_ptr(), token.0, buf, len, lstrip, special)
319322
};
320323

321324
match size {

llama-cpp-sys-2/build.rs

Lines changed: 39 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,24 @@ compile_error!("feature \"vulkan\" cannot be enabled alongside other GPU based f
8686

8787
static LLAMA_PATH: Lazy<PathBuf> = Lazy::new(|| PathBuf::from("./llama.cpp"));
8888

89-
fn compile_bindings(out_path: &Path, llama_header_path: &Path) {
89+
fn compile_bindings(
90+
out_path: &Path,
91+
llama_header_path: &Path,
92+
) -> Result<(), Box<dyn std::error::Error + 'static>> {
9093
println!("Generating bindings..");
94+
95+
let includes = [
96+
llama_header_path.join("ggml").join("include"),
97+
];
98+
9199
let bindings = bindgen::Builder::default()
92-
// .header(llama_header_path.join("ggml.h").to_string_lossy())
93-
.header(llama_header_path.join("llama.h").to_string_lossy())
100+
.clang_args(includes.map(|path| format!("-I{}", path.to_string_lossy())))
101+
.header(
102+
llama_header_path
103+
.join("include")
104+
.join("llama.h")
105+
.to_string_lossy(),
106+
)
94107
.derive_partialeq(true)
95108
.allowlist_function("ggml_.*")
96109
.allowlist_type("ggml_.*")
@@ -106,11 +119,11 @@ fn compile_bindings(out_path: &Path, llama_header_path: &Path) {
106119
bindings = bindings.parse_callbacks(Box::new(GGMLLinkRename {}));
107120
}
108121

109-
let bindings = bindings.generate().expect("Unable to generate bindings");
122+
let bindings = bindings.generate()?;
110123

111-
bindings
112-
.write_to_file(out_path.join("bindings.rs"))
113-
.expect("Couldn't write bindings!");
124+
bindings.write_to_file(out_path.join("bindings.rs"))?;
125+
126+
Ok(())
114127
}
115128

116129
#[cfg(all(
@@ -319,26 +332,6 @@ fn push_feature_flags(cx: &mut Build, cxx: &mut Build) {
319332
}
320333
}
321334

322-
fn compile_opencl(cx: &mut Build, cxx: &mut Build) {
323-
println!("Compiling OpenCL GGML..");
324-
325-
// TODO
326-
println!("cargo:warning=OpenCL compilation and execution has not been properly tested yet");
327-
328-
cx.define("GGML_USE_CLBLAST", None);
329-
cxx.define("GGML_USE_CLBLAST", None);
330-
331-
if cfg!(target_os = "linux") {
332-
println!("cargo:rustc-link-lib=OpenCL");
333-
println!("cargo:rustc-link-lib=clblast");
334-
} else if cfg!(target_os = "macos") {
335-
println!("cargo:rustc-link-lib=framework=OpenCL");
336-
println!("cargo:rustc-link-lib=clblast");
337-
}
338-
339-
cxx.file(LLAMA_PATH.join("ggml-opencl.cpp"));
340-
}
341-
342335
fn compile_openblas(cx: &mut Build) {
343336
println!("Compiling OpenBLAS GGML..");
344337

@@ -650,22 +643,28 @@ fn compile_vulkan(cx: &mut Build, cxx: &mut Build) -> &'static str {
650643

651644
fn compile_ggml(mut cx: Build) {
652645
println!("Compiling GGML..");
646+
let ggml_src = LLAMA_PATH.join("ggml").join("src");
647+
let ggml_include = LLAMA_PATH.join("ggml").join("include");
653648
cx.std("c11")
654-
.include(LLAMA_PATH.as_path())
655-
.file(LLAMA_PATH.join("ggml.c"))
656-
.file(LLAMA_PATH.join("ggml-alloc.c"))
657-
.file(LLAMA_PATH.join("ggml-backend.c"))
658-
.file(LLAMA_PATH.join("ggml-quants.c"))
649+
.include(ggml_include)
650+
.file(ggml_src.join("ggml.c"))
651+
.file(ggml_src.join("ggml-alloc.c"))
652+
.file(ggml_src.join("ggml-backend.c"))
653+
.file(ggml_src.join("ggml-quants.c"))
659654
.compile("ggml");
660655
}
661656

662657
fn compile_llama(mut cxx: Build, _out_path: impl AsRef<Path>) {
663658
println!("Compiling Llama.cpp..");
659+
let llama_cpp_src = LLAMA_PATH.join("src");
660+
let llama_include = LLAMA_PATH.join("include");
661+
let ggml_include = LLAMA_PATH.join("ggml").join("include");
664662
cxx.std("c++11")
665-
.include(LLAMA_PATH.as_path())
666-
.file(LLAMA_PATH.join("unicode.cpp"))
667-
.file(LLAMA_PATH.join("unicode-data.cpp"))
668-
.file(LLAMA_PATH.join("llama.cpp"))
663+
.include(llama_include)
664+
.include(ggml_include)
665+
.file(llama_cpp_src.join("unicode.cpp"))
666+
.file(llama_cpp_src.join("unicode-data.cpp"))
667+
.file(llama_cpp_src.join("llama.cpp"))
669668
.compile("llama");
670669
}
671670

@@ -678,9 +677,10 @@ fn main() {
678677

679678
let llama_header_path = std::env::var("LLAMA_HEADE");
680679
if let Ok(llama_header_path) = llama_header_path {
681-
compile_bindings(&out_path, Path::new(&llama_header_path));
680+
compile_bindings(&out_path, Path::new(&llama_header_path))
681+
.expect("failed to generate bindings");
682682
} else {
683-
compile_bindings(&out_path, &LLAMA_PATH);
683+
compile_bindings(&out_path, &LLAMA_PATH).expect("failed to generate bindings");
684684
}
685685

686686
if let Ok(llama_lib_path) = std::env::var("LLAMA_LIB") {
@@ -698,7 +698,7 @@ fn main() {
698698

699699
println!("cargo:rerun-if-changed={}", LLAMA_PATH.display());
700700

701-
compile_bindings(&out_path, &LLAMA_PATH);
701+
compile_bindings(&out_path, &LLAMA_PATH).expect("failed to generate bindings");
702702

703703
let mut cx = Build::new();
704704
let mut cxx = Build::new();
@@ -715,9 +715,6 @@ fn main() {
715715
Some(compile_vulkan(&mut cx, &mut cxx))
716716
} else if cfg!(feature = "cuda") {
717717
Some(compile_cuda(&mut cx, &mut cxx, featless_cxx))
718-
} else if cfg!(feature = "opencl") {
719-
compile_opencl(&mut cx, &mut cxx);
720-
None
721718
} else if cfg!(feature = "openblas") {
722719
compile_openblas(&mut cx);
723720
None

llama-cpp-sys-2/llama.cpp

0 commit comments

Comments
 (0)