Skip to content

Distribute and run LLMs with a single file.

Fix the build

Justine Tunney29e417fbc377
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index bd291b0e..ad99cb34 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -1,9 +1,9 @@
name: CI
on:
push:
- branches: [ master, main ]
+ branches: [ master, main, fix ]
pull_request:
- branches: [ master, main ]
+ branches: [ master, main, fix ]
jobs:
Tests:
timeout-minutes: 60
@@ -11,35 +11,32 @@ jobs:
strategy:
fail-fast: false
matrix:
- os: [ macos-latest ] # ubuntu-latest, windows-latest are currently non-functional, requiring adaptation for proper functionality.
+ os: [ macos-latest ] # ubuntu-latest, windows-latest are currently non-functional, requiring adaptation for proper functionality.
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- - name: Build and Install CosmoCC
+ - name: Build
shell: bash
run: |
- mkdir -p cosmocc
- cd cosmocc
- curl -o cosmocc.zip -L https://cosmo.zip/pub/cosmocc/cosmocc.zip
- unzip cosmocc.zip
- cd ..
- ./cosmocc/bin/make -j8 && ./cosmocc/bin/make install
- - name: Create LLM Executable
+ curl -L -o make https://cosmo.zip/pub/cosmos/bin/make
+ chmod +x make
+ ./make -j8
+ - name: Make Llamafile
shell: bash
run: |
- curl -o mistral.gguf -L https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.1-GGUF/resolve/main/mistral-7b-instruct-v0.1.Q4_K_M.gguf
+ curl -L -o tinyllama.gguf https://huggingface.co/TheBloke/TinyLlama-1.1B-Chat-v0.3-GGUF/resolve/main/tinyllama-1.1b-chat-v0.3.Q4_0.gguf
cat << EoF > .args
-m
- mistral.gguf
+ tinyllama.gguf
...
EoF
- cp /usr/local/bin/llamafile llamafile_exe
- chmod +x llamafile_exe
- zipalign -j0 \
- llamafile_exe \
- mistral.gguf \
+ cp o//llama.cpp/main/main \
+ tinyllama.llamafile
+ o//llamafile/zipalign -j0 \
+ tinyllama.llamafile \
+ tinyllama.gguf \
.args
- - name: Execute LLM CLI
+ - name: Execute LLM CLI CPU # GA doesn't have "support_simdgroup_reduction" for RMS_NORM :'(
shell: bash
run: |
- ./llamafile_exe --temp 0.7 --n-predict 50 -p '[INST]Write a story about llamas[/INST]'
+ ./tinyllama.llamafile -e -p '## Famous Speech\n\nFour score and seven' -n 50 -ngl 0
diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.c
index d8996593..11bdd360 100644
--- a/llama.cpp/ggml.c
+++ b/llama.cpp/ggml.c
@@ -21812,11 +21812,7 @@ int ggml_cpu_has_blas(void) {
}
int ggml_cpu_has_cuda(void) {
-#if defined(GGML_USE_CUDA)
- return 1;
-#else
- return 0;
-#endif
+ return llamafile_has_cuda();
}
int ggml_cpu_has_cublas(void) {
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cpp
index cee83fba..c788c059 100644
--- a/llama.cpp/llama.cpp
+++ b/llama.cpp/llama.cpp
@@ -5673,17 +5673,17 @@ static bool llm_load_tensors(
}
model.bufs.push_back(buf);
bufs.emplace(idx, buf);
-#ifdef GGML_USE_CUDA
- if (n_layer >= n_gpu_layers) {
+// #ifdef GGML_USE_CUDA
+ if (llamafile_has_cuda() && n_layer >= n_gpu_layers) {
ggml_backend_cuda_register_host_buffer(
ggml_backend_buffer_get_base(buf),
ggml_backend_buffer_get_size(buf));
}
-#endif
+// #endif
}
}
-#ifdef GGML_USE_METAL
- else if (ml.use_mmap && use_mmap_buffer && buft == ggml_backend_metal_buffer_type()) {
+// #ifdef GGML_USE_METAL
+ else if (llamafile_has_metal() && ml.use_mmap && use_mmap_buffer && buft == ggml_backend_metal_buffer_type()) {
for (uint32_t idx = 0; idx < ml.files.size(); idx++) {
const size_t max_size = ggml_get_max_tensor_size(ctx);
void * addr = nullptr;
@@ -5700,7 +5700,7 @@ static bool llm_load_tensors(
bufs.emplace(idx, buf);
}
}
-#endif
+// #endif
else {
ggml_backend_buffer_t buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft);
if (buf == nullptr) {
@@ -10880,11 +10880,11 @@ static void llama_graph_compute(
ggml_mpi_graph_compute_pre(lctx.ctx_mpi, gf, n_layer);
#endif
-#ifdef GGML_USE_METAL
+// #ifdef GGML_USE_METAL
if (ggml_backend_is_metal(lctx.backend_metal)) {
ggml_backend_metal_set_n_cb(lctx.backend_metal, n_threads);
}
-#endif
+// #endif
if (lctx.backend_cpu != nullptr) {
ggml_backend_cpu_set_n_threads(lctx.backend_cpu, n_threads);
@@ -14884,8 +14884,8 @@ struct llama_model_params llama_model_default_params() {
};
if (llamafile_has_metal()) {
- // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
- result.n_gpu_layers = 999;
+ // // note: we usually have plenty of VRAM, so by default offload all layers to the GPU
+ // result.n_gpu_layers = 999; // [jart] let llamafile/gpu.c handle this
}
return result;
@@ -15303,11 +15303,13 @@ if (llamafile_has_metal()) {
// enabling pipeline parallelism in the scheduler increases memory usage, so it is only done when necessary
bool pipeline_parallel = llama_get_device_count() > 1 && model->n_gpu_layers > (int)model->hparams.n_layer && model->split_mode == LLAMA_SPLIT_MODE_LAYER;
-#ifndef GGML_USE_CUDA
+// #ifndef GGML_USE_CUDA
+ if (llamafile_has_cuda()) {
// pipeline parallelism requires support for async compute and events
// currently this is only implemented in the CUDA backend
pipeline_parallel = false;
-#endif
+ }
+// #endif
ctx->sched = ggml_backend_sched_new(ctx->backends.data(), backend_buft.data(), ctx->backends.size(), LLAMA_MAX_NODES, pipeline_parallel);
if (pipeline_parallel) {
diff --git a/llama.cpp/llava/clip.cpp b/llama.cpp/llava/clip.cpp
index 809800bd..ec8d3654 100644
--- a/llama.cpp/llava/clip.cpp
+++ b/llama.cpp/llava/clip.cpp
@@ -1899,11 +1899,11 @@ bool clip_image_batch_encode(clip_ctx * ctx, const int n_threads, const clip_ima
ggml_backend_cpu_set_n_threads(ctx->backend, n_threads);
}
-#ifdef GGML_USE_METAL
+// #ifdef GGML_USE_METAL
if (ggml_backend_is_metal(ctx->backend)) {
ggml_backend_metal_set_n_cb(ctx->backend, n_threads);
}
-#endif
+// #endif
ggml_backend_graph_compute(ctx->backend, gf);
diff --git a/llamafile/cuda.c b/llamafile/cuda.c
index 9c678982..9e26173a 100644
--- a/llamafile/cuda.c
+++ b/llamafile/cuda.c
@@ -99,6 +99,7 @@ static struct Cuda {
typeof(ggml_backend_cuda_get_device_memory) *GGML_CALL get_device_memory;
typeof(ggml_backend_cuda_get_device_count) *GGML_CALL get_device_count;
typeof(ggml_backend_cuda_unregister_host_buffer) *GGML_CALL unreg_host_buf;
+ typeof(ggml_backend_cuda_register_host_buffer) *GGML_CALL register_host_buffer;
} ggml_cuda;
static const char *Dlerror(void) {
@@ -257,10 +258,10 @@ int RemoveDuplicatesFromStringArray(char **strings, int num_strings) {
return tail + 1;
}
-void FreeStringList(struct StringList* string_list) {
- struct StringListEntry* current = string_list->head;
+void FreeStringList(struct StringList *string_list) {
+ struct StringListEntry *current = string_list->head;
while (current != NULL) {
- struct StringListEntry* next = current->next;
+ struct StringListEntry *next = current->next;
free(current->string);
free(current);
current = next;
@@ -309,7 +310,7 @@ static bool get_amd_offload_arch_flag(char out[static 64]) {
int rc;
int t = 0;
char buf[512];
- char name[64] = {'g','f','x'};
+ char name[64] = {'g', 'f', 'x'};
int j = 3;
struct StringList name_list = {NULL, NULL, 0};
while ((rc = read(pipefds[0], buf, sizeof(buf))) > 0) {
@@ -368,7 +369,7 @@ static bool get_amd_offload_arch_flag(char out[static 64]) {
return false;
}
char *p = stpcpy(out, "--offload-arch=");
- char** names = NULL;
+ char **names = NULL;
CopyStringListToStringArray(&name_list, &names);
int num_names = RemoveDuplicatesFromStringArray(names, name_list.length);
@@ -707,6 +708,7 @@ static bool link_cuda_dso(const char *dso, const char *dir) {
ok &= !!(ggml_cuda.get_device_memory = imp(lib, "ggml_backend_cuda_get_device_memory"));
ok &= !!(ggml_cuda.get_device_count = imp(lib, "ggml_backend_cuda_get_device_count"));
ok &= !!(ggml_cuda.unreg_host_buf = imp(lib, "ggml_backend_cuda_unregister_host_buffer"));
+ ok &= !!(ggml_cuda.register_host_buffer = imp(lib, "ggml_backend_cuda_register_host_buffer"));
if (!ok) {
tinylog(__func__, ": error: not all cuda symbols could be imported\n", NULL);
cosmo_dlclose(lib);
@@ -973,3 +975,9 @@ GGML_CALL void ggml_backend_cuda_unregister_host_buffer(void *buffer) {
return;
return ggml_cuda.unreg_host_buf(buffer);
}
+
+GGML_CALL bool ggml_backend_cuda_register_host_buffer(void *buffer, size_t size) {
+ if (!llamafile_has_cuda())
+ return false;
+ return ggml_cuda.register_host_buffer(buffer, size);
+}