Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Upgrade to Cosmopolitan v3.5.8
Justine Tunney13cf96b0d1a1
diff --git a/build/config.mk b/build/config.mkindex e786c27c..d48def45 100644--- a/build/config.mk+++ b/build/config.mk@@ -2,7 +2,7 @@#── vi: set noet ft=make ts=8 sw=8 fenc=utf-8 :vi ────────────────────┘PREFIX = /usr/local-COSMOCC = .cosmocc/3.5.7+COSMOCC = .cosmocc/3.5.8TOOLCHAIN = $(COSMOCC)/bin/cosmoAR = $(TOOLCHAIN)ar@@ -52,5 +52,5 @@ clean:; rm -rf o.PHONY: distcleandistclean:; rm -rf o .cosmocc-.cosmocc/3.5.7:- build/download-cosmocc.sh $@ 3.5.7 596876951b62ad2530c63afc40edd805d751fcb2416e544d249af04ad00bb4ed+.cosmocc/3.5.8:+ build/download-cosmocc.sh $@ 3.5.8 80bea0e523b666d4d4e74fb729ac1e4bd924d1b9f2892d939af62ae2c4a0f622diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.cindex 05ae2667..ca9fb3b6 100644--- a/llama.cpp/ggml.c+++ b/llama.cpp/ggml.c@@ -18886,11 +18886,6 @@ static thread_ret_t ggml_graph_compute_thread(void * data) {return 0;}-static thread_ret_t ggml_graph_compute_thread_callback(void * data) {- protect_against_stack_overflow(); // [jart]- return ggml_graph_compute_thread(data);-}-struct ggml_cplan ggml_graph_plan(const struct ggml_cgraph * cgraph, int n_threads) {if (n_threads <= 0) {n_threads = GGML_DEFAULT_N_THREADS;@@ -19202,8 +19197,13 @@ enum ggml_status ggml_graph_compute(struct ggml_cgraph * cgraph, struct ggml_cpl.is_main_thread = false, // [jart]};- const int rc = ggml_thread_create((pthread_t *)&workers[j].thrd, NULL,- ggml_graph_compute_thread_callback, &workers[j]);+ pthread_attr_t attr;+ pthread_attr_init(&attr);+ pthread_attr_setguardsize(&attr, sysconf(_SC_PAGESIZE));+ pthread_attr_setsigaltstacksize_np(&attr, sysconf(_SC_MINSIGSTKSZ) + 16384);+ const int rc = ggml_thread_create((pthread_t *)&workers[j].thrd, &attr,+ ggml_graph_compute_thread, &workers[j]);+ pthread_attr_destroy(&attr);GGML_ASSERT(rc == 0);UNUSED(rc);}diff --git a/llamafile/crash.h b/llamafile/crash.hindex 37d8aa19..99922760 100644--- a/llamafile/crash.h+++ b/llamafile/crash.h@@ -9,7 +9,6 @@ struct StackFrame;char *describe_crash(char *, size_t, int, siginfo_t *, void *);char *describe_backtrace(char *, size_t, const struct StackFrame *);-void protect_against_stack_overflow(void);#ifdef __cplusplus}diff --git a/llamafile/protect_against_stack_overflow.cpp b/llamafile/protect_against_stack_overflow.cppdeleted file mode 100644index 16399b7c..00000000--- a/llamafile/protect_against_stack_overflow.cpp+++ /dev/null@@ -1,50 +0,0 @@-// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*--// vi: set et ft=cpp 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 "crash.h"--#include <pthread.h>-#include <signal.h>-#include <stdlib.h>-#include <unistd.h>--#include "threadlocal.h"--// Call this from your thread start function-// Your thread needs to have a guard page (default)-// You also need SIGSEGV and SIGBUS handlers w/ SA_ONSTACK-void protect_against_stack_overflow(void) {- static ThreadLocal<struct sigaltstack> cleanup([](struct sigaltstack *ss) {- ss->ss_flags = SS_DISABLE;- if (!sigaltstack(ss, 0)) {- free(ss->ss_sp);- free(ss);- }- });- struct sigaltstack *ss;- if ((ss = (struct sigaltstack *)malloc(sizeof(struct sigaltstack)))) {- ss->ss_flags = 0;- ss->ss_size = sysconf(_SC_MINSIGSTKSZ) + 16384;- if ((ss->ss_sp = malloc(ss->ss_size))) {- if (sigaltstack(ss, 0))- __builtin_trap();- cleanup.set(ss);- } else {- free(ss);- }- }-}diff --git a/llamafile/server/server.cpp b/llamafile/server/server.cppindex 8dc48a8c..bf9fa248 100644--- a/llamafile/server/server.cpp+++ b/llamafile/server/server.cpp@@ -23,6 +23,7 @@#include <stdio.h>#include <sys/socket.h>#include <time.h>+#include <unistd.h>#include "llamafile/crash.h"#include "llamafile/llamafile.h"@@ -90,7 +91,6 @@ Server::close()void*worker_thread(void* arg){- protect_against_stack_overflow();Worker* worker = (Worker*)arg;worker->run();return 0;@@ -104,7 +104,9 @@ Server::spawn()pthread_attr_t attr;worker = new Worker(this);pthread_attr_init(&attr);+ pthread_attr_setguardsize(&attr, sysconf(_SC_PAGESIZE));pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);+ pthread_attr_setsigaltstacksize_np(&attr, sysconf(_SC_MINSIGSTKSZ) + 16384);if ((err = pthread_create(&worker->th, &attr, worker_thread, worker)))delete worker;pthread_attr_destroy(&attr);diff --git a/llamafile/server/time.cpp b/llamafile/server/time.cppindex 3f216bea..7dc98cf8 100644--- a/llamafile/server/time.cpp+++ b/llamafile/server/time.cpp@@ -134,7 +134,6 @@ time_worker(void* arg)sigaddset(&ss, SIGUSR1);sigaddset(&ss, SIGALRM);pthread_sigmask(SIG_SETMASK, &ss, 0);- protect_against_stack_overflow();set_thread_name("localtime");for (;;) {sleep(10);