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

test(inkling): set batch modes through the Windows CRT

JustVugg6a37a54a9a52
diff --git a/c/tests/bench_inkling_shared_batch.c b/c/tests/bench_inkling_shared_batch.c
index e7a22a8b..b82eedda 100644
--- a/c/tests/bench_inkling_shared_batch.c
+++ b/c/tests/bench_inkling_shared_batch.c
@@ -13,6 +13,21 @@
enum { S = 16, D = 2048, I = 1024, K = 2, NS = 2, REPS = 3 };
+static void env_set(const char *name,const char *value) {
+#ifdef _WIN32
+ _putenv_s(name,value);
+#else
+ setenv(name,value,1);
+#endif
+}
+static void env_unset(const char *name) {
+#ifdef _WIN32
+ _putenv_s(name,"");
+#else
+ unsetenv(name);
+#endif
+}
+
static uint16_t to_bf16(float x) {
union { float f; uint32_t u; } v = { x };
return (uint16_t)(v.u >> 16);
@@ -46,8 +61,8 @@ static void init_q4(Wt *w, int rows, int cols, int salt) {
static double one(Model *m, Layer *l, const float *x, const float *wgt,
const float *seed, float *out, const char *mode) {
float *g = falloc(2 * I), *u = g + I, *hh = falloc(D);
- if (mode) setenv("INK_SHARED_BATCH", mode, 1);
- else unsetenv("INK_SHARED_BATCH");
+ if (mode) env_set("INK_SHARED_BATCH", mode);
+ else env_unset("INK_SHARED_BATCH");
memcpy(out, seed, (size_t)S * D * sizeof(float));
double t0 = now_s();
shared_experts_cpu(m, l, x, S, out, wgt, g, u, hh);
@@ -110,7 +125,7 @@ int main(void) {
qbytes+=(double)(NS*I*((D+63)/64)*2+NS*D*((I+63)/64))*sizeof(float);
failed|=measure("int4-g64",qbytes/1048576.0,&m,&l,x,wgt,seed,scalar,batch);
- unsetenv("INK_SHARED_BATCH");
+ env_unset("INK_SHARED_BATCH");
free(l.sh_g.q4);free(l.sh_g.qs);free(l.sh_u.q4);free(l.sh_u.qs);
free(l.sh_d.q4);free(l.sh_d.qs);
free(x); free(wgt); free(seed); free(scalar); free(batch);
diff --git a/c/tests/test_inkling_shared_batch.c b/c/tests/test_inkling_shared_batch.c
index 505e2af2..f9cdfe67 100644
--- a/c/tests/test_inkling_shared_batch.c
+++ b/c/tests/test_inkling_shared_batch.c
@@ -13,6 +13,24 @@ static int failures;
fprintf(stderr,"FAIL %s:%d: ",__FILE__,__LINE__); \
fprintf(stderr,__VA_ARGS__); fputc('\n',stderr); failures++; } } while (0)
+/* compat.h maps setenv() to SetEnvironmentVariableA on Windows, but getenv()
+ * reads the CRT environment copy. Use _putenv_s there so the production
+ * getenv("INK_SHARED_BATCH") sees the mode under test. */
+static void env_set(const char *name,const char *value) {
+#ifdef _WIN32
+ _putenv_s(name,value);
+#else
+ setenv(name,value,1);
+#endif
+}
+static void env_unset(const char *name) {
+#ifdef _WIN32
+ _putenv_s(name,"");
+#else
+ unsetenv(name);
+#endif
+}
+
static float value(int64_t i, int salt) {
int v = (int)((i * 37 + salt * 17) % 101);
return (float)(v - 50) / (float)(71 + salt);
@@ -45,7 +63,7 @@ static void compare_paths(const char *format,Model *m,Layer *l,int S,
size_t out_bytes=(size_t)S*D*sizeof(float);
float *scalar=falloc((int64_t)S*D),*out=falloc((int64_t)S*D);
float *g=falloc((int64_t)2*I),*u=g+I,*hh=falloc(D);
- memcpy(scalar,seed,out_bytes);setenv("INK_SHARED_BATCH","0",1);
+ memcpy(scalar,seed,out_bytes);env_set("INK_SHARED_BATCH","0");
g_matmul_w_calls=0;shared_experts_cpu(m,l,x,S,scalar,wgt,g,u,hh);
CHECK(g_matmul_w_calls==(uint64_t)S*ns*3,
"%s scalar calls=%llu expected=%d",format,
@@ -54,7 +72,7 @@ static void compare_paths(const char *format,Model *m,Layer *l,int S,
const char *modes[]={NULL,"3"};
for(int z=0;z<2;z++){
memcpy(out,seed,out_bytes);
- if(modes[z])setenv("INK_SHARED_BATCH",modes[z],1);else unsetenv("INK_SHARED_BATCH");
+ if(modes[z])env_set("INK_SHARED_BATCH",modes[z]);else env_unset("INK_SHARED_BATCH");
g_matmul_w_calls=0;shared_experts_cpu(m,l,x,S,out,wgt,g,u,hh);
int chunks=modes[z]?(S+2)/3:1,expected=chunks*ns*3;
CHECK(!memcmp(out,scalar,out_bytes),"%s mode=%s is not scalar bit-exact",
@@ -68,7 +86,7 @@ static void compare_paths(const char *format,Model *m,Layer *l,int S,
}
/* Decode must remain the original one-row GEMV path. */
- memcpy(out,seed,(size_t)D*sizeof(float));unsetenv("INK_SHARED_BATCH");
+ memcpy(out,seed,(size_t)D*sizeof(float));env_unset("INK_SHARED_BATCH");
g_matmul_w_calls=0;shared_experts_cpu(m,l,x,1,out,wgt,g,u,hh);
CHECK(!memcmp(out,scalar,(size_t)D*sizeof(float)),"%s decode row changed",format);
CHECK(g_matmul_w_calls==(uint64_t)ns*3,"%s decode used batch shape",format);
@@ -100,7 +118,7 @@ int main(void){
run_format("f32",0,12,17,13);
run_format("bf16",1,12,32,16);
run_format("int4-g64",2,12,64,64);
- unsetenv("INK_SHARED_BATCH");
+ env_unset("INK_SHARED_BATCH");
if(failures){fprintf(stderr,"inkling shared batch: %d failure(s)\n",failures);return 1;}
puts("inkling shared batch: ok");return 0;
}