Skip to content

A 2.78-trillion-parameter Kimi K3 running inference on a single CPU in 8.24 GB of RAM. Portable C99: no BLAS, no framework, no GPU.

fix(cli): refuse --ids and --layers values that do not fit an int

Fareed Khan54e448fc2496
diff --git a/Makefile b/Makefile
index 0a738d6..1156656 100644
--- a/Makefile
+++ b/Makefile
@@ -256,6 +256,16 @@ test: $(CLI_BIN) $(TEST_BINS)
echo " pass on the loader's error and prove nothing"; exit 1;; \
esac; \
done; echo " 3 malformed stop lists refused, each for the right reason"
+ @echo "== numeric CLI contract =="; \
+ for bad in "--ids 4294967296" "--ids 9999999999999999999999" "--layers 1junk" "--layers 9999999999999999999999"; do \
+ out=$$(./$(CLI_BIN) $(FIXTURES)/st --config $(FIXTURES)/cfg/flat.json $$bad 2>&1); rc=$$?; \
+ test $$rc -eq 2 || { echo " k3 $$bad returned $$rc, expected 2"; exit 1; }; \
+ case "$$out" in *--ids*|*--layers*) ;; \
+ *) echo " '$$bad' was refused, but not because of its number:"; \
+ echo " $$out"; \
+ echo " asserting only the exit code would pass on the loader's error"; exit 1;; \
+ esac; \
+ done; echo " 4 malformed numerics refused, each for the right reason"
@echo "== op kernels =="; ./$(BIN)/test_ops $(FIXTURES)/ops
@echo "== streaming cache =="; ./$(BIN)/test_cache $(FIXTURES)/cache
@echo "== safetensors =="; ./$(BIN)/test_st $(FIXTURES)/st $(BUILD)/st_index.json \
diff --git a/src/cli/k3_run.c b/src/cli/k3_run.c
index 4d68196..4c05607 100644
--- a/src/cli/k3_run.c
+++ b/src/cli/k3_run.c
@@ -46,6 +46,7 @@
#include <math.h>
#include <errno.h>
+#include <limits.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
@@ -860,6 +861,23 @@ static int chat_run(Tok *tok, const K3ChatTemplate *tmpl, K3ChatHistory *history
}
}
+/* Strict argv integers. atoi("1junk") is 1 and a value past INT_MAX is
+ * undefined behaviour, so a typo would silently run with a different number
+ * than the one typed. The whole string must be an integer in int range;
+ * value semantics (like the --layers -1 sentinel) stay with the caller. */
+static int parse_int_strict(const char *s, int *out)
+{
+ char *end = NULL;
+ long v;
+ if (!s || !*s) return -1;
+ errno = 0;
+ v = strtol(s, &end, 10);
+ if (errno == ERANGE || end == s || *end != '\0' || v < INT_MIN || v > INT_MAX) return -1;
+ *out = (int)v;
+ return 0;
+}
+
+
int main(int argc, char **argv)
{
/* Informational flags are answered before anything else, because they must work
@@ -942,7 +960,17 @@ int main(int argc, char **argv)
}
}
else if (!strcmp(argv[i], "--cache-gb") && i + 1 < argc) cache_gb = atof(argv[++i]);
- else if (!strcmp(argv[i], "--layers") && i + 1 < argc) want_layers = atoi(argv[++i]);
+ else if (!strcmp(argv[i], "--layers") && i + 1 < argc) {
+ /* Syntax here, range later: the range check needs the layer count
+ * from config.json, but "1junk" must not survive until then as a
+ * silent 1. "-1" still parses, so the not-given sentinel keeps
+ * working. */
+ if (parse_int_strict(argv[i + 1], &want_layers) != 0) {
+ fprintf(stderr, "--layers %s is not an integer\n", argv[i + 1]);
+ return 2;
+ }
+ i++;
+ }
else if (!strcmp(argv[i], "--out") && i + 1 < argc) { outp = argv[++i]; out_set = 1; }
else if (!strcmp(argv[i], "--trunk") && i + 1 < argc) trunk_dir = argv[++i];
else if (!strcmp(argv[i], "--spec") && i + 1 < argc) spec_n = atoi(argv[++i]);
@@ -1290,10 +1318,14 @@ int main(int argc, char **argv)
* p, so the loop makes no progress and, because id 0 is exactly what
* a failed strtol call returns, silently fills the prompt with zeros
* instead of refusing it. Require each piece to parse as a whole
- * integer ending at a separator or the string's end. */
+ * integer ending at a separator or the string's end. Values must
+ * also fit an int: 4294967296 wraps to 0 on LP64, which is a real
+ * token id, so the vocabulary check below would wave it through. */
char *end = NULL;
+ errno = 0;
const long v = strtol(p, &end, 10);
- if (end == p || (*end != ',' && *end != ' ' && *end != '\0')) {
+ if (errno == ERANGE || end == p || v < INT_MIN || v > INT_MAX ||
+ (*end != ',' && *end != ' ' && *end != '\0')) {
fprintf(stderr, "bad --ids: '%s' is not a comma separated list of "
"integers\n", ids_s);
return 2;
diff --git a/tests/fixtures/cfg/flat.json b/tests/fixtures/cfg/flat.json
new file mode 100644
index 0000000..267fc5f
--- /dev/null
+++ b/tests/fixtures/cfg/flat.json
@@ -0,0 +1,37 @@
+{
+ "hidden_size": 128,
+ "num_hidden_layers": 13,
+ "vocab_size": 256,
+ "rms_norm_eps": 1e-05,
+ "tie_word_embeddings": false,
+ "kda_num_heads": 4,
+ "kda_head_dim": 16,
+ "short_conv_kernel_size": 4,
+ "gate_lower_bound": -5.0,
+ "num_attention_heads": 4,
+ "q_lora_rank": 64,
+ "kv_lora_rank": 32,
+ "qk_nope_head_dim": 24,
+ "qk_rope_head_dim": 8,
+ "v_head_dim": 16,
+ "mla_use_output_gate": true,
+ "num_experts": 8,
+ "num_experts_per_token": 2,
+ "num_shared_experts": 2,
+ "routed_expert_hidden_size": 64,
+ "moe_intermediate_size": 48,
+ "routed_scaling_factor": 1.0,
+ "moe_renormalize": true,
+ "latent_moe_use_norm": true,
+ "first_k_dense_replace": 1,
+ "intermediate_size": 96,
+ "attn_res_block_size": 3,
+ "situ_beta": 4.0,
+ "situ_linear_beta": 25.0,
+ "full_attn_layers": [
+ 4,
+ 8,
+ 12,
+ 13
+ ]
+}