forked from utilityai/llama-cpp-rs
-
Notifications
You must be signed in to change notification settings - Fork 0
model-free gbnf validator #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
19ea078
model-free gbnf validator
mcharytoniuk 2a92d77
Fix cross-platform CI: drop unused wrapper includes, link ggml-metal …
mcharytoniuk 7eb1ef9
Use i64 for UnrecognizedStatusCode code so it fits MSVC signed enum a…
mcharytoniuk 1656311
Dead-strip unreferenced code on MSVC so the gbnf-sys minimal build links
mcharytoniuk 64a1dd8
Reuse the shared libllama for the gbnf grammar wrapper so it links on…
mcharytoniuk d229881
Link the shared-build ggml statically so dynamic-link configurations …
mcharytoniuk 99a81db
Surface grammar-engine allocation failures as typed errors instead of…
mcharytoniuk e27ed7b
Close the unbalanced parenthesis in the Rust parameter-names rule
mcharytoniuk fd32484
Map a zero decode/encode return code to a dedicated error instead of …
mcharytoniuk be9fb9b
Update decode/encode zero-return-code tests for the dedicated error v…
mcharytoniuk 4882150
Right-size multimodal eval_chunks context to 2048 so the 35B tests fi…
mcharytoniuk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| use std::env; | ||
| use std::path::Path; | ||
| use std::path::PathBuf; | ||
|
|
||
| use crate::target_os::TargetOs; | ||
|
|
||
| const LLAMA_BINDINGS_SYS_RELATIVE_PATH: &str = "../llama-cpp-bindings-sys"; | ||
| const LLAMA_SUBMODULE_RELATIVE_PATH: &str = "../llama-cpp-bindings-sys/llama.cpp"; | ||
|
|
||
| pub fn build_gbnf() { | ||
| let manifest_dir = | ||
| env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var is required"); | ||
| let target_triple = env::var("TARGET").expect("TARGET env var is required in build scripts"); | ||
| let target_os = TargetOs::from_target_triple(&target_triple) | ||
| .unwrap_or_else(|error| panic!("Failed to parse target OS: {error}")); | ||
|
|
||
| let manifest = PathBuf::from(&manifest_dir); | ||
| let llama_src = manifest.join(LLAMA_SUBMODULE_RELATIVE_PATH); | ||
| let bindings_sys = manifest.join(LLAMA_BINDINGS_SYS_RELATIVE_PATH); | ||
| let grammar_source_dir = llama_src.join("src"); | ||
|
|
||
| assert!( | ||
| grammar_source_dir.join("llama-grammar.h").is_file(), | ||
| "grammar headers not found under {}; ensure the llama.cpp submodule is checked out", | ||
| grammar_source_dir.display() | ||
| ); | ||
|
|
||
| register_rebuild_triggers(&manifest); | ||
|
|
||
| compile_wrapper( | ||
| &manifest, | ||
| &llama_src, | ||
| &bindings_sys, | ||
| &grammar_source_dir, | ||
| &target_os, | ||
| ); | ||
| } | ||
|
|
||
| fn compile_wrapper( | ||
| manifest: &Path, | ||
| llama_src: &Path, | ||
| bindings_sys: &Path, | ||
| grammar_source_dir: &Path, | ||
| target_os: &TargetOs, | ||
| ) { | ||
| let fault_injection = env::var_os("CARGO_FEATURE_FAULT_INJECTION").is_some(); | ||
|
|
||
| let mut build = cc::Build::new(); | ||
|
|
||
| build | ||
| .cpp(true) | ||
| .warnings(false) | ||
| .include(manifest) | ||
| .include(bindings_sys) | ||
| .include(llama_src.join("include")) | ||
| .include(grammar_source_dir) | ||
| .include(llama_src.join("ggml/include")) | ||
| .flag_if_supported("-std=c++17") | ||
| .pic(true) | ||
| .file(manifest.join("wrapper_gbnf.cpp")); | ||
|
|
||
| if fault_injection { | ||
| build.define("GBNF_FAULT_INJECTION", None); | ||
| build.file(manifest.join("alloc_fault.cpp")); | ||
| } | ||
|
|
||
| if target_os.is_msvc() { | ||
| build.flag("/std:c++17"); | ||
| build.flag("/EHsc"); | ||
| } | ||
|
|
||
| build.compile("llama_cpp_gbnf_wrapper"); | ||
| } | ||
|
|
||
| fn register_rebuild_triggers(manifest: &Path) { | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
|
|
||
| for source in [ | ||
| "wrapper_gbnf.cpp", | ||
| "wrapper_gbnf.h", | ||
| "alloc_fault.cpp", | ||
| "alloc_fault.h", | ||
| ] { | ||
| println!("cargo:rerun-if-changed={}", manifest.join(source).display()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| use std::env; | ||
| use std::path::Path; | ||
| use std::path::PathBuf; | ||
|
|
||
| use cmake::Config; | ||
|
|
||
| use crate::cmake_common; | ||
| use crate::ggml_cmake_options; | ||
| use crate::target_os::TargetOs; | ||
|
|
||
| const GGML_SUBMODULE_RELATIVE_PATH: &str = "../llama-cpp-bindings-sys/llama.cpp/ggml"; | ||
|
|
||
| pub fn build_ggml() { | ||
| let manifest_dir = | ||
| env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR env var is required"); | ||
| let target_triple = env::var("TARGET").expect("TARGET env var is required in build scripts"); | ||
| let target_os = TargetOs::from_target_triple(&target_triple) | ||
| .unwrap_or_else(|error| panic!("Failed to parse target OS: {error}")); | ||
| let profile = env::var("LLAMA_LIB_PROFILE").unwrap_or_else(|_| "Release".to_string()); | ||
|
|
||
| let wrapper_cmake_dir = PathBuf::from(&manifest_dir); | ||
| let ggml_src = wrapper_cmake_dir.join(GGML_SUBMODULE_RELATIVE_PATH); | ||
|
|
||
| assert!( | ||
| ggml_src.join("CMakeLists.txt").is_file(), | ||
| "ggml source not found at {}; ensure the llama.cpp submodule is checked out", | ||
| ggml_src.display() | ||
| ); | ||
|
|
||
| register_rebuild_triggers(&ggml_src); | ||
|
|
||
| let install_dir = configure_and_install_ggml( | ||
| &wrapper_cmake_dir, | ||
| &ggml_src, | ||
| &target_triple, | ||
| &target_os, | ||
| &profile, | ||
| ); | ||
|
|
||
| emit_metadata(&install_dir); | ||
| } | ||
|
|
||
| fn configure_and_install_ggml( | ||
| wrapper_cmake_dir: &Path, | ||
| ggml_src: &Path, | ||
| target_triple: &str, | ||
| target_os: &TargetOs, | ||
| profile: &str, | ||
| ) -> PathBuf { | ||
| let mut config = Config::new(wrapper_cmake_dir); | ||
|
|
||
| let ggml_source_dir = ggml_src | ||
| .to_str() | ||
| .expect("ggml source path must be valid UTF-8") | ||
| .replace('\\', "/"); | ||
| config.define("GGML_SOURCE_DIR", &ggml_source_dir); | ||
| config.define("GGML_BUILD_TESTS", "OFF"); | ||
| config.define("GGML_BUILD_EXAMPLES", "OFF"); | ||
| config.cflag("-w"); | ||
| config.cxxflag("-w"); | ||
|
|
||
| cmake_common::pass_cmake_env_vars(&mut config); | ||
| cmake_common::configure_compiler_launchers(&mut config); | ||
| cmake_common::configure_shared_libs(&mut config, false); | ||
|
mcharytoniuk marked this conversation as resolved.
|
||
| ggml_cmake_options::configure_cpu_features(&mut config, target_triple); | ||
| ggml_cmake_options::configure_gpu_backends(&mut config, target_os); | ||
| ggml_cmake_options::configure_openmp(&mut config, target_os); | ||
|
|
||
| if let TargetOs::Apple(_) = target_os { | ||
| config.define("GGML_BLAS", "OFF"); | ||
| cmake_common::override_archive_commands_for_apple_ar(&mut config); | ||
| } | ||
|
|
||
| config | ||
| .profile(profile) | ||
| .very_verbose(env::var("CMAKE_VERBOSE").is_ok()) | ||
| .always_configure(false); | ||
|
|
||
| config.build() | ||
| } | ||
|
|
||
| fn emit_metadata(install_dir: &Path) { | ||
| let lib_dir = resolve_lib_dir(install_dir); | ||
|
|
||
| println!("cargo:root={}", install_dir.display()); | ||
| println!("cargo:include={}", install_dir.join("include").display()); | ||
| println!("cargo:lib={}", lib_dir.display()); | ||
| println!( | ||
| "cargo:cmake={}", | ||
| lib_dir.join("cmake").join("ggml").display() | ||
| ); | ||
| } | ||
|
|
||
| fn resolve_lib_dir(install_dir: &Path) -> PathBuf { | ||
| let lib64 = install_dir.join("lib64"); | ||
|
|
||
| if lib64.is_dir() { | ||
| lib64 | ||
| } else { | ||
| install_dir.join("lib") | ||
| } | ||
| } | ||
|
|
||
| fn register_rebuild_triggers(ggml_src: &Path) { | ||
| println!("cargo:rerun-if-changed=build.rs"); | ||
| println!("cargo:rerun-if-env-changed=LLAMA_LIB_PROFILE"); | ||
| println!("cargo:rerun-if-env-changed=LLAMA_DISABLE_CCACHE"); | ||
| println!("cargo:rerun-if-env-changed=CMAKE_VERBOSE"); | ||
| println!( | ||
| "cargo:rerun-if-changed={}", | ||
| ggml_src.join("CMakeLists.txt").display() | ||
| ); | ||
| println!("cargo:rerun-if-changed={}", ggml_src.join("src").display()); | ||
| println!( | ||
| "cargo:rerun-if-changed={}", | ||
| ggml_src.join("include").display() | ||
| ); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.