Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Display progress of loading weights in terminal
Justine Tunney674b48f7f509
diff --git a/llama.cpp/ggml.h b/llama.cpp/ggml.hindex d47e68fc..749bd2c7 100644--- a/llama.cpp/ggml.h+++ b/llama.cpp/ggml.h@@ -2158,6 +2158,7 @@ extern "C" {GGML_API void ggml_check_cpu(void);GGML_API void ggml_get_app_dir(char *path, size_t size);+ GGML_API void ggml_schlep(const void *data, size_t size);GGML_API bool ggml_extract(const char *zip, const char *to);GGML_API bool ggml_is_newer_than(const char *path, const char *other);diff --git a/llama.cpp/llama.cc b/llama.cpp/llama.ccindex 19f4b93a..1193b733 100644--- a/llama.cpp/llama.cc+++ b/llama.cpp/llama.cc@@ -765,14 +765,14 @@ struct llama_mmap {#ifdef __linux__if (prefetch) { flags |= MAP_POPULATE; }#endif- addr = mmap(NULL, ggml_file_size(file->file), PROT_READ, flags, fd, 0);+ addr = mmap(NULL, size, PROT_READ, flags, fd, 0);if (addr == MAP_FAILED) {ThrowRuntimeError(format("mmap failed: %s", strerror(errno)));}if (prefetch > 0) {// Advise the kernel to preload the mapped memory- if (posix_madvise(addr, std::min(ggml_file_size(file->file), prefetch), POSIX_MADV_WILLNEED)) {+ if (posix_madvise(addr, std::min(size, prefetch), POSIX_MADV_WILLNEED)) {fprintf(stderr, "warning: posix_madvise(.., POSIX_MADV_WILLNEED) failed: %s\n",strerror(errno));}@@ -780,11 +780,14 @@ struct llama_mmap {if (numa) {// advise the kernel not to use readahead// (because the next page might not belong on the same node)- if (posix_madvise(addr, ggml_file_size(file->file), POSIX_MADV_RANDOM)) {+ if (posix_madvise(addr, size, POSIX_MADV_RANDOM)) {fprintf(stderr, "warning: posix_madvise(.., POSIX_MADV_RANDOM) failed: %s\n",strerror(errno));}}++ // report terminal progress of loading weights+ ggml_schlep(addr, size);}~llama_mmap() {diff --git a/llama.cpp/schlep.c b/llama.cpp/schlep.cnew file mode 100644index 00000000..9492964d--- /dev/null+++ b/llama.cpp/schlep.c@@ -0,0 +1,105 @@+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-+// vi: set net ft=c ts=4 sts=4 sw=4 fenc=utf-8 :vi+#define _COSMO_SOURCE+#include <cosmo.h>+#include <stdio.h>+#include <errno.h>+#include <unistd.h>+#include <pthread.h>+#include <sys/auxv.h>+#include <stdatomic.h>+#include "ggml.h"++#define FPS 24++struct Schlep {+ long n;+ atomic_long i;+};++static char Peek(volatile const char *ptr) {+ return *ptr;+}++char (*pPeek)(volatile const char *) = Peek;++static void FormatPercent(char sbuf[static 8], double x) {+ x *= 100000.5;+ int n = x;+ char *p = sbuf;+ if (n >= 100000) *p++ = '0' + n / 100000 % 10;+ if (n >= 10000) *p++ = '0' + n / 10000 % 10;+ *p++ = '0' + n / 1000 % 10;+ *p++ = '.';+ *p++ = '0' + n / 100 % 10;+ *p++ = '0' + n / 10 % 10;+ *p++ = '0' + n % 10;+ *p = 0;+}++static void *ProgressReporter(void *arg) {+ struct Schlep *s = arg;+ pthread_setcancelstate(PTHREAD_CANCEL_MASKED, 0);+ while (!usleep(1. / FPS * 1e6)) {+ long i = atomic_load_explicit(&s->i, memory_order_acquire);+ char percent[8];+ FormatPercent(percent, (double)i / s->n);+ tinyprint(2, "\rmemory map ", percent, "% loaded...\033[K", NULL);+ }+ tinyprint(2, "\r\033[K", NULL);+ return 0;+}++/**+ * Loads memory off disk while reporting progress.+ */+void ggml_schlep(const void *data, size_t size) {++ // don't bother if memory is small+ if (size < 128 * 1024 * 1024) {+ return;+ }++ // don't bother if stderr isn't a terminal+ if (!isatty(2)) {+ return;+ }++ // setup shared memory+ struct Schlep s = {size};++ // get microprocessor page size+ long pagesz = getauxval(AT_PAGESZ);++ // create worker thread+ errno_t err;+ pthread_t th;+ sigset_t blockall;+ pthread_attr_t attr;+ sigfillset(&blockall);+ pthread_attr_init(&attr);+ pthread_attr_setstacksize(&attr, 65536);+ pthread_attr_setguardsize(&attr, pagesz);+ pthread_attr_setsigmask_np(&attr, &blockall);+ err = pthread_create(&th, &attr, ProgressReporter, &s);+ pthread_attr_destroy(&attr);+ if (err) {+ // don't bother without thread+ errno = err;+ perror("pthread_create");+ return;+ }++ // fault each page in memory region+ long i = 0;+ volatile const char *p = data;+ while (i < s.n) {+ pPeek(p + i);+ i += pagesz;+ atomic_store_explicit(&s.i, i, memory_order_release);+ }++ // terminate worker thread+ pthread_cancel(th);+ pthread_join(th, 0);+}