Skip to content
This repository has been archived by the owner on Sep 12, 2024. It is now read-only.

Commit

Permalink
feat: temp workaround for cublas build (#42)
Browse files Browse the repository at this point in the history
will write docs for this. not easy for end users
  • Loading branch information
hlhr202 authored May 4, 2023
1 parent 5c43b7d commit d573d74
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 11 deletions.
3 changes: 2 additions & 1 deletion packages/llama-cpp/.npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ __test__
renovate.json
*.bin
tmp/
llama-sys/llama.cpp/
llama-sys/llama.cpp/
*.so
2 changes: 1 addition & 1 deletion packages/llama-cpp/example/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from "path";
const llama = LLama.load(
path.resolve(process.cwd(), "../../ggml-vicuna-7b-1.1-q4_1.bin"),
null,
false
true
);

const template = `Who is the president of the United States?`;
Expand Down
7 changes: 6 additions & 1 deletion packages/llama-cpp/llama-sys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ license = "MIT"

[build-dependencies]
bindgen = "0.64"
platforms = "3.0.2"
platforms = "3.0.2"

[features]
default = []
cublas = ["dynamic"]
dynamic = []
55 changes: 47 additions & 8 deletions packages/llama-cpp/llama-sys/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,13 @@ use std::env;
use std::path::PathBuf;

fn main() {
let initial_dir = env::current_dir().unwrap();

// warning
println!("cargo:warning=working_dir: {}", initial_dir.display());

let target = env::var("TARGET").unwrap();
let platform = Platform::find(&target).unwrap();
// env::set_var("RUSTFLAGS", "-C target-feature=+crt-static");
env::set_var("CXXFLAGS", "-fPIC");
env::set_var("CFLAGS", "-fPIC");

Expand All @@ -20,8 +24,16 @@ fn main() {
println!("cargo:rustc-link-lib=framework=Accelerate");
}

#[allow(unused_mut, unused_assignments)]
let mut link_type = "static";

#[cfg(feature = "dynamic")]
{
link_type = "dylib";
}

println!("cargo:rustc-link-search={}", env::var("OUT_DIR").unwrap());
println!("cargo:rustc-link-lib=static=llama");
println!("cargo:rustc-link-lib={}=llama", link_type);
println!("cargo:rerun-if-changed=wrapper.h");

let bindings = bindgen::Builder::default()
Expand Down Expand Up @@ -65,13 +77,26 @@ fn main() {
let command = command
.arg("..")
.arg("-DCMAKE_BUILD_TYPE=Release")
// .arg("-DLLAMA_CUBLAS=ON")
.arg("-DLLAMA_STATIC=ON")
.arg("-DLLAMA_ALL_WARNINGS=OFF")
.arg("-DLLAMA_ALL_WARNINGS_3RD_PARTY=OFF")
.arg("-DLLAMA_BUILD_TESTS=OFF")
.arg("-DLLAMA_BUILD_EXAMPLES=OFF");

#[cfg(feature = "cublas")]
{
command.arg("-DLLAMA_CUBLAS=ON");
}

#[allow(unused_mut, unused_assignments)]
let mut link_type = "-DLLAMA_STATIC=ON";
#[cfg(feature = "dynamic")]
{
command.arg("-DBUILD_SHARED_LIBS=ON");
link_type = "-DLLAMA_STATIC=OFF";
}

command.arg(link_type);

if platform.target_os == OS::MacOS {
if platform.target_arch == Arch::AArch64 {
command
Expand Down Expand Up @@ -105,21 +130,35 @@ fn main() {
panic!("Failed to build lib");
}

#[allow(unused_mut, unused_assignments)]
let mut link_ext = ("lib", "a");

#[allow(unused_mut, unused_assignments)]
let mut out_dir = env::var("OUT_DIR").unwrap();
#[cfg(feature = "dynamic")]
{
link_ext = ("dll", "so");
out_dir = initial_dir.parent().unwrap().to_str().unwrap().to_owned();
out_dir.push_str("/@llama-node");
}

println!("cargo:warning=out_dir: {:?}", out_dir);

// move libllama.a to where Cargo expects it (OUT_DIR)
#[cfg(target_os = "windows")]
{
std::fs::copy(
"Release/llama.lib",
format!("{}/llama.lib", env::var("OUT_DIR").unwrap()),
format!("Release/llama.{}", link_ext.0),
format!("{}/llama.{}", out_dir, link_ext.0),
)
.expect("Failed to copy lib");
}

#[cfg(not(target_os = "windows"))]
{
std::fs::copy(
"libllama.a",
format!("{}/libllama.a", env::var("OUT_DIR").unwrap()),
format!("libllama.{}", link_ext.1),
format!("{}/libllama.{}", out_dir, link_ext.1),
)
.expect("Failed to copy lib");
}
Expand Down
1 change: 1 addition & 0 deletions packages/llama-cpp/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ impl LLamaContext {
logits[nl] = nl_logit;
}

#[allow(unused_assignments)]
let mut id = 0;

if temp <= 0.0_f32 {
Expand Down

0 comments on commit d573d74

Please sign in to comment.