Distribute and run LLMs with a single file.
C++ cross-platformggufllama-cpplocal-ailocal-inferencelocal-llmopen-source-aisingle-file-executablespeech-to-text
Add armv8.2-a+dotprod kernel for q8_0
Justine Tunney6d8fa43bd0fb
diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.cindex bc5dd65b..3a51c987 100644--- a/llama.cpp/ggml.c+++ b/llama.cpp/ggml.c@@ -10716,24 +10716,28 @@ static void ggml_compute_forward_mul_mat(}#endif- bool can_use_tinyblas = src1_cont &&- ((ne2 == 1 && ne3 == 1) &&- (ne02 == 1 && ne03 == 1) &&- (ne12 == 1 && ne13 == 1));-- if (can_use_tinyblas &&- src0->type == GGML_TYPE_F16 &&- llamafile_sgemm(ne01, ne11, ne00,- src0->data, nb01/ggml_type_size(src0->type),- src1->data, nb11/ggml_type_size(src1->type),- dst->data, nb1/4,- ith, nth,- params->type,- src0->type,- src1->type,- dst->type)) {+ if (src1_cont && (src1->type == vec_dot_type ||+ src0->type == GGML_TYPE_F16)) {+ for (int64_t j = 0; j < ne13; j++)+ for (int64_t i = 0; i < ne12; i++)+ if (!llamafile_sgemm(ne01, ne11, ne00/ggml_blck_size(src0->type),+ (char *)src0->data + i/r2*nb02 + j/r3*nb03,+ nb01/ggml_type_size(src0->type),+ (char *)src1->data + i*nb12 + j*nb13,+ nb11/ggml_type_size(src1->type),+ (char *)dst->data + i*nb2 + j*nb3,+ nb1/ggml_type_size(dst->type),+ ith, nth,+ params->type,+ src0->type,+ src1->type,+ dst->type))+ goto UseGgmlGemm1;+ if (!ith && ne11 > 1 && params->type == GGML_TASK_TYPE_COMPUTE)+ llamafile_sgemm_was_used(ne01 * ne11 * ne00);return;}+UseGgmlGemm1:if (params->type == GGML_TASK_TYPE_INIT) {if (ith != 0) {@@ -10766,18 +10770,31 @@ static void ggml_compute_forward_mul_mat(const void * wdata = (src1->type == vec_dot_type) ? src1->data : params->wdata;const size_t row_size = ggml_row_size(vec_dot_type, ne10);- if (can_use_tinyblas &&- llamafile_sgemm(ne01, ne11, ne00,- src0->data, nb01/ggml_type_size(src0->type),- wdata, row_size/ggml_type_size(vec_dot_type),- dst->data, nb1/4,- ith, nth,- params->type,- src0->type,- vec_dot_type,- dst->type)) {+ if (src1_cont) {+ for (int64_t j = 0; j < ne13; j++)+ for (int64_t i = 0; i < ne12; i++)+ if (!llamafile_sgemm(ne01, ne11, ne00/ggml_blck_size(src0->type),+ (char *)src0->data + i/r2*nb02 + j/r3*nb03,+ nb01/ggml_type_size(src0->type),+ (char *)wdata + (nb12/ggml_type_size(src1->type)*ggml_type_size(vec_dot_type)*i ++ nb13/ggml_type_size(src1->type)*ggml_type_size(vec_dot_type)*j),+ row_size/ggml_type_size(vec_dot_type),+ (char *)dst->data + i*nb2 + j*nb3,+ nb1/ggml_type_size(dst->type),+ ith, nth,+ params->type,+ src0->type,+ vec_dot_type,+ dst->type))+ goto UseGgmlGemm2;+ if (!ith && ne11 > 1 && params->type == GGML_TASK_TYPE_COMPUTE)+ llamafile_sgemm_was_used(ne01 * ne11 * ne00);return;}+UseGgmlGemm2:++ if (!ith && ne11 > 1)+ llamafile_sgemm_was_not_used(ne01 * ne11 * ne00 * ne13 * ne12, src1_cont);const int64_t nr0 = ne01; // src0 rowsconst int64_t nr1 = ne1*ne12*ne13; // src1 rowsdiff --git a/llamafile/BUILD.mk b/llamafile/BUILD.mkindex d8901515..02e7a665 100644--- a/llamafile/BUILD.mk+++ b/llamafile/BUILD.mk@@ -52,6 +52,7 @@ o/$(MODE)/llamafile/sgemm_hhs_neon.o: private TARGET_ARCH += -Xaarch64-march=armo/$(MODE)/llamafile/sgemm_q0q0s_avx512vnni.o: private TARGET_ARCH += -Xx86_64-mavx512vl -Xx86_64-mavx512vnnio/$(MODE)/llamafile/sgemm_q0q0s_avxvnni.o: private TARGET_ARCH += -Xx86_64-mfma -Xx86_64-mavxvnnio/$(MODE)/llamafile/sgemm_q0q0s_fma.o: private TARGET_ARCH += -Xx86_64-mavx2 -Xx86_64-mfma+o/$(MODE)/llamafile/sgemm_q0q0s_dotprod.o: private TARGET_ARCH += -Xaarch64-march=armv8.2-a+dotprodo/$(MODE)/llamafile/sgemm_e0q0s_avx512vnni.o: private TARGET_ARCH += -Xx86_64-mavx512vl -Xx86_64-mavx512vnnio/$(MODE)/llamafile/sgemm_e0q0s_avxvnni.o: private TARGET_ARCH += -Xx86_64-mfma -Xx86_64-mavxvnnio/$(MODE)/llamafile/sgemm_e0q0s_fma.o: private TARGET_ARCH += -Xx86_64-mavx2 -Xx86_64-mfmadiff --git a/llamafile/hsum.h b/llamafile/hsum.hindex 75af50ff..7890ca17 100644--- a/llamafile/hsum.h+++ b/llamafile/hsum.h@@ -41,10 +41,3 @@ inline float hsum(__m512 x) {#endif // __AVX512F__#endif // __AVX__#endif // __SSE__--template <typename T> float hsums(const T *x, int n) {- float sum = 0;- for (int i = 0; i < n; ++i)- sum += hsum(x[i]);- return sum;-}diff --git a/llamafile/llamafile.h b/llamafile/llamafile.hindex 71694858..7bf4aab3 100644--- a/llamafile/llamafile.h+++ b/llamafile/llamafile.h@@ -51,6 +51,14 @@ const char *llamafile_describe_gpu(void);bool llamafile_sgemm(int, int, int, const void *, int, const void *, int, void *, int, int, int,int, int, int, int);+#if 0+void llamafile_sgemm_was_used(long long);+void llamafile_sgemm_was_not_used(long long, bool);+#else+#define llamafile_sgemm_was_used(x)+#define llamafile_sgemm_was_not_used(x, y)+#endif+#ifdef __cplusplus}#endifdiff --git a/llamafile/sgemm.cpp b/llamafile/sgemm.cppindex 91103a1a..90ccfc37 100644--- a/llamafile/sgemm.cpp+++ b/llamafile/sgemm.cpp@@ -115,18 +115,22 @@ bool llamafile_sgemm(int m, int n, int k, const void *A, int lda, const void *B,#ifdef __x86_64__if (!X86_HAVE(AVX))return false;- if (X86_HAVE(AVX512VL) && X86_HAVE(AVX512_VNNI) && !(k % 32))+ if (X86_HAVE(AVX512VL) && X86_HAVE(AVX512_VNNI))return llamafile_sgemm_q0q0s_avx512vnni(m, n, k, (const block_q8_0 *)A, lda,(const block_q8_0 *)B, ldb, (float *)C, ldc,ith, nth, task);- if (X86_HAVE(FMA) && X86_HAVE(AVXVNNI) && !(k % 32))+ if (X86_HAVE(FMA) && X86_HAVE(AVXVNNI))return llamafile_sgemm_q0q0s_avxvnni(m, n, k, (const block_q8_0 *)A, lda,(const block_q8_0 *)B, ldb, (float *)C, ldc, ith,nth, task);- if (X86_HAVE(FMA) && X86_HAVE(AVX2) && !(k % 32))+ if (X86_HAVE(FMA) && X86_HAVE(AVX2))return llamafile_sgemm_q0q0s_fma(m, n, k, (const block_q8_0 *)A, lda,(const block_q8_0 *)B, ldb, (float *)C, ldc, ith, nth,task);+#elif defined(__aarch64__)+ return llamafile_sgemm_q0q0s_dotprod(m, n, k, (const block_q8_0 *)A, lda,+ (const block_q8_0 *)B, ldb, (float *)C, ldc, ith, nth,+ task);#endifreturn false;@@ -136,15 +140,15 @@ bool llamafile_sgemm(int m, int n, int k, const void *A, int lda, const void *B,#ifdef __x86_64__if (!X86_HAVE(AVX))return false;- if (X86_HAVE(AVX512VL) && X86_HAVE(AVX512_VNNI) && !(k % 32))+ if (X86_HAVE(AVX512VL) && X86_HAVE(AVX512_VNNI))return llamafile_sgemm_e0q0s_avx512vnni(m, n, k, (const block_q4_0 *)A, lda,(const block_q8_0 *)B, ldb, (float *)C, ldc,ith, nth, task);- if (X86_HAVE(FMA) && X86_HAVE(AVXVNNI) && !(k % 32))+ if (X86_HAVE(FMA) && X86_HAVE(AVXVNNI))return llamafile_sgemm_e0q0s_avxvnni(m, n, k, (const block_q4_0 *)A, lda,(const block_q8_0 *)B, ldb, (float *)C, ldc, ith,nth, task);- if (X86_HAVE(FMA) && X86_HAVE(AVX2) && !(k % 32))+ if (X86_HAVE(FMA) && X86_HAVE(AVX2))return llamafile_sgemm_e0q0s_fma(m, n, k, (const block_q4_0 *)A, lda,(const block_q8_0 *)B, ldb, (float *)C, ldc, ith, nth,task);@@ -157,15 +161,15 @@ bool llamafile_sgemm(int m, int n, int k, const void *A, int lda, const void *B,#ifdef __x86_64__if (!X86_HAVE(AVX))return false;- if (X86_HAVE(AVX512VL) && X86_HAVE(AVX512_VNNI) && !(k % 32))+ if (X86_HAVE(AVX512VL) && X86_HAVE(AVX512_VNNI))return llamafile_sgemm_e1q1s_avx512vnni(m, n, k, (const block_q4_1 *)A, lda,(const block_q8_1 *)B, ldb, (float *)C, ldc,ith, nth, task);- if (X86_HAVE(FMA) && X86_HAVE(AVXVNNI) && !(k % 32))+ if (X86_HAVE(FMA) && X86_HAVE(AVXVNNI))return llamafile_sgemm_e1q1s_avxvnni(m, n, k, (const block_q4_1 *)A, lda,(const block_q8_1 *)B, ldb, (float *)C, ldc, ith,nth, task);- if (X86_HAVE(FMA) && X86_HAVE(AVX2) && !(k % 32))+ if (X86_HAVE(FMA) && X86_HAVE(AVX2))return llamafile_sgemm_e1q1s_fma(m, n, k, (const block_q4_1 *)A, lda,(const block_q8_1 *)B, ldb, (float *)C, ldc, ith, nth,task);diff --git a/llamafile/sgemm.h b/llamafile/sgemm.hindex 034989fc..b7b45f8d 100644--- a/llamafile/sgemm.h+++ b/llamafile/sgemm.h@@ -29,6 +29,8 @@ bool llamafile_sgemm_q0q0s_avxvnni(int, int, int, const block_q8_0 *, int, constfloat *, int, int, int, int);bool llamafile_sgemm_q0q0s_fma(int, int, int, const block_q8_0 *, int, const block_q8_0 *, int,float *, int, int, int, int);+bool llamafile_sgemm_q0q0s_dotprod(int, int, int, const block_q8_0 *, int, const block_q8_0 *, int,+ float *, int, int, int, int);bool llamafile_sgemm_e0q0s_avx512vnni(int, int, int, const block_q4_0 *, int, const block_q8_0 *,int, float *, int, int, int, int);diff --git a/llamafile/sgemm_accountant.cpp b/llamafile/sgemm_accountant.cppnew file mode 100644index 00000000..e4e2248d--- /dev/null+++ b/llamafile/sgemm_accountant.cpp@@ -0,0 +1,61 @@+// -*- 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 "llamafile.h"+#include "log.h"+#include <cassert>++namespace {++struct SgemmAccountant {++ long long wont;++ long long ggml_ops;+ long long llamafile_ops;++ long long ggml_flops;+ long long llamafile_flops;++ SgemmAccountant() = default;++ ~SgemmAccountant() {+ if (!ggml_ops && !llamafile_ops)+ return;+ double total_ops = ggml_ops + llamafile_ops;+ double total_flops = ggml_flops + llamafile_flops;+ tinylogf("llamafile_sgemm: successfully accelerated %g%% of flops and %g%% of ops on cpu"+ " (%lld cant %lld wont)\n",+ llamafile_ops / total_ops * 100, llamafile_flops / total_flops * 100,+ ggml_ops - wont, wont);+ }+};++struct SgemmAccountant g_sgemm_accountant;++} // namespace++void(llamafile_sgemm_was_used)(long long flops) {+ g_sgemm_accountant.llamafile_ops++;+ g_sgemm_accountant.llamafile_flops += flops;+}++void(llamafile_sgemm_was_not_used)(long long flops, bool wont) {+ g_sgemm_accountant.ggml_ops++;+ g_sgemm_accountant.ggml_flops += flops;+ g_sgemm_accountant.wont += wont;+}diff --git a/llamafile/sgemm_q0q0s_dotprod.cpp b/llamafile/sgemm_q0q0s_dotprod.cppnew file mode 100644index 00000000..184b70e6--- /dev/null+++ b/llamafile/sgemm_q0q0s_dotprod.cpp@@ -0,0 +1,183 @@+// -*- 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.++#ifdef __aarch64__++#include <arm_neon.h>++#include "llama.cpp/ggml-impl.h"+#include "llama.cpp/ggml.h"++#include "hsum.h"+#include "kernel.h"+#include "sgemm.h"++namespace {++class GEMMERQ0ARM {+ public:+ GEMMERQ0ARM(int k, const block_q8_0 *A, int lda, const block_q8_0 *B, int ldb, float *C,+ int ldc, int ith, int nth)+ : A(A), B(B), C(C), k(k), lda(lda), ldb(ldb), ldc(ldc), ith(ith), nth(nth) {+ }++ void matmul(int m, int n) {+ mnpack(0, m, 0, n);+ }++ private:+ dontinline void mnpack(int m0, int m, int n0, int n) {+ if (m - m0 <= 0 || n - n0 <= 0)+ return;+ int mc, nc, mp, np;+ if (m - m0 >= 3 && n - n0 >= 3) {+ mc = 3;+ nc = 3;+ gemm3x3(m0, m, n0, n);+ } else {+ mc = 1;+ nc = 1;+ gemm1x1(m0, m, n0, n);+ }+ mp = m0 + (m - m0) / mc * mc;+ np = n0 + (n - n0) / nc * nc;+ mnpack(mp, m, n0, np);+ mnpack(m0, mp, np, n);+ mnpack(mp, m, np, n);+ }++ dontinline void gemm3x3(int m0, int m, int n0, int n) {+ BEGIN_KERNEL(3, 3)+ int32x4_t zero = vdupq_n_s32(0);+ float32x4_t c00 = vdupq_n_f32(0.f);+ float32x4_t c01 = vdupq_n_f32(0.f);+ float32x4_t c02 = vdupq_n_f32(0.f);+ float32x4_t c10 = vdupq_n_f32(0.f);+ float32x4_t c11 = vdupq_n_f32(0.f);+ float32x4_t c12 = vdupq_n_f32(0.f);+ float32x4_t c20 = vdupq_n_f32(0.f);+ float32x4_t c21 = vdupq_n_f32(0.f);+ float32x4_t c22 = vdupq_n_f32(0.f);+ const block_q8_0 *Ap0 = A + lda * (i + 0);+ const block_q8_0 *Ap1 = A + lda * (i + 1);+ const block_q8_0 *Ap2 = A + lda * (i + 2);+ const block_q8_0 *Bp0 = B + ldb * (j + 0);+ const block_q8_0 *Bp1 = B + ldb * (j + 1);+ const block_q8_0 *Bp2 = B + ldb * (j + 2);+ for (int l = 0; l < k; ++l) {+ c00 = vmlaq_n_f32(+ c00,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap0[l].qs), vld1q_s8(Bp0[l].qs)),+ vld1q_s8(Ap0[l].qs + 16), vld1q_s8(Bp0[l].qs + 16))),+ unhalf(Ap0[l].d) * unhalf(Bp0[l].d));+ c01 = vmlaq_n_f32(+ c01,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap0[l].qs), vld1q_s8(Bp1[l].qs)),+ vld1q_s8(Ap0[l].qs + 16), vld1q_s8(Bp1[l].qs + 16))),+ unhalf(Ap0[l].d) * unhalf(Bp1[l].d));+ c02 = vmlaq_n_f32(+ c02,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap0[l].qs), vld1q_s8(Bp2[l].qs)),+ vld1q_s8(Ap0[l].qs + 16), vld1q_s8(Bp2[l].qs + 16))),+ unhalf(Ap0[l].d) * unhalf(Bp2[l].d));+ c10 = vmlaq_n_f32(+ c10,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap1[l].qs), vld1q_s8(Bp0[l].qs)),+ vld1q_s8(Ap1[l].qs + 16), vld1q_s8(Bp0[l].qs + 16))),+ unhalf(Ap1[l].d) * unhalf(Bp0[l].d));+ c11 = vmlaq_n_f32(+ c11,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap1[l].qs), vld1q_s8(Bp1[l].qs)),+ vld1q_s8(Ap1[l].qs + 16), vld1q_s8(Bp1[l].qs + 16))),+ unhalf(Ap1[l].d) * unhalf(Bp1[l].d));+ c12 = vmlaq_n_f32(+ c12,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap1[l].qs), vld1q_s8(Bp2[l].qs)),+ vld1q_s8(Ap1[l].qs + 16), vld1q_s8(Bp2[l].qs + 16))),+ unhalf(Ap1[l].d) * unhalf(Bp2[l].d));+ c20 = vmlaq_n_f32(+ c20,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap2[l].qs), vld1q_s8(Bp0[l].qs)),+ vld1q_s8(Ap2[l].qs + 16), vld1q_s8(Bp0[l].qs + 16))),+ unhalf(Ap2[l].d) * unhalf(Bp0[l].d));+ c21 = vmlaq_n_f32(+ c21,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap2[l].qs), vld1q_s8(Bp1[l].qs)),+ vld1q_s8(Ap2[l].qs + 16), vld1q_s8(Bp1[l].qs + 16))),+ unhalf(Ap2[l].d) * unhalf(Bp1[l].d));+ c22 = vmlaq_n_f32(+ c22,+ vcvtq_f32_s32(vdotq_s32(vdotq_s32(zero, vld1q_s8(Ap2[l].qs), vld1q_s8(Bp2[l].qs)),+ vld1q_s8(Ap2[l].qs + 16), vld1q_s8(Bp2[l].qs + 16))),+ unhalf(Ap2[l].d) * unhalf(Bp2[l].d));+ }+ 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 + 1) + (i + 0)] = hsum(c01);+ C[ldc * (j + 1) + (i + 1)] = hsum(c11);+ C[ldc * (j + 1) + (i + 2)] = hsum(c21);+ C[ldc * (j + 2) + (i + 0)] = hsum(c02);+ C[ldc * (j + 2) + (i + 1)] = hsum(c12);+ C[ldc * (j + 2) + (i + 2)] = hsum(c22);+ END_KERNEL()+ }++ dontinline void gemm1x1(int m0, int m, int n0, int n) {+ BEGIN_KERNEL(1, 1)+ float32x4_t acc = vdupq_n_f32(0.f);+ const block_q8_0 *Ap = A + lda * i;+ const block_q8_0 *Bp = B + ldb * j;+ for (int l = 0; l < k; ++l) {+ acc = vmlaq_n_f32(acc,+ vcvtq_f32_s32(vdotq_s32(+ vdotq_s32(vdupq_n_s32(0), vld1q_s8(Ap[l].qs), vld1q_s8(Bp[l].qs)),+ vld1q_s8(Ap[l].qs + 16), vld1q_s8(Bp[l].qs + 16))),+ unhalf(Ap[l].d) * unhalf(Bp[l].d));+ }+ C[ldc * j + i] = hsum(acc);+ END_KERNEL()+ }++ static inline float unhalf(unsigned short d) {+ return GGML_FP16_TO_FP32(d);+ }++ const block_q8_0 *const A;+ const block_q8_0 *const B;+ float *const C;+ const int k;+ const int lda;+ const int ldb;+ const int ldc;+ const int ith;+ const int nth;+};++} // namespace++bool llamafile_sgemm_q0q0s_dotprod(int m, int n, int k, const block_q8_0 *A, int lda,+ const block_q8_0 *B, int ldb, float *C, int ldc, int ith,+ int nth, int task) {+ if (task != GGML_TASK_TYPE_COMPUTE)+ return true;+ GEMMERQ0ARM tb{k, A, lda, B, ldb, C, ldc, ith, nth};+ tb.matmul(m, n);+ return true;+}++#endif // __aarch64__diff --git a/llamafile/sgemmer0.inc b/llamafile/sgemmer0.incindex 310c8962..0ff72d7b 100644--- a/llamafile/sgemmer0.inc+++ b/llamafile/sgemmer0.inc@@ -65,7 +65,6 @@ class SGEMMER0 {dontinline void gemm4x3(int m0, int m, int n0, int n) {BEGIN_KERNEL(4, 3)- int k2 = k / 32;__m256 c00 = _mm256_setzero_ps();__m256 c10 = _mm256_setzero_ps();__m256 c20 = _mm256_setzero_ps();@@ -85,7 +84,7 @@ class SGEMMER0 {const TB *Bp0 = B + ldb * (j + 0);const TB *Bp1 = B + ldb * (j + 1);const TB *Bp2 = B + ldb * (j + 2);- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {float da0 = unhalf(Ap0[l].d);float da1 = unhalf(Ap1[l].d);float da2 = unhalf(Ap2[l].d);@@ -157,7 +156,6 @@ class SGEMMER0 {dontinline void gemm4x1(int m0, int m, int n0, int n) {BEGIN_KERNEL(4, 1)- int k2 = k / 32;__m256 c0 = _mm256_setzero_ps();__m256 c1 = _mm256_setzero_ps();__m256 c2 = _mm256_setzero_ps();@@ -167,7 +165,7 @@ class SGEMMER0 {const TA *Ap2 = A + lda * (i + 2);const TA *Ap3 = A + lda * (i + 3);const TB *Bp = B + ldb * j;- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {float db0 = unhalf(Bp[l].d);__m256i f = load(Bp + l);__m256i u = _mm256_sign_epi8(f, f);@@ -201,7 +199,6 @@ class SGEMMER0 {dontinline void gemm1x4(int m0, int m, int n0, int n) {BEGIN_KERNEL(1, 4)- int k2 = k / 32;__m256 c0 = _mm256_setzero_ps();__m256 c1 = _mm256_setzero_ps();__m256 c2 = _mm256_setzero_ps();@@ -211,7 +208,7 @@ class SGEMMER0 {const TB *Bp2 = B + ldb * (j + 2);const TB *Bp3 = B + ldb * (j + 3);const TA *Ap = A + lda * i;- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {float da0 = unhalf(Ap[l].d);__m256i f = load(Ap + l);__m256i u = _mm256_sign_epi8(f, f);@@ -237,11 +234,10 @@ class SGEMMER0 {dontinline void gemm1x1(int m0, int m, int n0, int n) {BEGIN_KERNEL(1, 1)- int k2 = k / 32;__m256 c = _mm256_setzero_ps();const TA *Ap = A + lda * i;const TB *Bp = B + ldb * j;- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {__m256 d = _mm256_set1_ps(unhalf(Ap[l].d) * unhalf(Bp[l].d));__m256i e = load(Ap + l);__m256i f = load(Bp + l);diff --git a/llamafile/sgemmer1.inc b/llamafile/sgemmer1.incindex 92b79fe4..9c487dbc 100644--- a/llamafile/sgemmer1.inc+++ b/llamafile/sgemmer1.inc@@ -61,7 +61,6 @@ class SGEMMER1 {dontinline void gemm4x2(int m0, int m, int n0, int n) {BEGIN_KERNEL(4, 2)- int k2 = k / 32;__m128 s0 = _mm_setzero_ps();__m128 s1 = _mm_setzero_ps();__m256 c00 = _mm256_setzero_ps();@@ -78,7 +77,7 @@ class SGEMMER1 {const TA *Ap3 = A + lda * (i + 3);const TB *Bp0 = B + ldb * (j + 0);const TB *Bp1 = B + ldb * (j + 1);- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {__m128 sa = _mm_set_ps(unhalf(Ap3[l].dm >> 16), //unhalf(Ap2[l].dm >> 16), //unhalf(Ap1[l].dm >> 16), //@@ -119,7 +118,6 @@ class SGEMMER1 {dontinline void gemm4x1(int m0, int m, int n0, int n) {BEGIN_KERNEL(4, 1)- int k2 = k / 32;__m128 s = _mm_setzero_ps();__m256 c0 = _mm256_setzero_ps();__m256 c1 = _mm256_setzero_ps();@@ -130,7 +128,7 @@ class SGEMMER1 {const TA *Ap2 = A + lda * (i + 2);const TA *Ap3 = A + lda * (i + 3);const TB *Bp = B + ldb * j;- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {s = madd(_mm_set1_ps(unhalf(Bp[l].ds >> 16)),_mm_set_ps(unhalf(Ap3[l].dm >> 16), //unhalf(Ap2[l].dm >> 16), //@@ -157,12 +155,11 @@ class SGEMMER1 {dontinline void gemm1x1(int m0, int m, int n0, int n) {BEGIN_KERNEL(1, 1)- int k2 = k / 32;float s = 0;__m256 c = _mm256_setzero_ps();const TA *Ap = A + lda * i;const TB *Bp = B + ldb * j;- for (int l = 0; l < k2; ++l) {+ for (int l = 0; l < k; ++l) {unsigned dm = Ap[l].dm;unsigned ds = Bp[l].ds;__m256 d0 = _mm256_set1_ps(unhalf(dm));