Skip to content

Distribute and run LLMs with a single file.

Improve vector dot accuracy on cpu

Justine Tunney1219c9f50a52
diff --git a/llamafile/BUILD.mk b/llamafile/BUILD.mk
index 02e7a665..6934e553 100644
--- a/llamafile/BUILD.mk
+++ b/llamafile/BUILD.mk
@@ -63,8 +63,12 @@ o/$(MODE)/llamafile/sgemm_e1q1s_fma.o: private TARGET_ARCH += -Xx86_64-mavx2 -Xx
################################################################################
# testing
-o/$(MODE)/llamafile/sgemm_test: \
- o/$(MODE)/llamafile/sgemm_test.o \
+o/$(MODE)/llamafile/sgemm_sss_test: \
+ o/$(MODE)/llamafile/sgemm_sss_test.o \
+ o/$(MODE)/llama.cpp/llama.cpp.a
+
+o/$(MODE)/llamafile/sgemm_vecdot_test: \
+ o/$(MODE)/llamafile/sgemm_vecdot_test.o \
o/$(MODE)/llama.cpp/llama.cpp.a
o/$(MODE)/llamafile/%.o: llamafile/%.cu llamafile/BUILD.mk
diff --git a/llamafile/bench.h b/llamafile/bench.h
new file mode 100644
index 00000000..461ff081
--- /dev/null
+++ b/llamafile/bench.h
@@ -0,0 +1,20 @@
+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
+#pragma once
+
+#include <stdio.h>
+
+#include "micros.h"
+
+#define BENCH(x) \
+ do { \
+ x; \
+ __asm__ volatile("" ::: "memory"); \
+ long long start = micros(); \
+ for (int i = 0; i < ITERATIONS; ++i) { \
+ __asm__ volatile("" ::: "memory"); \
+ x; \
+ __asm__ volatile("" ::: "memory"); \
+ } \
+ printf("%8lld us %s\n", (micros() - start + ITERATIONS - 1) / ITERATIONS, #x); \
+ } while (0)
diff --git a/llamafile/flt.h b/llamafile/float.h
similarity index 50%
rename from llamafile/flt.h
rename to llamafile/float.h
index b40fc435..3157d0be 100644
--- a/llamafile/flt.h
+++ b/llamafile/float.h
@@ -2,30 +2,8 @@
// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
#pragma once
-// floating point utilities
-//
-// these functions let you fiddle with float bits. some of these
-// functions e.g. flt::isnan(), won't be optimized away when the
-// compiler is placed in -ffast-math mode.
-//
-// @see IEEE 754-2008
-
-#ifndef __HIP__
-#include <cuda_fp16.h>
-#else
-#include <hip/hip_fp16.h>
-#endif
-
namespace flt {
-inline int toint(half f) {
- union {
- half f;
- unsigned short i;
- } u = {f};
- return u.i;
-}
-
inline unsigned toint(float f) {
union {
float f;
@@ -34,35 +12,18 @@ inline unsigned toint(float f) {
return u.i;
}
-inline bool isnan(half f) {
- return (toint(f) & 0x7fff) > 0x7c00;
-}
-
inline bool isnan(float f) {
return (toint(f) & 0x7fffffff) > 0x7f800000;
}
-inline bool isinf(half f) {
- return (toint(f) & 0x7fff) == 0x7c00;
-}
-
inline bool isinf(float f) {
return (toint(f) & 0x7fffffff) == 0x7f800000;
}
-inline int signbit(half f) {
- return toint(f) & 0x8000;
-}
-
inline unsigned signbit(float f) {
return toint(f) & 0x80000000u;
}
-inline bool isnormal(half f) {
- int expo = toint(f) & 0x7c00;
- return expo && expo != 0x7c00;
-}
-
inline bool isnormal(float f) {
int expo = toint(f) & 0x7f800000;
return expo && expo != 0x7f800000;
diff --git a/llamafile/half.h b/llamafile/half.h
new file mode 100644
index 00000000..c1563bca
--- /dev/null
+++ b/llamafile/half.h
@@ -0,0 +1,38 @@
+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
+#pragma once
+
+#ifndef __HIP__
+#include <cuda_fp16.h>
+#else
+#include <hip/hip_fp16.h>
+#endif
+
+namespace flt {
+
+inline int toint(half f) {
+ union {
+ half f;
+ unsigned short i;
+ } u = {f};
+ return u.i;
+}
+
+inline bool isnan(half f) {
+ return (toint(f) & 0x7fff) > 0x7c00;
+}
+
+inline bool isinf(half f) {
+ return (toint(f) & 0x7fff) == 0x7c00;
+}
+
+inline int signbit(half f) {
+ return toint(f) & 0x8000;
+}
+
+inline bool isnormal(half f) {
+ int expo = toint(f) & 0x7c00;
+ return expo && expo != 0x7c00;
+}
+
+} // namespace flt
diff --git a/llamafile/micros.h b/llamafile/micros.h
new file mode 100644
index 00000000..de3c5356
--- /dev/null
+++ b/llamafile/micros.h
@@ -0,0 +1,36 @@
+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=c++ ts=4 sts=4 sw=4 fenc=utf-8 :vi
+#pragma once
+
+#include <ctime>
+
+#ifndef _WIN32
+#include <unistd.h>
+#else
+#include <windows.h>
+#endif
+
+#ifdef _WIN32
+static long long GetQueryPerformanceFrequency() {
+ LARGE_INTEGER t;
+ QueryPerformanceFrequency(&t);
+ return t.QuadPart;
+}
+static long long GetQueryPerformanceCounter() {
+ LARGE_INTEGER t;
+ QueryPerformanceCounter(&t);
+ return t.QuadPart;
+}
+#endif
+
+static long long micros(void) {
+#ifndef _WIN32
+ struct timespec ts;
+ clock_gettime(CLOCK_REALTIME, &ts);
+ return ts.tv_sec * 1000000 + (ts.tv_nsec + 999) / 1000;
+#else
+ static long long timer_freq = GetQueryPerformanceFrequency();
+ static long long timer_start = GetQueryPerformanceCounter();
+ return ((GetQueryPerformanceCounter() - timer_start) * 1000000) / timer_freq;
+#endif
+}
diff --git a/llamafile/numba.h b/llamafile/numba.h
new file mode 100644
index 00000000..8dfed67e
--- /dev/null
+++ b/llamafile/numba.h
@@ -0,0 +1,50 @@
+#pragma once
+
+inline int rand32(void) {
+ static unsigned long long lcg = 1;
+ lcg *= 6364136223846793005;
+ lcg += 1442695040888963407;
+ return lcg >> 32;
+}
+
+inline int popcount(unsigned x) {
+ x = x - ((x >> 1) & 0x55555555);
+ x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
+ x = (x + (x >> 4)) & 0x0F0F0F0F;
+ x = (x + (x >> 16));
+ return (x + (x >> 8)) & 0x0000003F;
+}
+
+inline int hamming(int x, int y) {
+ return popcount(x ^ y);
+}
+
+inline float float01(unsigned x) { // (0,1)
+ return 1.f / 8388608 * ((x >> 9) + .5f);
+}
+
+inline float numba(void) { // (-1,1)
+ return float01(rand32()) * 2 - 1;
+}
+
+template <typename T> void randomize(T *A, int n) {
+ for (int i = 0; i < n; ++i)
+ A[i] = numba();
+}
+
+template <typename T> void randomize(int m, int n, T *A, int lda) {
+ for (int j = 0; j < n; ++j)
+ for (int i = 0; i < m; ++i)
+ A[lda * j + i] = numba();
+}
+
+template <typename T, typename U> void broadcast(T *A, int n, U x) {
+ for (int i = 0; i < n; ++i)
+ A[i] = x;
+}
+
+template <typename T, typename U> void broadcast(int m, int n, T *A, int lda, U x) {
+ for (int j = 0; j < n; ++j)
+ for (int i = 0; i < m; ++i)
+ A[lda * j + i] = x;
+}
diff --git a/llamafile/sgemm_test.cpp b/llamafile/sgemm_sss_test.cpp
similarity index 70%
rename from llamafile/sgemm_test.cpp
rename to llamafile/sgemm_sss_test.cpp
index 0553f650..79c00839 100644
--- a/llamafile/sgemm_test.cpp
+++ b/llamafile/sgemm_sss_test.cpp
@@ -15,35 +15,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+#include "bench.h"
+#include "float.h"
#include "gemm.h"
#include "llama.cpp/ggml.h"
#include "llamafile.h"
+#include "numba.h"
-static int rand32(void) {
- static unsigned long long lcg = 1;
- lcg *= 6364136223846793005;
- lcg += 1442695040888963407;
- return lcg >> 32;
-}
-
-static float float01(unsigned x) { // (0,1)
- return 1.f / 8388608 * ((x >> 9) + .5f);
-}
-
-static float numba(void) { // (-1,1)
- return float01(rand32()) * 2 - 1;
-}
-
-template <typename T> void broadcast(T *A, int n, T x) {
- for (int i = 0; i < n; ++i)
- A[i] = x;
-}
-
-template <typename T> void randomize(int m, int n, T *A, int lda) {
- for (int j = 0; j < n; ++j)
- for (int i = 0; i < m; ++i)
- A[lda * j + i] = numba();
-}
+#define ITERATIONS 5
int main(int argc, char *argv[]) {
float tolerance = 2e-5;
@@ -65,21 +44,20 @@ int main(int argc, char *argv[]) {
randomize(k, m, A, lda);
randomize(k, n, B, ldb);
- gemm(true, false, m, n, k, 1., A, lda, B, ldb, 0., G, ldc);
- if (!llamafile_sgemm(m, n, k, A, lda, B, ldb, C, ldc, 0, 1, GGML_TASK_TYPE_COMPUTE,
- GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32))
- return 1;
+ BENCH(gemm(true, false, m, n, k, 1., A, lda, B, ldb, 0., G, ldc));
+ BENCH(llamafile_sgemm(m, n, k, A, lda, B, ldb, C, ldc, 0, 1, GGML_TASK_TYPE_COMPUTE,
+ GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32));
for (int i = 0; i < m; ++i)
for (int j = 0; j < n; ++j) {
auto g = G[ldc * j + i];
auto c = C[ldc * j + i];
- if (isnan(g)) {
+ if (flt::isnan(g)) {
fprintf(stderr, "%s:%d: found nan in reference matrix: i=%d j=%d\n", __FILE__,
__LINE__, i, j);
return 3;
}
- if (isnan(c)) {
+ if (flt::isnan(c)) {
fprintf(stderr, "%s:%d: found nan in output matrix: i=%d j=%d\n", __FILE__,
__LINE__, i, j);
return 4;
diff --git a/llamafile/sgemm_vecdot_test.cpp b/llamafile/sgemm_vecdot_test.cpp
new file mode 100644
index 00000000..908a8b11
--- /dev/null
+++ b/llamafile/sgemm_vecdot_test.cpp
@@ -0,0 +1,97 @@
+// -*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
+// vi: set et ft=c++ 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 "bench.h"
+#include "float.h"
+#include "gemm.h"
+#include "llama.cpp/ggml.h"
+#include "llamafile.h"
+#include "numba.h"
+
+#define ITERATIONS 30
+
+int main(int argc, char *argv[]) {
+ int m = 1025;
+ int n = 1;
+ int k = 32768;
+ int l = 3;
+ int lda = k + l;
+ int ldb = k + l;
+ int ldc = m + l;
+ float *A = new float[lda * m];
+ float *B = new float[ldb * n];
+ float *C = new float[ldc * n];
+ float *G = new float[ldc * n];
+ broadcast(A, lda * m, NAN);
+ broadcast(B, ldb * n, NAN);
+ broadcast(C, ldc * n, NAN);
+ broadcast(G, ldc * n, NAN);
+ randomize(k, m, A, lda);
+ randomize(k, n, B, ldb);
+
+ BENCH(gemm(true, false, m, n, k, 1., A, lda, B, ldb, 0., G, ldc));
+ BENCH(llamafile_sgemm(m, n, k, A, lda, B, ldb, C, ldc, 0, 1, GGML_TASK_TYPE_COMPUTE,
+ GGML_TYPE_F32, GGML_TYPE_F32, GGML_TYPE_F32));
+
+ double err_sum = 0;
+ long long err_worst = 0;
+ for (int i = 0; i < m; ++i)
+ for (int j = 0; j < n; ++j) {
+ float g = G[ldc * j + i];
+ float c = C[ldc * j + i];
+ if (flt::isnan(g)) {
+ fprintf(stderr, "%s:%err: found nan in reference matrix: i=%err j=%err\n", __FILE__,
+ __LINE__, i, j);
+ return 3;
+ }
+ if (flt::isnan(c)) {
+ fprintf(stderr, "%s:%err: found nan in output matrix: i=%err j=%err\n", __FILE__,
+ __LINE__, i, j);
+ return 4;
+ }
+ long long gi = flt::toint(g);
+ long long ci = flt::toint(c);
+ long long err = gi - ci;
+ if (err < 0)
+ err = -err;
+ err_sum += err;
+ if (err > err_worst)
+ err_worst = err;
+ }
+
+ double err_avg = err_sum / (m * n);
+ fprintf(stderr, "note: %g average ulp\n", err_avg);
+ fprintf(stderr, "note: %lld worst ulp\n", err_worst);
+
+ // using one accumulator
+ // 52.7393 average ulp
+ // 9052 worst ulp
+
+ // using three accumulators
+ // 22.291 average ulp
+ // 1566 worst ulp
+
+ if (err_avg > 30)
+ return 5;
+ if (err_worst > 3000)
+ return 6;
+
+ delete[] G;
+ delete[] C;
+ delete[] B;
+ delete[] A;
+}
diff --git a/llamafile/sgemmer.inc b/llamafile/sgemmer.inc
index 79a07471..c7a0f9c4 100644
--- a/llamafile/sgemmer.inc
+++ b/llamafile/sgemmer.inc
@@ -158,82 +158,6 @@ class SGEMMER {
END_KERNEL()
}
- dontinline void gemm5x4(int m0, int m, int n0, int n) {
- BEGIN_KERNEL(5, 4)
- V c00 = zero();
- V c01 = zero();
- V c02 = zero();
- V c03 = zero();
- V c10 = zero();
- V c11 = zero();
- V c12 = zero();
- V c13 = zero();
- V c20 = zero();
- V c21 = zero();
- V c22 = zero();
- V c23 = zero();
- V c30 = zero();
- V c31 = zero();
- V c32 = zero();
- V c33 = zero();
- V c40 = zero();
- V c41 = zero();
- V c42 = zero();
- V c43 = zero();
- for (int l = 0; l < k; l += KN) {
- V k0 = load(B + ldb * (j + 0) + l);
- V k1 = load(B + ldb * (j + 1) + l);
- V k2 = load(B + ldb * (j + 2) + l);
- V k3 = load(B + ldb * (j + 3) + l);
- V a0 = load(A + lda * (i + 0) + l);
- c00 = madd(a0, k0, c00);
- c01 = madd(a0, k1, c01);
- c02 = madd(a0, k2, c02);
- c03 = madd(a0, k3, c03);
- V a1 = load(A + lda * (i + 1) + l);
- c10 = madd(a1, k0, c10);
- c11 = madd(a1, k1, c11);
- c12 = madd(a1, k2, c12);
- c13 = madd(a1, k3, c13);
- V a2 = load(A + lda * (i + 2) + l);
- c20 = madd(a2, k0, c20);
- c21 = madd(a2, k1, c21);
- c22 = madd(a2, k2, c22);
- c23 = madd(a2, k3, c23);
- V a3 = load(A + lda * (i + 3) + l);
- c30 = madd(a3, k0, c30);
- c31 = madd(a3, k1, c31);
- c32 = madd(a3, k2, c32);
- c33 = madd(a3, k3, c33);
- V a4 = load(A + lda * (i + 4) + l);
- c40 = madd(a4, k0, c40);
- c41 = madd(a4, k1, c41);
- c42 = madd(a4, k2, c42);
- c43 = madd(a4, k3, c43);
- }
- C[ldc * (j + 0) + (i + 0)] = hsum(c00);
- C[ldc * (j + 0) + (i + 1)] = hsum(c10);
- C[ldc * (j + 0) + (i + 2)] = hsum(c20);
- C[ldc * (j + 0) + (i + 3)] = hsum(c30);
- C[ldc * (j + 0) + (i + 4)] = hsum(c40);
- C[ldc * (j + 1) + (i + 0)] = hsum(c01);
- C[ldc * (j + 1) + (i + 1)] = hsum(c11);
- C[ldc * (j + 1) + (i + 2)] = hsum(c21);
- C[ldc * (j + 1) + (i + 3)] = hsum(c31);
- C[ldc * (j + 1) + (i + 4)] = hsum(c41);
- C[ldc * (j + 2) + (i + 0)] = hsum(c02);
- C[ldc * (j + 2) + (i + 1)] = hsum(c12);
- C[ldc * (j + 2) + (i + 2)] = hsum(c22);
- C[ldc * (j + 2) + (i + 3)] = hsum(c32);
- C[ldc * (j + 2) + (i + 4)] = hsum(c42);
- C[ldc * (j + 3) + (i + 0)] = hsum(c03);
- C[ldc * (j + 3) + (i + 1)] = hsum(c13);
- C[ldc * (j + 3) + (i + 2)] = hsum(c23);
- C[ldc * (j + 3) + (i + 3)] = hsum(c33);
- C[ldc * (j + 3) + (i + 4)] = hsum(c43);
- END_KERNEL()
- }
-
dontinline void gemm3x4(int m0, int m, int n0, int n) {
BEGIN_KERNEL(3, 4)
V c00 = zero();
@@ -311,33 +235,78 @@ class SGEMMER {
dontinline void gemm4x1(int m0, int m, int n0, int n) {
BEGIN_KERNEL(4, 1)
V c00 = zero();
+ V c01 = zero();
+ V c02 = zero();
V c10 = zero();
+ V c11 = zero();
+ V c12 = zero();
V c20 = zero();
+ V c21 = zero();
+ V c22 = zero();
V c30 = zero();
- for (int l = 0; l < k; l += KN) {
+ V c31 = zero();
+ V c32 = zero();
+ int l = 0;
+ while (l + KN * 3 <= k) {
+ {
+ V k0 = load(B + ldb * (j + 0) + l);
+ c00 = madd(load(A + lda * (i + 0) + l), k0, c00);
+ c10 = madd(load(A + lda * (i + 1) + l), k0, c10);
+ c20 = madd(load(A + lda * (i + 2) + l), k0, c20);
+ c30 = madd(load(A + lda * (i + 3) + l), k0, c30);
+ }
+ l += KN;
+ {
+ V k0 = load(B + ldb * (j + 0) + l);
+ c01 = madd(load(A + lda * (i + 0) + l), k0, c01);
+ c11 = madd(load(A + lda * (i + 1) + l), k0, c11);
+ c21 = madd(load(A + lda * (i + 2) + l), k0, c21);
+ c31 = madd(load(A + lda * (i + 3) + l), k0, c31);
+ }
+ l += KN;
+ {
+ V k0 = load(B + ldb * (j + 0) + l);
+ c02 = madd(load(A + lda * (i + 0) + l), k0, c02);
+ c12 = madd(load(A + lda * (i + 1) + l), k0, c12);
+ c22 = madd(load(A + lda * (i + 2) + l), k0, c22);
+ c32 = madd(load(A + lda * (i + 3) + l), k0, c32);
+ }
+ l += KN;
+ }
+ for (; l < k; l += KN) {
V k0 = load(B + ldb * (j + 0) + l);
- V a0 = load(A + lda * (i + 0) + l);
- c00 = madd(a0, k0, c00);
- V a1 = load(A + lda * (i + 1) + l);
- c10 = madd(a1, k0, c10);
- V a2 = load(A + lda * (i + 2) + l);
- c20 = madd(a2, k0, c20);
- V a3 = load(A + lda * (i + 3) + l);
- c30 = madd(a3, k0, c30);
+ c00 = madd(load(A + lda * (i + 0) + l), k0, c00);
+ c10 = madd(load(A + lda * (i + 1) + l), k0, c10);
+ c20 = madd(load(A + lda * (i + 2) + l), k0, c20);
+ c30 = madd(load(A + lda * (i + 3) + l), k0, c30);
}
- C[ldc * (j + 0) + (i + 0)] = hsum(c00);
- C[ldc * (j + 0) + (i + 1)] = hsum(c10);
- C[ldc * (j + 0) + (i + 2)] = hsum(c20);
- C[ldc * (j + 0) + (i + 3)] = hsum(c30);
+ C[ldc * (j + 0) + (i + 0)] = hsum(c00) + hsum(c01) + hsum(c02);
+ C[ldc * (j + 0) + (i + 1)] = hsum(c10) + hsum(c11) + hsum(c12);
+ C[ldc * (j + 0) + (i + 2)] = hsum(c20) + hsum(c21) + hsum(c22);
+ C[ldc * (j + 0) + (i + 3)] = hsum(c30) + hsum(c31) + hsum(c32);
END_KERNEL()
}
dontinline void gemm1x1(int m0, int m, int n0, int n) {
BEGIN_KERNEL(1, 1)
- V c = zero();
- for (int l = 0; l < k; l += KN)
- c = madd(load(A + lda * i + l), load(B + ldb * j + l), c);
- C[ldc * j + i] = hsum(c);
+ V c0 = zero();
+ V c1 = zero();
+ V c2 = zero();
+ V c3 = zero();
+ int l = 0;
+ while (l + KN * 4 <= k) {
+ c0 = madd(load(A + lda * i + l), load(B + ldb * j + l), c0);
+ l += KN;
+ c1 = madd(load(A + lda * i + l), load(B + ldb * j + l), c1);
+ l += KN;
+ c2 = madd(load(A + lda * i + l), load(B + ldb * j + l), c2);
+ l += KN;
+ c3 = madd(load(A + lda * i + l), load(B + ldb * j + l), c3);
+ l += KN;
+ }
+ for (; l < k; l += KN)
+ c0 = madd(load(A + lda * i + l), load(B + ldb * j + l), c0);
+ C[ldc * j + i] = hsum(c0) + hsum(c1) + hsum(c2) + hsum(c3);
END_KERNEL()
}
diff --git a/llamafile/tester.cu b/llamafile/tester.cu
index f0f5c0f9..8f5f8974 100644
--- a/llamafile/tester.cu
+++ b/llamafile/tester.cu
@@ -19,15 +19,8 @@
#include <algorithm>
#include <atomic>
-#include <ctime>
#include <iostream>
-#ifndef _WIN32
-#include <unistd.h>
-#else
-#include <windows.h>
-#endif
-
#ifdef __HIP__
#define VENDOR "AMD"
#else
@@ -44,33 +37,6 @@ const int kPageSize =
4096;
#endif
-int rand32(void) {
- static unsigned long long lcg = 1;
- lcg *= 6364136223846793005;
- lcg += 1442695040888963407;
- return lcg >> 32;
-}
-
-float float01(unsigned x) { // (0,1)
- return 1.f / 8388608 * ((x >> 9) + .5f);
-}
-
-float numba(void) { // (-1,1)
- return float01(rand32()) * 2 - 1;
-}
-
-int hamming(int x, int y) {
- return popcount(x ^ y);
-}
-
-int popcount(unsigned x) {
- x = x - ((x >> 1) & 0x55555555);
- x = ((x >> 2) & 0x33333333) + (x & 0x33333333);
- x = (x + (x >> 4)) & 0x0F0F0F0F;
- x = (x + (x >> 16));
- return (x + (x >> 8)) & 0x0000003F;
-}
-
void cudaFreeOrDie(void *p) {
CUDA_OR_DIE(cudaFree(p));
}
@@ -81,31 +47,6 @@ void *cudaMallocManagedOrDie(size_t n) {
return p;
}
-#ifdef _WIN32
-static long long GetQueryPerformanceFrequency() {
- LARGE_INTEGER t;
- QueryPerformanceFrequency(&t);
- return t.QuadPart;
-}
-static long long GetQueryPerformanceCounter() {
- LARGE_INTEGER t;
- QueryPerformanceCounter(&t);
- return t.QuadPart;
-}
-#endif
-
-long long micros(void) {
-#ifndef _WIN32
- struct timespec ts;
- clock_gettime(CLOCK_REALTIME, &ts);
- return ts.tv_sec * 1000000 + (ts.tv_nsec + 999) / 1000;
-#else
- static long long timer_freq = GetQueryPerformanceFrequency();
- static long long timer_start = GetQueryPerformanceCounter();
- return ((GetQueryPerformanceCounter() - timer_start) * 1000000) / timer_freq;
-#endif
-}
-
[[noreturn]] void cuda_die(const char *stmt, const char *func, const char *file, int line,
const char *msg) {
int id = -1;
diff --git a/llamafile/tester.h b/llamafile/tester.h
index fef7f6a2..f8de5e08 100644
--- a/llamafile/tester.h
+++ b/llamafile/tester.h
@@ -17,8 +17,9 @@
#pragma once
#include "cuda.h"
-#include "flt.h"
+#include "float.h"
#include "gemm.h"
+#include "half.h"
#include "macros.h"
#include "naive.h"
#include "tinyblas.h"
@@ -72,11 +73,6 @@ extern const int kPageSize;
extern std::recursive_mutex g_log_lock;
extern thread_local const char *is_self_testing;
-float float01(unsigned);
-float numba(void);
-int hamming(int, int);
-int popcount(unsigned);
-int rand32(void);
long long micros(void);
void *cudaMallocManagedOrDie(size_t);
void cudaFreeOrDie(void *);
@@ -84,28 +80,6 @@ void show_cuda_device(int);
void show_cuda_devices();
void test_matmul(std::function<void(int, int, int, int, float, float)>);
-template <typename T> void randomize(T *A, int n) {
- for (int i = 0; i < n; ++i)
- A[i] = numba();
-}
-
-template <typename T> void randomize(int m, int n, T *A, int lda) {
- for (int j = 0; j < n; ++j)
- for (int i = 0; i < m; ++i)
- A[lda * j + i] = numba();
-}
-
-template <typename T, typename U> void broadcast(T *A, int n, U x) {
- for (int i = 0; i < n; ++i)
- A[i] = x;
-}
-
-template <typename T, typename U> void broadcast(int m, int n, T *A, int lda, U x) {
- for (int j = 0; j < n; ++j)
- for (int i = 0; i < m; ++i)
- A[lda * j + i] = x;
-}
-
template <typename T> struct cuda_memory {
const size_t size;
T *const p;
@@ -277,19 +251,6 @@ void check(double tol, //
#define CHECK(tol, m, n, k, A, lda, B, ldb) check(tol, m, n, k, A, lda, B, ldb, __FILE__, __LINE__)
-#define BENCH_CPU(x) \
- do { \
- x; \
- __asm__ volatile("" ::: "memory"); \
- long long start = micros(); \
- for (int i = 0; i < ITERATIONS; ++i) { \
- __asm__ volatile("" ::: "memory"); \
- x; \
- __asm__ volatile("" ::: "memory"); \
- } \
- printf("%8lld us %s\n", (micros() - start + ITERATIONS - 1) / ITERATIONS, #x); \
- } while (0)
-
#define CUDA_OR_DIE(x) \
do { \
cudaError_t err_ = (x); \
diff --git a/llamafile/tinyblas_test.cu b/llamafile/tinyblas_test.cu
index 75755b47..3f3d3774 100644
--- a/llamafile/tinyblas_test.cu
+++ b/llamafile/tinyblas_test.cu
@@ -15,6 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
+#include "numba.h"
#include "tester.h"
void checkTinyblasWorksHHHH() {