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.
C avx2c99cpu-inferencedeep-learningfrom-scratchinference-enginekimi-k3linear-attentionllmllm-inferencemachine-learningmemory-efficientmixture-of-expertsmoemxfp4quantizationsimdsystems-programmingtransformerzero-dependencies
ci: add the build matrix, sanitizers and lint gates
FareedKhan-dev65f4fbec6a60
diff --git a/.github/ISSUE_TEMPLATE/bug_report.yml b/.github/ISSUE_TEMPLATE/bug_report.ymlnew file mode 100644index 0000000..d11ca40--- /dev/null+++ b/.github/ISSUE_TEMPLATE/bug_report.yml@@ -0,0 +1,37 @@+name: Bug report+description: Something behaves incorrectly+labels: [bug]+body:+ - type: markdown+ attributes:+ value: |+ Wrong *output* is the most important class of bug here, the engine can produce+ fluent text from a wrong model. If tokens look off, say so even without a crash.+ - type: textarea+ id: what+ attributes:+ label: What happened+ description: Include the exact command line.+ validations: {required: true}+ - type: textarea+ id: expected+ attributes:+ label: What you expected+ validations: {required: true}+ - type: input+ id: cpu+ attributes:+ label: CPU+ placeholder: "e.g. AMD EPYC 7763, 64 cores, AVX2 (no AVX-512)"+ validations: {required: true}+ - type: input+ id: mem+ attributes:+ label: RAM and storage+ placeholder: "e.g. 64 GB, NVMe measured at 3.2 GB/s"+ - type: textarea+ id: gates+ attributes:+ label: Output of `make test`+ render: shell+ validations: {required: true}diff --git a/.github/ISSUE_TEMPLATE/config.yml b/.github/ISSUE_TEMPLATE/config.ymlnew file mode 100644index 0000000..0086358--- /dev/null+++ b/.github/ISSUE_TEMPLATE/config.yml@@ -0,0 +1 @@+blank_issues_enabled: truediff --git a/.github/ISSUE_TEMPLATE/feature_request.yml b/.github/ISSUE_TEMPLATE/feature_request.ymlnew file mode 100644index 0000000..a9d6ba6--- /dev/null+++ b/.github/ISSUE_TEMPLATE/feature_request.yml@@ -0,0 +1,19 @@+name: Feature request+description: Suggest a capability+labels: [enhancement]+body:+ - type: textarea+ id: problem+ attributes:+ label: What can't you do today+ validations: {required: true}+ - type: textarea+ id: proposal+ attributes:+ label: What you would like+ - type: checkboxes+ id: roadmap+ attributes:+ label: Checked the roadmap+ options:+ - label: I read docs/ROADMAP.md and this is not already listeddiff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.mdnew file mode 100644index 0000000..bab9408--- /dev/null+++ b/.github/PULL_REQUEST_TEMPLATE.md@@ -0,0 +1,28 @@+## What this changes++<!-- One or two sentences. What behaviour is different after this PR? -->++## Why++<!-- The problem being solved. Link an issue if there is one. -->++## Verification++- [ ] `make test` passes (all weightless gates)+- [ ] `make portable` builds with no new warnings+- [ ] If kernels changed: `./bin/test_ops tests/fixtures/ops` still passes at declared tolerance+- [ ] If the config or tokenizer path changed: `make cfg` and `make tok` pass+- [ ] If output could change: the oracle gates (`./bin/k3_model tests/fixtures`) still match exactly++## Numbers, if this is a performance change++<!-- Timing claims need a noise floor. Report at least 3 runs of each arm, or say+ explicitly that the effect was not measured against variance. See docs/BENCHMARKING.md. -->++| arm | run 1 | run 2 | run 3 | mean |+|-----|-------|-------|-------|------|+| | | | | |++## Risk++<!-- What could this break that the tests would not catch? -->diff --git a/.github/dependabot.yml b/.github/dependabot.ymlnew file mode 100644index 0000000..8fe43a2--- /dev/null+++ b/.github/dependabot.yml@@ -0,0 +1,14 @@+version: 2+updates:+ - package-ecosystem: github-actions+ directory: "/"+ schedule:+ interval: weekly+ commit-message:+ prefix: "ci"+ - package-ecosystem: pip+ directory: "/tools"+ schedule:+ interval: weekly+ commit-message:+ prefix: "deps"diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlnew file mode 100644index 0000000..dcdbb36--- /dev/null+++ b/.github/workflows/ci.yml@@ -0,0 +1,163 @@+name: CI++on:+ push:+ branches: [main]+ pull_request:+ workflow_dispatch:++# Cancel superseded runs on the same ref, a push to a busy PR should not queue builds.+concurrency:+ group: ${{ github.workflow }}-${{ github.ref }}+ cancel-in-progress: true++permissions:+ contents: read++jobs:+ # Every test in this job runs WITHOUT model weights. That is deliberate: the checkpoint+ # is 1.56 TB, so correctness has to be verifiable without it or it will not be verified.+ build-and-test:+ name: build + test (${{ matrix.os }}, ${{ matrix.cc }})+ runs-on: ${{ matrix.os }}+ strategy:+ fail-fast: false+ matrix:+ os: [ubuntu-latest, ubuntu-22.04]+ cc: [gcc, clang]+ steps:+ - uses: actions/checkout@v4++ - name: Install toolchain+ run: |+ sudo apt-get update -qq+ sudo apt-get install -y --no-install-recommends libomp-dev+ ${{ matrix.cc }} --version++ # -march=native is wrong for CI: the runner's CPU is not the user's. `portable`+ # targets a documented baseline (AVX2 + FMA) instead.+ - name: Build (portable baseline)+ run: make portable CC=${{ matrix.cc }} -j"$(nproc)"++ - name: Build tests+ run: make CC=${{ matrix.cc }} bin/test_ops bin/test_cache bin/test_st bin/test_cfg bin/test_tok bin/k3_model -j"$(nproc)"++ - name: Op kernels+ run: ./bin/test_ops tests/fixtures/ops++ - name: Streaming expert cache+ run: ./bin/test_cache tests/fixtures/cache++ - name: Safetensors reader+ run: |+ ./bin/test_st tests/fixtures/st /tmp/idx.json \+ plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32++ - name: Config reader (both layouts + refusal cases)+ run: ./bin/test_cfg fixture tests/fixtures/ref_k3.json++ # The end-to-end gate: teacher forcing, greedy decode, and incremental decode must+ # all match a reference implementation exactly on the tiny oracle model.+ - name: Full-model oracle gates+ run: ./bin/k3_model tests/fixtures++ # Warnings are defects here. The engine does arithmetic on `const void *` weight+ # pointers, where a missing -Wpointer-arith silently strides by one byte.+ strict-warnings:+ name: warnings as errors+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ - run: sudo apt-get update -qq && sudo apt-get install -y --no-install-recommends libomp-dev+ - name: Build with -Werror+ run: make portable CFLAGS="-O2 -std=gnu99 -Wall -Wextra -Wpointer-arith -Wshadow -Wvla -Wno-unused-parameter -Werror -fopenmp -ffp-contract=off" -j"$(nproc)"++ sanitizers:+ name: ASan + UBSan+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ - run: sudo apt-get update -qq && sudo apt-get install -y --no-install-recommends libomp-dev+ # test_cache and test_st are the two that matter most here. SECURITY.md puts the+ # safetensors parser and the streaming cache in scope precisely because they read+ # attacker-influenceable bytes: a hand-written binary parser and an O_DIRECT slot+ # allocator are where an out-of-bounds read actually lives.+ - name: Build instrumented tests+ 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/k3_model \+ -j"$(nproc)"+ - name: Run under sanitizers+ env:+ ASAN_OPTIONS: detect_leaks=0:abort_on_error=1+ UBSAN_OPTIONS: print_stacktrace=1:halt_on_error=1+ run: |+ ./bin/test_ops tests/fixtures/ops+ ./bin/test_cfg fixture tests/fixtures/ref_k3.json+ ./bin/test_cache tests/fixtures/cache+ ./bin/test_st tests/fixtures/st "$RUNNER_TEMP/idx.json" \+ plain.f32.2d plain.bf16.1d tricky.f16.1d packed.u8.2d scalar.f32 second.shard.f32+ ./bin/k3_model tests/fixtures++ # The tokenizer is portable C99 and must agree with the reference implementation+ # token-for-token. A tokenizer that is merely "close" corrupts every prompt silently.+ tokenizer-parity:+ name: tokenizer parity+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ - uses: actions/setup-python@v5+ with:+ python-version: '3.12'+ - run: pip install --quiet tiktoken++ # This job compiles the tokenizer and, when the vocabulary is available, checks it+ # against the reference implementation.+ #+ # It CANNOT gate on a clean checkout. Both the parity check and the byte-exact+ # roundtrip need tiktoken.model, which ships with the model weights and is not in+ # this repository, 163,584 vocabulary entries are not something to vendor. So the+ # job reports NOT RUN rather than passing quietly: a check that silently degrades+ # to nothing is worse than no check at all, because the green tick still gets+ # believed. See docs/TESTING.md, which lists this as the one gate CI cannot run.+ - run: make bin/test_tok+ - name: Parity vs reference tokenizer+ env:+ K3_TOK_FILES: ${{ github.workspace }}/tests/fixtures/tokenizer+ run: |+ if [ -f "$K3_TOK_FILES/tiktoken.model" ]; then+ ./bin/test_tok "$K3_TOK_FILES" roundtrip README.md+ python tools/tok_parity.py ./bin/test_tok+ else+ echo "::warning title=Tokenizer parity NOT RUN::tiktoken.model is absent" \+ "(it ships with the model weights, not with this repository), so" \+ "neither parity nor the byte-exact roundtrip was checked. Run" \+ "'make tok' locally against a downloaded checkpoint."+ fi++ python-lint:+ name: python tools lint+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ - uses: actions/setup-python@v5+ with:+ python-version: '3.12'+ - run: pip install --quiet ruff+ # Gates. Style drift in tools that generate the test fixtures is worth catching,+ # and an unparseable tool is a broken fixture pipeline.+ - run: ruff check tools/ --output-format=github+ - name: Syntax check every tool+ run: python -m compileall -q tools/++ shellcheck:+ name: shellcheck+ runs-on: ubuntu-latest+ steps:+ - uses: actions/checkout@v4+ - run: sudo apt-get update -qq && sudo apt-get install -y --no-install-recommends shellcheck+ # benchmarks/ is included deliberately. Its scripts run unattended for hours and+ # record whatever they observe as a result, so a quoting or pipefail defect there+ # does not crash, it produces a plausible-looking table of wrong numbers.+ - run: find scripts examples benchmarks -name '*.sh' -print0 | xargs -0 -r shellcheck -S warning