Skip to content

Distribute and run LLMs with a single file.

Make GPU auto configuration more resilient

Justine Tunneye34b35c33940
diff --git a/llama.cpp/ggml-backend.c b/llama.cpp/ggml-backend.c
index 9cabc92a..ac89c1dc 100644
--- a/llama.cpp/ggml-backend.c
+++ b/llama.cpp/ggml-backend.c
@@ -5,6 +5,7 @@
#include "ggml-alloc.h"
#include "llama.cpp/ggml-metal.h"
#include "ggml-impl.h"
+#include "llama.cpp/ggml-cuda.h"
#include "libc/thread/tls.h"
#include <assert.h>
@@ -296,11 +297,16 @@ GGML_CALL static void ggml_backend_registry_init(void) {
initialized = true;
ggml_backend_register("CPU", ggml_backend_reg_cpu_init, ggml_backend_cpu_buffer_type(), NULL);
- ggml_backend_register("Metal", ggml_backend_reg_metal_init, ggml_backend_metal_buffer_type(), NULL);
+
+ if (ggml_metal_supported()) {
+ ggml_backend_register("Metal", ggml_backend_reg_metal_init, ggml_backend_metal_buffer_type(), NULL);
+ }
// add forward decls here to avoid including the backend headers
extern int ggml_backend_cuda_reg_devices(void);
- ggml_backend_cuda_reg_devices();
+ if (!ggml_metal_supported() && ggml_cublas_loaded()) {
+ ggml_backend_cuda_reg_devices();
+ }
}
void ggml_backend_register(const char * name, ggml_backend_init_fn init_fn, ggml_backend_buffer_type_t default_buffer_type, void * user_data) {
diff --git a/llama.cpp/ggml-cuda.cu b/llama.cpp/ggml-cuda.cu
index 8391b1f1..fbd0653b 100644
--- a/llama.cpp/ggml-cuda.cu
+++ b/llama.cpp/ggml-cuda.cu
@@ -1,5 +1,5 @@
-// -*- mode:cuda;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
-// vi: set et ft=cuda ts=4 sts=4 sw=4 fenc=utf-8 :vi
+// -*- mode:c;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi
#include <algorithm>
#include <assert.h>
@@ -252,6 +252,7 @@
#define CUBLAS_HANDLE(id) g_cublas_handles[id]
#endif
+static bool FLAG_log_disable;
static const struct ggml_backend_api *g_backend;
#define exit g_backend->exit
#define ggml_is_quantized g_backend->ggml_is_quantized
@@ -7441,11 +7442,19 @@ GGML_CALL bool ggml_cublas_loaded() {
return g_cublas_loaded;
}
-GGML_CALL void ggml_init_cublas() {
+GGML_CALL void ggml_init_cublas(bool log_disable) {
static bool initialized = false;
+ FLAG_log_disable = log_disable;
if (!initialized) {
+ // rocblas_initialize() will abort() if no devices exist
+ if (cudaGetDeviceCount(&g_device_count) != cudaSuccess || g_device_count == 0) {
+ initialized = true;
+ g_cublas_loaded = false;
+ return;
+ }
+
#ifdef __HIP_PLATFORM_AMD__
#ifndef GGML_USE_TINYBLAS
// Workaround for a rocBLAS bug when using multiple graphics cards:
@@ -7463,6 +7472,7 @@ GGML_CALL void ggml_init_cublas() {
GGML_ASSERT(g_device_count <= GGML_CUDA_MAX_DEVICES);
int64_t total_vram = 0;
+ if (!FLAG_log_disable) {
#if defined(GGML_CUDA_FORCE_MMQ)
fprintf(stderr, "%s: GGML_CUDA_FORCE_MMQ: yes\n", __func__);
#else
@@ -7474,6 +7484,7 @@ GGML_CALL void ggml_init_cublas() {
fprintf(stderr, "%s: CUDA_USE_TENSOR_CORES: no\n", __func__);
#endif
fprintf(stderr, "%s: found %d " GGML_CUDA_NAME " devices:\n", __func__, g_device_count);
+ }
for (int id = 0; id < g_device_count; ++id) {
int device_vmm = 0;
@@ -7494,7 +7505,9 @@ GGML_CALL void ggml_init_cublas() {
cudaDeviceProp prop;
CUDA_CHECK(cudaGetDeviceProperties(&prop, id));
+ if (!FLAG_log_disable) {
fprintf(stderr, " Device %d: %s, compute capability %d.%d, VMM: %s\n", id, prop.name, prop.major, prop.minor, device_vmm ? "yes" : "no");
+ }
g_tensor_split[id] = total_vram;
total_vram += prop.totalGlobalMem;
@@ -10075,7 +10088,9 @@ GGML_CALL void ggml_cuda_set_main_device(const int main_device) {
g_main_device = main_device;
cudaDeviceProp prop;
CUDA_CHECK(cudaGetDeviceProperties(&prop, g_main_device));
+ if (!FLAG_log_disable) {
fprintf(stderr, "%s: using device %d (%s) as main device\n", __func__, g_main_device, prop.name);
+ }
}
}
@@ -10112,7 +10127,9 @@ GGML_CALL bool ggml_cuda_compute_forward(struct ggml_compute_params * params, st
if (tensor->op == GGML_OP_MUL_MAT) {
if (tensor->src[0]->ne[3] != tensor->src[1]->ne[3]) {
#ifndef NDEBUG
+ if (!FLAG_log_disable) {
fprintf(stderr, "%s: cannot compute %s: src0->ne[3] = %" PRId64 ", src1->ne[3] = %" PRId64 " - fallback to CPU\n", __func__, tensor->name, tensor->src[0]->ne[3], tensor->src[1]->ne[3]);
+ }
#endif
return false;
}
@@ -10765,7 +10782,7 @@ static ggml_backend_i cuda_backend_i = {
};
GGML_CALL ggml_backend_t ggml_backend_cuda_init(int device) {
- ggml_init_cublas(); // TODO: remove from ggml.c
+ ggml_init_cublas(FLAG_log_disable); // TODO: remove from ggml.c
if (device < 0 || device >= ggml_cuda_get_device_count()) {
fprintf(stderr, "%s: error: invalid device %d\n", __func__, device);
diff --git a/llama.cpp/ggml-cuda.h b/llama.cpp/ggml-cuda.h
index 3c9ce29d..59e74857 100644
--- a/llama.cpp/ggml-cuda.h
+++ b/llama.cpp/ggml-cuda.h
@@ -13,7 +13,7 @@ extern "C" {
#define GGML_CUDA_MAX_DEVICES 16
// Always success. To check if CUDA is actually loaded, use `ggml_cublas_loaded`.
-GGML_API void ggml_init_cublas(void) GGML_CALL;
+GGML_API void ggml_init_cublas(bool log_disable) GGML_CALL;
GGML_API bool ggml_cuda_supported(void);
GGML_API void ggml_cuda_link(const struct ggml_backend_api *) GGML_CALL;
diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.c
index a54a1e5c..0e70ab62 100644
--- a/llama.cpp/ggml.c
+++ b/llama.cpp/ggml.c
@@ -32,6 +32,7 @@ SOFTWARE.\"");
#include "ggml-quants.h"
#include "ggml-metal.h"
#include "ggml-cuda.h"
+#include "llamafile/log.h"
#include "libc/log/backtrace.internal.h"
#include <alloca.h>
@@ -2307,7 +2308,7 @@ struct ggml_context * ggml_init(struct ggml_init_params params) {
GGML_PRINT_DEBUG("%s: g_state initialized in %f ms\n", __func__, (t_end - t_start)/1000.0f);
}
- ggml_init_cublas();
+ ggml_init_cublas(FLAG_log_disable);
#if defined(GGML_USE_CLBLAST)
ggml_cl_init();
#endif
diff --git a/llamafile/BUILD.mk b/llamafile/BUILD.mk
index a3cf257f..f2142e2b 100644
--- a/llamafile/BUILD.mk
+++ b/llamafile/BUILD.mk
@@ -5,11 +5,14 @@ PKGS += LLAMAFILE
LLAMAFILE_FILES := $(wildcard llamafile/*.*)
LLAMAFILE_HDRS = $(filter %.h,$(LLAMAFILE_FILES))
-LLAMAFILE_SRCS = $(filter %.c,$(LLAMAFILE_FILES))
+LLAMAFILE_SRCS_C = $(filter %.c,$(LLAMAFILE_FILES))
+LLAMAFILE_SRCS_CPP = $(filter %.cpp,$(LLAMAFILE_FILES))
+LLAMAFILE_SRCS = $(LLAMAFILE_SRCS_C) $(LLAMAFILE_SRCS_CPP)
LLAMAFILE_DOCS = $(filter %.1,$(LLAMAFILE_FILES))
LLAMAFILE_OBJS = \
- $(LLAMAFILE_SRCS:%.c=o/$(MODE)/%.o) \
+ $(LLAMAFILE_SRCS_C:%.c=o/$(MODE)/%.o) \
+ $(LLAMAFILE_SRCS_CPP:%.cpp=o/$(MODE)/%.o) \
$(LLAMAFILE_FILES:%=o/$(MODE)/%.zip.o)
o/$(MODE)/llamafile/zipalign: \
@@ -22,9 +25,15 @@ o/$(MODE)/llamafile/zipcheck: \
o/$(MODE)/llamafile/zipcheck.o \
o/$(MODE)/llamafile/zip.o
+o/$(MODE)/llamafile/simple: \
+ o/$(MODE)/llamafile/simple.o \
+ o/$(MODE)/llama.cpp/llava/llava.a \
+ o/$(MODE)/llama.cpp/llama.cpp.a
+
.PHONY: o/$(MODE)/llamafile
o/$(MODE)/llamafile: \
$(LLAMAFILE_OBJS) \
+ o/$(MODE)/llamafile/simple \
o/$(MODE)/llamafile/zipalign \
o/$(MODE)/llamafile/zipcheck \
o/$(MODE)/llamafile/addnl
diff --git a/llamafile/cuda.c b/llamafile/cuda.c
index 14c12f44..4db76c0f 100644
--- a/llamafile/cuda.c
+++ b/llamafile/cuda.c
@@ -891,8 +891,14 @@ static void import_cuda(void) {
return;
}
if (import_cuda_impl()) {
- tinylog(__func__, ": GPU support successfully linked and loaded\n", NULL);
- ggml_cuda.supported = true;
+ tinylog(__func__, ": GPU support linked\n", NULL);
+ ggml_cuda.ggml_init_cublas(FLAG_log_disable);
+ if (ggml_cuda.ggml_cublas_loaded()) {
+ tinylog(__func__, ": GPU support loaded\n", NULL);
+ ggml_cuda.supported = true;
+ } else {
+ tinylog(__func__, ": GPU support not possible\n", NULL);
+ }
} else if (FLAG_gpu == LLAMAFILE_GPU_AMD ||
FLAG_gpu == LLAMAFILE_GPU_NVIDIA) {
tinyprint(2, "fatal error: support for --gpu ",
@@ -907,9 +913,9 @@ bool ggml_cuda_supported(void) {
return ggml_cuda.supported;
}
-GGML_CALL void ggml_init_cublas(void) {
+GGML_CALL void ggml_init_cublas(bool log_disable) {
if (!ggml_cuda_supported()) return;
- return ggml_cuda.ggml_init_cublas();
+ return ggml_cuda.ggml_init_cublas(log_disable);
}
GGML_CALL bool ggml_cublas_loaded(void) {
@@ -1020,12 +1026,12 @@ GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_buffer_type(int device) {
GGML_CALL ggml_backend_t ggml_backend_reg_cuda_init(const char * params, void * user_data) {
if (!ggml_cuda_supported()) return 0;
- ggml_cuda.backend_reg_init(params, user_data);
+ return ggml_cuda.backend_reg_init(params, user_data);
}
GGML_CALL ggml_backend_buffer_type_t ggml_backend_cuda_host_buffer_type() {
if (!ggml_cuda_supported()) return 0;
- ggml_cuda.ggml_backend_cuda_host_buffer_type();
+ return ggml_cuda.ggml_backend_cuda_host_buffer_type();
}
int ggml_backend_cuda_reg_devices(void) {
diff --git a/llamafile/gpu.c b/llamafile/gpu.c
index 440a5b8e..816747ce 100644
--- a/llamafile/gpu.c
+++ b/llamafile/gpu.c
@@ -51,11 +51,7 @@ const char *llamafile_describe_gpu(void) {
* Returns true if GPU support is available.
*/
bool llamafile_gpu_supported(void) {
- if (ggml_metal_supported()) {
- return true;
- }
- ggml_init_cublas();
- return ggml_cublas_loaded();
+ return ggml_metal_supported() || ggml_cublas_loaded();
}
/**
diff --git a/llamafile/simple.cpp b/llamafile/simple.cpp
new file mode 100644
index 00000000..222acd89
--- /dev/null
+++ b/llamafile/simple.cpp
@@ -0,0 +1,89 @@
+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-
+// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
+//
+// Copyright 2024 Mozilla Foundation
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include <cmath>
+#include <cstdio>
+#include <string>
+#include <vector>
+#include <cosmo.h>
+
+#include "llama.cpp/llama.h"
+#include "llama.cpp/common.h"
+#include "llamafile/llamafile.h"
+
+static bool eval_tokens(struct llama_context *ctx_llama,
+ std::vector<llama_token> tokens,
+ int n_batch, int *n_past) {
+ int N = (int)tokens.size();
+ for (int i = 0; i < N; i += n_batch) {
+ int n_eval = (int)tokens.size() - i;
+ if (n_eval > n_batch) n_eval = n_batch;
+ if (llama_decode(ctx_llama, llama_batch_get_one(&tokens[i], n_eval, *n_past, 0))) {
+ return false; // probably ran out of context
+ }
+ *n_past += n_eval;
+ }
+ return true;
+}
+
+static bool eval_id(struct llama_context *ctx_llama, int id, int *n_past) {
+ std::vector<llama_token> tokens;
+ tokens.push_back(id);
+ return eval_tokens(ctx_llama, tokens, 1, n_past);
+}
+
+static bool eval_string(struct llama_context *ctx_llama, const char *str,
+ int n_batch, int *n_past, bool add_bos) {
+ std::string str2 = str;
+ std::vector<llama_token> embd_inp = ::llama_tokenize(ctx_llama, str2, add_bos);
+ return eval_tokens(ctx_llama, embd_inp, n_batch, n_past);
+}
+
+int main(int argc, char **argv) {
+ gpt_params params;
+ if (!gpt_params_parse(argc, argv, params)) return 1;
+ if (params.prompt.empty()) params.prompt = "The";
+ params.n_ctx = 0;
+ FLAG_log_disable = true;
+ FLAG_gpu = LLAMAFILE_GPU_AUTO;
+ llama_backend_init(params.numa);
+ llama_model_params model_params = llama_model_default_params();
+ model_params.n_gpu_layers = llamafile_gpu_layers(35);
+ llama_model *model = llama_load_model_from_file(params.model.c_str(), model_params);
+ if (model == NULL) return 2;
+ llama_context_params ctx_params = llama_context_params_from_gpt_params(params);
+ llama_context *ctx = llama_new_context_with_model(model, ctx_params);
+ if (ctx == NULL) return 3;
+ printf("%s", params.prompt.c_str());
+ int n_past = 0;
+ bool add_bos = llama_should_add_bos_token(llama_get_model(ctx));
+ eval_string(ctx, params.prompt.c_str(), params.n_batch, &n_past, add_bos);
+ struct llama_sampling_context *ctx_sampling = llama_sampling_init(params.sparams);
+ for (;;) {
+ llama_token id = llama_sampling_sample(ctx_sampling, ctx, NULL);
+ llama_sampling_accept(ctx_sampling, ctx, id, true);
+ if (id == llama_token_eos(model)) break;
+ printf("%s", llama_token_to_piece(ctx, id).c_str());
+ fflush(stdout);
+ if (!eval_id(ctx, id, &n_past)) break;
+ }
+ printf("\n");
+ llama_sampling_free(ctx_sampling);
+ llama_free(ctx);
+ llama_free_model(model);
+ llama_backend_free();
+}