Skip to content

Distribute and run LLMs with a single file.

Make --log-disable work better

Justine Tunney7d23bc994fe3
diff --git a/llama.cpp/ggml-cuda.cu b/llama.cpp/ggml-cuda.cu
index ffd1ac36..cfbf9b4a 100644
--- a/llama.cpp/ggml-cuda.cu
+++ b/llama.cpp/ggml-cuda.cu
@@ -403,7 +403,6 @@ static ggml_backend_buffer_t ggml_backend_buffer_init(
struct ggml_backend_buffer_i iface,
ggml_backend_buffer_context_t context,
size_t size) {
- fprintf(stderr, "WARNING: using untested foreign malloc due to ggml backend\n");
ggml_backend_buffer_t buffer = (ggml_backend_buffer_t)malloc(sizeof(struct ggml_backend_buffer));
memset(buffer, 0, sizeof(*buffer));
buffer->iface = iface;
@@ -512,7 +511,6 @@ static struct ggml_backend_buffer_i cpu_backend_buffer_i = {
static ggml_backend_buffer_t ggml_backend_cpu_buffer_type_alloc_buffer(ggml_backend_buffer_type_t buft, size_t size) {
size += TENSOR_ALIGNMENT; // malloc may return an address that is not aligned
- fprintf(stderr, "WARNING: using untested foreign malloc due to ggml backend\n");
void * data = malloc(size); // TODO: maybe use GGML_ALIGNED_MALLOC?
return ggml_backend_buffer_init(buft, cpu_backend_buffer_i, data, size);
}
diff --git a/llama.cpp/ggml-metal.m b/llama.cpp/ggml-metal.m
index 6c43ae7d..fed9917d 100644
--- a/llama.cpp/ggml-metal.m
+++ b/llama.cpp/ggml-metal.m
@@ -19,9 +19,6 @@ static ggml_backend_buffer_t ggml_backend_buffer_init(
struct ggml_backend_buffer_i iface,
ggml_backend_buffer_context_t context,
size_t size) {
- // TODO(jart): this malloc() looks problematic
- // what does this backend framework do?
- fprintf(stderr, "WARNING: using untested foreign malloc due to ggml backend\n");
ggml_backend_buffer_t buffer = malloc(sizeof(struct ggml_backend_buffer));
(*buffer) = (struct ggml_backend_buffer) {
/* .interface = */ iface,
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cpp
index b8e48b80..0ad5b4e3 100644
--- a/llama.cpp/llama.cpp
+++ b/llama.cpp/llama.cpp
@@ -14,6 +14,8 @@
#include "ggml-metal.h"
#include "ggml-cuda.h"
+#include "llamafile/log.h"
+
#if defined(GGML_USE_CLBLAST)
# include "ggml-opencl.h"
#endif
@@ -10823,6 +10825,7 @@ static void llama_log_internal_v(ggml_log_level level, const char * format, va_l
static void llama_log_internal(ggml_log_level level, const char * format, ...) {
va_list args;
+ if (FLAG_log_disable) return;
va_start(args, format);
llama_log_internal_v(level, format, args);
va_end(args);
diff --git a/llama.cpp/llava/clip.cpp b/llama.cpp/llava/clip.cpp
index d2d831f7..7f384137 100644
--- a/llama.cpp/llava/clip.cpp
+++ b/llama.cpp/llava/clip.cpp
@@ -17,8 +17,11 @@
#include <vector>
#include "clip.h"
+#include "llamafile/log.h"
#include "llama.cpp/ggml.h"
#include "llama.cpp/runtime.h"
+#include "llama.cpp/ggml-cuda.h"
+#include "llama.cpp/ggml-metal.h"
#include "llama.cpp/ggml-alloc.h"
#include "llama.cpp/ggml-backend.h"
@@ -494,15 +497,15 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
const int idx_name = gguf_find_key(ctx, KEY_NAME);
if (idx_name != -1) { // make name optional temporarily as some of the uploaded models missing it due to a bug
const std::string name = gguf_get_val_str(ctx, idx_name);
- fprintf(stderr, "%s: model name: %s\n", __func__, name.c_str());
+ tinylogf("%s: model name: %s\n", __func__, name.c_str());
}
- fprintf(stderr, "%s: description: %s\n", __func__, description.c_str());
- fprintf(stderr, "%s: GGUF version: %d\n", __func__, gguf_get_version(ctx));
- fprintf(stderr, "%s: alignment: %zu\n", __func__, gguf_get_alignment(ctx));
- fprintf(stderr, "%s: n_tensors: %d\n", __func__, n_tensors);
- fprintf(stderr, "%s: n_kv: %d\n", __func__, n_kv);
- fprintf(stderr, "%s: ftype: %s\n", __func__, ftype_str.c_str());
- fprintf(stderr, "\n");
+ tinylogf("%s: description: %s\n", __func__, description.c_str());
+ tinylogf("%s: GGUF version: %d\n", __func__, gguf_get_version(ctx));
+ tinylogf("%s: alignment: %zu\n", __func__, gguf_get_alignment(ctx));
+ tinylogf("%s: n_tensors: %d\n", __func__, n_tensors);
+ tinylogf("%s: n_kv: %d\n", __func__, n_kv);
+ tinylogf("%s: ftype: %s\n", __func__, ftype_str.c_str());
+ tinylogf("\n");
}
const int n_tensors = gguf_get_n_tensors(ctx);
// kv
@@ -512,9 +515,9 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
for (int i = 0; i < n_kv; ++i) {
const char * key = gguf_get_key(ctx, i);
- fprintf(stderr, "%s: kv[%d]: key = %s\n", __func__, i, key);
+ tinylogf("%s: kv[%d]: key = %s\n", __func__, i, key);
}
- fprintf(stderr, "\n");
+ tinylogf("\n");
}
// data
@@ -527,7 +530,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
size_t tensor_size = ggml_nbytes(cur);
buffer_size += tensor_size;
if (verbosity >= 3) {
- fprintf(stderr, "%s: tensor[%d]: n_dims = %d, name = %s, tensor_size=%zu, offset=%zu\n", __func__, i,
+ tinylogf("%s: tensor[%d]: n_dims = %d, name = %s, tensor_size=%zu, offset=%zu\n", __func__, i,
ggml_n_dims(cur), cur->name, tensor_size, offset);
}
}
@@ -537,21 +540,18 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
clip_ctx * new_clip = new clip_ctx;
-#if 0
- // TODO(jart): destroy this backend framework
if (llamafile_gpu_supported() == LLAMAFILE_GPU_NVIDIA) {
- new_clip->backend = ggml_backend_cuda_init(0);
- fprintf(stderr, "%s: CLIP using CUDA backend\n", __func__);
+ 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();
- fprintf(stderr, "%s: CLIP using Metal backend\n", __func__);
+ new_clip->backend = ggml_backend_metal_init();
+ tinylogf("%s: CLIP using Metal backend\n", __func__);
}
-#endif
if (!new_clip->backend) {
new_clip->backend = ggml_backend_cpu_init();
- fprintf(stderr, "%s: CLIP using CPU backend\n", __func__);
+ tinylogf("%s: CLIP using CPU backend\n", __func__);
}
// model size and capabilities
@@ -575,15 +575,15 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
new_clip->use_gelu = gguf_get_val_bool(ctx, idx);
if (verbosity >= 1) {
- fprintf(stderr, "%s: text_encoder: %d\n", __func__, new_clip->has_text_encoder);
- fprintf(stderr, "%s: vision_encoder: %d\n", __func__, new_clip->has_vision_encoder);
- fprintf(stderr, "%s: llava_projector: %d\n", __func__, new_clip->has_llava_projector);
- fprintf(stderr, "%s: model size: %.2f MB\n", __func__, buffer_size / 1024.0 / 1024.0);
- fprintf(stderr, "%s: metadata size: %.2f MB\n", __func__, ggml_get_mem_size(meta) / 1024.0 / 1024.0);
+ tinylogf("%s: text_encoder: %d\n", __func__, new_clip->has_text_encoder);
+ tinylogf("%s: vision_encoder: %d\n", __func__, new_clip->has_vision_encoder);
+ tinylogf("%s: llava_projector: %d\n", __func__, new_clip->has_llava_projector);
+ tinylogf("%s: model size: %.2f MB\n", __func__, buffer_size / 1024.0 / 1024.0);
+ tinylogf("%s: metadata size: %.2f MB\n", __func__, ggml_get_mem_size(meta) / 1024.0 / 1024.0);
}
}
- fprintf(stderr, "%s: params backend buffer size = % 6.2f MB (%i tensors)\n", __func__, buffer_size / (1024.0 * 1024.0), n_tensors);
+ tinylogf("%s: params backend buffer size = % 6.2f MB (%i tensors)\n", __func__, buffer_size / (1024.0 * 1024.0), n_tensors);
// load tensors
{
@@ -596,7 +596,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
new_clip->ctx_data = ggml_init(params);
if (!new_clip->ctx_data) {
- fprintf(stderr, "%s: ggml_init() failed\n", __func__);
+ tinylogf("%s: ggml_init() failed\n", __func__);
clip_free(new_clip);
return nullptr;
}
@@ -667,14 +667,14 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
}
if (verbosity >= 2) {
- fprintf(stderr, "\n%s: vision model hparams\n", __func__);
- fprintf(stderr, "image_size %d\n", hparams.image_size);
- fprintf(stderr, "patch_size %d\n", hparams.patch_size);
- fprintf(stderr, "v_hidden_size %d\n", hparams.hidden_size);
- fprintf(stderr, "v_n_intermediate %d\n", hparams.n_intermediate);
- fprintf(stderr, "v_projection_dim %d\n", hparams.projection_dim);
- fprintf(stderr, "v_n_head %d\n", hparams.n_head);
- fprintf(stderr, "v_n_layer %d\n", hparams.n_layer);
+ tinylogf("\n%s: vision model hparams\n", __func__);
+ tinylogf("image_size %d\n", hparams.image_size);
+ tinylogf("patch_size %d\n", hparams.patch_size);
+ tinylogf("v_hidden_size %d\n", hparams.hidden_size);
+ tinylogf("v_n_intermediate %d\n", hparams.n_intermediate);
+ tinylogf("v_projection_dim %d\n", hparams.projection_dim);
+ tinylogf("v_n_head %d\n", hparams.n_head);
+ tinylogf("v_n_layer %d\n", hparams.n_layer);
}
vision_model.patch_embeddings = get_tensor(new_clip->ctx_data, TN_PATCH_EMBD);
@@ -725,7 +725,7 @@ struct clip_ctx * clip_model_load(const char * fname, const int verbosity = 1) {
new_clip->compute_buffer = ggml_backend_alloc_buffer(new_clip->backend, compute_memory_buffer_size);
new_clip->compute_alloc = ggml_allocr_new_from_buffer(new_clip->compute_buffer);
- fprintf(stderr, "%s: compute allocated memory: %.2f MB\n", __func__, compute_memory_buffer_size /1024.0/1024.0);
+ tinylogf("%s: compute allocated memory: %.2f MB\n", __func__, compute_memory_buffer_size /1024.0/1024.0);
}
return new_clip;
@@ -1088,7 +1088,7 @@ bool clip_model_quantize(const char * fname_inp, const char * fname_out, const i
fout.put(0);
}
- fprintf(stderr, "%s: n_dims = %d | quantize=%d | size = %f MB -> %f MB\n", name.c_str(), ggml_n_dims(cur), quantize,
+ tinylogf("%s: n_dims = %d | quantize=%d | size = %f MB -> %f MB\n", name.c_str(), ggml_n_dims(cur), quantize,
orig_size / 1024.0 / 1024.0, new_size / 1024.0 / 1024.0);
}
@@ -1104,19 +1104,19 @@ bool clip_model_quantize(const char * fname_inp, const char * fname_out, const i
gguf_free(ctx_out);
{
- fprintf(stderr, "%s: original size = %8.2f MB\n", __func__, total_size_org / 1024.0 / 1024.0);
- fprintf(stderr, "%s: quantized size = %8.2f MB\n", __func__, total_size_new / 1024.0 / 1024.0);
+ tinylogf("%s: original size = %8.2f MB\n", __func__, total_size_org / 1024.0 / 1024.0);
+ tinylogf("%s: quantized size = %8.2f MB\n", __func__, total_size_new / 1024.0 / 1024.0);
int64_t sum_all = 0;
for (size_t i = 0; i < hist_all.size(); ++i) {
sum_all += hist_all[i];
}
- fprintf(stderr, "%s: hist: ", __func__);
+ tinylogf("%s: hist: ", __func__);
for (size_t i = 0; i < hist_all.size(); ++i) {
- fprintf(stderr, "%5.3f ", hist_all[i] / (float)sum_all);
+ tinylogf("%5.3f ", hist_all[i] / (float)sum_all);
}
- fprintf(stderr, "\n");
+ tinylogf("\n");
}
return true;
diff --git a/llama.cpp/llava/llava-cli.cpp b/llama.cpp/llava/llava-cli.cpp
index 347eb222..93c8bf60 100644
--- a/llama.cpp/llava/llava-cli.cpp
+++ b/llama.cpp/llava/llava-cli.cpp
@@ -126,7 +126,7 @@ static struct llava_image_embed * load_image(llava_context * ctx_llava, gpt_para
auto prompt = params->prompt;
if (prompt_contains_image(prompt)) {
if (!params->image.empty()) {
- fprintf(stderr, "using base64 encoded image instead of command line image path\n");
+ tinylogf("using base64 encoded image instead of command line image path\n");
}
embed = llava_image_embed_make_with_prompt_base64(ctx_llava->ctx_clip, params->n_threads, prompt);
if (!embed) {
@@ -158,7 +158,7 @@ static void process_prompt(struct llava_context * ctx_llava, struct llava_image_
// generate the response
- fprintf(stderr, "\n");
+ tinylogf("\n");
struct llama_sampling_context * ctx_sampling = llama_sampling_init(params->sparams);
diff --git a/llama.cpp/llava/llava.cpp b/llama.cpp/llava/llava.cpp
index 8f6c0e6b..2b71f1b6 100644
--- a/llama.cpp/llava/llava.cpp
+++ b/llama.cpp/llava/llava.cpp
@@ -34,7 +34,7 @@ static bool encode_image_with_clip(clip_ctx * ctx_clip, int n_threads, const cli
const int64_t t_img_enc_end_us = ggml_time_us();
float t_img_enc_ms = (t_img_enc_end_us - t_img_enc_start_us) / 1000.0;
- printf("\n%s: image encoded in %8.2f ms by CLIP (%8.2f ms per image patch)\n", __func__, t_img_enc_ms, t_img_enc_ms / *n_img_pos);
+ tinylogf("%s: image encoded in %8.2f ms by CLIP (%8.2f ms per image patch)\n", __func__, t_img_enc_ms, t_img_enc_ms / *n_img_pos);
return true;
}
diff --git a/llamafile/cuda.c b/llamafile/cuda.c
index f75ff4d7..efe94bed 100644
--- a/llamafile/cuda.c
+++ b/llamafile/cuda.c
@@ -118,6 +118,7 @@ static struct Cuda {
typeof(ggml_backend_cuda_buffer_type) *buffer_type;
typeof(ggml_backend_reg_cuda_init) *backend_reg_init;
typeof(ggml_backend_cuda_host_buffer_type) *backend_host_buffer_type;
+ typeof(ggml_backend_cuda_init) *backend_init;
} ggml_cuda;
static const char *Dlerror(void) {
@@ -622,6 +623,7 @@ static bool LinkCudaDso(const char *dso, const char *dir) {
ok &= !!(ggml_cuda.get_device_description = cosmo_dlsym(lib, "ggml_cuda_get_device_description"));
ok &= !!(ggml_cuda.backend_reg_init = cosmo_dlsym(lib, "ggml_backend_reg_cuda_init"));
ok &= !!(ggml_cuda.backend_host_buffer_type = cosmo_dlsym(lib, "ggml_backend_cuda_host_buffer_type"));
+ ok &= !!(ggml_cuda.backend_init = cosmo_dlsym(lib, "ggml_backend_cuda_init"));
if (!ok) {
tinylog(Dlerror(), ": not all symbols could be imported\n", NULL);
return false;
@@ -968,3 +970,8 @@ int ggml_backend_cuda_reg_devices(void) {
}
return device_count;
}
+
+ggml_backend_t ggml_backend_cuda_init(int device) {
+ if (!ggml_cuda_supported()) return 0;
+ return ggml_cuda.backend_init(device);
+}
diff --git a/llamafile/log.h b/llamafile/log.h
index bbd5ce90..e2f08312 100644
--- a/llamafile/log.h
+++ b/llamafile/log.h
@@ -1,5 +1,6 @@
#ifndef LLAMAFILE_LOG_H_
#define LLAMAFILE_LOG_H_
+#include <stdio.h>
#include <stdarg.h>
#include <stdbool.h>
#ifdef __cplusplus
@@ -9,7 +10,9 @@ extern "C" {
extern bool FLAG_log_disable;
void tinylog(const char *, ...);
+
#define tinylog(...) (void)(!FLAG_log_disable && (tinylog(__VA_ARGS__), 0))
+#define tinylogf(...) (void)(!FLAG_log_disable && (fprintf(stderr, __VA_ARGS__), 0))
#ifdef __cplusplus
}