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.

test(sampler): run the chat sampler checks without a tokenizer

Fareed Khan6bc20f49939c
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 0b52ca7..0b2eac4 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -175,7 +175,7 @@ jobs:
run: |
make CC=clang CFLAGS="-O1 -g -std=gnu99 -Wall -Wextra -fsanitize=address,undefined -fno-omit-frame-pointer" \
LDFLAGS="-lm -fsanitize=address,undefined -lpsapi -lwinpthread" ARCH= \
- bin/test_ops bin/test_cfg bin/test_cache bin/test_st bin/test_st_faults bin/test_json bin/k3_model
+ bin/test_ops bin/test_cfg bin/test_cache bin/test_st bin/test_st_faults bin/test_sampler bin/test_json bin/k3_model
cp /clang64/bin/libclang_rt.asan_dynamic-x86_64.dll /clang64/bin/libwinpthread-1.dll /clang64/bin/libc++.dll bin/
- name: Run under sanitizers
env:
@@ -192,6 +192,7 @@ jobs:
plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32
./bin/test_st_faults.exe tests/fixtures/st /tmp/stfault
./bin/test_json.exe
+ ./bin/test_sampler.exe
./bin/k3_model.exe tests/fixtures
# CMakeLists.txt documents itself as staying interchangeable with the Make build, but
@@ -247,7 +248,7 @@ jobs:
run: |
make CFLAGS="-O1 -g -std=gnu99 -Wall -Wextra -fsanitize=address,undefined -fno-omit-frame-pointer" \
LDFLAGS="-lm -fsanitize=address,undefined" ARCH= \
- bin/test_ops bin/test_cfg bin/test_cache bin/test_st bin/test_st_faults bin/test_json \
+ bin/test_ops bin/test_cfg bin/test_cache bin/test_st bin/test_st_faults bin/test_sampler bin/test_json \
bin/test_model_stream bin/k3_model \
bin/bench_kernels \
-j"$(nproc)"
@@ -266,6 +267,7 @@ jobs:
plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32
./bin/test_st_faults tests/fixtures/st "$RUNNER_TEMP/stfault"
./bin/test_json
+ ./bin/test_sampler
./bin/test_model_stream tests/fixtures/st
./bin/k3_model tests/fixtures
# The ms and GFLOP/s it prints are meaningless under -O1 with ASan interceptors
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 26e4565..f4b25d9 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -153,6 +153,8 @@ k3_add_test(test_chat tests/unit/test_chat.c)
k3_add_test(test_st_faults tests/unit/test_st_faults.c)
k3_add_test(test_json tests/unit/test_json.c)
target_link_libraries(test_chat PRIVATE k3_chat)
+k3_add_test(test_sampler tests/unit/test_sampler.c)
+target_link_libraries(test_sampler PRIVATE k3_chat)
k3_add_test(scale_test tests/unit/scale_test.c)
k3_add_test(test_trunk tests/unit/test_trunk.c)
k3_add_test(k3_model tests/unit/k3_model.c)
@@ -190,6 +192,7 @@ add_test(NAME safetensors COMMAND test_st ${FIX}/st ${CMAKE_CURRENT_BINARY_DIR
plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32)
add_test(NAME shard_faults COMMAND test_st_faults ${FIX}/st ${CMAKE_CURRENT_BINARY_DIR}/stfault)
add_test(NAME json_refusals COMMAND test_json)
+add_test(NAME sampler COMMAND test_sampler)
add_test(NAME model_stream COMMAND test_model_stream ${FIX}/st)
add_test(NAME ultra_cli_contract
COMMAND ${CMAKE_COMMAND} -DCLI=$<TARGET_FILE:k3_cli>
diff --git a/Makefile b/Makefile
index 1156656..dc3544f 100644
--- a/Makefile
+++ b/Makefile
@@ -156,7 +156,7 @@ CHAT_SRC := src/chat/k3_chat.c src/chat/k3_sampler.c
CLI_BIN := $(BIN)/k3
# Tests that need no checkpoint. These run in CI on every push.
-UNIT_TESTS := test_ops test_cache test_st test_model_stream test_cfg test_tok test_chat scale_test k3_model test_trunk test_st_faults test_json
+UNIT_TESTS := test_ops test_cache test_st test_model_stream test_cfg test_tok test_chat test_sampler scale_test k3_model test_trunk test_st_faults test_json
# Tests that need real shards. Built and run by `make test-all` with SHARD_DIR set;
# see the weights-test target below.
WEIGHT_TESTS := test_expert test_real_layer
@@ -212,6 +212,8 @@ $(BIN)/test_tok: tests/unit/test_tok.c | $(BIN)
$(BIN)/test_chat: tests/unit/test_chat.c src/chat/k3_chat.c src/chat/k3_sampler.c | $(BIN)
$(CC) $(CFLAGS) -Wno-unused-function $(INCLUDES) $^ -o $@ $(LDFLAGS)
+$(BIN)/test_sampler: tests/unit/test_sampler.c src/chat/k3_sampler.c | $(BIN)
+ $(CC) $(CFLAGS) -Wno-unused-function $(INCLUDES) $^ -o $@ $(LDFLAGS)
$(BIN)/test_json: tests/unit/test_json.c | $(BIN)
$(CC) -O2 -std=c99 $(WARN) -Wno-unused-function $(INCLUDES) $< -o $@ -lm
@@ -304,6 +306,7 @@ test: $(CLI_BIN) $(TEST_BINS)
echo " the XTML template is checked against the released tokenizer,"; \
echo " which ships with the checkpoint. Run: make test TOK_FILES=/path/to/k3model"; \
fi
+ @echo "== sampler =="; ./$(BIN)/test_sampler
@echo "== real dimensions =="; ./$(BIN)/scale_test
@echo "== trunk streaming =="; ./$(BIN)/test_trunk
@echo "== full-model oracle =="; ./$(BIN)/k3_model $(FIXTURES)
diff --git a/tests/unit/test_chat.c b/tests/unit/test_chat.c
index 0e4b3ac..d0388ca 100644
--- a/tests/unit/test_chat.c
+++ b/tests/unit/test_chat.c
@@ -1,4 +1,4 @@
-/* test_chat.c - weightless Kimi K3 XTML, transcript, parser, and sampler gates. */
+/* test_chat.c - weightless Kimi K3 XTML, transcript and parser gates. */
#define _POSIX_C_SOURCE 200809L
#include <stdio.h>
#include <stdlib.h>
@@ -6,7 +6,6 @@
#include <unistd.h>
#include "k3_chat.h"
-#include "k3_sampler.h"
static int fails;
@@ -238,16 +237,6 @@ int main(int argc, char **argv)
"history write failure is reported without replacing a transcript");
unlink(path); k3_chat_history_free(&loaded);
- float logits[] = {0.0f, 1.0f, 2.0f, 3.0f}; int a, b, g;
- K3Sampler sa, sb; k3_sampler_init(&sa, 1.0, .95, 42, 2); k3_sampler_init(&sb, 1.0, .95, 42, 2);
- ok(k3_sampler_next(&sa, logits, 4, 0, &a) == 0 && k3_sampler_next(&sb, logits, 4, 0, &b) == 0 && a == b,
- "fixed seed sampler is deterministic");
- ok(k3_sampler_next(&sa, logits, 4, 1, &g) == 0 && g == 3, "greedy sampler remains argmax");
- K3Sampler bad_sampler; k3_sampler_init(&bad_sampler, 1.0, 0.0, 1, 1);
- ok(k3_sampler_next(&bad_sampler, logits, 4, 0, &g) != 0, "invalid top-p is rejected");
- k3_sampler_free(&bad_sampler);
- k3_sampler_free(&sa); k3_sampler_free(&sb);
-
free(rendered); k3_chat_segments_free(&segs); k3_chat_history_free(&h);
printf("\n%s\n", fails ? "CHAT TESTS FAILED" : "CHAT TESTS PASSED");
return fails ? 1 : 0;
diff --git a/tests/unit/test_sampler.c b/tests/unit/test_sampler.c
new file mode 100644
index 0000000..b6328e9
--- /dev/null
+++ b/tests/unit/test_sampler.c
@@ -0,0 +1,121 @@
+/* test_sampler.c - the chat sampler, without the tokenizer.
+ *
+ * WHY THIS FILE EXISTS
+ * The sampler gates used to live at the end of test_chat.c, after the
+ * tokenizer load. Without the released vocabulary the whole binary,
+ * sampler checks included, was skipped: the one component with no
+ * legitimate reason to need a checkpoint was gated behind one. These
+ * checks need nothing but the sampler itself, so they run on every
+ * machine, in both build systems and under sanitizers.
+ *
+ * WHAT IS CHECKED
+ * Every property is deterministic, never statistical: a seed that must
+ * replay exactly, greedy decoding that never touches the stream, masked
+ * logits that can never win, and invalid configurations that must be
+ * refused rather than sampled from.
+ *
+ * usage: test_sampler
+ */
+#include <math.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "k3_sampler.h"
+
+static int fails;
+
+static void ok(int cond, const char *what)
+{
+ if (cond) printf(" ok %s\n", what);
+ else { printf(" FAIL %s\n", what); fails++; }
+}
+
+int main(void)
+{
+ float logits[] = {0.0f, 1.0f, 2.0f, 3.0f};
+ int a, b, g, i;
+
+ /* Same seed and turn replays the same 50-draw sequence exactly. */
+ {
+ K3Sampler sa, sb;
+ int same = 1;
+ k3_sampler_init(&sa, 0.7, 0.9, 1234, 5);
+ k3_sampler_init(&sb, 0.7, 0.9, 1234, 5);
+ for (i = 0; i < 50; i++) {
+ k3_sampler_next(&sa, logits, 4, 0, &a);
+ k3_sampler_next(&sb, logits, 4, 0, &b);
+ if (a != b) same = 0;
+ }
+ ok(same, "fixed seed sampler replays a 50-draw sequence");
+ k3_sampler_free(&sa);
+ k3_sampler_free(&sb);
+ }
+
+ /* Greedy is argmax, and never advances the stream: two greedy draws
+ * followed by a sampled one match a single sampled draw. */
+ {
+ K3Sampler sa, sb;
+ int x[3], y;
+ k3_sampler_init(&sa, 1.0, 0.95, 42, 2);
+ k3_sampler_init(&sb, 1.0, 0.95, 42, 2);
+ ok(k3_sampler_next(&sa, logits, 4, 1, &g) == 0 && g == 3,
+ "greedy sampler remains argmax");
+ k3_sampler_next(&sa, logits, 4, 1, &x[0]);
+ k3_sampler_next(&sa, logits, 4, 1, &x[1]);
+ k3_sampler_next(&sa, logits, 4, 0, &x[2]);
+ k3_sampler_next(&sb, logits, 4, 0, &y);
+ ok(x[2] == y, "greedy draws do not advance the stream");
+ k3_sampler_free(&sa);
+ k3_sampler_free(&sb);
+ }
+
+ /* A masked logit has zero mass and sorts last, so it can never win,
+ * however many tokens are drawn. */
+ {
+ float masked[] = {0.0f, 1.0f, 2.0f, -1e30f};
+ K3Sampler s;
+ int picked = 0;
+ k3_sampler_init(&s, 1.0, 1.0, 7, 0);
+ for (i = 0; i < 20000; i++) {
+ k3_sampler_next(&s, masked, 4, 0, &a);
+ if (a == 3) picked = 1;
+ }
+ ok(!picked, "masked -inf logit is never selected in 20000 draws");
+ k3_sampler_free(&s);
+ }
+
+ /* One logit is one outcome. */
+ {
+ float one[] = {5.0f};
+ K3Sampler s;
+ k3_sampler_init(&s, 1.0, 0.95, 1, 1);
+ ok(k3_sampler_next(&s, one, 1, 0, &a) == 0 && a == 0,
+ "single-logit sampler always picks it");
+ k3_sampler_free(&s);
+ }
+
+ /* Invalid configurations are refused, never sampled from. */
+ {
+ K3Sampler bad;
+ k3_sampler_init(&bad, 1.0, 0.0, 1, 1);
+ ok(k3_sampler_next(&bad, logits, 4, 0, &g) != 0, "top-p 0 is rejected");
+ k3_sampler_free(&bad);
+ k3_sampler_init(&bad, 1.0, 1.5, 1, 1);
+ ok(k3_sampler_next(&bad, logits, 4, 0, &g) != 0, "top-p above 1 is rejected");
+ k3_sampler_free(&bad);
+ k3_sampler_init(&bad, 0.0, 0.95, 1, 1);
+ ok(k3_sampler_next(&bad, logits, 4, 0, &g) != 0, "temperature 0 is rejected");
+ k3_sampler_free(&bad);
+ k3_sampler_init(&bad, NAN, 0.95, 1, 1);
+ ok(k3_sampler_next(&bad, logits, 4, 0, &g) != 0, "NaN temperature is rejected");
+ k3_sampler_free(&bad);
+ k3_sampler_init(&bad, 1.0, 0.95, 1, 1);
+ ok(k3_sampler_next(&bad, logits, 0, 0, &g) != 0, "empty vocabulary is rejected");
+ ok(k3_sampler_next(&bad, NULL, 4, 0, &g) != 0, "NULL logits are rejected");
+ ok(k3_sampler_next(&bad, logits, 4, 0, NULL) != 0, "NULL out is rejected");
+ k3_sampler_free(&bad);
+ }
+
+ printf("\n%s\n", fails ? "SAMPLER TESTS FAILED" : "SAMPLER TESTS PASSED");
+ return fails ? 1 : 0;
+}