Run frontier MoE models on hardware you already own — pure C, zero deps, experts streamed from disk. Tiny engine, immense model. 🐦
C
deepseek_v4: mirror cache on unified memory and the frozen-cache recycle (#1538)
JustVugg78a9ee942ec2
diff --git a/c/backend_cuda_dsv4.cu b/c/backend_cuda_dsv4.cuindex 055e9e97..06308e13 100644--- a/c/backend_cuda_dsv4.cu+++ b/c/backend_cuda_dsv4.cu@@ -720,6 +720,17 @@ extern "C" long long dsv4_cuda_mem_free_mb(int device){return (long long)(free_b>>20);}+/* 1 when the device shares its memory with the host (GB10 / DGX Spark, Jetson,+ * any integrated part): cudaMemGetInfo then reports the system's free memory,+ * not headroom on a card, so a VRAM reserve compared against it says nothing+ * about whether one more mirror fits (#1538). */+extern "C" int dsv4_cuda_device_unified(int device){+ int integrated=0,host_tables=0;+ if(cudaDeviceGetAttribute(&integrated,cudaDevAttrIntegrated,device)!=cudaSuccess){cudaGetLastError();integrated=0;}+ if(cudaDeviceGetAttribute(&host_tables,cudaDevAttrPageableMemoryAccessUsesHostPageTables,device)!=cudaSuccess){cudaGetLastError();host_tables=0;}+ return (integrated||host_tables)?1:0;+}+extern "C" int dsv4_cuda_kv_ring_append(int device,int layer,const float*rows,int start_pos,int count,int window,int dim){Dev*c=ctx(device);diff --git a/c/backend_cuda_dsv4.h b/c/backend_cuda_dsv4.hindex beae36f9..9395e667 100644--- a/c/backend_cuda_dsv4.h+++ b/c/backend_cuda_dsv4.h@@ -64,6 +64,10 @@ int dsv4_cuda_fp8_ref_matmul(int device,const uint8_t *w,const float *bscale,int dsv4_cuda_backend_arch_ok(int device);const char *dsv4_cuda_backend_name(void);long long dsv4_cuda_mem_free_mb(int device);+/* 1 on unified-memory devices (integrated GPU, or pageable access through the+ * host page tables), where mem_free_mb is the system's free memory rather+ * than headroom on a card. 0 when unknown. */+int dsv4_cuda_device_unified(int device);/* Drain the device's expert stream (hybrid split: async fill DMA is enqueued,* the CPU subset computes meanwhile, this closes the pipeline). Returns 1 on* success, 0 when the backend is unavailable. */diff --git a/c/backend_loader_dsv4.c b/c/backend_loader_dsv4.cindex e3391385..4ce09ef1 100644--- a/c/backend_loader_dsv4.c+++ b/c/backend_loader_dsv4.c@@ -172,6 +172,7 @@ typedef int (*fn_tensor_refill_fp4)(Dsv4CudaTensor *t, const uint8_ttypedef int (*fn_backend_arch_ok)(int device);typedef const char *(*fn_backend_name)(void);typedef long long (*fn_mem_free_mb)(int device);+typedef int (*fn_device_unified)(int device);typedef int (*fn_stream_drain)(int device);typedef int (*fn_kv_ring_append)(int device, int layer, const float *rows,int start_pos, int count, int window, int dim);@@ -312,6 +313,7 @@ static struct {fn_backend_name backend_name; /* optional */char loaded_name[64];fn_mem_free_mb mem_free_mb;+ fn_device_unified device_unified; /* optional: older DLLs answer 0 (discrete) */fn_stream_drain stream_drain; /* optional (older DLLs) */fn_kv_ring_append kv_ring_append;fn_kv_comp_append kv_comp_append;@@ -492,6 +494,7 @@ static int dsv4_cuda_resolve(const char *dllname){_Pragma("GCC diagnostic push")_Pragma("GCC diagnostic ignored \"-Wcast-function-type\"")g_dsv4.backend_arch_ok = (fn_backend_arch_ok)GetProcAddress(g_dsv4.dll, "dsv4_cuda_backend_arch_ok");+ g_dsv4.device_unified = (fn_device_unified)GetProcAddress(g_dsv4.dll, "dsv4_cuda_device_unified");/* optional: an older DLL without the export keeps the whole tier alive;* the engine probes this and simply stays on synchronous attaches. */g_dsv4.stream_drain = (fn_stream_drain)GetProcAddress(g_dsv4.dll, "dsv4_cuda_stream_drain");@@ -843,6 +846,11 @@ long long dsv4_cuda_mem_free_mb(int device){return g_dsv4.mem_free_mb(device);}+int dsv4_cuda_device_unified(int device){+ if(!g_dsv4.available || !g_dsv4.device_unified) return 0;+ return g_dsv4.device_unified(device);+}+int dsv4_cuda_stream_drain(int device){/* Without the DLL there is never in-flight DMA to wait for, so a drain* trivially succeeds. With an older DLL that lacks the export, reportdiff --git a/c/deepseek_v4.c b/c/deepseek_v4.cindex 977a96f8..e2a8bf27 100644--- a/c/deepseek_v4.c+++ b/c/deepseek_v4.c@@ -10347,7 +10347,22 @@ static int v4_gpu_expert_attach_cached_ex(V4GpuExpertMirrorCache *cache,/* cudaMemGetInfo is a driver round trip; with a large capacity the* guard is consulted on every miss (hundreds per token), so re-check* free VRAM only every 64 misses and reuse the last answer between. */- if (grow && cache->count > 0) {+ /* On unified memory (GB10, Jetson) cudaMemGetInfo's free is the+ * system's MemFree, which the page cache holding the model keeps+ * near zero; measured against a VRAM reserve it froze this cache at+ * a handful of entries on a 130 GB box (#1538). There is no separate+ * card to keep headroom on, so the guard does not apply; the+ * capacity (DSV4_CUDA_EXPERT_MIRRORS) bounds the cache instead. */+ static int unified = -1;+ if (unified < 0) {+ unified = dsv4_cuda_device_unified(cache->device) ? 1 : 0;+ if (unified)+ fprintf(stderr, "v4_gpu mirror-cache: unified memory, VRAM reserve "+ "guard off (free memory is the system's, not a "+ "card's); capacity %d bounds the cache\n",+ cache->capacity);+ }+ if (grow && cache->count > 0 && !unified) {static long long last_free_mb = -1;static unsigned probes;if (last_free_mb < 0 || (probes++ & 63) == 0)@@ -10358,6 +10373,24 @@ static int v4_gpu_expert_attach_cached_ex(V4GpuExpertMirrorCache *cache,pthread_mutex_unlock(&cache->mutex);return -1;}+ /* The "fewer than 8" rule above is about LIVE entries, not the+ * configured capacity: a cache frozen by the reserve with two or+ * three mirrors would recycle in place a slot the current token's+ * earlier view still references, and the output is garbage with no+ * error (#1538). Stay on the CPU for this expert until it can grow. */+ if (!grow && cache->count < 8) {+ static int warned;+ if (!warned) {+ warned = 1;+ fprintf(stderr, "v4_gpu mirror-cache: frozen below the VRAM reserve "+ "with %d live entries; a layer attaches up to 8 "+ "before computing, so experts stay on the CPU until "+ "the cache can grow (raise capacity or lower "+ "DSV4_CUDA_VRAM_RESERVE_MB)\n", cache->count);+ }+ pthread_mutex_unlock(&cache->mutex);+ return -1;+ }if (!grow || cache->count >= cache->capacity) {found = 0;for (int i = 1; i < cache->count; i++)