Skip to content

Distribute and run LLMs with a single file.

Make gemma2-27b-it the same as aistudio.google.com

Justine Tunneyaf226957b61e
diff --git a/llama.cpp/ggml-vector-amd-avx.c b/llama.cpp/ggml-vector-amd-avx.c
index 6c5a75fe..5309e530 100644
--- a/llama.cpp/ggml-vector-amd-avx.c
+++ b/llama.cpp/ggml-vector-amd-avx.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_amd_avx
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_amd_avx
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_amd_avx
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_amd_avx
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_amd_avx
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_amd_avx
#define ggml_vec_silu_f32 ggml_vec_silu_f32_amd_avx
diff --git a/llama.cpp/ggml-vector-amd-avx2.c b/llama.cpp/ggml-vector-amd-avx2.c
index 820d0dcb..547a7b1b 100644
--- a/llama.cpp/ggml-vector-amd-avx2.c
+++ b/llama.cpp/ggml-vector-amd-avx2.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_amd_avx2
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_amd_avx2
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_amd_avx2
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_amd_avx2
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_amd_avx2
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_amd_avx2
#define ggml_vec_silu_f32 ggml_vec_silu_f32_amd_avx2
diff --git a/llama.cpp/ggml-vector-amd-avx512.c b/llama.cpp/ggml-vector-amd-avx512.c
index 45afda41..4f07c007 100644
--- a/llama.cpp/ggml-vector-amd-avx512.c
+++ b/llama.cpp/ggml-vector-amd-avx512.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_amd_avx512
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_amd_avx512
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_amd_avx512
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_amd_avx512
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_amd_avx512
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_amd_avx512
#define ggml_vec_silu_f32 ggml_vec_silu_f32_amd_avx512
diff --git a/llama.cpp/ggml-vector-amd-avx512bf16.c b/llama.cpp/ggml-vector-amd-avx512bf16.c
index c636e716..f0278fc6 100644
--- a/llama.cpp/ggml-vector-amd-avx512bf16.c
+++ b/llama.cpp/ggml-vector-amd-avx512bf16.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_amd_avx512bf16
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_amd_avx512bf16
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_amd_avx512bf16
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_amd_avx512bf16
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_amd_avx512bf16
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_amd_avx512bf16
#define ggml_vec_silu_f32 ggml_vec_silu_f32_amd_avx512bf16
diff --git a/llama.cpp/ggml-vector-amd-f16c.c b/llama.cpp/ggml-vector-amd-f16c.c
index 331ce2d3..21cbc28a 100644
--- a/llama.cpp/ggml-vector-amd-f16c.c
+++ b/llama.cpp/ggml-vector-amd-f16c.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_amd_f16c
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_amd_f16c
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_amd_f16c
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_amd_f16c
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_amd_f16c
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_amd_f16c
#define ggml_vec_silu_f32 ggml_vec_silu_f32_amd_f16c
diff --git a/llama.cpp/ggml-vector-amd-fma.c b/llama.cpp/ggml-vector-amd-fma.c
index db0ec4de..0e3793e2 100644
--- a/llama.cpp/ggml-vector-amd-fma.c
+++ b/llama.cpp/ggml-vector-amd-fma.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_amd_fma
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_amd_fma
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_amd_fma
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_amd_fma
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_amd_fma
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_amd_fma
#define ggml_vec_silu_f32 ggml_vec_silu_f32_amd_fma
diff --git a/llama.cpp/ggml-vector-arm80.c b/llama.cpp/ggml-vector-arm80.c
index 03fa85f7..ecc1a178 100644
--- a/llama.cpp/ggml-vector-arm80.c
+++ b/llama.cpp/ggml-vector-arm80.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_arm80
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_arm80
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_arm80
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_arm80
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_arm80
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_arm80
#define ggml_vec_silu_f32 ggml_vec_silu_f32_arm80
diff --git a/llama.cpp/ggml-vector-arm82.c b/llama.cpp/ggml-vector-arm82.c
index 234d7fd3..cd0eba76 100644
--- a/llama.cpp/ggml-vector-arm82.c
+++ b/llama.cpp/ggml-vector-arm82.c
@@ -40,7 +40,6 @@
#define ggml_vec_leaky_relu_f32 ggml_vec_leaky_relu_f32_arm82
#define ggml_vec_hardswish_f32 ggml_vec_hardswish_f32_arm82
#define ggml_vec_hardsigmoid_f32 ggml_vec_hardsigmoid_f32_arm82
-#define ggml_vec_gelu_f16 ggml_vec_gelu_f16_arm82
#define ggml_vec_gelu_f32 ggml_vec_gelu_f32_arm82
#define ggml_vec_gelu_quick_f32 ggml_vec_gelu_quick_f32_arm82
#define ggml_vec_silu_f32 ggml_vec_silu_f32_arm82
diff --git a/llama.cpp/ggml-vector.cpp b/llama.cpp/ggml-vector.cpp
index 52c199c1..00dc9d58 100644
--- a/llama.cpp/ggml-vector.cpp
+++ b/llama.cpp/ggml-vector.cpp
@@ -372,15 +372,6 @@ extern "C" void ggml_vec_hardsigmoid_f32_amd_avx (const int n, float * y, const
extern "C" void ggml_vec_hardsigmoid_f32_arm82 (const int n, float * y, const float * x);
extern "C" void ggml_vec_hardsigmoid_f32_arm80 (const int n, float * y, const float * x);
-extern "C" void ggml_vec_gelu_f16_amd_avx512bf16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_amd_avx512(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_amd_avx2(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_amd_f16c(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_amd_fma(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_amd_avx(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_arm82(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-extern "C" void ggml_vec_gelu_f16_arm80(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
-
extern "C" void ggml_vec_gelu_f32_amd_avx512bf16(const int n, float * y, const float * x);
extern "C" void ggml_vec_gelu_f32_amd_avx512(const int n, float * y, const float * x);
extern "C" void ggml_vec_gelu_f32_amd_avx2(const int n, float * y, const float * x);
@@ -549,7 +540,6 @@ static const struct VectorFuncs {
typeof(ggml_vec_leaky_relu_f32) *ptr_ggml_vec_leaky_relu_f32;
typeof(ggml_vec_hardswish_f32) *ptr_ggml_vec_hardswish_f32;
typeof(ggml_vec_hardsigmoid_f32) *ptr_ggml_vec_hardsigmoid_f32;
- typeof(ggml_vec_gelu_f16) *ptr_ggml_vec_gelu_f16;
typeof(ggml_vec_gelu_f32) *ptr_ggml_vec_gelu_f32;
typeof(ggml_vec_gelu_quick_f32) *ptr_ggml_vec_gelu_quick_f32;
typeof(ggml_vec_silu_f32) *ptr_ggml_vec_silu_f32;
@@ -609,7 +599,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_amd_avx512bf16;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_amd_avx512bf16;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_amd_avx512bf16;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_amd_avx512bf16;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_amd_avx512bf16;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_amd_avx512bf16;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_amd_avx512bf16;
@@ -670,7 +659,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_amd_avx512;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_amd_avx512;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_amd_avx512;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_amd_avx512;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_amd_avx512;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_amd_avx512;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_amd_avx512;
@@ -731,7 +719,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_amd_avx2;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_amd_avx2;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_amd_avx2;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_amd_avx2;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_amd_avx2;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_amd_avx2;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_amd_avx2;
@@ -792,7 +779,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_amd_f16c;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_amd_f16c;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_amd_f16c;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_amd_f16c;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_amd_f16c;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_amd_f16c;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_amd_f16c;
@@ -853,7 +839,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_amd_fma;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_amd_fma;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_amd_fma;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_amd_fma;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_amd_fma;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_amd_fma;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_amd_fma;
@@ -914,7 +899,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_amd_avx;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_amd_avx;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_amd_avx;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_amd_avx;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_amd_avx;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_amd_avx;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_amd_avx;
@@ -975,7 +959,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_arm82;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_arm82;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_arm82;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_arm82;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_arm82;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_arm82;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_arm82;
@@ -1036,7 +1019,6 @@ static const struct VectorFuncs {
ptr_ggml_vec_leaky_relu_f32 = ggml_vec_leaky_relu_f32_arm80;
ptr_ggml_vec_hardswish_f32 = ggml_vec_hardswish_f32_arm80;
ptr_ggml_vec_hardsigmoid_f32 = ggml_vec_hardsigmoid_f32_arm80;
- ptr_ggml_vec_gelu_f16 = ggml_vec_gelu_f16_arm80;
ptr_ggml_vec_gelu_f32 = ggml_vec_gelu_f32_arm80;
ptr_ggml_vec_gelu_quick_f32 = ggml_vec_gelu_quick_f32_arm80;
ptr_ggml_vec_silu_f32 = ggml_vec_silu_f32_arm80;
@@ -1221,10 +1203,6 @@ void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x) {
return funcs.ptr_ggml_vec_hardsigmoid_f32(n, y, x);
}
-void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
- return funcs.ptr_ggml_vec_gelu_f16(n, y, x);
-}
-
void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
return funcs.ptr_ggml_vec_gelu_f32(n, y, x);
}
diff --git a/llama.cpp/ggml-vector.h b/llama.cpp/ggml-vector.h
index b986fa26..11215976 100644
--- a/llama.cpp/ggml-vector.h
+++ b/llama.cpp/ggml-vector.h
@@ -6,8 +6,6 @@ extern "C" {
typedef double ggml_float;
-extern float *ggml_table_gelu_f16;
-
void ggml_fp16_to_fp32_row(const ggml_fp16_t * x, float * y, int64_t n);
void ggml_fp32_to_fp16_row(const float * x, ggml_fp16_t * y, int64_t n);
void ggml_bf16_to_fp32_row(const ggml_bf16_t * x, float * y, int64_t n);
@@ -49,7 +47,6 @@ void ggml_vec_relu_f32 (const int n, float * y, const float * x);
void ggml_vec_leaky_relu_f32 (const int n, float * y, const float * x, const float ns);
void ggml_vec_hardswish_f32 (const int n, float * y, const float * x);
void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x);
-void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x);
void ggml_vec_gelu_f32(const int n, float * y, const float * x);
void ggml_vec_gelu_quick_f32(const int n, float * y, const float * x);
void ggml_vec_silu_f32(const int n, float * y, const float * x);
diff --git a/llama.cpp/ggml-vector.inc b/llama.cpp/ggml-vector.inc
index 854a8940..3bf68b48 100644
--- a/llama.cpp/ggml-vector.inc
+++ b/llama.cpp/ggml-vector.inc
@@ -1329,31 +1329,11 @@ void ggml_vec_hardsigmoid_f32 (const int n, float * y, const float * x) { for (i
// -7 -6 -5 -4 -3 -2 -1 0 1
//
-inline static float ggml_gelu_f32(float x) {
+static inline float ggml_gelu_f32(float x) {
// GeLU approximation that goes slower and we seem to be stuck with.
return .5f * x * (1.f + tanhf(sqrtf(M_2_PI) * (x + .044715f * x * x * x)));
}
-static void ggml_vec_gelu_f16_init(void) {
- ggml_table_gelu_f16 = malloc(sizeof(float) * 65536);
- for (int i = 0; i < 65536; ++i) {
- union {
- unsigned short i;
- ggml_fp16_t f;
- } u = {i};
- ggml_table_gelu_f16[i] = ggml_gelu_f32(u.f);
- }
-}
-
-void ggml_vec_gelu_f16(const int n, ggml_fp16_t * y, const ggml_fp16_t * x) {
- static atomic_uint once;
- ggml_once(&once, ggml_vec_gelu_f16_init);
- const uint16_t * i16 = (const uint16_t *) x;
- for (int i = 0; i < n; ++i) {
- y[i] = ggml_table_gelu_f16[i16[i]];
- }
-}
-
void ggml_vec_gelu_f32(const int n, float * y, const float * x) {
for (int i = 0; i < n; ++i) {
y[i] = ggml_gelu_f32(x[i]);
@@ -1424,32 +1404,27 @@ inline static __m512 ggml_v_expf(__m512 x) {
const __m512 r = _mm512_set1_ps(0x1.8p23f);
const __m512 z = _mm512_fmadd_ps(x, _mm512_set1_ps(0x1.715476p+0f), r);
const __m512 n = _mm512_sub_ps(z, r);
- const __m512 b = _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
- _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
- const __m512i e = _mm512_slli_epi32(_mm512_castps_si512(z), 23);
- const __m512 k = _mm512_castsi512_ps(_mm512_add_epi32(e, _mm512_castps_si512(_mm512_set1_ps(1))));
- const __mmask16 c = _mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(126), _CMP_GT_OQ);
- const __m512 u = _mm512_mul_ps(b, b);
- const __m512 j = _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
- _mm512_set1_ps(0x1.573e2ep-5f)), u,
- _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
- _mm512_set1_ps(0x1.fffdb6p-2f))),
- u, _mm512_mul_ps(_mm512_set1_ps(0x1.ffffecp-1f), b));
- if (_mm512_kortestz(c, c))
- return _mm512_fmadd_ps(j, k, k);
- const __m512i g = _mm512_and_si512(
- _mm512_movm_epi32(_mm512_cmp_ps_mask(n, _mm512_setzero_ps(), _CMP_LE_OQ)),
- _mm512_set1_epi32(0x82000000u));
- const __m512 s1 =
- _mm512_castsi512_ps(_mm512_add_epi32(g, _mm512_set1_epi32(0x7f000000u)));
- const __m512 s2 = _mm512_castsi512_ps(_mm512_sub_epi32(e, g));
+ const __m512 b =
+ _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.7f7d1cp-20f),
+ _mm512_fnmadd_ps(n, _mm512_set1_ps(0x1.62e4p-1f), x));
const __mmask16 d =
_mm512_cmp_ps_mask(_mm512_abs_ps(n), _mm512_set1_ps(192), _CMP_GT_OQ);
- return _mm512_mask_blend_ps(
- d, _mm512_mask_blend_ps(
- c, _mm512_fmadd_ps(k, j, k),
- _mm512_mul_ps(_mm512_fmadd_ps(s2, j, s2), s1)),
- _mm512_mul_ps(s1, s1));
+ const __m512 u = _mm512_mul_ps(b, b);
+ const __m512 j = _mm512_fmadd_ps(
+ _mm512_fmadd_ps(_mm512_fmadd_ps(_mm512_set1_ps(0x1.0e4020p-7f), b,
+ _mm512_set1_ps(0x1.573e2ep-5f)),
+ u,
+ _mm512_fmadd_ps(_mm512_set1_ps(0x1.555e66p-3f), b,
+ _mm512_set1_ps(0x1.fffdb6p-2f))),
+ u,
+ _mm512_fmadd_ps(_mm512_set1_ps(0x1.ffffecp-1f), b, _mm512_set1_ps(1.0F)));
+ const __m512 res = _mm512_scalef_ps(j, n);
+ if (_mm512_kortestz(d, d))
+ return res;
+ const __m512 zero = _mm512_setzero_ps();
+ const __m512 alt = _mm512_mask_blend_ps(
+ _mm512_cmp_ps_mask(n, zero, _CMP_LE_OQ), _mm512_set1_ps(INFINITY), zero);
+ return _mm512_mask_blend_ps(d, res, alt);
}
// computes silu x/(1+exp(-x)) in single precision vector
diff --git a/llama.cpp/ggml.c b/llama.cpp/ggml.c
index 5826a604..946f18cc 100644
--- a/llama.cpp/ggml.c
+++ b/llama.cpp/ggml.c
@@ -248,8 +248,6 @@ typedef double ggml_float;
// global data
//
-float *ggml_table_gelu_f16;
-
// precomputed f32 table for f16 (256 KB) (ggml-impl.h)
float ggml_table_f32_f16[1 << 16];
@@ -6035,38 +6033,6 @@ void ggml_flash_attn_ext_set_prec(
ggml_set_op_params_i32(a, 2, prec_i32); // scale is on first pos, max_bias on second
}
-// ggml_flash_ff
-
-struct ggml_tensor * ggml_flash_ff(
- struct ggml_context * ctx,
- struct ggml_tensor * a,
- struct ggml_tensor * b0,
- struct ggml_tensor * b1,
- struct ggml_tensor * c0,
- struct ggml_tensor * c1) {
- GGML_ASSERT(ggml_can_mul_mat(b0, a));
- // TODO: more checks
-
- bool is_node = false;
-
- if (a->grad || b0->grad || b1->grad || c0->grad || c1->grad) {
- is_node = true;
- }
-
- //struct ggml_tensor * result = ggml_dup_tensor(ctx, a);
- struct ggml_tensor * result = ggml_new_tensor(ctx, GGML_TYPE_F32, GGML_MAX_DIMS, a->ne);
-
- result->op = GGML_OP_FLASH_FF;
- result->grad = is_node ? ggml_dup_tensor(ctx, result) : NULL;
- result->src[0] = a;
- result->src[1] = b0;
- result->src[2] = b1;
- result->src[3] = c0;
- result->src[4] = c1;
-
- return result;
-}
-
// ggml_flash_attn_back
struct ggml_tensor * ggml_flash_attn_back(
@@ -15272,165 +15238,6 @@ static void ggml_compute_forward_flash_attn_ext(
}
}
-// ggml_compute_forward_flash_ff
-
-static void ggml_compute_forward_flash_ff_f16(
- const struct ggml_compute_params * params,
- struct ggml_tensor * dst) {
-
- const struct ggml_tensor * a = dst->src[0]; // F16
- const struct ggml_tensor * b0 = dst->src[1]; // F16 fc_w
- const struct ggml_tensor * b1 = dst->src[2]; // F32 fc_b
- const struct ggml_tensor * c0 = dst->src[3]; // F16 proj_w
- const struct ggml_tensor * c1 = dst->src[4]; // F32 proj_b
-
- int64_t t0 = ggml_perf_time_us();
- UNUSED(t0);
-
- GGML_TENSOR_LOCALS(int64_t, nea, a, ne)
- GGML_TENSOR_LOCALS(size_t, nba, a, nb)
- GGML_TENSOR_LOCALS(int64_t, neb0, b0, ne)
- GGML_TENSOR_LOCALS(size_t, nbb0, b0, nb)
- GGML_TENSOR_LOCALS(int64_t, neb1, b1, ne)
- GGML_TENSOR_LOCALS(size_t, nbb1, b1, nb)
- GGML_TENSOR_LOCALS(int64_t, nec0, c0, ne)
- GGML_TENSOR_LOCALS(size_t, nbc0, c0, nb)
- GGML_TENSOR_LOCALS(int64_t, nec1, c1, ne)
- GGML_TENSOR_LOCALS(size_t, nbc1, c1, nb)
- GGML_TENSOR_LOCALS(int64_t, ne, dst, ne)
- GGML_TENSOR_LOCALS(size_t, nb, dst, nb)
-
- const int ith = params->ith;
- const int nth = params->nth;
-
- const int64_t D = nea0;
- //const int64_t N = nea1;
- const int64_t M = neb01;
-
- GGML_ASSERT(ne0 == nea0);
- GGML_ASSERT(ne1 == nea1);
- GGML_ASSERT(ne2 == nea2);
-
- GGML_ASSERT(nba0 == sizeof(ggml_fp16_t));
- GGML_ASSERT(nbb00 == sizeof(ggml_fp16_t));
- GGML_ASSERT(nbb10 == sizeof(float));
- GGML_ASSERT(nbc00 == sizeof(ggml_fp16_t));
- GGML_ASSERT(nbc10 == sizeof(float));
-
- GGML_ASSERT(neb00 == D);
- GGML_ASSERT(neb01 == M);
- GGML_ASSERT(neb10 == M);
- GGML_ASSERT(neb11 == 1);
-
- GGML_ASSERT(nec00 == M);
- GGML_ASSERT(nec01 == D);
- GGML_ASSERT(nec10 == D);
- GGML_ASSERT(nec11 == 1);
-
- // dst cannot be transposed or permuted
- GGML_ASSERT(nb0 == sizeof(float));
- GGML_ASSERT(nb0 <= nb1);
- GGML_ASSERT(nb1 <= nb2);
- GGML_ASSERT(nb2 <= nb3);
-
- if (params->type == GGML_TASK_TYPE_INIT) {
- return;
- }
-
- if (params->type == GGML_TASK_TYPE_FINALIZE) {
- return;
- }
-
- // parallelize by a rows using ggml_vec_dot_f32
-
- // total rows in a
- const int nr = nea1*nea2*nea3;
-
- // rows per thread
- const int dr = (nr + nth - 1)/nth;
-
- // row range for this thread
- const int ir0 = dr*ith;
- const int ir1 = MIN(ir0 + dr, nr);
-
- for (int ir = ir0; ir < ir1; ++ir) {
- // a indices
- const int ia3 = ir/(nea2*nea1);
- const int ia2 = (ir - ia3*nea2*nea1)/nea1;
- const int ia1 = (ir - ia3*nea2*nea1 - ia2*nea1);
-
- float * S = (float *) params->wdata + ith*(2*M + CACHE_LINE_SIZE_F32);
-
- for (int64_t ic = 0; ic < neb01; ++ic) {
- // b0 indices
- const int ib03 = ia3;
- const int ib02 = ia2;
- const int ib01 = ic;
-
- // S indices
- const int i1 = ib01;
-
- ggml_vec_dot_f16(nea0,
- S + i1, 0,
- (ggml_fp16_t *) ((char *) b0->data + (ib01*nbb01 + ib02*nbb02 + ib03*nbb03)), 0,
- (ggml_fp16_t *) ((char *) a->data + ( ia1*nba1 + ia2*nba2 + ia3*nba3)), 0, 1);
- }
-
- ggml_vec_add_f32(neb01, S, S, (float *) b1->data);
- //ggml_vec_gelu_f32(neb01, S, S);
-
- ggml_fp16_t * S16 = (ggml_fp16_t *) ((float *) params->wdata + ith*(2*M + CACHE_LINE_SIZE_F32) + M);
-
- for (int64_t i = 0; i < M; i++) {
- S16[i] = GGML_FP32_TO_FP16(S[i]);
- }
-
- ggml_vec_gelu_f16(neb01, S16, S16);
-
- {
- // dst indices
- const int i1 = ia1;
- const int i2 = ia2;
- const int i3 = ia3;
-
- for (int64_t ic = 0; ic < nec01; ++ic) {
-
- ggml_vec_dot_f16(neb01,
- (float *) ((char *) dst->data + (ic*nb0 + i1*nb1 + i2*nb2 + i3*nb3)), 0,
- (ggml_fp16_t *) ((char *) c0->data + ( ic*nbc01 + i2*nbc02 + i3*nbc03)), 0,
- S16, 0, 1);
- }
-
- ggml_vec_add_f32(nec01,
- (float *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3)),
- (float *) ((char *) dst->data + (i1*nb1 + i2*nb2 + i3*nb3)),
- (float *) c1->data);
- }
- }
-}
-
-static void ggml_compute_forward_flash_ff(
- const struct ggml_compute_params * params,
- struct ggml_tensor * dst) {
-
- const struct ggml_tensor * b0 = dst->src[1];
-
- switch (b0->type) {
- case GGML_TYPE_F16:
- {
- ggml_compute_forward_flash_ff_f16(params, dst);
- } break;
- case GGML_TYPE_F32:
- {
- GGML_ASSERT(false); // TODO
- } break;
- default:
- {
- GGML_ASSERT(false);
- } break;
- }
-}
-
// ggml_compute_forward_flash_attn_back
static void ggml_compute_forward_flash_attn_back_f32(
@@ -17022,10 +16829,6 @@ static void ggml_compute_forward(struct ggml_compute_params * params, struct ggm
{
ggml_compute_forward_flash_attn_ext(params, tensor->src[0], tensor->src[1], tensor->src[2], tensor->src[3], tensor);
} break;
- case GGML_OP_FLASH_FF:
- {
- ggml_compute_forward_flash_ff(params, tensor);
- } break;
case GGML_OP_FLASH_ATTN_BACK:
{
int32_t t = ggml_get_op_params_i32(tensor, 0);
@@ -18095,10 +17898,6 @@ static void ggml_compute_backward(struct ggml_context * ctx, struct ggml_tensor
zero_table);
}
} break;
- case GGML_OP_FLASH_FF:
- {
- GGML_ASSERT(false); // not supported
- } break;
case GGML_OP_FLASH_ATTN_BACK:
{
GGML_ASSERT(false); // not supported
@@ -18791,10 +18590,6 @@ static int ggml_get_n_tasks(struct ggml_tensor * node, int n_threads, int n_cur_
{
n_tasks = n_threads;
} break;
- case GGML_OP_FLASH_FF:
- {
- n_tasks = n_threads;
- } break;
case GGML_OP_FLASH_ATTN_BACK:
{
n_tasks = n_threads;
@@ -19250,19 +19045,6 @@ struct ggml_cplan ggml_graph_plan(const struct ggml_cgraph * cgraph, int n_threa
cur = 3*sizeof(float)*ne00*n_tasks; // 3x head size/thread
} break;
- case GGML_OP_FLASH_FF:
- {
- if (node->src[1]->type == GGML_TYPE_F32) {
- cur = sizeof(float)*node->src[1]->ne[1]*n_tasks; // TODO: this can become (n_tasks-1)
- cur += sizeof(float)*node->src[1]->ne[1]*n_tasks; // this is overestimated by x2
- } else if (node->src[1]->type == GGML_TYPE_F16) {
- cur = sizeof(float)*node->src[1]->ne[1]*n_tasks; // TODO: this can become (n_tasks-1)
- cur += sizeof(float)*node->src[1]->ne[1]*n_tasks; // this is overestimated by x2
- } else if (node->src[1]->type == GGML_TYPE_BF16) {
- cur = sizeof(float)*node->src[1]->ne[1]*n_tasks; // TODO: this can become (n_tasks-1)
- cur += sizeof(float)*node->src[1]->ne[1]*n_tasks; // this is overestimated by x2
- }
- } break;
case GGML_OP_FLASH_ATTN_BACK:
{
const int64_t D = node->src[0]->ne[0];
diff --git a/llama.cpp/ggml.h b/llama.cpp/ggml.h
index b53902b3..996e1554 100644
--- a/llama.cpp/ggml.h
+++ b/llama.cpp/ggml.h
@@ -1801,14 +1801,6 @@ extern "C" {
struct ggml_tensor * d,
bool masked);
- GGML_API struct ggml_tensor * ggml_flash_ff(
- struct ggml_context * ctx,
- struct ggml_tensor * a,
- struct ggml_tensor * b0,
- struct ggml_tensor * b1,
- struct ggml_tensor * c0,
- struct ggml_tensor * c1);
-
GGML_API struct ggml_tensor * ggml_ssm_conv(
struct ggml_context * ctx,
struct ggml_tensor * s,
diff --git a/llama.cpp/llama.cpp b/llama.cpp/llama.cpp
index 3ee82ca1..6db75b3b 100644
--- a/llama.cpp/llama.cpp
+++ b/llama.cpp/llama.cpp
@@ -1808,6 +1808,14 @@ struct llama_control_vector {
return tensors[il];
}
+ struct ggml_tensor * apply_to(struct ggml_context * ctx, struct ggml_tensor * cur, int il) const {
+ ggml_tensor * layer_dir = tensor_for(il);
+ if (layer_dir != nullptr) {
+ cur = ggml_add(ctx, cur, layer_dir);
+ }
+ return cur;
+ }
+
~llama_control_vector() {
for (struct ggml_context * ctx : ctxs) {
ggml_free(ctx);
@@ -4015,8 +4023,8 @@ static void llm_load_hparams(
hparams.attn_soft_cap = true;
switch (hparams.n_layer) {
- case 18: model.type = e_model::MODEL_9B; break;
- case 28: model.type = e_model::MODEL_27B; break;
+ case 42: model.type = e_model::MODEL_9B; break;
+ case 46: model.type = e_model::MODEL_27B; break;
default: model.type = e_model::MODEL_UNKNOWN;
}
} break;
@@ -5713,7 +5721,7 @@ static bool llm_load_tensors(
layer.wk = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa});
layer.wv = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa});
layer.wo = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * hparams.n_head, n_embd});
- layer.attn_post_norm = ml.create_tensor(ctx_split, tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd});
+ layer.attn_post_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd});
layer.ffn_norm = ml.create_tensor(ctx_layer, tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd});
layer.ffn_gate = ml.create_tensor(ctx_split, tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff});
@@ -9987,11 +9995,6 @@ struct llm_build_context {
inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
}
- cur = llm_build_norm(ctx0, cur, hparams,
- model.layers[il].attn_post_norm, NULL,
- LLM_NORM_RMS, cb, il);
- cb(cur, "attn_post_norm", il);
-
struct ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
cb(sa_out, "sa_out", il);
@@ -10003,7 +10006,7 @@ struct llm_build_context {
// feed-forward network
{
cur = llm_build_ffn(ctx0, cur,
- model.layers[il].ffn_up, NULL,
+ model.layers[il].ffn_up, NULL,
model.layers[il].ffn_gate, NULL,
model.layers[il].ffn_down, NULL,
NULL,
@@ -10017,6 +10020,7 @@ struct llm_build_context {
cb(cur, "ffn_post_norm", -1);
cur = ggml_add(ctx0, cur, sa_out);
+ cur = lctx.cvec.apply_to(ctx0, cur, il);
cb(cur, "l_out", il);
// input for next layer
@@ -10032,13 +10036,14 @@ struct llm_build_context {
// lm_head
cur = ggml_mul_mat(ctx0, model.output, cur);
- cb(cur, "result_output", -1);
// final logit soft-capping
cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
cur = ggml_tanh(ctx0, cur);
cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
+ cb(cur, "result_output", -1);
+
ggml_build_forward_expand(gf, cur);
return gf;