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

fix(cuda): preserve active contexts on repeated initialization

ZacharyZcRf0f8dfc4eae3
diff --git a/c/backend_cuda.cu b/c/backend_cuda.cu
index f2fa210a..4585207e 100644
--- a/c/backend_cuda.cu
+++ b/c/backend_cuda.cu
@@ -1253,7 +1253,12 @@ extern "C" int coli_cuda_init(const int *devices, int count) {
return 0;
}
}
- g_nctx = 0;
+ if (g_nctx) {
+ int same = count == g_nctx;
+ for (int i = 0; same && i < count; i++) same = devices[i] == g_ctx[i].device;
+ if (!same) std::fprintf(stderr, "[CUDA] device list change requires shutdown first\n");
+ return same;
+ }
for (int i = 0; i < count; i++) {
int device = devices[i];
DeviceContext *ctx = &g_ctx[g_nctx];
diff --git a/c/backend_cuda.h b/c/backend_cuda.h
index 953e8b39..956a8173 100644
--- a/c/backend_cuda.h
+++ b/c/backend_cuda.h
@@ -53,7 +53,10 @@ static inline int coli_cuda_weight_at_supported(int fmt) {
/* Opaque, persistent device copy of one resident quantized tensor. */
typedef struct ColiCudaTensor ColiCudaTensor;
-/* Devices are CUDA ordinals, not positions in the input list. */
+/* Devices are CUDA ordinals, not positions in the input list.
+ * Repeating the same ordered list preserves active contexts. Changing an
+ * active list returns 0 without replacing it; release tensors and shut down
+ * before selecting a different list. Init/shutdown require caller serialization. */
COLI_CUDA_DLLEXPORT int coli_cuda_init(const int *devices, int count);
COLI_CUDA_DLLEXPORT void coli_cuda_shutdown(void);
/* Number of CUDA devices visible to this process, before a device list is
diff --git a/c/tests/test_cuda_init_failure.cu b/c/tests/test_cuda_init_failure.cu
index 93e97be6..855fbb73 100644
--- a/c/tests/test_cuda_init_failure.cu
+++ b/c/tests/test_cuda_init_failure.cu
@@ -3,10 +3,15 @@
#include "../backend_gpu_compat.h"
#include <cstdio>
static int attempts, live_streams, fail_stream;
+static int fail_select=-1, fail_props=-1;
static cudaError_t real_count(int *n) { return cudaGetDeviceCount(n); }
static cudaError_t test_count(int *n) { *n=2;return cudaSuccess; }
-static cudaError_t test_select(int) { return cudaSetDevice(0); }
-static cudaError_t test_props(cudaDeviceProp *p,int) { return cudaGetDeviceProperties(p,0); }
+static cudaError_t test_select(int device) {
+ return device==fail_select ? cudaErrorMemoryAllocation : cudaSetDevice(0);
+}
+static cudaError_t test_props(cudaDeviceProp *p,int device) {
+ return device==fail_props ? cudaErrorMemoryAllocation : cudaGetDeviceProperties(p,0);
+}
static cudaError_t test_create(cudaStream_t *s,unsigned flags) {
if(++attempts==fail_stream) return cudaErrorMemoryAllocation;
cudaError_t e=cudaStreamCreateWithFlags(s,flags);if(e==cudaSuccess)live_streams++;return e;
@@ -36,11 +41,33 @@ int main(void) {
attempts=0;
if(coli_cuda_init(ids,2)||live_streams||coli_cuda_device_count()) return 3;
}
- fail_stream=0;attempts=0;
+ fail_stream=0;
+ for(int device=0;device<2;device++){
+ g_current_device=-1; /* force the runtime selection fault past the cache */
+ fail_select=device;
+ if(coli_cuda_init(ids,2)||live_streams||coli_cuda_device_count()) return 11;
+ fail_select=-1;
+ fail_props=device;
+ if(coli_cuda_init(ids,2)||live_streams||coli_cuda_device_count()) return 12;
+ fail_props=-1;
+ }
+ attempts=0;
if(!coli_cuda_init(ids,2)||live_streams!=2||coli_cuda_device_count()!=2) return 4;
/* Rejected input must leave an existing backend reachable. */
if(coli_cuda_init(invalid,2)||live_streams!=2||coli_cuda_device_count()!=2) return 5;
if(coli_cuda_init(duplicate,2)||live_streams!=2||coli_cuda_device_count()!=2) return 6;
+ if(cudaMalloc(&g_ctx[0].x,256)!=cudaSuccess) return 10;
+ float *scratch=g_ctx[0].x;
+ cudaStream_t original=g_ctx[0].stream;
+ int before=attempts;
+ if(!coli_cuda_init(ids,2)||attempts!=before||live_streams!=2||g_ctx[0].stream!=original||g_ctx[0].x!=scratch){
+ puts("FAIL: repeated init replaced live contexts");return 8;
+ }
+ int reversed[]={1,0};
+ if(coli_cuda_init(ids,1)||coli_cuda_init(reversed,2)||attempts!=before||
+ live_streams!=2||g_ctx[0].stream!=original||g_ctx[0].x!=scratch||coli_cuda_device_count()!=2){
+ puts("FAIL: device-list change modified active contexts");return 9;
+ }
coli_cuda_shutdown();if(live_streams||coli_cuda_device_count()) return 7;
}
puts("CUDA initialization rollback: PASS");return 0;