Skip to content

Commit

Permalink
Make --log-disable work a bit better
Browse files Browse the repository at this point in the history
  • Loading branch information
jart committed Jan 4, 2024
1 parent 474b44f commit 50bdf69
Show file tree
Hide file tree
Showing 10 changed files with 102 additions and 33 deletions.
2 changes: 1 addition & 1 deletion llama.cpp/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions llama.cpp/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <algorithm>
#include <cinttypes>

#include "llamafile/log.h"

// --------------------------------
//
// Basic usage:
Expand Down Expand Up @@ -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);
}

Expand All @@ -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);
}

Expand Down
4 changes: 3 additions & 1 deletion llama.cpp/server/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.", {});
}
Expand Down Expand Up @@ -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},
Expand Down
43 changes: 22 additions & 21 deletions llamafile/cuda.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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,
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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})) {
Expand All @@ -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})) {
Expand All @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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];
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -818,7 +819,7 @@ static bool ImportCudaImpl(void) {

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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion llamafile/extract.c
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion llamafile/gpu.c
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
5 changes: 3 additions & 2 deletions llamafile/launch_browser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
43 changes: 43 additions & 0 deletions llamafile/log.c
Original file line number Diff line number Diff line change
@@ -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);
}
17 changes: 17 additions & 0 deletions llamafile/log.h
Original file line number Diff line number Diff line change
@@ -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_ */
12 changes: 6 additions & 6 deletions llamafile/metal.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 50bdf69

Please sign in to comment.