Skip to content

Distribute and run LLMs with a single file.

Make --log-disable work a bit better

Justine Tunney50bdf6971162
diff --git a/llama.cpp/common.h b/llama.cpp/common.h
index cc0ff163..29bb2a13 100644
--- a/llama.cpp/common.h
+++ b/llama.cpp/common.h
@@ -31,7 +31,7 @@
#define die_fmt(fmt, ...) do { fprintf(stderr, "error: " fmt "\n", __VA_ARGS__); exit(1); } while (0)
#define print_build_info() do { \
- fprintf(stderr, "%s: llamafile version " LLAMAFILE_VERSION_STRING "\n", __func__, LLAMA_BUILD_NUMBER, LLAMA_COMMIT); \
+ LOG("%s: llamafile version " LLAMAFILE_VERSION_STRING "\n", __func__, LLAMA_BUILD_NUMBER, LLAMA_COMMIT); \
} while(0)
// build info
diff --git a/llama.cpp/log.h b/llama.cpp/log.h
index fc37b3ff..7916083e 100644
--- a/llama.cpp/log.h
+++ b/llama.cpp/log.h
@@ -12,6 +12,8 @@
#include <algorithm>
#include <cinttypes>
+#include "llamafile/log.h"
+
// --------------------------------
//
// Basic usage:
@@ -430,6 +432,7 @@ inline FILE *log_handler2_impl(bool change = false, LogTriState append = LogTriS
// INTERNAL, DO NOT USE
inline FILE *log_disable_impl()
{
+ FLAG_log_disable = true;
return log_handler1_impl(true, LogTriStateSame, LogTriStateTrue);
}
@@ -439,6 +442,7 @@ inline FILE *log_disable_impl()
// INTERNAL, DO NOT USE
inline FILE *log_enable_impl()
{
+ FLAG_log_disable = false;
return log_handler1_impl(true, LogTriStateSame, LogTriStateFalse);
}
diff --git a/llama.cpp/server/server.cpp b/llama.cpp/server/server.cpp
index af438dc2..3e7d6040 100644
--- a/llama.cpp/server/server.cpp
+++ b/llama.cpp/server/server.cpp
@@ -12,6 +12,7 @@
#include "llama.cpp/ggml-cuda.h"
#include "llamafile/llamafile.h"
#include "llamafile/version.h"
+#include "llamafile/log.h"
#define CPPHTTPLIB_NO_EXCEPTIONS 1
#define CPPHTTPLIB_FORM_URL_ENCODED_PAYLOAD_MAX_LENGTH 1048576
@@ -2313,6 +2314,7 @@ static void server_params_parse(int argc, char **argv, server_params &sparams,
}
else if (arg == "--log-disable")
{
+ FLAG_log_disable = true;
log_set_target(stdout);
LOG_INFO("logging to file is disabled.", {});
}
@@ -3086,7 +3088,7 @@ int server_cli(int argc, char ** argv) {
}
}
- tinyprint(2, "loading weights...\n", NULL);
+ tinylog("loading weights...\n", NULL);
LOG_INFO("HTTP server listening", {
{"hostname", sparams.hostname},
diff --git a/llamafile/cuda.c b/llamafile/cuda.c
index c265df8c..dffde900 100644
--- a/llamafile/cuda.c
+++ b/llamafile/cuda.c
@@ -31,6 +31,7 @@
#include <sys/wait.h>
#include <sys/stat.h>
#include <stdatomic.h>
+#include "llamafile/log.h"
#include "llama.cpp/ggml-cuda.h"
__static_yoink("llama.cpp/ggml.h");
@@ -160,20 +161,20 @@ static bool CreateTempPath(const char *path, char tmp[static PATH_MAX]) {
static void LogCommand(char *args[]) {
for (int i = 0; args[i]; ++i) {
if (i) {
- tinyprint(2, " ", NULL);
+ tinylog(" ", NULL);
}
// this quoting should be close enough to correct to be
// copy/pastable on both unix and windows command terms
bool need_quotes = !!strchr(args[i], ' ');
if (need_quotes) {
- tinyprint(2, "\"", NULL);
+ tinylog("\"", NULL);
}
- tinyprint(2, args[i], NULL);
+ tinylog(args[i], NULL);
if (need_quotes) {
- tinyprint(2, "\"", NULL);
+ tinylog("\"", NULL);
}
}
- tinyprint(2, "\n", NULL);
+ tinylog("\n", NULL);
}
static bool Compile(const char *src,
@@ -196,7 +197,7 @@ static bool Compile(const char *src,
}
}
if (ws) {
- tinyprint(2, args[0], ": returned nonzero exit status\n", NULL);
+ tinylog(args[0], ": returned nonzero exit status\n", NULL);
unlink(tmp);
return false;
}
@@ -318,13 +319,13 @@ static bool GetAmdOffloadArchFlag(char out[static 64]) {
}
}
if (ws) {
- tinyprint(2, "error: hipInfo returned non-zero exit status\n", NULL);
+ tinylog("error: hipInfo returned non-zero exit status\n", NULL);
return false;
}
// Serialize value for --offload-arch=LIST flag.
if (!archs) {
- tinyprint(2, "warning: hipInfo output didn't list any graphics cards\n", NULL);
+ tinylog("warning: hipInfo output didn't list any graphics cards\n", NULL);
return false;
}
bool gotsome = false;
@@ -396,7 +397,7 @@ static dontinline bool GetNvccArchFlag(const char *nvcc, char flag[static 32]) {
// nvidia-smi in cuda 11.5+ can do this but (1) it takes longer to
// run than compiling / running this script and (2) the nvidia-smi
// command isn't available on Jetson devices.
- tinyprint(2, "building nvidia compute capability detector...\n", NULL);
+ tinylog("building nvidia compute capability detector...\n", NULL);
if (!Compile(src, tmp, exe, (char *[]){(char *)nvcc, "-o", tmp, src, 0})) {
return false;
}
@@ -434,14 +435,14 @@ static dontinline bool GetNvccArchFlag(const char *nvcc, char flag[static 32]) {
}
}
if (ws) {
- tinyprint(2, "error: compute capability detector returned nonzero exit status\n", NULL);
+ tinylog("error: compute capability detector returned nonzero exit status\n", NULL);
return false;
}
// parse output of detector
char *endptr;
if (!*ibuf || !strtol(ibuf, &endptr, 10) || *endptr) {
- tinyprint(2, "error: bad compute capability detector output\n", NULL);
+ tinylog("error: bad compute capability detector output\n", NULL);
return false;
}
@@ -528,7 +529,7 @@ static bool CompileNvidia(const char *nvcc, const char *dso, const char *src) {
}
// try building dso with host nvidia microarchitecture
- tinyprint(2, "building ggml-cuda with nvcc -arch=native...\n", NULL);
+ tinylog("building ggml-cuda with nvcc -arch=native...\n", NULL);
if (Compile(src, tmpdso, dso, (char *[]){
(char *)nvcc, "-arch=native", NVCC_FLAGS, "-o", tmpdso,
(char *)src, NVCC_LIBS, NULL})) {
@@ -540,7 +541,7 @@ static bool CompileNvidia(const char *nvcc, const char *dso, const char *src) {
if (!GetNvccArchFlag(nvcc, archflag)) {
return false;
}
- tinyprint(2, "building ggml-cuda with nvcc ", archflag, "...\n", NULL);
+ tinylog("building ggml-cuda with nvcc ", archflag, "...\n", NULL);
if (Compile(src, tmpdso, dso, (char *[]){
(char *)nvcc, archflag, NVCC_FLAGS, "-o", tmpdso,
(char *)src, NVCC_LIBS, NULL})) {
@@ -560,7 +561,7 @@ static bool ExtractCudaDso(const char *dso, const char *name) {
strlcat(zip, ".", sizeof(zip));
strlcat(zip, GetDsoExtension(), sizeof(zip));
if (!FileExists(zip)) {
- tinyprint(2, "prebuilt binary ", zip, " not found\n", NULL);
+ tinylog("prebuilt binary ", zip, " not found\n", NULL);
return false;
}
@@ -579,16 +580,16 @@ static bool LinkCudaDso(const char *dso, const char *dir) {
// runtime link dynamic shared object
void *lib;
- tinyprint(2, "dynamically linking ", dso, "\n", NULL);
+ tinylog("dynamically linking ", dso, "\n", NULL);
lib = cosmo_dlopen(dso, RTLD_LAZY);
if (dir) {
chdir(cwd);
}
if (!lib) {
char cc[PATH_MAX];
- tinyprint(2, Dlerror(), ": failed to load library\n", NULL);
+ tinylog(Dlerror(), ": failed to load library\n", NULL);
if ((IsLinux() || IsBsd()) && !commandv("cc", cc, PATH_MAX)) {
- tinyprint(2, "you need to install cc for gpu support\n", NULL);
+ tinylog("you need to install cc for gpu support\n", NULL);
}
return false;
}
@@ -618,7 +619,7 @@ static bool LinkCudaDso(const char *dso, const char *dir) {
ok &= !!(ggml_cuda.reg_cuda_init = cosmo_dlsym(lib, "ggml_backend_reg_cuda_init"));
ok &= !!(ggml_cuda.buffer_type = cosmo_dlsym(lib, "ggml_backend_cuda_buffer_type"));
if (!ok) {
- tinyprint(2, Dlerror(), ": not all symbols could be imported\n", NULL);
+ tinylog(Dlerror(), ": not all symbols could be imported\n", NULL);
return false;
}
@@ -643,7 +644,7 @@ static bool ImportCudaImpl(void) {
default:
return false;
}
- tinyprint(2, "initializing gpu module...\n", NULL);
+ tinylog("initializing gpu module...\n", NULL);
// extract source code
char src[PATH_MAX];
@@ -738,7 +739,7 @@ static bool ImportCudaImpl(void) {
}
}
} else {
- tinyprint(2, "note: won't compile AMD GPU support because $HIP_PATH/bin/clang++ is missing\n", NULL);
+ tinylog("note: won't compile AMD GPU support because $HIP_PATH/bin/clang++ is missing\n", NULL);
}
// Try extracting prebuilt tinyBLAS DSO from PKZIP.
@@ -818,7 +819,7 @@ TryNvidia:
static void ImportCuda(void) {
if (!ggml_cuda.disabled && ImportCudaImpl()) {
- tinyprint(2, "GPU support successfully linked and loaded\n", NULL);
+ tinylog("GPU support successfully linked and loaded\n", NULL);
ggml_cuda.supported = true;
}
}
diff --git a/llamafile/extract.c b/llamafile/extract.c
index 28a98b91..198aa901 100644
--- a/llamafile/extract.c
+++ b/llamafile/extract.c
@@ -24,6 +24,7 @@
#include <unistd.h>
#include <sys/stat.h>
#include "llamafile.h"
+#include "llamafile/log.h"
/**
* Returns true if `zip` was successfully copied to `to`.
@@ -35,7 +36,7 @@
bool llamafile_extract(const char *zip, const char *to) {
int fdin, fdout;
char stage[PATH_MAX];
- tinyprint(2, "extracting ", zip, " to ", to, "\n", NULL);
+ tinylog("extracting ", zip, " to ", to, "\n", NULL);
strlcpy(stage, to, sizeof(stage));
if (strlcat(stage, ".XXXXXX", sizeof(stage)) >= sizeof(stage)) {
errno = ENAMETOOLONG;
diff --git a/llamafile/gpu.c b/llamafile/gpu.c
index 6deb3c70..178aec2b 100644
--- a/llamafile/gpu.c
+++ b/llamafile/gpu.c
@@ -1,7 +1,7 @@
// -*- 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
//
-// Copyright 2023 Mozilla Foundation
+// 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.
diff --git a/llamafile/launch_browser.c b/llamafile/launch_browser.c
index 9221c349..23be56f0 100644
--- a/llamafile/launch_browser.c
+++ b/llamafile/launch_browser.c
@@ -22,12 +22,13 @@
#include <stdio.h>
#include <string.h>
#include <sys/wait.h>
+#include "llamafile/log.h"
static void report_failure(const char *url,
const char *cmd,
const char *reason) {
- tinyprint(2, "failed to open ", url, " in a browser tab using ", cmd,
- ": ", reason, "\n", NULL);
+ tinylog("failed to open ", url, " in a browser tab using ", cmd,
+ ": ", reason, "\n", NULL);
}
/**
diff --git a/llamafile/log.c b/llamafile/log.c
new file mode 100644
index 00000000..aea85ebd
--- /dev/null
+++ b/llamafile/log.c
@@ -0,0 +1,43 @@
+// -*- 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
+//
+// 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 "log.h"
+#include <unistd.h>
+#include <pthread.h>
+
+bool FLAG_log_disable;
+
+void (tinylog)(const char *s, ...) {
+ size_t n;
+ int c, cs;
+ va_list va;
+ char buf[512];
+ pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, &cs);
+ va_start(va, s);
+ for (n = 0; s; s = va_arg(va, const char *)) {
+ while ((c = *s++)) {
+ buf[n++] = c;
+ if (n == sizeof(buf)) {
+ write(2, buf, n);
+ n = 0;
+ }
+ }
+ }
+ va_end(va);
+ write(2, buf, n);
+ pthread_setcancelstate(cs, 0);
+}
diff --git a/llamafile/log.h b/llamafile/log.h
new file mode 100644
index 00000000..bbd5ce90
--- /dev/null
+++ b/llamafile/log.h
@@ -0,0 +1,17 @@
+#ifndef LLAMAFILE_LOG_H_
+#define LLAMAFILE_LOG_H_
+#include <stdarg.h>
+#include <stdbool.h>
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+extern bool FLAG_log_disable;
+
+void tinylog(const char *, ...);
+#define tinylog(...) (void)(!FLAG_log_disable && (tinylog(__VA_ARGS__), 0))
+
+#ifdef __cplusplus
+}
+#endif
+#endif /* LLAMAFILE_LOG_H_ */
diff --git a/llamafile/metal.c b/llamafile/metal.c
index b81d84f0..1ffb82aa 100644
--- a/llamafile/metal.c
+++ b/llamafile/metal.c
@@ -130,7 +130,7 @@ static bool BuildMetal(const char *dso) {
// compile dynamic shared object
if (needs_rebuild) {
- tinyprint(2, "building ggml-metal.dylib with xcode...\n", NULL);
+ tinylog("building ggml-metal.dylib with xcode...\n", NULL);
int fd;
char tmpdso[PATH_MAX];
strlcpy(tmpdso, dso, sizeof(tmpdso));
@@ -162,7 +162,7 @@ static bool BuildMetal(const char *dso) {
if (err) {
perror("cc");
if (err == ENOENT) {
- tinyprint(2, "PLEASE RUN: xcode-select --install\n", NULL);
+ tinylog("PLEASE RUN: xcode-select --install\n", NULL);
}
return false;
}
@@ -173,7 +173,7 @@ static bool BuildMetal(const char *dso) {
}
}
if (ws) {
- tinyprint(2, "compiler returned nonzero exit status\n", NULL);
+ tinylog("compiler returned nonzero exit status\n", NULL);
return false;
}
if (rename(tmpdso, dso)) {
@@ -191,7 +191,7 @@ static bool LinkMetal(const char *dso) {
void *lib;
lib = cosmo_dlopen(dso, RTLD_LAZY);
if (!lib) {
- tinyprint(2, Dlerror(), ": failed to load library\n", NULL);
+ tinylog(Dlerror(), ": failed to load library\n", NULL);
return false;
}
@@ -209,7 +209,7 @@ static bool LinkMetal(const char *dso) {
ok &= !!(ggml_metal.set_n_cb = cosmo_dlsym(lib, "ggml_metal_set_n_cb"));
ok &= !!(ggml_metal.backend_init = cosmo_dlsym(lib, "ggml_backend_metal_init"));
if (!ok) {
- tinyprint(2, Dlerror(), ": not all symbols could be imported\n", NULL);
+ tinylog(Dlerror(), ": not all symbols could be imported\n", NULL);
return false;
}
@@ -253,7 +253,7 @@ static void ImportMetal(void) {
if (ImportMetalImpl()) {
ggml_metal.supported = true;
ggml_metal.backend_init();
- tinyprint(2, "Apple Metal GPU support successfully loaded\n", NULL);
+ tinylog("Apple Metal GPU support successfully loaded\n", NULL);
}
}