Skip to content

Distribute and run LLMs with a single file.

Upgrade to Cosmopolitan v3.5.6

Justine Tunney3ec19d013b67
diff --git a/build/config.mk b/build/config.mk
index 03ba1cf8..f7548fdf 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.4
+COSMOCC = .cosmocc/3.5.6
TOOLCHAIN = $(COSMOCC)/bin/cosmo
AR = $(TOOLCHAIN)ar
@@ -52,5 +52,5 @@ clean:; rm -rf o
.PHONY: distclean
distclean:; rm -rf o .cosmocc
-.cosmocc/3.5.4:
- build/download-cosmocc.sh $@ 3.5.4 1e822906021a5eb56172b4e89840c0b0cd7db97b718355a41414b4ca913171e0
+.cosmocc/3.5.6:
+ build/download-cosmocc.sh $@ 3.5.6 efdc021d1825a27830a45e88d408668c08f22dcb7f6a1ca289fdaf77a937aa66
diff --git a/llama.cpp/ggml-cuda.cu b/llama.cpp/ggml-cuda.cu
index 1015daf1..67222b74 100644
--- a/llama.cpp/ggml-cuda.cu
+++ b/llama.cpp/ggml-cuda.cu
@@ -235,7 +235,7 @@
#include "ggml-backend-impl.h"
static const struct ggml_backend_api *g_backend;
-#define getenv g_backend->getenv
+// #define getenv g_backend->getenv // [jart] due to strace !!memory
#define FLAG_log_disable (*g_backend->FLAG_log_disable)
#define ggml_backend_register g_backend->ggml_backend_register
#define ggml_is_quantized g_backend->ggml_is_quantized
diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.c
index b91cc467..05ae2667 100644
--- a/llama.cpp/ggml.c
+++ b/llama.cpp/ggml.c
@@ -61,7 +61,6 @@ SOFTWARE.");
#include <unistd.h>
#include <cosmo.h>
#include <threads.h>
-#include <sys/auxv.h>
#include <libc/log/backtrace.internal.h>
#include <pthread.h>
@@ -166,7 +165,7 @@ void ggml_print_backtrace(void) {
#else
inline static void * ggml_aligned_malloc(size_t size) {
if (size == 0) {
- GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_aligned_malloc!\n");
+ // GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_aligned_malloc!\n");
return NULL;
}
void * aligned_memory = NULL;
@@ -203,7 +202,7 @@ inline static void * ggml_aligned_malloc(size_t size) {
inline static void * ggml_malloc(size_t size) {
if (size == 0) {
- GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_malloc!\n");
+ // GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_malloc!\n");
return NULL;
}
void * result = malloc(size);
@@ -217,7 +216,7 @@ inline static void * ggml_malloc(size_t size) {
// calloc
inline static void * ggml_calloc(size_t num, size_t size) {
if (num == 0 || size == 0) {
- GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_calloc!\n");
+ // GGML_PRINT("WARNING: Behavior may be unexpected when allocating 0 bytes for ggml_calloc!\n");
return NULL;
}
void * result = calloc(num, size);
diff --git a/llama.cpp/llama-bench/main.cpp b/llama.cpp/llama-bench/main.cpp
index a04961fd..5ef63eb2 100644
--- a/llama.cpp/llama-bench/main.cpp
+++ b/llama.cpp/llama-bench/main.cpp
@@ -16,7 +16,6 @@
#include <unistd.h>
#include <vector>
#include <tool/args/args.h>
-#include <sys/auxv.h>
#include <stdatomic.h>
#include <third_party/nsync/futex.internal.h>
diff --git a/llama.cpp/main/main.cpp b/llama.cpp/main/main.cpp
index 8dc7f0d3..6f8a3657 100644
--- a/llama.cpp/main/main.cpp
+++ b/llama.cpp/main/main.cpp
@@ -16,7 +16,6 @@
#include <unistd.h>
#include <vector>
#include <tool/args/args.h>
-#include <sys/auxv.h>
#include <stdatomic.h>
#include <third_party/nsync/futex.internal.h>
diff --git a/llama.cpp/random.cpp b/llama.cpp/random.cpp
deleted file mode 100644
index c5ba5f38..00000000
--- a/llama.cpp/random.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
-#include <random>
-#include <unistd.h>
-
-// TODO(jart): delete this with next cosmocc update
-
-namespace std {
-
-random_device::random_device(const string& __token)
-{
- if (__token != "/dev/urandom")
- __throw_system_error(ENOENT, ("random device not supported " + __token).c_str());
-}
-
-random_device::~random_device()
-{
-}
-
-unsigned
-random_device::operator()()
-{
- unsigned r;
- size_t n = sizeof(r);
- int err = getentropy(&r, n);
- if (err)
- __throw_system_error(errno, "random_device getentropy failed");
- return r;
-}
-
-double
-random_device::entropy() const noexcept
-{
- return 0;
-}
-
-} // namespace std
diff --git a/llamafile/llamafile.c b/llamafile/llamafile.c
index 3b44c19a..ed94f5fc 100644
--- a/llamafile/llamafile.c
+++ b/llamafile/llamafile.c
@@ -212,7 +212,7 @@ static struct llamafile *llamafile_open_zip(const char *prog, const char *fname,
file->fname);
// map the file into memory
- long pagesz = sysconf(_SC_PAGESIZE);
+ long pagesz = sysconf(_SC_GRANSIZE);
off_t mapoff = off & -pagesz;
long skew = off - mapoff;
file->mapsize = skew + file->size;
diff --git a/llamafile/protect_against_stack_overflow.cpp b/llamafile/protect_against_stack_overflow.cpp
index e414a801..16399b7c 100644
--- a/llamafile/protect_against_stack_overflow.cpp
+++ b/llamafile/protect_against_stack_overflow.cpp
@@ -28,20 +28,23 @@
// 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<void> cleanup([](void *ptr) {
- struct sigaltstack ss;
- ss.ss_flags = SS_DISABLE;
- ss.ss_size = 0;
- ss.ss_sp = 0;
- sigaltstack(&ss, 0);
- free(ptr);
+ 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;
- 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.ss_sp);
+ 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/schlep.c b/llamafile/schlep.c
index 79358379..d647ba64 100644
--- a/llamafile/schlep.c
+++ b/llamafile/schlep.c
@@ -22,7 +22,6 @@
#include <pthread.h>
#include <stdatomic.h>
#include <stdio.h>
-#include <sys/auxv.h>
#include <unistd.h>
#define FPS 24
@@ -87,13 +86,9 @@ void llamafile_schlep(const void *data, size_t size) {
// launch threads
errno_t err;
atomic_long faults = 0;
+ long pagesz = getpagesize();
long stride = size / THREADS;
- long pagesz = getauxval(AT_PAGESZ);
long pages = (stride + pagesz - 1) / pagesz * THREADS;
- pthread_attr_t attr;
- pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, 65536);
- pthread_attr_setguardsize(&attr, pagesz);
struct PageFaulter pf[THREADS];
for (int i = 0; i < THREADS; ++i) {
pf[i].data = data;
@@ -101,14 +96,13 @@ void llamafile_schlep(const void *data, size_t size) {
pf[i].faults = &faults;
pf[i].left = stride * i;
pf[i].right = stride * (i + 1);
- err = pthread_create(&pf[i].th, &attr, PageFaulter, pf + i);
+ err = pthread_create(&pf[i].th, 0, PageFaulter, pf + i);
if (err) {
errno = err;
perror("pthread_create");
exit(1);
}
}
- pthread_attr_destroy(&attr);
// report progress
for (;;) {
diff --git a/llamafile/server/buffer.cpp b/llamafile/server/buffer.cpp
index 65bfc349..fa0d0f00 100644
--- a/llamafile/server/buffer.cpp
+++ b/llamafile/server/buffer.cpp
@@ -17,10 +17,10 @@
#include "buffer.h"
-#include <sys/auxv.h>
#include <sys/mman.h>
+#include <unistd.h>
-static int pagesz = getauxval(AT_PAGESZ);
+static int pagesz = getpagesize();
Buffer::Buffer(size_t capacity) noexcept
: i(0)
diff --git a/llamafile/server/server.cpp b/llamafile/server/server.cpp
index 1bc5aaf4..8dc48a8c 100644
--- a/llamafile/server/server.cpp
+++ b/llamafile/server/server.cpp
@@ -21,7 +21,6 @@
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <stdio.h>
-#include <sys/auxv.h>
#include <sys/socket.h>
#include <time.h>
diff --git a/llamafile/server/time.cpp b/llamafile/server/time.cpp
index 3420bc0f..3f216bea 100644
--- a/llamafile/server/time.cpp
+++ b/llamafile/server/time.cpp
@@ -20,7 +20,6 @@
#include <atomic>
#include <pthread.h>
#include <signal.h>
-#include <sys/auxv.h>
#include <unistd.h>
#include "llamafile/crash.h"
@@ -150,8 +149,6 @@ time_init()
update_time();
pthread_attr_t attr;
pthread_attr_init(&attr);
- pthread_attr_setstacksize(&attr, 65536);
- pthread_attr_setguardsize(&attr, getauxval(AT_PAGESZ));
if (pthread_create(&g_time_thread, &attr, time_worker, 0))
__builtin_trap();
pthread_attr_destroy(&attr);
diff --git a/stable-diffusion.cpp/BUILD.mk b/stable-diffusion.cpp/BUILD.mk
index 2483214b..e0e4f9a2 100644
--- a/stable-diffusion.cpp/BUILD.mk
+++ b/stable-diffusion.cpp/BUILD.mk
@@ -24,7 +24,9 @@ $(STABLE_DIFFUSION_CPP_OBJS): private \
$(STABLE_DIFFUSION_CPP_OBJS): private \
CXXFLAGS += \
- -frtti
+ -frtti \
+ -Wno-alloc-size-larger-than \
+ -Wno-deprecated-declarations
o/$(MODE)/stable-diffusion.cpp/main: \
o/$(MODE)/stable-diffusion.cpp/main.o \