Skip to content

Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦

C

quant: NEON rows16 fp4 kernels for aarch64

andres-gb104aaf5644ac85
diff --git a/c/deepseek_v4.c b/c/deepseek_v4.c
index de16212a..68c52af7 100644
--- a/c/deepseek_v4.c
+++ b/c/deepseek_v4.c
@@ -5198,13 +5198,13 @@ static void hot_fill_view(ColiTensorView *view,
const V4HotPolicy *policy,
const V4ExpertStoreState *state) {
fill_tensor_view(view, record, slot, matrix);
-#ifdef __AVX512F__
+#ifdef COLI_FP4_ROWS16_KERNEL
if (policy->packed[hot_slot_index(state, slot)]) view->block_rows = 16;
#endif
}
static int hot_pack_matrix(ColiTensorView *view, unsigned char *scratch) {
-#ifndef __AVX512F__
+#ifndef COLI_FP4_ROWS16_KERNEL
(void)view; (void)scratch; return -1;
#else
unsigned char *packed_scales = scratch + view->data_bytes;
@@ -5220,7 +5220,7 @@ static int hot_pack_slot_locked(V4HotPolicy *policy,
V4ExpertStoreState *state,
const V4ExpertRecord *record,
V4ExpertSlot *slot) {
-#ifndef __AVX512F__
+#ifndef COLI_FP4_ROWS16_KERNEL
(void)policy; (void)state; (void)record; (void)slot; return -1;
#else
size_t slot_index = hot_slot_index(state, slot);
@@ -5548,7 +5548,7 @@ int coli_v4_shared_expert_forward_ref(float *output,
int coli_v4_expert_forward_ref(float *output, const ColiExpertView *expert,
const float *input, float route_weight,
float swiglu_limit) {
-#ifndef __AVX512F__
+#ifndef COLI_FP4_ROWS16_KERNEL
return coli_v4_expert_forward_v17_fallback(
output, expert, input, route_weight, swiglu_limit);
#else
diff --git a/c/native_quant_fp4_rows16.c b/c/native_quant_fp4_rows16.c
index 5be408a5..f19a926f 100644
--- a/c/native_quant_fp4_rows16.c
+++ b/c/native_quant_fp4_rows16.c
@@ -2,6 +2,8 @@
#ifdef __AVX512F__
#include <immintrin.h>
+#elif defined(__aarch64__)
+#include <arm_neon.h>
#endif
#include <stdint.h>
#include <stdlib.h>
@@ -71,14 +73,74 @@ static __m512 decode_scales_rows16(const unsigned char *scales,
_mm_loadu_si128((const __m128i *)scales));
return _mm512_i32gather_ps(codes, e8, 4);
}
+#elif defined(__aarch64__)
+/* The 16 packed rows map onto four float32x4 accumulators. Per-row work is
+ * (activation * value) * scale added once per column, columns ascending —
+ * the exact operation sequence of the scalar reference and of the AVX-512
+ * kernel, so all three produce bit-identical rows. No fused multiply-add. */
+
+typedef struct NeonRows16Tables {
+ /* The 16-value E2M1 table is exactly 64 bytes, so one four-register TBL
+ * can gather whole floats: spread[group] replicates each of four code
+ * lanes into four byte positions and byte_offsets walks float bytes. */
+ uint8x16x4_t fp4_bytes;
+ uint8x16_t spread[4];
+ uint8x16_t byte_offsets;
+ float e8[256];
+} NeonRows16Tables;
+
+static void neon_rows16_tables(NeonRows16Tables *tables) {
+ float fp4[16];
+ for (int i = 0; i < 16; i++) fp4[i] = coli_e2m1_decode((uint8_t)i);
+ for (int i = 0; i < 256; i++) tables->e8[i] = coli_e8m0_decode((uint8_t)i);
+ const unsigned char *bytes = (const unsigned char *)fp4;
+ for (int group = 0; group < 4; group++)
+ tables->fp4_bytes.val[group] = vld1q_u8(bytes + 16 * group);
+ for (int group = 0; group < 4; group++) {
+ uint8_t lanes[16];
+ for (int byte = 0; byte < 16; byte++)
+ lanes[byte] = (uint8_t)(4 * group + byte / 4);
+ tables->spread[group] = vld1q_u8(lanes);
+ }
+ uint8_t offsets[16];
+ for (int byte = 0; byte < 16; byte++) offsets[byte] = (uint8_t)(byte % 4);
+ tables->byte_offsets = vld1q_u8(offsets);
+}
+
+static inline void neon_rows16_block_scales(float32x4_t scales[4],
+ const unsigned char *codes,
+ const NeonRows16Tables *tables) {
+ float decoded[16];
+ for (int lane = 0; lane < 16; lane++)
+ decoded[lane] = tables->e8[codes[lane]];
+ for (int group = 0; group < 4; group++)
+ scales[group] = vld1q_f32(decoded + 4 * group);
+}
+
+static inline void neon_rows16_accumulate(float32x4_t sums[4],
+ uint8x16_t codes, float activation,
+ const float32x4_t scales[4],
+ const NeonRows16Tables *tables) {
+ uint8x16_t byte_base = vshlq_n_u8(codes, 2);
+ float32x4_t x = vdupq_n_f32(activation);
+ for (int group = 0; group < 4; group++) {
+ uint8x16_t gather = vaddq_u8(
+ vqtbl1q_u8(byte_base, tables->spread[group]),
+ tables->byte_offsets);
+ float32x4_t values =
+ vreinterpretq_f32_u8(vqtbl4q_u8(tables->fp4_bytes, gather));
+ sums[group] = vaddq_f32(
+ sums[group], vmulq_f32(vmulq_f32(x, values), scales[group]));
+ }
+}
#endif
int coli_fp4_matvec_rows16_v10(float *output,
const ColiTensorView *weight,
const float *input) {
-#ifndef __AVX512F__
+#if !defined(COLI_FP4_ROWS16_KERNEL)
(void)output; (void)weight; (void)input; return -1;
-#else
+#elif defined(__AVX512F__)
if (!output || !input || !packed_valid(weight)) return -1;
size_t columns = (size_t)weight->columns;
size_t data_stride = columns / 2, scale_stride = columns / 32;
@@ -113,6 +175,48 @@ int coli_fp4_matvec_rows16_v10(float *output,
_mm512_storeu_ps(output + (size_t)tile * 16, sum);
}
free(activation_scales); free(activation); return 0;
+#else /* __aarch64__ */
+ if (!output || !input || !packed_valid(weight)) return -1;
+ size_t columns = (size_t)weight->columns;
+ size_t data_stride = columns / 2, scale_stride = columns / 32;
+ float *activation = malloc(columns * sizeof(*activation));
+ uint8_t *activation_scales = malloc(columns / 128);
+ if (!activation || !activation_scales) {
+ free(activation_scales); free(activation); return -1;
+ }
+ if (coli_fp8_activation_qdq_ref(activation, activation_scales,
+ input, columns, 128)) {
+ free(activation_scales); free(activation); return -1;
+ }
+ NeonRows16Tables tables;
+ neon_rows16_tables(&tables);
+ const unsigned char *data = weight->data, *scales = weight->scales;
+ #pragma omp parallel for schedule(static)
+ for (int64_t tile = 0; tile < weight->rows / 16; tile++) {
+ float32x4_t sums[4] = {vdupq_n_f32(0.0f), vdupq_n_f32(0.0f),
+ vdupq_n_f32(0.0f), vdupq_n_f32(0.0f)};
+ for (size_t base = 0; base < columns; base += 32) {
+ float32x4_t block_scales[4];
+ neon_rows16_block_scales(
+ block_scales,
+ scales + ((size_t)tile * scale_stride + base / 32) * 16,
+ &tables);
+ for (size_t offset = 0; offset < 32; offset += 2) {
+ const unsigned char *codes = data +
+ ((size_t)tile * data_stride + (base + offset) / 2) * 16;
+ uint8x16_t bytes = vld1q_u8(codes);
+ neon_rows16_accumulate(
+ sums, vandq_u8(bytes, vdupq_n_u8(15)),
+ activation[base + offset], block_scales, &tables);
+ neon_rows16_accumulate(
+ sums, vshrq_n_u8(bytes, 4),
+ activation[base + offset + 1], block_scales, &tables);
+ }
+ }
+ for (int group = 0; group < 4; group++)
+ vst1q_f32(output + (size_t)tile * 16 + 4 * group, sums[group]);
+ }
+ free(activation_scales); free(activation); return 0;
#endif
}
@@ -120,9 +224,9 @@ int coli_fp4_dual_matvec_rows16_v10(float *output_a, float *output_b,
const ColiTensorView *a,
const ColiTensorView *b,
const float *input) {
-#ifndef __AVX512F__
+#if !defined(COLI_FP4_ROWS16_KERNEL)
(void)output_a; (void)output_b; (void)a; (void)b; (void)input; return -1;
-#else
+#elif defined(__AVX512F__)
if (!output_a || !output_b || !input || !packed_valid(a) ||
!packed_valid(b) || a->rows != b->rows ||
a->columns != b->columns) return -1;
@@ -169,5 +273,65 @@ int coli_fp4_dual_matvec_rows16_v10(float *output_a, float *output_b,
_mm512_storeu_ps(output_b + (size_t)tile * 16, sum_b);
}
free(activation_scales); free(activation); return 0;
+#else /* __aarch64__ */
+ if (!output_a || !output_b || !input || !packed_valid(a) ||
+ !packed_valid(b) || a->rows != b->rows ||
+ a->columns != b->columns) return -1;
+ size_t columns = (size_t)a->columns;
+ size_t data_stride = columns / 2, scale_stride = columns / 32;
+ float *activation = malloc(columns * sizeof(*activation));
+ uint8_t *activation_scales = malloc(columns / 128);
+ if (!activation || !activation_scales) {
+ free(activation_scales); free(activation); return -1;
+ }
+ if (coli_fp8_activation_qdq_ref(activation, activation_scales,
+ input, columns, 128)) {
+ free(activation_scales); free(activation); return -1;
+ }
+ NeonRows16Tables tables;
+ neon_rows16_tables(&tables);
+ const unsigned char *data_a = a->data, *data_b = b->data;
+ const unsigned char *scales_a = a->scales, *scales_b = b->scales;
+ #pragma omp parallel for schedule(static)
+ for (int64_t tile = 0; tile < a->rows / 16; tile++) {
+ float32x4_t sums_a[4] = {vdupq_n_f32(0.0f), vdupq_n_f32(0.0f),
+ vdupq_n_f32(0.0f), vdupq_n_f32(0.0f)};
+ float32x4_t sums_b[4] = {vdupq_n_f32(0.0f), vdupq_n_f32(0.0f),
+ vdupq_n_f32(0.0f), vdupq_n_f32(0.0f)};
+ for (size_t base = 0; base < columns; base += 32) {
+ size_t scale_offset =
+ ((size_t)tile * scale_stride + base / 32) * 16;
+ float32x4_t block_scales_a[4], block_scales_b[4];
+ neon_rows16_block_scales(block_scales_a, scales_a + scale_offset,
+ &tables);
+ neon_rows16_block_scales(block_scales_b, scales_b + scale_offset,
+ &tables);
+ for (size_t offset = 0; offset < 32; offset += 2) {
+ size_t packed_offset =
+ ((size_t)tile * data_stride + (base + offset) / 2) * 16;
+ uint8x16_t bytes_a = vld1q_u8(data_a + packed_offset);
+ uint8x16_t bytes_b = vld1q_u8(data_b + packed_offset);
+ neon_rows16_accumulate(
+ sums_a, vandq_u8(bytes_a, vdupq_n_u8(15)),
+ activation[base + offset], block_scales_a, &tables);
+ neon_rows16_accumulate(
+ sums_b, vandq_u8(bytes_b, vdupq_n_u8(15)),
+ activation[base + offset], block_scales_b, &tables);
+ neon_rows16_accumulate(
+ sums_a, vshrq_n_u8(bytes_a, 4),
+ activation[base + offset + 1], block_scales_a, &tables);
+ neon_rows16_accumulate(
+ sums_b, vshrq_n_u8(bytes_b, 4),
+ activation[base + offset + 1], block_scales_b, &tables);
+ }
+ }
+ for (int group = 0; group < 4; group++) {
+ vst1q_f32(output_a + (size_t)tile * 16 + 4 * group,
+ sums_a[group]);
+ vst1q_f32(output_b + (size_t)tile * 16 + 4 * group,
+ sums_b[group]);
+ }
+ }
+ free(activation_scales); free(activation); return 0;
#endif
}
diff --git a/c/native_quant_fp4_rows16.h b/c/native_quant_fp4_rows16.h
index 2455eacb..dbe855b1 100644
--- a/c/native_quant_fp4_rows16.h
+++ b/c/native_quant_fp4_rows16.h
@@ -3,6 +3,13 @@
#include "native_quant.h"
+/* Targets with a rows16 fast kernel: the matvec entry points below return 0
+ * and the engine may pack hot experts into the 16-row interleaved layout.
+ * Elsewhere they return -1 and callers keep the flat reference path. */
+#if defined(__AVX512F__) || defined(__aarch64__)
+#define COLI_FP4_ROWS16_KERNEL 1
+#endif
+
int coli_fp4_pack_rows16_v10(unsigned char *packed_data,
unsigned char *packed_scales,
const ColiTensorView *source);