Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Make browser tab launching more reliable
Justine Tunney585c2d89a4f9
diff --git a/llamafile/cuda.c b/llamafile/cuda.cindex 5264f0fb..e5aa9031 100644--- a/llamafile/cuda.c+++ b/llamafile/cuda.c@@ -166,31 +166,12 @@ 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) {- 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) {- tinylog("\"", NULL);- }- tinylog(args[i], NULL);- if (need_quotes) {- tinylog("\"", NULL);- }- }- tinylog("\n", NULL);-}-static bool Compile(const char *src,const char *tmp,const char *out,char *args[]) {int pid, ws;- LogCommand(args);+ llamafile_log_command(args);errno_t err = posix_spawnp(&pid, args[0], NULL, NULL, args, environ);if (err) {perror(args[0]);@@ -253,6 +234,7 @@ static bool GetAmdOffloadArchFlag(char out[static 64]) {// Run HIP info program.int pid;char *args[] = {hip_info_path, 0};+ llamafile_log_command(args);posix_spawn_file_actions_t fa;posix_spawn_file_actions_init(&fa);posix_spawn_file_actions_adddup2(&fa, pipefds[1], 1);@@ -425,6 +407,7 @@ static dontinline bool GetNvccArchFlag(const char *nvcc, char flag[static 32]) {// run nvidia compute capability detectorint pid;char *args[] = {exe, 0};+ llamafile_log_command(args);posix_spawn_file_actions_t fa;posix_spawn_file_actions_init(&fa);posix_spawn_file_actions_adddup2(&fa, pipefds[1], 1);diff --git a/llamafile/launch_browser.c b/llamafile/launch_browser.cindex 23be56f0..e8b5842e 100644--- a/llamafile/launch_browser.c+++ b/llamafile/launch_browser.c@@ -20,10 +20,19 @@#include <spawn.h>#include <errno.h>#include <stdio.h>+#include <stdlib.h>#include <string.h>+#include <unistd.h>+#include <signal.h>#include <sys/wait.h>#include "llamafile/log.h"+static volatile bool g_timed_out;++static void handle_timeout(int sig) {+ g_timed_out = true;+}+static void report_failure(const char *url,const char *cmd,const char *reason) {@@ -34,7 +43,19 @@ static void report_failure(const char *url,/*** Opens browser tab on host system.*/-bool llamafile_launch_browser(const char *url) {+void llamafile_launch_browser(const char *url) {++ // perform this task from a subprocess so it doesn't block server+ tinylog("opening browser tab... (pass --nobrowser to disable)\n", NULL);+ switch (fork()) {+ case 0:+ break;+ default:+ return;+ case -1:+ perror("fork");+ return;+ }// determine which command opens browser tabconst char *cmd;@@ -50,30 +71,44 @@ bool llamafile_launch_browser(const char *url) {// set process group so ctrl-c won't kill browserint pid, err;posix_spawnattr_t sa;- char *args[] = {(char *)cmd, (char *)url, NULL};+ char *args[] = {(char *)cmd, (char *)url, 0};posix_spawnattr_init(&sa);posix_spawnattr_setflags(&sa, POSIX_SPAWN_SETPGROUP);- err = posix_spawnp(&pid, cmd, NULL, &sa, args, environ);+ err = posix_spawnp(&pid, cmd, 0, &sa, args, environ);posix_spawnattr_destroy(&sa);if (err) {report_failure(url, cmd, strerror(err));- return false;+ _exit(0);}+ // kill command if it takes more than three seconds+ // we need it because xdg-open acts weird on headless systems+ struct sigaction hand;+ hand.sa_flags = 0;+ sigemptyset(&hand.sa_mask);+ hand.sa_handler = handle_timeout;+ sigaction(SIGALRM, &hand, 0);+ alarm(3);+// wait for tab to finish opening// the browser will still be running after this completesint ws;while (waitpid(pid, &ws, 0) == -1) {if (errno != EINTR) {report_failure(url, cmd, strerror(errno));- return false;+ kill(pid, SIGKILL);+ _exit(0);+ }+ if (g_timed_out) {+ report_failure(url, cmd, "process timed out");+ kill(pid, SIGKILL);+ _exit(0);}}if (ws) {report_failure(url, cmd, "process exited with non-zero status");- return false;}- // report success- return true;+ // we're done+ _exit(0);}diff --git a/llamafile/llamafile.h b/llamafile/llamafile.hindex 0e5ba057..b52c106e 100644--- a/llamafile/llamafile.h+++ b/llamafile/llamafile.h@@ -20,13 +20,14 @@ FILE *llamafile_fp(struct llamafile *);void llamafile_init(void);void llamafile_check_cpu(void);void llamafile_help(const char *);+void llamafile_log_command(char *[]);const char *llamafile_get_tmp_dir(void);bool llamafile_has(char **, const char *);bool llamafile_extract(const char *, const char *);int llamafile_is_file_newer_than(const char *, const char *);void llamafile_schlep(const void *, size_t);void llamafile_get_app_dir(char *, size_t);-bool llamafile_launch_browser(const char *);+void llamafile_launch_browser(const char *);#define LLAMAFILE_GPU_ERROR -2#define LLAMAFILE_GPU_DISABLE -1diff --git a/llamafile/log.c b/llamafile/log.cindex 27f87444..03ca3304 100644--- a/llamafile/log.c+++ b/llamafile/log.c@@ -17,6 +17,7 @@#include "log.h"#include <unistd.h>+#include <string.h>#include <pthread.h>bool FLAG_log_disable;@@ -41,3 +42,22 @@ void (tinylog)(const char *s, ...) {write(2, buf, n);pthread_setcancelstate(cs, 0);}++void llamafile_log_command(char *args[]) {+ for (int i = 0; args[i]; ++i) {+ if (i) {+ 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) {+ tinylog("\"", NULL);+ }+ tinylog(args[i], NULL);+ if (need_quotes) {+ tinylog("\"", NULL);+ }+ }+ tinylog("\n", NULL);+}diff --git a/llamafile/metal.c b/llamafile/metal.cindex d6ab957c..f4c2cc5b 100644--- a/llamafile/metal.c+++ b/llamafile/metal.c@@ -167,6 +167,7 @@ static bool BuildMetal(const char *dso) {NULL,};int pid, ws;+ llamafile_log_command(args);errno_t err = posix_spawnp(&pid, "cc", NULL, NULL, args, environ);if (err) {perror("cc");