Skip to content

Distribute and run LLMs with a single file.

Refactor cpu_get_num_math() with cores.h header

Justine Tunney484a1b266c11
diff --git a/llama.cpp/common.cpp b/llama.cpp/common.cpp
index 9c4f53ac..80d6353b 100644
--- a/llama.cpp/common.cpp
+++ b/llama.cpp/common.cpp
@@ -90,7 +90,6 @@ using json = nlohmann::ordered_json;
//
int32_t cpu_get_num_physical_cores();
-int32_t cpu_get_num_math();
//
// CLI argument parsing
diff --git a/llama.cpp/common.h b/llama.cpp/common.h
index 7cb02d35..8e731d01 100644
--- a/llama.cpp/common.h
+++ b/llama.cpp/common.h
@@ -6,6 +6,7 @@
#pragma once
#include "llamafile/log.h"
+#include "llama.cpp/cores.h"
#include "llama.h"
#include "sampling.h"
@@ -61,7 +62,6 @@ struct llama_control_vector_load_info;
//
int32_t cpu_get_num_physical_cores();
-int32_t cpu_get_num_math();
//
// CLI argument parsing
diff --git a/llama.cpp/cores.cpp b/llama.cpp/cores.cpp
index f138ea0f..52746db9 100644
--- a/llama.cpp/cores.cpp
+++ b/llama.cpp/cores.cpp
@@ -1,3 +1,5 @@
+#include "cores.h"
+
#include <cosmo.h>
#include <fstream>
#include <thread>
@@ -87,10 +89,7 @@ static int count_math_cpus(int cpu_count) {
#endif // __x86_64__ && __linux__
-/**
- * Returns number of CPUs on system that are useful for math.
- */
-int cpu_get_num_math() {
+static int cpu_get_num_math_impl() {
#if defined(__x86_64__) && (defined(__linux__) || defined(__COSMOPOLITAN__)) && !defined(__ANDROID__)
int cpu_count = sysconf(_SC_NPROCESSORS_ONLN);
if (cpu_count < 1) {
@@ -109,3 +108,16 @@ int cpu_get_num_math() {
#endif
return get_num_physical_cores();
}
+
+/**
+ * Returns number of CPUs on system that are useful for math.
+ */
+int cpu_get_num_math() {
+ static bool once;
+ static int result;
+ if (!once) {
+ result = cpu_get_num_math_impl();
+ once = true;
+ }
+ return result;
+}
diff --git a/llama.cpp/cores.h b/llama.cpp/cores.h
new file mode 100644
index 00000000..b15e2f6f
--- /dev/null
+++ b/llama.cpp/cores.h
@@ -0,0 +1,10 @@
+#pragma once
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int cpu_get_num_math(void);
+
+#ifdef __cplusplus
+}
+#endif
diff --git a/llama.cpp/llama-bench/llama-bench.cpp b/llama.cpp/llama-bench/llama-bench.cpp
index 2ae41288..46a18bb7 100644
--- a/llama.cpp/llama-bench/llama-bench.cpp
+++ b/llama.cpp/llama-bench/llama-bench.cpp
@@ -24,6 +24,7 @@
#include <sys/stat.h>
#include <sys/auxv.h>
#include <libc/intrin/x86.h>
+#include "llama.cpp/cores.h"
#include <libc/sysv/consts/hwcap.h>
#include "llama.cpp/ggml.h"
diff --git a/llamafile/ansiblas.h b/llamafile/ansiblas.h
index bf5bff61..38f5bf2b 100644
--- a/llamafile/ansiblas.h
+++ b/llamafile/ansiblas.h
@@ -20,7 +20,7 @@
#include <cmath>
#include <unistd.h>
-int cpu_get_num_math();
+#include "llama.cpp/cores.h"
namespace {
namespace ansiBLAS {
diff --git a/llamafile/core_manager.cpp b/llamafile/core_manager.cpp
index d7627c0f..461ee5a7 100644
--- a/llamafile/core_manager.cpp
+++ b/llamafile/core_manager.cpp
@@ -19,7 +19,7 @@
#include <assert.h>
-int cpu_get_num_math();
+#include "llama.cpp/cores.h"
CoreManager g_core_manager;
diff --git a/llamafile/flags.cpp b/llamafile/flags.cpp
index 05858acb..613310e2 100644
--- a/llamafile/flags.cpp
+++ b/llamafile/flags.cpp
@@ -16,6 +16,7 @@
// limitations under the License.
#include "debug.h"
+#include "llama.cpp/cores.h"
#include "llamafile.h"
#include "trust.h"
@@ -67,8 +68,6 @@ int FLAG_verbose = 0;
int FLAG_warmup = true;
int FLAG_workers;
-int cpu_get_num_math();
-
static wontreturn void usage(int rc, int fd) {
tinyprint(fd, "usage: ", program_invocation_name, " -m MODEL -l [HOST:]PORT\n", NULL);
exit(rc);
diff --git a/llamafile/sgemm_matmul_test.cpp b/llamafile/sgemm_matmul_test.cpp
index 4880d416..78954af7 100644
--- a/llamafile/sgemm_matmul_test.cpp
+++ b/llamafile/sgemm_matmul_test.cpp
@@ -29,8 +29,6 @@
#define ITERATIONS 30
#define ALLOC(n) (float *)memalign(4096, sizeof(float) * (n))
-int cpu_get_num_math();
-
void llamafile_sgemm_openmp(long m, long n, long k, const void *A, long lda, const void *B,
long ldb, void *C, long ldc, int Atype, int Btype, int Ctype) {
static int nth = cpu_get_num_math();
diff --git a/llamafile/sgemm_sss_test.cpp b/llamafile/sgemm_sss_test.cpp
index da59ccbf..2c849c59 100644
--- a/llamafile/sgemm_sss_test.cpp
+++ b/llamafile/sgemm_sss_test.cpp
@@ -28,8 +28,6 @@
#define ITERATIONS 5
#define ALLOC(n) (float *)memalign(4096, sizeof(float) * (n))
-int cpu_get_num_math();
-
void llamafile_sgemm_openmp(long m, long n, long k, const void *A, long lda, const void *B,
long ldb, void *C, long ldc, int Atype, int Btype, int Ctype) {
static int nth = cpu_get_num_math();
diff --git a/llamafile/sgemm_vecdot_test.cpp b/llamafile/sgemm_vecdot_test.cpp
index cb775172..af5cf1d1 100644
--- a/llamafile/sgemm_vecdot_test.cpp
+++ b/llamafile/sgemm_vecdot_test.cpp
@@ -27,8 +27,6 @@
#define ITERATIONS 30
#define ALLOC(n) (float *)memalign(4096, sizeof(float) * (n))
-int cpu_get_num_math();
-
void llamafile_sgemm_openmp(long m, long n, long k, const void *A, long lda, const void *B,
long ldb, void *C, long ldc, int Atype, int Btype, int Ctype) {
static int nth = cpu_get_num_math();
diff --git a/stable-diffusion.cpp/BUILD.mk b/stable-diffusion.cpp/BUILD.mk
index e66eeeaa..2a6c95f1 100644
--- a/stable-diffusion.cpp/BUILD.mk
+++ b/stable-diffusion.cpp/BUILD.mk
@@ -24,7 +24,6 @@ $(STABLE_DIFFUSION_CPP_OBJS): private \
$(STABLE_DIFFUSION_CPP_OBJS): private \
CXXFLAGS += \
-frtti \
- -Wno-alloc-size-larger-than \
-Wno-deprecated-declarations
o/$(MODE)/stable-diffusion.cpp/main: \
diff --git a/stable-diffusion.cpp/darts.h b/stable-diffusion.cpp/darts.h
index 8892aaaa..a9952174 100644
--- a/stable-diffusion.cpp/darts.h
+++ b/stable-diffusion.cpp/darts.h
@@ -1459,7 +1459,7 @@ class DoubleArrayBuilder {
private:
enum { BLOCK_SIZE = 256 };
enum { NUM_EXTRA_BLOCKS = 16 };
- enum { NUM_EXTRAS = BLOCK_SIZE * NUM_EXTRA_BLOCKS };
+ enum { NUM_EXTRAS = (int)BLOCK_SIZE * (int)NUM_EXTRA_BLOCKS };
enum { UPPER_MASK = 0xFF << 21 };
enum { LOWER_MASK = 0xFF };
diff --git a/stable-diffusion.cpp/main.cpp b/stable-diffusion.cpp/main.cpp
index 65cd586c..9458ea76 100644
--- a/stable-diffusion.cpp/main.cpp
+++ b/stable-diffusion.cpp/main.cpp
@@ -5,6 +5,7 @@
#include <random>
#include <string>
#include <vector>
+#include "llama.cpp/cores.h"
#include <cosmo.h>
// #include "preprocessing.hpp"
@@ -527,7 +528,6 @@ void parse_args(int argc, const char** argv, SDParams& params) {
exit(1);
}
if (params.n_threads <= 0) {
- int cpu_get_num_math(void);
params.n_threads = cpu_get_num_math();
}
diff --git a/stable-diffusion.cpp/tae.hpp b/stable-diffusion.cpp/tae.hpp
index 0e03b884..ba268147 100644
--- a/stable-diffusion.cpp/tae.hpp
+++ b/stable-diffusion.cpp/tae.hpp
@@ -191,8 +191,8 @@ struct TinyAutoEncoder : public GGMLRunner {
ggml_type wtype,
bool decoder_only = true)
: decode_only(decoder_only),
- taesd(decode_only),
GGMLRunner(backend, wtype) {
+ taesd = TAESD(decode_only); // [jart] fix ub
taesd.init(params_ctx, wtype);
}
@@ -248,4 +248,4 @@ struct TinyAutoEncoder : public GGMLRunner {
}
};
-#endif // __TAE_HPP__
\ No newline at end of file
+#endif // __TAE_HPP__
diff --git a/whisper.cpp/main.cpp b/whisper.cpp/main.cpp
index ca613bef..68275fae 100644
--- a/whisper.cpp/main.cpp
+++ b/whisper.cpp/main.cpp
@@ -1,7 +1,9 @@
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
+
#include "llamafile/version.h"
#include "llamafile/llamafile.h"
+#include "llama.cpp/cores.h"
#include "common.h"
#include "whisper.h"
@@ -25,11 +27,9 @@
#pragma warning(disable: 4244 4267) // possible loss of data
#endif
-int cpu_get_num_math();
-
// command-line parameters
struct whisper_params {
- int32_t n_threads = cpu_get_num_math();
+ int32_t n_threads = std::min(16, cpu_get_num_math());
int32_t n_processors = 1;
int32_t offset_t_ms = 0;
int32_t offset_n = 0;
diff --git a/whisper.cpp/whisper.cpp b/whisper.cpp/whisper.cpp
index f8c18a59..c6489311 100644
--- a/whisper.cpp/whisper.cpp
+++ b/whisper.cpp/whisper.cpp
@@ -1,6 +1,7 @@
// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;tab-width:8;coding:utf-8 -*-
// vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
#include "llama.cpp/ggml-vector.h"
+#include "llama.cpp/cores.h"
#include "whisper.h"
#define GGML_USE_CUDA
@@ -4777,7 +4778,7 @@ struct whisper_full_params whisper_full_default_params(enum whisper_sampling_str
struct whisper_full_params result = {
/*.strategy =*/ strategy,
- /*.n_threads =*/ std::min(4, (int32_t) std::thread::hardware_concurrency()),
+ /*.n_threads =*/ std::min(8, (int32_t) cpu_get_num_math()),
/*.n_max_text_ctx =*/ 16384,
/*.offset_ms =*/ 0,
/*.duration_ms =*/ 0,