Skip to content

Distribute and run LLMs with a single file.

Make benchmark tool work more reliably

Justine Tunney8b2f8d8e52d6
diff --git a/Makefile b/Makefile
index a8d74839..ceb108ee 100644
--- a/Makefile
+++ b/Makefile
@@ -32,6 +32,7 @@ install: llamafile/zipalign.1 \
o/$(MODE)/llama.cpp/main/main \
o/$(MODE)/llama.cpp/imatrix/imatrix \
o/$(MODE)/llama.cpp/quantize/quantize \
+ o/$(MODE)/llama.cpp/llama-bench/llama-bench \
o/$(MODE)/llama.cpp/perplexity/perplexity \
o/$(MODE)/llama.cpp/llava/llava-quantize
mkdir -p $(PREFIX)/bin
@@ -40,6 +41,7 @@ install: llamafile/zipalign.1 \
$(INSTALL) o/$(MODE)/llama.cpp/main/main $(PREFIX)/bin/llamafile
$(INSTALL) o/$(MODE)/llama.cpp/imatrix/imatrix $(PREFIX)/bin/llamafile-imatrix
$(INSTALL) o/$(MODE)/llama.cpp/quantize/quantize $(PREFIX)/bin/llamafile-quantize
+ $(INSTALL) o/$(MODE)/llama.cpp/llama-bench/llama-bench $(PREFIX)/bin/llamafile-bench
$(INSTALL) build/llamafile-convert $(PREFIX)/bin/llamafile-convert
$(INSTALL) build/llamafile-upgrade-engine $(PREFIX)/bin/llamafile-upgrade-engine
$(INSTALL) o/$(MODE)/llama.cpp/perplexity/perplexity $(PREFIX)/bin/llamafile-perplexity
diff --git a/llama.cpp/llama-bench/bench-llamafile.sh b/llama.cpp/llama-bench/bench-llamafile.sh
index a31f5407..8421b10b 100755
--- a/llama.cpp/llama-bench/bench-llamafile.sh
+++ b/llama.cpp/llama-bench/bench-llamafile.sh
@@ -2,8 +2,6 @@
cd ~/llamafile
make -j16 o//llama.cpp/llama-bench/llama-bench || exit
o//llama.cpp/llama-bench/llama-bench \
- $(for f in $(ls -S /weights/TinyLlama-1.1B-Chat-v1.0.*.gguf \
- /weights/mixtral-8x7b-instruct-v0.1.*.gguf); do
- echo -m $f
- done) \
+ /weights/TinyLlama-1.1B-Chat-v1.0.*.gguf \
+ /weights/mixtral-8x7b-instruct-v0.1.*.gguf \
"$@"
diff --git a/llama.cpp/llama-bench/llama-bench.cpp b/llama.cpp/llama-bench/llama-bench.cpp
index 3e045ffb..ba03b00e 100644
--- a/llama.cpp/llama-bench/llama-bench.cpp
+++ b/llama.cpp/llama-bench/llama-bench.cpp
@@ -18,11 +18,13 @@
#include <vector>
#include <cosmo.h>
#include <libgen.h>
+#include <sys/stat.h>
#include "llama.cpp/ggml.h"
#include "llama.cpp/llama.h"
#include "llama.cpp/common.h"
#include "llama.cpp/ggml-cuda.h"
+#include "llamafile/llamafile.h"
// utils
static uint64_t get_time_ns() {
@@ -107,6 +109,12 @@ static std::string get_cpu_info() {
}
fclose(f);
}
+ } else if (IsXnu()) {
+ char cpu_name[128] = {0};
+ size_t size = sizeof(cpu_name);
+ if (sysctlbyname("machdep.cpu.brand_string", cpu_name, &size, NULL, 0) != -1) {
+ id = cpu_name;
+ }
}
// TODO: other platforms
return id;
@@ -211,7 +219,7 @@ static const cmd_params cmd_params_defaults = {
/* use_mmap */ {true},
/* embeddings */ {false},
/* numa */ GGML_NUMA_STRATEGY_DISABLED,
- /* reps */ 1,
+ /* reps */ 3,
/* verbose */ false,
/* output_format */ MARKDOWN
};
@@ -499,9 +507,13 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
}
} else if (arg == "-v" || arg == "--verbose") {
params.verbose = true;
- } else {
+ } else if (arg[0] == '-') {
invalid_param = true;
break;
+ } else {
+ // [jart] let me glob without needing -m flag
+ auto p = split<std::string>(argv[i], split_delim);
+ params.model.insert(params.model.end(), p.begin(), p.end());
}
}
if (invalid_param) {
@@ -510,6 +522,20 @@ static cmd_params parse_cmd_params(int argc, char ** argv) {
exit(1);
}
+ // [jart] sort larger models first
+ std::sort(params.model.begin(), params.model.end(), [](const std::string& a, const std::string& b) {
+ struct stat statA, statB;
+ if (stat(a.c_str(), &statA)) {
+ perror(a.c_str());
+ exit(1);
+ }
+ if (stat(b.c_str(), &statB)) {
+ perror(b.c_str());
+ exit(1);
+ }
+ return statA.st_size > statB.st_size;
+ });
+
// set defaults
if (params.model.empty()) { params.model = cmd_params_defaults.model; }
if (params.n_prompt.empty()) { params.n_prompt = cmd_params_defaults.n_prompt; }
@@ -1010,7 +1036,7 @@ struct markdown_printer : public printer {
return 48; // [jart]
}
if (field == "model_filename") {
- return 35; // [jart]
+ return 40; // [jart]
}
if (field == "size" || field == "params") {
return 10;
@@ -1063,7 +1089,7 @@ struct markdown_printer : public printer {
fields.emplace_back("cpu_info"); // [jart]
fields.emplace_back("model_filename");
// fields.emplace_back("model");
- // fields.emplace_back("size"); // [jart]
+ fields.emplace_back("size"); // [jart]
// fields.emplace_back("params"); // [jart]
// fields.emplace_back("backend"); // [jart]
bool is_cpu_backend = test::get_backend() == "CPU" || test::get_backend() == "BLAS";
@@ -1168,10 +1194,10 @@ struct markdown_printer : public printer {
}
int width = get_field_width(field);
- if (field == "t/s") {
- // HACK: the utf-8 character is 2 bytes
- width += 1;
- }
+ // if (field == "t/s") { // [jart]
+ // // HACK: the utf-8 character is 2 bytes
+ // width += 1;
+ // }
fprintf(fout, " %*s |", width, value.c_str());
}
fprintf(fout, "\n");
@@ -1269,12 +1295,20 @@ __attribute__((__constructor__(101))) static void init(void) {
}
int main(int argc, char ** argv) {
+
+ ShowCrashReports();
+
// try to set locale for unicode characters in markdown
setlocale(LC_CTYPE, ".UTF-8");
-#if !defined(NDEBUG)
- fprintf(stderr, "warning: asserts enabled, performance may be affected\n");
-#endif
+ __warn_if_powersave(); // [jart]
+ if (!getenv("LLAMAFILE_TEMPERATURE_FILE") || !getenv("LLAMAFILE_TEMPERATURE_MAX"))
+ fprintf(stderr, "warning: don't know how to govern your cpu temperature; "
+ "consider setting the environment variables described in llamafile/govern.cpp\n");
+
+// #if !defined(NDEBUG)
+// fprintf(stderr, "warning: asserts enabled, performance may be affected\n");
+// #endif
#if (defined(_MSC_VER) && defined(_DEBUG)) || (!defined(_MSC_VER) && !defined(__OPTIMIZE__))
fprintf(stderr, "warning: debug build, performance may be affected\n");
@@ -1358,6 +1392,8 @@ int main(int argc, char ** argv) {
for (int i = 0; i < params.reps; i++) {
llama_kv_cache_clear(ctx);
+ llamafile_govern(); // [jart] see docs in llamafile/govern.cpp
+
uint64_t t_start = get_time_ns();
if (t.n_prompt > 0) {
@@ -1376,9 +1412,6 @@ int main(int argc, char ** argv) {
llama_print_timings(ctx);
llama_free(ctx);
-
- // [jart] let cpu cool off
- sleep(7);
}
llama_free_model(lmodel);
diff --git a/llamafile/govern.cpp b/llamafile/govern.cpp
new file mode 100644
index 00000000..01c3f3ca
--- /dev/null
+++ b/llamafile/govern.cpp
@@ -0,0 +1,85 @@
+// -*- 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 "llamafile.h"
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+// cpu governor for llamafile-bench
+//
+// benchmarks can be negatively impacted when the operating system or
+// motherboard throttles the cpu due to high temperature. we can say:
+//
+// echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
+// export LLAMAFILE_TEMPERATURE_FILE=/sys/class/hwmon/hwmon3/temp1_input
+// export LLAMAFILE_TEMPERATURE_MAX=60000 # 10° beneath mobo's governor
+// o//llama.cpp/llama-bench/llama-bench -m model1.gguf -m model2.gguf
+//
+// to ask llama-bench to wait until the temperature drops to 60°C before
+// running a benchmark. this number should be lower than the motherboard
+// threshold, which jart configures to 80°C. the actual path of your cpu
+// temperature sensor may vary.
+
+int llamafile_read_temperature(void) {
+ int fd;
+ const char *path;
+ char buf[13] = {0};
+ if (!(path = getenv("LLAMAFILE_TEMPERATURE_FILE")))
+ return -1;
+ if ((fd = open(path, O_RDONLY)) == -1) {
+ perror(path);
+ exit(1);
+ }
+ pread(fd, buf, 12, 0);
+ close(fd);
+ return atoi(buf);
+}
+
+int llamafile_govern_threshold(void) {
+ int temp;
+ const char *s;
+ if (!(s = getenv("LLAMAFILE_TEMPERATURE_MAX")))
+ return -1;
+ temp = atoi(s);
+ if (temp <= 0) {
+ errno = EINVAL;
+ perror("LLAMAFILE_TEMPERATURE_MAX");
+ exit(1);
+ }
+ return temp;
+}
+
+void llamafile_govern(void) {
+ int max, cur;
+ int delay = 1;
+ for (;;) {
+ if ((max = llamafile_govern_threshold()) == -1)
+ return;
+ if ((cur = llamafile_read_temperature()) == -1)
+ return;
+ if (cur <= max)
+ return;
+ usleep(delay);
+ if (delay < 16384)
+ delay <<= 1;
+ }
+}
diff --git a/llamafile/llamafile.h b/llamafile/llamafile.h
index b8c8b1d9..72067adf 100644
--- a/llamafile/llamafile.h
+++ b/llamafile/llamafile.h
@@ -17,6 +17,7 @@ size_t llamafile_tell(struct llamafile *);
size_t llamafile_size(struct llamafile *);
FILE *llamafile_fp(struct llamafile *);
+void llamafile_govern(void);
void llamafile_check_cpu(void);
void llamafile_help(const char *);
void llamafile_log_command(char *[]);
diff --git a/llamafile/tinyblas_cpu.h b/llamafile/tinyblas_cpu.h
index 43021159..fdffa0d1 100644
--- a/llamafile/tinyblas_cpu.h
+++ b/llamafile/tinyblas_cpu.h
@@ -458,7 +458,7 @@ class tinyBLAS {
case 0x22:
mc = 2;
nc = 2;
- gemm<2, 2, true>(m0, m, n0, n);
+ gemm<2, 2, false>(m0, m, n0, n);
break;
case 0x51:
case 0x41:
@@ -466,7 +466,7 @@ class tinyBLAS {
case 0x21:
mc = 2;
nc = 1;
- gemm<2, 1, true>(m0, m, n0, n);
+ gemm<2, 1, false>(m0, m, n0, n);
break;
case 0x15:
case 0x14:
@@ -474,12 +474,12 @@ class tinyBLAS {
case 0x12:
mc = 1;
nc = 2;
- gemm<1, 2, true>(m0, m, n0, n);
+ gemm<1, 2, false>(m0, m, n0, n);
break;
case 0x11:
mc = 1;
nc = 1;
- gemm<1, 1, true>(m0, m, n0, n);
+ gemm<1, 1, false>(m0, m, n0, n);
break;
default:
return;
@@ -539,25 +539,25 @@ class tinyBLAS {
case 0x22:
mc = 2;
nc = 2;
- gemm<2, 2, true>(m0, m, n0, n);
+ gemm<2, 2, false>(m0, m, n0, n);
break;
case 0x41:
case 0x31:
case 0x21:
mc = 2;
nc = 1;
- gemm<2, 1, true>(m0, m, n0, n);
+ gemm<2, 1, false>(m0, m, n0, n);
break;
case 0x13:
case 0x12:
mc = 1;
nc = 2;
- gemm<1, 2, true>(m0, m, n0, n);
+ gemm<1, 2, false>(m0, m, n0, n);
break;
case 0x11:
mc = 1;
nc = 1;
- gemm<1, 1, true>(m0, m, n0, n);
+ gemm<1, 1, false>(m0, m, n0, n);
break;
default:
return;
@@ -693,18 +693,18 @@ class tinyBLAS_Q0_ARM {
case 0x21:
mc = 2;
nc = 1;
- gemm<2, 1, true>(m0, m, n0, n);
+ gemm<2, 1, false>(m0, m, n0, n);
break;
case 0x13:
case 0x12:
mc = 1;
nc = 2;
- gemm<1, 2, true>(m0, m, n0, n);
+ gemm<1, 2, false>(m0, m, n0, n);
break;
case 0x11:
mc = 1;
nc = 1;
- gemm<1, 1, true>(m0, m, n0, n);
+ gemm<1, 1, false>(m0, m, n0, n);
break;
default:
return;
@@ -931,17 +931,17 @@ class tinyBLAS_Q0_AVX2 {
case 0x21:
mc = 2;
nc = 1;
- gemm<2, 1, true>(m0, m, n0, n);
+ gemm<2, 1, false>(m0, m, n0, n);
break;
case 0x12:
mc = 1;
nc = 2;
- gemm<1, 2, true>(m0, m, n0, n);
+ gemm<1, 2, false>(m0, m, n0, n);
break;
case 0x11:
mc = 1;
nc = 1;
- gemm<1, 1, true>(m0, m, n0, n);
+ gemm<1, 1, false>(m0, m, n0, n);
break;
default:
return;
diff --git a/llamafile/tinyblas_cpu_mixmul.inc b/llamafile/tinyblas_cpu_mixmul.inc
index 120aa33a..eb61f93c 100644
--- a/llamafile/tinyblas_cpu_mixmul.inc
+++ b/llamafile/tinyblas_cpu_mixmul.inc
@@ -182,9 +182,15 @@ class MixMul {
if (thought->type != GGML_TYPE_F32 && thought->type != GGML_TYPE_BF16)
return false;
#if defined(__AVX512BF16__)
- return mixmat<32, 1,
- tinyBLAS<NCB | NCC, 32, __m512, __m512bh, ggml_bf16_t, ggml_bf16_t, TC>,
- ggml_bf16_t, ggml_bf16_t, TC>();
+ if (!FLAG_precise) {
+ return mixmat<
+ 32, 1, tinyBLAS<NCB | NCC, 32, __m512, __m512bh, ggml_bf16_t, ggml_bf16_t, TC>,
+ ggml_bf16_t, ggml_bf16_t, TC>();
+ } else {
+ return mixmat<16, 1,
+ tinyBLAS<NCB | NCC, 16, __m512, __m512, ggml_bf16_t, ggml_bf16_t, TC>,
+ ggml_bf16_t, ggml_bf16_t, TC>();
+ }
#elif defined(__AVX512F__)
return mixmat<16, 1,
tinyBLAS<NCB | NCC, 16, __m512, __m512, ggml_bf16_t, ggml_bf16_t, TC>,
diff --git a/llamafile/tinyblas_cpu_sgemm.inc b/llamafile/tinyblas_cpu_sgemm.inc
index bdc9ece7..7555e799 100644
--- a/llamafile/tinyblas_cpu_sgemm.inc
+++ b/llamafile/tinyblas_cpu_sgemm.inc
@@ -94,10 +94,17 @@ bool llamafile_sgemm_impl(long m, long n, long k, const void *A, long lda, const
return WANT_QUANTIZATION;
if (Btype != GGML_TYPE_BF16)
return NOT_SUPPORTED;
- tinyBLAS<0, 32, __m512, __m512bh, ggml_bf16_t, ggml_bf16_t, TC> tb{
- k, (const ggml_bf16_t *)A, lda, (const ggml_bf16_t *)B, ldb, C, ldc, ith, nth};
- tb.matmul(m, n, task);
- return true;
+ if (!FLAG_precise) {
+ tinyBLAS<0, 32, __m512, __m512bh, ggml_bf16_t, ggml_bf16_t, TC> tb{
+ k, (const ggml_bf16_t *)A, lda, (const ggml_bf16_t *)B, ldb, C, ldc, ith, nth};
+ tb.matmul(m, n, task);
+ return true;
+ } else {
+ tinyBLAS<0, 16, __m512, __m512, ggml_bf16_t, ggml_bf16_t, TC> tb{
+ k, (const ggml_bf16_t *)A, lda, (const ggml_bf16_t *)B, ldb, C, ldc, ith, nth};
+ tb.matmul(m, n, task);
+ return true;
+ }
#elif defined(__AVX512F__)
if (k % 16)
return NOT_SUPPORTED;
@@ -168,7 +175,7 @@ bool llamafile_sgemm_impl(long m, long n, long k, const void *A, long lda, const
return NOT_SUPPORTED;
}
#elif defined(__ARM_FEATURE_FP16_VECTOR_ARITHMETIC) && !defined(_MSC_VER)
- if (n < 2)
+ if (n < 2 && !FLAG_precise)
// TODO(jart): Why is ggml_vec_dot_f16_unroll() so fast at matvec?
return NOT_SUPPORTED;
if (precision == GGML_PREC_F32) {
@@ -193,7 +200,7 @@ bool llamafile_sgemm_impl(long m, long n, long k, const void *A, long lda, const
return true;
}
#elif defined(__ARM_NEON) && !defined(_MSC_VER)
- if (n < 2)
+ if (n < 2 && !FLAG_precise)
// TODO(jart): Why is ggml_vec_dot_f16_unroll() so fast at matvec?
return NOT_SUPPORTED;
if (k % 4)
diff --git a/llamafile/tinyblas_mnpack.py b/llamafile/tinyblas_mnpack.py
index 1279928f..c870b032 100644
--- a/llamafile/tinyblas_mnpack.py
+++ b/llamafile/tinyblas_mnpack.py
@@ -40,35 +40,17 @@ def doit(VECTOR_REGISTERS, PRECISE):
if (mc > EDGE_M or nc > EDGE_N) and (mc == 1 or nc == 1):
continue
- # always use precise if there's enough registers
- v = accumulators * 2 + memory_loads + OVERHEAD
- if v <= VECTOR_REGISTERS:
- if mc % 8 == 0:
- v += 2
- if mc % 4 == 0:
- v += 1
- specified[mc, nc] = v
- if PRECISE or (mc == 1 or nc == 1):
- precise.add((mc, nc))
- elif not PRECISE:
- v = accumulators + memory_loads + OVERHEAD
- if v <= VECTOR_REGISTERS:
- if mc % 8 == 0:
- v += 2
- if mc % 4 == 0:
- v += 1
- specified[mc, nc] = v
-
- # if PRECISE:
- # v = accumulators * 2 + memory_loads + OVERHEAD
- # if v <= VECTOR_REGISTERS:
- # if mc % 8 == 0:
- # v += 2
- # if mc % 4 == 0:
- # v += 1
- # specified[mc, nc] = v
+ # # always use precise if there's enough registers
+ # v = accumulators * 2 + memory_loads + OVERHEAD
+ # if v <= VECTOR_REGISTERS:
+ # if mc % 8 == 0:
+ # v += 2
+ # if mc % 4 == 0:
+ # v += 1
+ # specified[mc, nc] = v
+ # if PRECISE or (mc == 1 or nc == 1):
# precise.add((mc, nc))
- # else:
+ # elif not PRECISE:
# v = accumulators + memory_loads + OVERHEAD
# if v <= VECTOR_REGISTERS:
# if mc % 8 == 0:
@@ -77,6 +59,24 @@ def doit(VECTOR_REGISTERS, PRECISE):
# v += 1
# specified[mc, nc] = v
+ if PRECISE:
+ v = accumulators * 2 + memory_loads + OVERHEAD
+ if v <= VECTOR_REGISTERS:
+ if mc % 8 == 0:
+ v += 2
+ if mc % 4 == 0:
+ v += 1
+ specified[mc, nc] = v
+ precise.add((mc, nc))
+ else:
+ v = accumulators + memory_loads + OVERHEAD
+ if v <= VECTOR_REGISTERS:
+ if mc % 8 == 0:
+ v += 2
+ if mc % 4 == 0:
+ v += 1
+ specified[mc, nc] = v
+
# generate code for handling biggest tile (e.g. 5x5)
# generate code for handling edge tiles (i.e. <=2x2)
# avoid long compile times to generate tiles between