Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ c/tests/test_*
!c/tests/test_*.cu
!c/tests/test_*.mm
!c/tests/test_*.py
# scratch fixture directories the test binaries write at run time
c/tests/tmp_*
# bench_* and fuzz_* build to extensionless binaries in the same directory and
# were not covered here, which is how c/tests/bench_omp_grain reached the tree.
c/tests/bench_*
Expand Down
16 changes: 16 additions & 0 deletions THIRD_PARTY_NOTICES.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ executes an unmodified vLLM checkout (commit
`ffd46bfab2128bb84146050e98b51a617c6575ab`) as a behavioural oracle for the
native port; no vLLM code is vendored.

## Swiftlet qpack and MLX affine Metal GEMV

`c/qpack.c` and `c/qpack.h` implement the Swiftlet qpack v1 container schema
documented by `Sources/SwiftletCore/Qpack.swift` at the commit linked below.
The reader is a new portable implementation for Colibri and does not copy
Swift source code.

The MLX affine Q4/Q8 kernels in `c/backend_metal.mm` are adapted from
`gemv_affine_fast` and `gemv_affine_fast8` in Swiftlet:
https://github.com/leonickson1/Swiftlet at commit
`b3a04676748c7597800c5bcc8b80a32508f9f43d`.

Swiftlet is licensed under Apache License 2.0. The kernels were modified for
Colibri's checked descriptor, batched dispatch, buffer ownership, and fallback
contract. This repository's `LICENSE` contains the applicable Apache 2.0 text.

## DeepGEMM sm120 headers (fetched, not vendored: `c/third_party/deepgemm/`)

The DeepSeek V4 CUDA tier's DeepGEMM flavour (`make cuda-dsv4-dg-dll`,
Expand Down
70 changes: 65 additions & 5 deletions c/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ ifneq (,$(DARWIN))
# Niente -march: su arm64 NEON e' baseline (i kernel __ARM_NEON si attivano da soli).
CC = clang
OMPDIR := $(shell brew --prefix libomp 2>/dev/null)
# CLT-only Macs routinely have Homebrew installed but off the non-interactive
# PATH (`make` from scripts, SSH, CI), which turned this probe into a silent
# single-threaded build with libomp sitting right there on disk. Fall back to
# the two standard Homebrew prefixes before concluding it is absent.
ifeq ($(OMPDIR),)
OMPDIR := $(firstword $(wildcard /opt/homebrew/opt/libomp /usr/local/opt/libomp))
endif
# `brew --prefix libomp` can print the formula's prospective path even when it
# is not installed, so verify both artifacts before adding unusable flags.
ifneq ($(and $(OMPDIR),$(wildcard $(OMPDIR)/include/omp.h),$(wildcard $(OMPDIR)/lib/libomp.*)),)
Expand Down Expand Up @@ -838,19 +845,28 @@ cuda-dsv4-dg-dll: backend_cuda_dsv4.cu backend_cuda_dsv4.h dsv4.def $(DEEPGEMM_S
dsv4-cuda-loader-test: tests/test_dsv4_decode_cuda.c backend_loader_dsv4.c backend_cuda_dsv4.h
$(CC) $(CFLAGS) tests/test_dsv4_decode_cuda.c backend_loader_dsv4.c -o dsv4_decode_loader_test$(EXE)
./dsv4_decode_loader_test$(EXE)
backend_metal.o: backend_metal.mm backend_metal.h
backend_metal.o: backend_metal.mm backend_metal.h affine_quant.h
$(METALXX) -c backend_metal.mm -o $@

metal-test: tests/test_backend_metal.mm backend_metal.mm backend_metal.h
metal-test: tests/test_backend_metal.mm backend_metal.mm backend_metal.h affine_quant.h
$(METALXX) tests/test_backend_metal.mm backend_metal.mm -framework Metal -framework Foundation -o backend_metal_test
./backend_metal_test

# Standalone large-batch GEMM correctness sweep — reproduces the long-context prefill
# corruption in seconds (no model). OMP-parallel CPU reference.
gemm-test: tests/test_gemm_largebatch.mm backend_metal.mm backend_metal.h
gemm-test: tests/test_gemm_largebatch.mm backend_metal.mm backend_metal.h affine_quant.h
$(METALXX) $(OMPC) tests/test_gemm_largebatch.mm backend_metal.mm $(OMPL) -framework Metal -framework Foundation -o gemm_largebatch_test
./gemm_largebatch_test

# Apple gate: qwen36 routed experts through the MLX affine path, Metal vs
# the CPU reference -- one-layer MoE parity plus end-to-end logit parity on
# the tiny hybrid stack. Needs QWEN36_QPACK_FIXTURE=<dir> pointing at a real
# swiftlet-repack output of tiny-model-q4; without it the binary skips with a
# distinct marker instead of pretending to pass.
qwen36-metal-test: tests/test_qwen36_qpack_metal.c qwen36.c qwen36_qpack.c qwen36_qpack.h qpack.c qpack.h affine_quant.h qwen36_tier.h st.h json.h compat.h backend_metal.o
$(CC) $(CFLAGS) -DCOLI_METAL -DQWEN36_QPACK tests/test_qwen36_qpack_metal.c qwen36_qpack.c qpack.c backend_metal.o -o qwen36_qpack_metal_test -framework Metal -framework Foundation -lc++ $(LDFLAGS)
./qwen36_qpack_metal_test

# fmt=6 (E8/IQ3) encoder for tools/convert_fp8_to_int4.py. OPTIONAL: iq3_pack.py
# falls back to its numpy path when the library is absent, producing the same
# bytes ~13x slower. Uses the same ARCH as the engine, so an AVX2 host gets the
Expand Down Expand Up @@ -1019,8 +1035,16 @@ QWEN36_TIER_SRC =
QWEN36_CFLAGS = $(NOCUDA_CFLAGS)
QWEN36_LDFLAGS = $(NOCUDA_LDFLAGS)
endif
qwen36$(EXE): qwen36.c cli_args.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) qwen36.c $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o qwen36$(EXE) $(QWEN36_LDFLAGS)
# Apple METAL=1 engine builds also compile the qpack MLX-affine routed-
# expert path (activated at runtime by QWEN36_QPACK=<container dir>); other
# builds keep the inline stubs in qwen36_qpack.h and change nothing.
QWEN36_QPACK_SRC =
ifeq ($(METAL),1)
QWEN36_QPACK_SRC = qwen36_qpack.c qpack.c
QWEN36_CFLAGS += -DQWEN36_QPACK
endif
qwen36$(EXE): qwen36.c cli_args.h qwen36_tier.h qwen36_qpack.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(QWEN36_QPACK_SRC) $(CUDA_OBJ) $(METAL_OBJ)
$(CC) $(QWEN36_CFLAGS) qwen36.c $(QWEN36_TIER_SRC) $(QWEN36_QPACK_SRC) $(CUDA_OBJ) $(METAL_OBJ) -o qwen36$(EXE) $(QWEN36_LDFLAGS)

# Qwen3.8-Flash-Next text-only sibling. This target is intentionally CPU-only;
# qwen38.c owns the direct checkpoint loader and SERVE=1 protocol, with no
Expand Down Expand Up @@ -1064,9 +1088,29 @@ tests/test_qwen36_ctx$(EXE): tests/test_qwen36_ctx.c qwen36.c qwen36_tier.h st.h
tests/test_qwen36_dense_batch$(EXE): tests/test_qwen36_dense_batch.c qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)

# CPU gate: the qpack MLX-affine routed-expert glue (qwen36_qpack.c)
# against the portable affine reference, on a synthesized container -- no
# GPU, every platform. The Apple parity twin (Metal vs CPU reference on a
# real Swiftlet-generated fixture) is the qwen36-metal-test target.
tests/test_qwen36_qpack$(EXE): tests/test_qwen36_qpack.c qwen36.c qwen36_qpack.c qwen36_qpack.h qpack.c qpack.h affine_quant.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ) $(METAL_OBJ)
$(CC) $(QWEN36_CFLAGS) -DQWEN36_QPACK $< qwen36_qpack.c qpack.c $(QWEN36_TIER_SRC) $(CUDA_OBJ) $(METAL_OBJ) -o $@ $(QWEN36_LDFLAGS)

tests/test_qwen36_json_escape$(EXE): tests/test_qwen36_json_escape.c qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)

# Dense-bridge gate: qwen36's loader against an MLX affine
# model.safetensors (the dense half of a Swiftlet qpack container) --
# `language_model.` name resolution, U32 triple indexing, checked Q4/Q8
# expansion, refusals. With argv it doubles as the round-trip harness for
# tests/test_make_qwen36_qpack_snap.py.
tests/test_qwen36_dense_affine$(EXE): tests/test_qwen36_dense_affine.c qwen36.c affine_quant.h qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)

# Both tokenizer.json merge spellings ("a b" strings and ["a","b"] pairs)
# must index the same merge table; the pair form is what Qwen3.6 ships.
tests/test_qwen36_tok_merges$(EXE): tests/test_qwen36_tok_merges.c qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)

# Reproducible local timing evidence; intentionally not a noisy CI perf gate.
tests/bench_qwen36_dense_batch$(EXE): tests/bench_qwen36_dense_batch.c qwen36.c qwen36_tier.h st.h json.h compat.h $(QWEN36_TIER_SRC) $(CUDA_OBJ)
$(CC) $(QWEN36_CFLAGS) $< $(QWEN36_TIER_SRC) $(CUDA_OBJ) -o $@ $(QWEN36_LDFLAGS)
Expand Down Expand Up @@ -1117,6 +1161,9 @@ portable:
iobench$(EXE): iobench.c compat.h
$(CC) $(CFLAGS) iobench.c -o iobench$(EXE) $(LDFLAGS)

qpack-inspect$(EXE): qpack_inspect.c qpack.c qpack.h affine_quant.h json.h compat.h
$(CC) $(CFLAGS) qpack_inspect.c qpack.c -o qpack-inspect$(EXE) $(LDFLAGS)

tests/test_serve_sentinel$(EXE): tests/test_serve_sentinel.c compat.h serve_codec.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

Expand Down Expand Up @@ -1379,6 +1426,19 @@ tests/test_expert_store_ops$(EXE): tests/test_expert_store_ops.c expert_store.h
tests/test_native_quant$(EXE): tests/test_native_quant.c deepseek_v4.c native_quant.h tensor.h quant.h
$(CC) $(CFLAGS) -DCOLI_V4_UNIT_NATIVE_QUANT deepseek_v4.c $< -o $@ $(LDFLAGS)

# Portable MLX affine Q4/Q8 format contract and CPU oracle. This stays
# separate from QT fmt=4, whose signed-int4 scale-only layout is incompatible.
tests/test_affine_quant$(EXE): tests/test_affine_quant.c affine_quant.h backend_metal.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

tests/test_qpack$(EXE): tests/test_qpack.c qpack.c qpack.h affine_quant.h json.h compat.h
$(CC) $(CFLAGS) tests/test_qpack.c qpack.c -o $@ $(LDFLAGS)

# Loader-side expansion oracle for the same MLX affine contract: nibble/byte
# order, scalar decodings, parity against coli_affine_matmul_ref, refusals.
tests/test_affine_dequant$(EXE): tests/test_affine_dequant.c affine_quant.h
$(CC) $(CFLAGS) $< -o $@ $(LDFLAGS)

tests/test_edge_runtime$(EXE): tests/test_edge_runtime.c edge_runtime.c edge_runtime.h
$(CC) $(CFLAGS) tests/test_edge_runtime.c edge_runtime.c -o $@ $(LDFLAGS)

Expand Down
Loading