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(v4): harden production DSpark config parsing

DrewZ83522db15488
diff --git a/c/Makefile b/c/Makefile
index b9c9da0c..6ab486bf 100644
--- a/c/Makefile
+++ b/c/Makefile
@@ -218,7 +218,8 @@ ifneq (,$(LINUX))
TEST_BINS += tests/test_uring$(EXE)
endif
ifeq ($(COLI_V4_SUPPORTED),1)
-TEST_BINS += tests/test_deepseek_v4$(EXE) tests/test_v4_ownership$(EXE)
+TEST_BINS += tests/test_deepseek_v4$(EXE) tests/test_v4_ownership$(EXE) \
+ tests/test_v4_config_dspark_compat$(EXE)
endif
# Windows CUDA DLL path: host links the loader, NOT cudart.
@@ -532,6 +533,16 @@ $(V4_OWN_DIR)/safetensors_index.o: safetensors_index.c safetensors_index.h | $(V
tests/test_v4_ownership$(EXE): tests/test_v4_ownership.c $(V4_OWNERSHIP_OBJS)
$(CC) $(V4_OWN_CFLAGS) $< $(V4_OWNERSHIP_OBJS) -o $@ -pthread $(LDFLAGS)
+
+COLI_V4_UNIT_CONFIG_DSPARK_COMPAT.o: deepseek_v4_dspark.c deepseek_v4.h \
+ deepseek_v4_dspark.h deepseek_v4_internal.h Makefile.deepseek-v4
+ $(MAKE) -f Makefile.deepseek-v4 ARCH=$(ARCH) $@
+
+tests/test_v4_config_dspark_compat$(EXE): \
+ tests/test_v4_config_dspark_compat.c \
+ COLI_V4_UNIT_CONFIG_DSPARK_COMPAT.o
+ $(CC) $(CFLAGS) $< COLI_V4_UNIT_CONFIG_DSPARK_COMPAT.o \
+ -o $@ $(LDFLAGS)
endif
tests/test_dsa_select$(EXE): tests/test_dsa_select.c colibri.c st.h uring.h json.h tok.h tok_unicode.h compat.h grammar.h tier.h quant.h sample.h kv_persist.h telemetry.h
diff --git a/c/Makefile.deepseek-v4 b/c/Makefile.deepseek-v4
index 095212bc..75ff0d87 100644
--- a/c/Makefile.deepseek-v4
+++ b/c/Makefile.deepseek-v4
@@ -75,10 +75,12 @@ $(SHARED_OBJS): %.o: %.c
native_quant_parallel.o native_quant_dual.o native_quant_batch_avx512.o: \
native_quant.c native_quant.h
-$(TARGET_OBJS) $(TEST_UNIT_OBJS): %.o: deepseek_v4.c deepseek_v4.h deepseek_v4_dspark.h
+$(TARGET_OBJS) $(TEST_UNIT_OBJS): %.o: deepseek_v4.c deepseek_v4.h \
+ deepseek_v4_dspark.h deepseek_v4_internal.h
$(CC) $(CFLAGS) -D$* -c deepseek_v4.c -o $@
-$(DSPARK_OBJS): %.o: deepseek_v4_dspark.c deepseek_v4.h deepseek_v4_dspark.h
+$(DSPARK_OBJS): %.o: deepseek_v4_dspark.c deepseek_v4.h \
+ deepseek_v4_dspark.h deepseek_v4_internal.h
$(CC) $(CFLAGS) -D$* -c deepseek_v4_dspark.c -o $@
deepseek-v4-test-objs: $(TEST_UNIT_OBJS)
diff --git a/c/deepseek_v4_dspark.c b/c/deepseek_v4_dspark.c
index 3c0a3f80..74272773 100644
--- a/c/deepseek_v4_dspark.c
+++ b/c/deepseek_v4_dspark.c
@@ -10,6 +10,9 @@
/* ---- begin include deepseek_v4_config.c ---- */
#include "deepseek_v4_internal.h"
+#include <float.h>
+#include <limits.h>
+#include <math.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -27,20 +30,28 @@ static int set_error(char *error, size_t size, const char *format, ...) {
return -1;
}
+static int json_int_value(const jval *value, int *output) {
+ if (!value || value->t != J_NUM || !isfinite(value->num) ||
+ floor(value->num) != value->num ||
+ value->num < (double)INT_MIN || value->num > (double)INT_MAX)
+ return -1;
+ *output = (int)value->num;
+ return 0;
+}
+
static int required_int(jval *root, const char *name, int *output,
char *error, size_t error_size) {
- jval *value = json_get(root, name);
- if (!value || value->t != J_NUM)
- return set_error(error, error_size, "missing integer config field: %s", name);
- *output = (int)value->num;
+ if (json_int_value(json_get(root, name), output) != 0)
+ return set_error(error, error_size, "invalid integer config field: %s", name);
return 0;
}
static int required_float(jval *root, const char *name, float *output,
char *error, size_t error_size) {
jval *value = json_get(root, name);
- if (!value || value->t != J_NUM)
- return set_error(error, error_size, "missing numeric config field: %s", name);
+ if (!value || value->t != J_NUM || !isfinite(value->num) ||
+ fabs(value->num) > (double)FLT_MAX)
+ return set_error(error, error_size, "invalid numeric config field: %s", name);
*output = (float)value->num;
return 0;
}
@@ -62,6 +73,7 @@ int coli_v4_config_parse(ColiDeepSeekV4Config *config, const char *json,
char *arena = NULL;
jval *root = json_parse(json, &arena);
if (!root || root->t != J_OBJ) {
+ json_free(root);
free(arena);
return set_error(error, error_size, "DeepSeek-V4 config is not an object");
}
@@ -99,6 +111,7 @@ int coli_v4_config_parse(ColiDeepSeekV4Config *config, const char *json,
required_float(root, "rope_theta", &config->rope_theta, error, error_size) ||
required_float(root, "compress_rope_theta", &config->compress_rope_theta, error, error_size);
if (failed) {
+ json_free(root);
free(arena);
return -1;
}
@@ -109,27 +122,31 @@ int coli_v4_config_parse(ColiDeepSeekV4Config *config, const char *json,
required_int(rope, "beta_fast", &config->rope_beta_fast, error, error_size) ||
required_int(rope, "beta_slow", &config->rope_beta_slow, error, error_size) ||
required_float(rope, "factor", &config->rope_factor, error, error_size)) {
+ json_free(root);
free(arena);
return -1;
}
jval *ratios = json_get(root, "compress_ratios");
if (!ratios || ratios->t != J_ARR || ratios->len < 1 ||
ratios->len > COLI_V4_MAX_LAYERS) {
+ json_free(root);
free(arena);
return set_error(error, error_size, "invalid compress_ratios");
}
config->compress_ratio_count = ratios->len;
for (int index = 0; index < ratios->len; index++) {
- if (ratios->kids[index]->t != J_NUM) {
+ if (json_int_value(ratios->kids[index],
+ &config->compress_ratios[index]) != 0) {
+ json_free(root);
free(arena);
- return set_error(error, error_size, "non-numeric compress ratio");
+ return set_error(error, error_size, "invalid compress ratio");
}
- config->compress_ratios[index] = (int)ratios->kids[index]->num;
}
jval *quantization = json_get(root, "quantization_config");
if (!quantization || quantization->t != J_OBJ ||
require_string(quantization, "fmt", "e4m3", error, error_size) ||
require_string(quantization, "scale_fmt", "ue8m0", error, error_size)) {
+ json_free(root);
free(arena);
return -1;
}
@@ -140,9 +157,11 @@ int coli_v4_config_parse(ColiDeepSeekV4Config *config, const char *json,
config->n_shared_experts != 1 || config->hc_mult < 1 ||
config->compress_ratio_count != config->num_hidden_layers +
config->num_nextn_predict_layers) {
+ json_free(root);
free(arena);
return set_error(error, error_size, "inconsistent DeepSeek-V4 config dimensions");
}
+ json_free(root);
free(arena);
return 0;
}
@@ -190,20 +209,30 @@ int coli_v4_config_load(ColiDeepSeekV4Config *config, const char *model_dir,
int coli_v4_config_parse(ColiDeepSeekV4Config *config, const char *json,
char *error, size_t error_size) {
- int result = coli_v4_config_parse_strict(config, json, error, error_size);
- if (!result) return 0;
- if (!config || !json || !strstr(json, "\"dspark_block_size\"") ||
+ char strict_error[256] = {0};
+ int result = coli_v4_config_parse_strict(
+ config, json, strict_error, sizeof(strict_error));
+ if (!result) {
+ if (error && error_size) error[0] = 0;
+ return 0;
+ }
+ if (strcmp(strict_error, "inconsistent DeepSeek-V4 config dimensions") ||
+ !config || !json || !strstr(json, "\"dspark_block_size\"") ||
config->num_hidden_layers < 1 ||
config->compress_ratio_count <= config->num_hidden_layers)
- return result;
+ return set_error(error, error_size, "%s", strict_error);
int inferred = config->compress_ratio_count - config->num_hidden_layers;
if (inferred < 1 || inferred > COLI_V4_DSPARK_MAX_STAGES)
- return result;
+ return set_error(error, error_size, "%s", strict_error);
config->num_nextn_predict_layers = inferred;
- if (config->num_experts_per_tok < 1 ||
+ if (config->hidden_size < 1 || config->num_hidden_layers < 1 ||
+ config->num_attention_heads < 1 || config->n_routed_experts < 1 ||
+ config->num_experts_per_tok < 1 ||
config->num_experts_per_tok > config->n_routed_experts ||
- config->n_shared_experts != 1 || config->hc_mult < 1)
- return result;
+ config->n_shared_experts != 1 || config->hc_mult < 1 ||
+ config->compress_ratio_count != config->num_hidden_layers +
+ config->num_nextn_predict_layers)
+ return set_error(error, error_size, "%s", strict_error);
if (error && error_size) error[0] = 0;
return 0;
}
diff --git a/c/tests/test_v4_config_dspark_compat.c b/c/tests/test_v4_config_dspark_compat.c
new file mode 100644
index 00000000..b772913f
--- /dev/null
+++ b/c/tests/test_v4_config_dspark_compat.c
@@ -0,0 +1,135 @@
+/* Focused regression test for the config parser linked by the production V4
+ * binary. This intentionally links COLI_V4_UNIT_CONFIG_DSPARK_COMPAT, not the
+ * standalone COLI_V4_UNIT_CONFIG test object. */
+#include "../deepseek_v4.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+static const char target_config[] =
+ "{\"model_type\":\"deepseek_v4\",\"expert_dtype\":\"fp4\","
+ "\"scoring_func\":\"sqrtsoftplus\",\"topk_method\":\"noaux_tc\","
+ "\"hidden_size\":128,\"num_hidden_layers\":3,"
+ "\"num_attention_heads\":4,\"head_dim\":32,\"q_lora_rank\":64,"
+ "\"qk_rope_head_dim\":8,\"o_groups\":2,\"o_lora_rank\":64,"
+ "\"sliding_window\":16,\"index_n_heads\":4,\"index_head_dim\":16,"
+ "\"index_topk\":8,\"n_routed_experts\":8,\"num_experts_per_tok\":2,"
+ "\"n_shared_experts\":1,\"moe_intermediate_size\":32,"
+ "\"num_hash_layers\":1,\"num_nextn_predict_layers\":1,"
+ "\"hc_mult\":4,\"hc_sinkhorn_iters\":5,\"vocab_size\":256,"
+ "\"max_position_embeddings\":4096,\"rms_norm_eps\":1e-6,"
+ "\"hc_eps\":1e-6,\"routed_scaling_factor\":1.5,\"swiglu_limit\":10,"
+ "\"rope_theta\":10000,\"compress_rope_theta\":40000,"
+ "\"compress_ratios\":[0,4,128,0],"
+ "\"rope_scaling\":{\"original_max_position_embeddings\":1024,"
+ "\"beta_fast\":32,\"beta_slow\":1,\"factor\":4},"
+ "\"quantization_config\":{\"fmt\":\"e4m3\",\"scale_fmt\":\"ue8m0\"}}";
+
+static const char dspark_config[] =
+ "{\"model_type\":\"deepseek_v4\",\"expert_dtype\":\"fp4\","
+ "\"scoring_func\":\"sqrtsoftplus\",\"topk_method\":\"noaux_tc\","
+ "\"hidden_size\":128,\"num_hidden_layers\":3,"
+ "\"num_attention_heads\":4,\"head_dim\":32,\"q_lora_rank\":64,"
+ "\"qk_rope_head_dim\":8,\"o_groups\":2,\"o_lora_rank\":64,"
+ "\"sliding_window\":16,\"index_n_heads\":4,\"index_head_dim\":16,"
+ "\"index_topk\":8,\"n_routed_experts\":8,\"num_experts_per_tok\":2,"
+ "\"n_shared_experts\":1,\"moe_intermediate_size\":32,"
+ "\"num_hash_layers\":1,\"num_nextn_predict_layers\":0,"
+ "\"hc_mult\":4,\"hc_sinkhorn_iters\":5,\"vocab_size\":256,"
+ "\"max_position_embeddings\":4096,\"rms_norm_eps\":1e-6,"
+ "\"hc_eps\":1e-6,\"routed_scaling_factor\":1.5,\"swiglu_limit\":10,"
+ "\"rope_theta\":10000,\"compress_rope_theta\":40000,"
+ "\"compress_ratios\":[0,4,128,0],\"dspark_block_size\":5,"
+ "\"rope_scaling\":{\"original_max_position_embeddings\":1024,"
+ "\"beta_fast\":32,\"beta_slow\":1,\"factor\":4},"
+ "\"quantization_config\":{\"fmt\":\"e4m3\",\"scale_fmt\":\"ue8m0\"}}";
+
+static char *replace_once(const char *source, const char *needle,
+ const char *replacement) {
+ const char *match = strstr(source, needle);
+ if (!match) return NULL;
+ size_t prefix = (size_t)(match - source);
+ size_t length = prefix + strlen(replacement) +
+ strlen(match + strlen(needle)) + 1;
+ char *result = malloc(length);
+ if (!result) return NULL;
+ memcpy(result, source, prefix);
+ strcpy(result + prefix, replacement);
+ strcpy(result + prefix + strlen(replacement), match + strlen(needle));
+ return result;
+}
+
+static int parse_ok(const char *json, int expected_nextn, const char *label) {
+ ColiDeepSeekV4Config config;
+ char error[256] = {0};
+ if (coli_v4_config_parse(&config, json, error, sizeof(error))) {
+ fprintf(stderr, "%s rejected: %s\n", label, error);
+ return 1;
+ }
+ if (config.hidden_size != 128 || config.num_hidden_layers != 3 ||
+ config.num_nextn_predict_layers != expected_nextn ||
+ config.compress_ratio_count != 4 ||
+ config.compress_ratios[1] != 4 || config.compress_ratios[2] != 128) {
+ fprintf(stderr, "%s produced incorrect config values\n", label);
+ return 1;
+ }
+ return 0;
+}
+
+static int parse_rejected(const char *json, const char *label) {
+ ColiDeepSeekV4Config config;
+ char error[256] = {0};
+ if (!coli_v4_config_parse(&config, json, error, sizeof(error))) {
+ fprintf(stderr, "%s was accepted\n", label);
+ return 1;
+ }
+ return 0;
+}
+
+int main(void) {
+ static const struct {
+ const char *label;
+ const char *needle;
+ const char *replacement;
+ } invalid[] = {
+ {"fractional integer", "\"hidden_size\":128", "\"hidden_size\":1.5"},
+ {"integer NaN", "\"hidden_size\":128", "\"hidden_size\":NaN"},
+ {"integer Infinity", "\"hidden_size\":128", "\"hidden_size\":1e309"},
+ {"floating NaN", "\"rms_norm_eps\":1e-6", "\"rms_norm_eps\":NaN"},
+ {"floating Infinity", "\"rms_norm_eps\":1e-6",
+ "\"rms_norm_eps\":1e309"},
+ {"integer overflow", "\"hidden_size\":128",
+ "\"hidden_size\":2147483648"},
+ {"invalid compress ratio", "\"compress_ratios\":[0,4,128,0]",
+ "\"compress_ratios\":[0,1.5,128,0]"},
+ {"non-finite compress ratio", "\"compress_ratios\":[0,4,128,0]",
+ "\"compress_ratios\":[0,NaN,128,0]"},
+ {"overflowing compress ratio", "\"compress_ratios\":[0,4,128,0]",
+ "\"compress_ratios\":[0,2147483648,128,0]"},
+ };
+ enum { REPEAT_COUNT = 1000 };
+ char *invalid_json[sizeof(invalid) / sizeof(invalid[0])] = {0};
+ for (size_t item = 0; item < sizeof(invalid) / sizeof(invalid[0]); item++) {
+ invalid_json[item] = replace_once(
+ dspark_config, invalid[item].needle, invalid[item].replacement);
+ if (!invalid_json[item]) {
+ fprintf(stderr, "cannot build %s fixture\n", invalid[item].label);
+ return 1;
+ }
+ }
+
+ int failed = 0;
+ for (int iteration = 0; iteration < REPEAT_COUNT && !failed; iteration++) {
+ failed = parse_ok(target_config, 1, "target config") ||
+ parse_ok(dspark_config, 1, "DSpark compatibility config");
+ for (size_t item = 0;
+ item < sizeof(invalid) / sizeof(invalid[0]) && !failed; item++)
+ failed = parse_rejected(invalid_json[item], invalid[item].label);
+ }
+ for (size_t item = 0; item < sizeof(invalid) / sizeof(invalid[0]); item++)
+ free(invalid_json[item]);
+ if (failed) return 1;
+ puts("DeepSeek-V4 production DSpark config parser tests: ok");
+ return 0;
+}