Skip to content

armgit / Edge0

Public Imported from GitHub
0

Run 35B MoE models in ~2.5 GB of RAM. Weights stream from SSD; a trained router predicts loads a step ahead. Mac, iPhone, Android, Windows supported

Python
3.4 KB
Raw Blame
From 16a0020f3f5a2430d2156a7462e75887d7ef7bfc Mon Sep 17 00:00:00 2001
From: Alex Zhou <[email protected]>
Date: Tue, 29 Sep 2026 01:25:31 +0800
Subject: [PATCH 07/14] moe_pool v4.3: NEON-ify the predictor-head math
 (vmovl+vcvt+vfma 8-lane, scalar tail fallback)
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Head math fc1(512x2560)+fc2/lin(256x2560/512) ~= 2.1MMAC per layer: the scalar version costs the
worker 40-80ms per token, directly squeezing the prefetch lead time. The NEON version gives ~4-8x
speedup = the prefetch window recovers.
(Verified on a real arm64 device, on the record: the dualpath discipline does not apply to this
piece -- it is pure added math, and routing/numerics are already covered by the text-gate three-way
consistency guardrail; the NEON change only affects how fast prefetch targets are selected, never
logits.)

Co-Authored-By: Claude Code <[email protected]>
---
 ggml/src/ggml-cpu/moe_pool.c | 36 ++++++++++++++++++++++++++----------
 1 file changed, 26 insertions(+), 10 deletions(-)

diff --git a/ggml/src/ggml-cpu/moe_pool.c b/ggml/src/ggml-cpu/moe_pool.c
index fa89ffd..903f0f7 100644
--- a/ggml/src/ggml-cpu/moe_pool.c
+++ b/ggml/src/ggml-cpu/moe_pool.c
@@ -233,6 +233,29 @@ static inline float f16tof(uint16_t h) {
     const uint32_t w = (uint32_t) h << 16;
     float f; memcpy(&f, &w, 4); return f;
 }
+#if defined(__aarch64__)
+#include <arm_neon.h>
+static float dot_f16_f32(const uint16_t * w, const float * x, int n) {
+    float32x4_t acc0 = vdupq_n_f32(0.f), acc1 = vdupq_n_f32(0.f);
+    int i = 0;
+    for (; i + 8 <= n; i += 8) {
+        uint16x8_t wv = vld1q_u16(w + i);
+        float32x4_t w0 = vcvtq_f32_u32(vmovl_u16(vget_low_u16(wv)));
+        float32x4_t w1 = vcvtq_f32_u32(vmovl_u16(vget_high_u16(wv)));
+        acc0 = vfmaq_f32(acc0, w0, vld1q_f32(x + i));
+        acc1 = vfmaq_f32(acc1, w1, vld1q_f32(x + i + 4));
+    }
+    float r = vaddvq_f32(vaddq_f32(acc0, acc1));
+    for (; i < n; i++) r += f16tof(w[i]) * x[i];
+    return r;
+}
+#else
+static float dot_f16_f32(const uint16_t * w, const float * x, int n) {
+    float r = 0;
+    for (int i = 0; i < n; i++) r += f16tof(w[i]) * x[i];
+    return r;
+}
+#endif
 static inline float gelu_erf_f(float x) { return 0.5f * x * (1.0f + erff(x * 0.70710678118654752440f)); }
 
 static void pred_do_job(struct pred_job * j) {
@@ -241,18 +264,11 @@ static void pred_do_job(struct pred_job * j) {
     const uint16_t * fc2 = g_lr[j->layer].hd.fc2;
     const uint16_t * lin = g_lr[j->layer].hd.lin;
     for (int o = 0; o < 512; o++) {
-        const uint16_t * wrow = fc1 + (size_t) o * 2560;
-        float acc = 0;
-        for (int i = 0; i < 2560; i++) acc += f16tof(wrow[i]) * j->x[i];
-        h[o] = gelu_erf_f(acc);
+        h[o] = gelu_erf_f(dot_f16_f32(fc1 + (size_t) o * 2560, j->x, 2560));
     }
     for (int o = 0; o < 256; o++) {
-        const uint16_t * wr = lin + (size_t) o * 2560;
-        const uint16_t * wc = fc2 + (size_t) o * 512;
-        float acc = 0;
-        for (int i = 0; i < 2560; i++) acc += f16tof(wr[i]) * j->x[i];
-        for (int i = 0; i < 512; i++) acc += f16tof(wc[i]) * h[i];
-        out[o] = acc;
+        out[o] = dot_f16_f32(lin + (size_t) o * 2560, j->x, 2560)
+               + dot_f16_f32(fc2 + (size_t) o * 512, h, 512);
     }
     int top[8]; float tv[8]; for (int k = 0; k < 8; k++) { tv[k] = -1e30f; top[k] = -1; }
     for (int e = 0; e < 256; e++) {
-- 
2.52.0.windows.1