Skip to content

Distribute and run LLMs with a single file.

Clean up the GPU support code

Justine Tunneyef10913e1d6d
diff --git a/README.md b/README.md
index 9db05c81..e00a2b6f 100644
--- a/README.md
+++ b/README.md
@@ -599,18 +599,17 @@ The executable program is now in a weird hybrid state where two separate
C libraries exist which have different ABIs. For example, thread local
storage works differently on each operating system, and programs will
crash if the TLS register doesn't point to the appropriate memory. The
-way Cosmopolitan Libc solves that is by JITing a trampoline around each
-dlsym() import, which blocks signals using `sigprocmask()` and changes
-the TLS register using `arch_prctl()`. Under normal circumstances,
-aspecting each function call with four additional system calls would be
-prohibitively expensive, but for llama.cpp that cost is infinitesimal
-compared to the amount of compute used for LLM inference. Our technique
-has no noticeable slowdown. The major tradeoff is that, right now, you
-can't pass callback pointers to the dlopen()'d module. Only one such
-function needed to be removed from the llama.cpp codebase, which was an
-API intended for customizing logging. In the future, Cosmoplitan will
-just trampoline signal handlers and code morph the TLS instructions to
-avoid these tradeoffs entirely. See
+way Cosmopolitan Libc solves that on AMD is by using SSE to recompile
+the executable at runtime to change `%fs` register accesses into `%gs`
+which takes a millisecond. On ARM, Cosmo uses the `x28` register for TLS
+which can be made safe by passing the `-ffixed-x28` flag when compiling
+GPU modules. Lastly, llamafile uses the `__ms_abi__` attribute so that
+function pointers passed between the application and GPU modules conform
+to the Windows calling convention. Amazingly enough, every compiler we
+tested, including nvcc on Linux and even Objective-C on MacOS, all
+support compiling WIN32 style functions, thus ensuring your llamafile
+will be able to talk to Windows drivers, when it's run on Windows,
+without needing to be recompiled as a separate file for Windows. See
[cosmopolitan/dlopen.c](https://github.com/jart/cosmopolitan/blob/master/libc/dlopen/dlopen.c)
for further details.
diff --git a/llama.cpp/common.cpp b/llama.cpp/common.cpp
index 2c92125e..57386775 100644
--- a/llama.cpp/common.cpp
+++ b/llama.cpp/common.cpp
@@ -11,6 +11,7 @@
#include <cmath>
#include <cerrno>
#include <cstring>
+#include <climits>
#include <ctime>
#include <fstream>
#include <iterator>
@@ -138,6 +139,9 @@ bool gpt_params_parse(int argc, char ** argv, gpt_params & params) {
exit(1);
}
#endif
+ if (FLAG_gpu == LLAMAFILE_GPU_DISABLE) {
+ params.n_gpu_layers = 0;
+ }
return result;
}
@@ -533,7 +537,7 @@ bool gpt_params_parse_ex(int argc, char ** argv, gpt_params & params) {
break;
}
FLAG_gpu = llamafile_gpu_parse(argv[i]);
- if (FLAG_gpu == -1) {
+ if (FLAG_gpu == INT_MIN) {
fprintf(stderr, "error: invalid --gpu flag value: %s\n", argv[i]);
exit(1);
}
diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.c
index f6e4187d..4301ebaa 100644
--- a/llama.cpp/ggml.c
+++ b/llama.cpp/ggml.c
@@ -173,7 +173,7 @@ inline static void * ggml_aligned_malloc(size_t size) {
}
void * aligned_memory = NULL;
int result;
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
+ if (ggml_metal_supported()) {
result = posix_memalign(&aligned_memory, 16384, size);
} else {
result = posix_memalign(&aligned_memory, GGML_MEM_ALIGN, size);
@@ -14464,7 +14464,7 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
return;
}
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
bool skip_cpu = ggml_cuda_compute_forward(params, tensor);
if (skip_cpu) {
return;
@@ -16342,7 +16342,7 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads) {
//n_tasks = MIN(n_threads, MAX(1, nr0/128));
//printf("nr0 = %8d, nr1 = %8d, nr0*nr1 = %8d, n_tasks%d\n", nr0, nr1, nr0*nr1, n_tasks);
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
if (ggml_cuda_can_mul_mat(node->src[0], node->src[1], node)) {
n_tasks = 1; // TODO: this actually is doing nothing
// the threads are still spinning
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cpp
index 0ad5b4e3..56c81340 100644
--- a/llama.cpp/llama.cpp
+++ b/llama.cpp/llama.cpp
@@ -1031,13 +1031,11 @@ static std::string llama_token_to_piece(const struct llama_context * ctx, llama_
static ggml_backend_buffer_type_t llama_default_buffer_type(int n_gpu_layers) {
ggml_backend_buffer_type_t buft = nullptr;
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
if (n_gpu_layers > 0) {
buft = ggml_backend_metal_buffer_type();
}
- }
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (buft == nullptr) {
#if defined(LLAMA_GGML_BACKEND_CUDA_TEST)
if (n_gpu_layers > 0) {
buft = ggml_backend_cuda_buffer_type(0);
@@ -1270,7 +1268,7 @@ struct llama_kv_cache {
~llama_kv_cache() {
#if !defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
for (size_t i = 0; i < k_l.size(); ++i) {
ggml_cuda_free_data(k_l[i]);
ggml_cuda_free_data(v_l[i]);
@@ -1384,7 +1382,7 @@ struct llama_model {
~llama_model() {
#if !defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
for (size_t i = 0; i < tensors_by_name.size(); ++i) {
ggml_cuda_free_data(tensors_by_name[i].second);
}
@@ -2260,7 +2258,7 @@ struct llama_model_loader {
}
#if !defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- const bool legacy_offload = llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA;
+ const bool legacy_offload = ggml_cuda_supported();
#else
const bool legacy_offload = false;
#endif
@@ -2997,7 +2995,7 @@ static bool llm_load_tensors(
enum ggml_backend_type llama_backend_offload_split = GGML_BACKEND_CPU;
#if !defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
LLAMA_LOG_INFO("%s: using " GGML_CUDA_NAME " for GPU acceleration\n", __func__);
ggml_cuda_set_main_device(main_gpu);
@@ -3693,7 +3691,7 @@ static bool llm_load_tensors(
// create backend buffer
ggml_backend_buffer_t buf_mmap = nullptr;
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
+ if (ggml_metal_supported()) {
if (n_gpu_layers > 0) {
if (ml.use_mmap) {
const size_t max_size = ggml_get_max_tensor_size(ctx);
@@ -3705,7 +3703,7 @@ static bool llm_load_tensors(
}
}
#if defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
// for testing only
if (n_gpu_layers > 0) {
model.buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, ggml_backend_cuda_buffer_type(0));
@@ -3748,7 +3746,7 @@ static bool llm_load_tensors(
}
#if !defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ if (ggml_cuda_supported()) {
const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer));
LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_gpu);
@@ -6582,13 +6580,9 @@ static int llama_decode_internal(
ggml_mpi_graph_compute_pre(lctx.ctx_mpi, gf, n_layer);
#endif
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
if (ggml_backend_is_metal(lctx.backend)) {
ggml_backend_metal_set_n_cb(lctx.backend, n_threads);
- }
- }
-
- if (ggml_backend_is_cpu(lctx.backend)) {
+ } else if (ggml_backend_is_cpu(lctx.backend)) {
ggml_backend_cpu_set_n_threads(lctx.backend, n_threads);
}
ggml_backend_graph_compute(lctx.backend, gf);
@@ -9381,7 +9375,7 @@ struct llama_model_params llama_model_default_params() {
/*.use_mlock =*/ false,
};
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
+ if (ggml_metal_supported()) {
result.n_gpu_layers = 1;
}
@@ -9575,7 +9569,7 @@ struct llama_context * llama_new_context_with_model(
// reserve memory for context buffers
if (!hparams.vocab_only) {
// initialize backend
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
+ if (ggml_metal_supported()) {
if (model->n_gpu_layers > 0) {
ctx->backend = ggml_backend_metal_init();
if (ctx->backend == nullptr) {
@@ -9584,7 +9578,7 @@ struct llama_context * llama_new_context_with_model(
}
}
#if defined(LLAMA_GGML_BACKEND_CUDA_TEST)
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
+ else if (ggml_cuda_supported()) {
// for testing only
if (model->n_gpu_layers > 0) {
ctx->backend = ggml_backend_cuda_init(0);
diff --git a/llama.cpp/llava/clip.cpp b/llama.cpp/llava/clip.cpp
index 43b7e0e9..10ac6391 100644
--- a/llama.cpp/llava/clip.cpp
+++ b/llama.cpp/llava/clip.cpp
@@ -539,13 +539,12 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
clip_ctx * new_clip = new clip_ctx;
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
- new_clip->backend = ggml_backend_cuda_init(0);
+ if ((new_clip->backend = ggml_backend_cuda_init(0))) {
tinylogf("%s: CLIP using CUDA backend\n", __func__);
}
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
- new_clip->backend = ggml_backend_metal_init();
+ if (!new_clip->backend &&
+ (new_clip->backend = ggml_backend_metal_init())) {
tinylogf("%s: CLIP using Metal backend\n", __func__);
}
@@ -917,14 +916,9 @@ 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);
}
-#if 0
- // TODO(jart): destroy this backend framework
- if (llamafile_gpu_supported() == LLAMAFILE_GPU_APPLE) {
if (ggml_backend_is_metal(ctx->backend)) {
ggml_backend_metal_set_n_cb(ctx->backend, n_threads);
}
- }
-#endif
ggml_backend_graph_compute(ctx->backend, gf);
diff --git a/llamafile/cuda.c b/llamafile/cuda.c
index bb137ac5..29049fd2 100644
--- a/llamafile/cuda.c
+++ b/llamafile/cuda.c
@@ -636,8 +636,7 @@ static bool LinkCudaDso(const char *dso, const char *dir) {
// ask the library if actual gpu devices exist
ggml_cuda.ggml_cuda_link(ggml_backend_api());
- ggml_cuda.init();
- return ggml_cuda.loaded();
+ return true;
}
static bool ImportCudaImpl(void) {
@@ -833,6 +832,12 @@ static void ImportCuda(void) {
if (ImportCudaImpl()) {
tinylog("GPU support successfully linked and loaded\n", NULL);
ggml_cuda.supported = true;
+ } else if (FLAG_gpu == LLAMAFILE_GPU_AMD ||
+ FLAG_gpu == LLAMAFILE_GPU_NVIDIA) {
+ tinyprint(2, "fatal error: support for --gpu ",
+ llamafile_describe_gpu(), FLAG_tinyblas ? " --tinyblas" : "",
+ " was explicitly requested, but it wasn't available\n", NULL);
+ exit(1);
}
}
diff --git a/llamafile/gpu.c b/llamafile/gpu.c
index 178aec2b..ca53b93a 100644
--- a/llamafile/gpu.c
+++ b/llamafile/gpu.c
@@ -19,6 +19,7 @@
#include <string.h>
#include <unistd.h>
#include <stdlib.h>
+#include <limits.h>
#include "llama.cpp/ggml-cuda.h"
#include "llama.cpp/ggml-metal.h"
@@ -28,7 +29,7 @@ bool FLAG_tinyblas;
bool FLAG_nocompile;
bool FLAG_recompile;
-static const char *describe_required_gpu(void) {
+const char *llamafile_describe_gpu(void) {
switch (FLAG_gpu) {
case LLAMAFILE_GPU_AUTO:
return "auto";
@@ -46,29 +47,10 @@ static const char *describe_required_gpu(void) {
}
/**
- * Returns GPU subsystem if GPU support is available.
+ * Returns true if GPU support is available.
*/
int llamafile_gpu_supported(void) {
-
- // Auto-configure Apple Metal GPU support.
- if (ggml_metal_supported()) {
- return LLAMAFILE_GPU_APPLE;
- }
-
- // Auto-configure AMD or NVIDIA GPU support.
- if (ggml_cuda_supported()) {
- return LLAMAFILE_GPU_NVIDIA;
- }
-
- // Abort if user wants specific GPU but it's unavailable.
- if (FLAG_gpu > 0 || FLAG_tinyblas) {
- tinyprint(2, "fatal error: support for --gpu ",
- describe_required_gpu(), FLAG_tinyblas ? " --tinyblas" : "",
- " was explicitly requested, but it wasn't available\n", NULL);
- exit(1);
- }
-
- return 0;
+ return ggml_metal_supported() && ggml_cuda_supported();
}
/**
@@ -92,5 +74,5 @@ int llamafile_gpu_parse(const char *s) {
if (!strcasecmp(s, "rocm")) return LLAMAFILE_GPU_AMD;
if (!strcasecmp(s, "hip")) return LLAMAFILE_GPU_AMD;
- return -1;
+ return INT_MIN;
}
diff --git a/llamafile/llamafile.h b/llamafile/llamafile.h
index a401a7df..bfaf63f1 100644
--- a/llamafile/llamafile.h
+++ b/llamafile/llamafile.h
@@ -32,13 +32,14 @@ bool llamafile_launch_browser(const char *);
#define LLAMAFILE_GPU_AUTO 0
#define LLAMAFILE_GPU_AMD 1
#define LLAMAFILE_GPU_APPLE 2
-#define LLAMAFILE_GPU_NVIDIA 3
+#define LLAMAFILE_GPU_NVIDIA 4
extern int FLAG_gpu;
extern bool FLAG_tinyblas;
extern bool FLAG_nocompile;
extern bool FLAG_recompile;
int llamafile_gpu_supported(void);
int llamafile_gpu_parse(const char *);
+const char *llamafile_describe_gpu(void);
#ifdef __cplusplus
}
diff --git a/llamafile/metal.c b/llamafile/metal.c
index fa882b7b..79be3401 100644
--- a/llamafile/metal.c
+++ b/llamafile/metal.c
@@ -267,6 +267,11 @@ static void ImportMetal(void) {
if (ImportMetalImpl()) {
ggml_metal.supported = true;
tinylog("Apple Metal GPU support successfully loaded\n", NULL);
+ } else if (FLAG_gpu == LLAMAFILE_GPU_APPLE) {
+ tinyprint(2, "fatal error: support for --gpu ",
+ llamafile_describe_gpu(), FLAG_tinyblas ? " --tinyblas" : "",
+ " was explicitly requested, but it wasn't available\n", NULL);
+ exit(1);
}
}