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

Merge branch 'dev' into sse41-tier-upstream

Jaapjan Tinbergen558db57d4f34
diff --cc c/Makefile
index d0b5a68a,2bacd221..569e9897
--- a/c/Makefile
+++ b/c/Makefile
@@@ -471,16 -471,13 +471,18 @@@ endi
TEST_RULES := $(shell sed -n 's|^tests/\(test_[a-z0-9_]*\)\$$(EXE):.*|\1|p' $(firstword $(MAKEFILE_LIST)))
# test_uring is Linux-only. V4 engine tests are appended below only on supported
# x86-64 Linux/Windows and aarch64 Linux hosts; the V4 infrastructure tests have
-# unconditional rules and therefore remain portable gates.
+# unconditional rules and therefore remain portable gates. test_gsgemv_sse41/
+# test_qgemv_sse41 force -msse4.1/-mno-avx2/-mno-fma, which only exist as gcc/
+# clang flags on x86 -- unconditionally in TEST_BINS they take down `check` on
+# arm64 (e.g. macOS): "unsupported option '-msse4.1' for target arm64-...".
+# Appended back below only on x86-64 hosts, alongside the other conditional
+# platform tests already handled the same way.
+ # test_qwen38_tier_engine drives qwen38.c over the generated FP8 fixture
+ # (qwen38_tiny_fp8, gitignored); it runs from qwen38-tier-engine-check.
TEST_EXCLUDE = test_uring test_deepseek_v4 test_v4_ownership test_v4_serve_framing \
test_segment_adapters_registration test_segment_adapters_real \
test_edge_adapters_registration test_edge_adapters_real \
- test_gsgemv_sse41 test_qgemv_sse41
- test_qwen38_tier_engine
++ test_gsgemv_sse41 test_qgemv_sse41 test_qwen38_tier_engine
TEST_BINS = $(addprefix tests/,$(addsuffix $(EXE),$(filter-out $(TEST_EXCLUDE),$(TEST_RULES))))
ifneq (,$(LINUX))
TEST_BINS += tests/test_uring$(EXE)
@@@ -1013,7 -1046,7 +1054,7 @@@ fp8-bench: fp8_bench$(EXE
# the GPU sits idle. Same guard #783 put on kimi_k3, which since gaining an
# MXFP4 expert path no longer needs it. tests/test_makefile_cuda_scope.py
# asserts this shape.
- olmoe$(EXE): olmoe.c cli_args.h st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h route_trace.h serve_codec.h edge_adapters.h edge_runtime.h edge_tok_internal.h fused_simd.h segment_adapter_internal.h segment_adapters.h segment_runtime.h sse41_kernels.h
-olmoe$(EXE): olmoe.c cli_args.h st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h kv_prefix.h route_trace.h serve_codec.h edge_adapters.h edge_runtime.h edge_tok_internal.h fused_simd.h segment_adapter_internal.h segment_adapters.h segment_runtime.h
++olmoe$(EXE): olmoe.c cli_args.h st.h json.h compat.h sample.h tok.h tok_unicode.h tok_unicode_o200k.h omp_tune.h kv_prefix.h route_trace.h serve_codec.h edge_adapters.h edge_runtime.h edge_tok_internal.h fused_simd.h segment_adapter_internal.h segment_adapters.h segment_runtime.h sse41_kernels.h
$(CC) $(NOCUDA_CFLAGS) olmoe.c -o olmoe$(EXE) $(NOCUDA_LDFLAGS)
# Qwen3.6-35B-A3B engine (hybrid Gated Attention + Gated DeltaNet + streaming
@@@ -1035,14 -1072,22 +1080,22 @@@ QWEN36_TIER_SRC
QWEN36_CFLAGS = $(NOCUDA_CFLAGS)
QWEN36_LDFLAGS = $(NOCUDA_LDFLAGS)
endif
- qwen36$(EXE): qwen36.c serve_poll.h cli_args.h qwen36_tier.h st.h json.h compat.h edge_adapter_internal.h edge_adapters.h edge_runtime.h segment_adapter_internal.h segment_adapters.h segment_runtime.h gsgemv.h qgemv.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
-qwen36$(EXE): qwen36.c serve_poll.h cli_args.h qwen36_tier.h expert_ffn.h st.h json.h compat.h omp_tune.h kv_prefix.h edge_adapter_internal.h edge_adapters.h edge_runtime.h segment_adapter_internal.h segment_adapters.h segment_runtime.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
++qwen36$(EXE): qwen36.c serve_poll.h cli_args.h qwen36_tier.h expert_ffn.h st.h json.h compat.h omp_tune.h kv_prefix.h edge_adapter_internal.h edge_adapters.h edge_runtime.h segment_adapter_internal.h segment_adapters.h segment_runtime.h gsgemv.h qgemv.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) qwen36.c $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o qwen36$(EXE) $(QWEN36_LDFLAGS)
- # Qwen3.8-Flash-Next text-only sibling. This target is intentionally CPU-only;
- # qwen38.c owns the direct checkpoint loader and SERVE=1 protocol, with no
- # CUDA/Metal tier advertised by the control plane.
- qwen38$(EXE): qwen38.c cli_args.h qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h quant.h route_trace.h tok.h tok_unicode.h tok_unicode_o200k.h serve_codec.h edge_adapter_internal.h edge_adapters.h edge_runtime.h qwen38_vision.h segment_adapter_internal.h segment_adapters.h segment_runtime.h
- $(CC) $(NOCUDA_CFLAGS) qwen38.c -o qwen38$(EXE) $(NOCUDA_LDFLAGS)
+ # DeepSeek V4.1 Flash: one file, like every other portable engine. The fp4 experts
+ # stream from the official checkpoint and quant.h's mxfp4 kernel reads them as they
+ # are, so there is no conversion target here to go with it.
+ deepseek_v41$(EXE): deepseek_v41.c cli_args.h st.h json.h tok.h tok_unicode.h compat.h omp_tune.h kv_prefix.h quant.h \
+ sparse_attn.h hyper_connections.h serve_codec.h serve_poll.h .build-config
+ $(CC) $(CFLAGS) deepseek_v41.c -o deepseek_v41$(EXE) $(LDFLAGS)
+
+ # Qwen3.8-Flash-Next text-only sibling. qwen38.c owns the direct checkpoint
+ # loader and SERVE=1 protocol. With CUDA=1 it links the same expert tier as
+ # qwen36 (fp8 streaming mode: hot experts get VRAM copies, the RAM LRU stays);
+ # without it the tier header's inline stubs keep the build toolkit-free.
+ qwen38$(EXE): qwen38.c cli_args.h qwen38_core.h qwen38_nfc.h qwen38_nfc_tables.h st.h json.h compat.h omp_tune.h quant.h route_trace.h tok.h tok_unicode.h tok_unicode_o200k.h serve_codec.h edge_adapter_internal.h edge_adapters.h edge_runtime.h qwen38_vision.h segment_adapter_internal.h segment_adapters.h segment_runtime.h qwen36_tier.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
+ $(CC) $(QWEN36_CFLAGS) qwen38.c $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o qwen38$(EXE) $(QWEN36_LDFLAGS)
.PHONY: qwen38-tiny-generate qwen38-tiny-check
qwen38-tiny-generate:
diff --cc c/qwen36.c
index 98c0bd49,11794867..93e6734d
--- a/c/qwen36.c
+++ b/c/qwen36.c
@@@ -875,11 -968,101 +894,62 @@@ static void matmul_q_batch(float *y, co
}
/* Group-scaled int8 GEMV: one f32 scale per `gs` input elements per row
- * (gs64 expert containers). Row layout of `scale`: [O][I/gs] row-major. */
+ * (gs64 expert containers). Row layout of `scale`: [O][I/gs] row-major.
+ * matmul_q_gs lives in gsgemv.h so tests/test_gsgemv.c can link the exact
+ * kernel the engine runs. */
static int g_expert_gs = 0; /* set from qwen36_meta.json (expert_gs) at load */
+#include "gsgemv.h"
++
+ /* 1 = expert container packs int4 (tier fmt=4); 0 = int8 per-row (tier fmt=1).
+ * Same signal main's nbytes probe and tier_warmstart receive; the decode path
+ * needs it to offer int8 experts (#1391): on an int8 container e->g4 is NULL. */
+ static int g_expert_is_int4 = 1;
+
+ /* Shared expert kernel (expert_ffn.h): routed experts stay planar int4 in
+ * RAM and a layer runs as (expert, row-chunk) items. On by default for an
+ * int4 gs=64 container whose widths are multiples of 64, off under the CUDA
+ * expert tier (it uploads the pair-layout int4 and computes misses from the
+ * int8 copy) and with QWEN_EXPERT_KERNEL=0, which keeps the historical
+ * unpack-to-int8 path for A/Bs. Decided once from the container itself. */
+ static int container_layer_is_int4(Model *m, int layer);
+ static int xf_mode(Model *m) {
+ static int v = -1;
+ if (v >= 0) return v;
+ const char *e = getenv("QWEN_EXPERT_KERNEL");
+ int on = !(e && *e == '0');
+ #ifdef COLI_CUDA
+ { const char *cu = getenv("COLI_CUDA"); if (cu && *cu == '1') on = 0; }
+ #endif
+ Cfg *c = &m->c;
+ if (c->expert_gs != XF_BLOCK || !xf_layout_ok(c->hidden) || !xf_layout_ok(c->inter)) on = 0;
+ if (on) {
+ int probe = -1;
+ for (int l = 0; l < c->n_layers && probe < 0; l++) {
+ char nm[256];
+ snprintf(nm, sizeof(nm), "model.layers.%d.mlp.experts.0.merged_weight", m->active_of[l]);
+ if (st_find(&m->S, nm)) probe = container_layer_is_int4(m, m->active_of[l]);
+ }
+ if (probe != 1) on = 0;
+ }
+ v = on;
+ if (v) fprintf(stderr, "[qwen36] expert kernel: planar int4 in RAM (expert_ffn.h), QWEN_EXPERT_KERNEL=0 restores int8 unpack\n");
+ return v;
+ }
+
+ /* The single offer decision the decode path makes for a routed expert: offer
+ * whichever format the container actually packed, exactly what tier_warmstart
+ * does (int4 → the packed g4/u4/d4, int8 → the live RAM weights, tier fmt=1
+ * since #1334). moe()'s resident offer, the pilot-prefetch lookahead, and the
+ * #1391 test all call THIS function, so the gate can't drift between them.
+ * Read-only: never frees, never rewrites -- ownership stays in warmstart
+ * (#1341). */
+ static void tier_offer_slot(int layer, int eid, const Slot *s) {
+ if (s->g4)
+ qt_note(layer, eid, s->g4, s->u4, s->d4, s->gs, s->us, s->ds);
+ else if (!g_expert_is_int4 && s->g)
+ qt_note(layer, eid, (const uint8_t *)s->g, (const uint8_t *)s->u,
+ (const uint8_t *)s->d, s->gs, s->us, s->ds);
+ }
-static void matmul_q_gs(float *y, const float *x, const int8_t *q, const float *scale,
- int I, int O, int gs) {
- int ng = (I + gs - 1) / gs;
-#if defined(__AVX2__) && defined(__FMA__)
- if ((gs & 31) == 0) {
- #pragma omp parallel for schedule(static) if(O >= 256)
- for (int o = 0; o < O; o++) {
- const int8_t *w = q + (int64_t)o * I;
- const float *sc = scale + (int64_t)o * ng;
- float acc = 0.f;
- for (int gi = 0; gi < ng; gi++) {
- __m256 a0 = _mm256_setzero_ps(), a1 = _mm256_setzero_ps();
- int base = gi * gs, end = base + gs; if (end > I) end = I;
- for (int i = base; i + 16 <= end; i += 16) {
- __m128i b0 = _mm_loadu_si128((const __m128i*)(w + i));
- a0 = _mm256_fmadd_ps(_mm256_loadu_ps(x+i), _mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(b0)), a0);
- a1 = _mm256_fmadd_ps(_mm256_loadu_ps(x+i+8), _mm256_cvtepi32_ps(_mm256_cvtepi8_epi32(_mm_srli_si128(b0,8))), a1);
- }
- a0 = _mm256_add_ps(a0, a1);
- __m128 s = _mm_add_ps(_mm256_castps256_ps128(a0), _mm256_extractf128_ps(a0,1));
- s = _mm_add_ps(s, _mm_movehl_ps(s,s));
- s = _mm_add_ss(s, _mm_shuffle_ps(s,s,1));
- acc += _mm_cvtss_f32(s) * sc[gi];
- }
- y[o] = acc;
- }
- return;
- }
-#endif
- #pragma omp parallel for schedule(static) if(O >= 256)
- for (int o = 0; o < O; o++) {
- const int8_t *w = q + (int64_t)o * I;
- const float *sc = scale + (int64_t)o * ng;
- float acc = 0.f;
- for (int gi = 0; gi < ng; gi++) {
- int base = gi * gs, end = base + gs; if (end > I) end = I;
- float part = 0.f;
- for (int i = base; i < end; i++) part += x[i] * (float)w[i];
- acc += part * sc[gi];
- }
- y[o] = acc;
- }
-}
/* Expert-GEMV dispatch: per-row scales (classic) or grouped (gs64 container). */
static void matmul_qe(float *y, const float *x, const int8_t *q, const float *scale, int I, int O) {
if (g_expert_gs) matmul_q_gs(y, x, q, scale, I, O, g_expert_gs);