(very WIP!) Fast LLM inference in Rust & CUDA doll.tngl.io/firecrest
firecrest CONTRIBUTING.md
16 kB 161 lines
Markdown
at main

Contributing to Firecrest #

Firecrest's contribution rules apply to people and coding agents alike. They cover the layout and the build, the path of a change from an issue to main, the rules for the code, and the checks a pull request passes. AGENTS.md adds the notes that apply to agents only. README.md introduces Firecrest, and docs/README.md indexes the documentation.

Layout #

Path What
crates/ Shared crates: core types, CUDA driver bindings, the kernel image, quantization, GGUF and safetensors readers, tokenizer, sampling, adapters, policy scripts, the HTTP server and the firecrest command
engines/deepseek-v4.1/ The DeepSeek V4.1 Flash engine: engine (scheduling, the server-facing engine), model, engram, ctx (context store), ctxgen, prefill, gpu (decode), vision, vision-gpu; its model specification in docs/spec/
engines/inkling/ The experimental Inkling 2 Mini engine: model, gpu, chat, draft (speculative drafter), oracle (Python reference); specification in docs/spec/
engines/smollm3/ The SmolLM3-3B engine: engine (tokenizer, chat format, the served engine and its factory), model (GGUF weights and the CPU reference forward), gpu (the CUDA forward), oracle (Python reference); specification in docs/spec/
engines/qwen3.6/ The Qwen3.6-35B-A3B engine: engine (chat format, tool-call parser, the served engine, its factory and the expert placement tool), model (GGUF weights and the CPU reference forward), gpu (the CUDA forward with host-side and streamed experts), oracle (Python reference); specification in docs/spec/; prompt sets for expert placement and timing in bench/
kernels/ Every CUDA source, compiled into one image by firecrest-kernels
docs/ Operations, configuration, profiles, policies, adapters, design notes
bench/ Agentic benchmark harness and quality gates
ui/ The web console (Svelte)
xtask/ Generators of the tables committed to the tree (cargo xtask gen) and their pinned inputs in xtask/data/
scripts/ Release builder, development container (devrun.sh, dev/Containerfile), GPU lock helper, public-tree check
assets/brand/ Logo and icons
.agents/skills/ Agent skills for work on this tree (firecrest-port-model: the staged workflow for porting a new model)

Building and testing #

cargo build --release -p firecrest-cli        # the `firecrest` binary
cargo test --workspace                        # unit and fixture tests
  • nvcc is needed at build time only (CUDA_HOME, NVCC, or PATH). The kernel image carries code for FIRECREST_CUDA_SASS (default 80,86) plus PTX for FIRECREST_CUDA_ARCH (default sm_80). The build has no C++ dependencies.

  • Without a local CUDA toolkit or Rust, build in the development container: scripts/devrun.sh build (Podman or Docker plus the NVIDIA driver; scripts/devrun.sh run CMD and shell for anything else). The script also syncs the tree to another machine and builds there (scripts/devrun.sh remote build, with FIRECREST_DEV_REMOTE set in the untracked scripts/devrun.local). See "A build in the development container" in docs/operations.md and scripts/devrun.sh --help.

  • Tests and examples that need model files, reference dumps or a GPU read their locations from environment variables (FIRECREST_MODEL, FIRECREST_VISION, FIRECREST_REF_DIR, FIRECREST_INKLING_MODEL, FIRECREST_SMOLLM3_MODEL, FIRECREST_QWEN36_MODEL, FIRECREST_QWEN36_REF, FIRECREST_QWEN36_THREADS, ...) and skip with a message when one is unset. Each crate's README lists the variables it reads.

  • A kernel file that does not compile breaks every build, so check a new or changed file on its own before saving it under kernels/:

    nvcc -arch=sm_86 -O3 -I kernels --fatbin -o /dev/null kernels/FILE.cu
    

From issue to main #

The repository lives on Tangled, at do.llth.ing/firecrest, and the tg command line reaches its issues and pull requests.

  • Open issues are the roadmap. A feature, a design change or a change of rules starts as an issue that states the problem and a proposal. Its open questions are settled in the issue before the work starts. A bug fix or a documentation fix may go straight to a pull request.
  • Work happens on a topic branch, in a fork for contributors without push access. A branch name that starts with its author's handle (HANDLE.TOPIC) keeps branches from different people apart in a shared fork.
  • A pull request covers one topic. A pull request that needs another, unmerged one is stacked on that one's branch. The first lines of its description identify the pull request it builds on and list the commits that belong to it.
  • tg pr create opens a pull request from the pushed branch (--repo, --source-repo and --base for a fork). tg submits published commits and does not push, so the branch has to be pushed first. Each later round is a push of the branch followed by tg pr update ID.
  • A pull request description has four parts: a summary of the change, the changes by area, the testing with the hardware and commands, and the known limits. It describes the change against main, the state its readers know, and not against the earlier rounds of the same pull request.
  • A maintainer reviews the pull request. The author answers each review comment in a new round or in a reply. The maintainer merges by fast-forward when the branch sits on the tip of main, and otherwise asks for a rebase onto main. The history of main stays linear, without merge commits.
  • A commit that resolves an issue cites it in its body (Closes #12, Part of #11).

Rules for the code #

From scratch #

The engine's code is original: do not port code from other inference engines (ds4, llama.cpp/ggml, vLLM, ...) into it, whether wholesale or translated line by line. Read them, and the model's reference code, to learn the math and the file formats, then write idiomatic Rust and CUDA of your own. A short expression that a file format dictates may follow an MIT-licensed source when it is written in this codebase's style and cited. Tooling (generators, oracles, fixture writers) may use other projects' libraries, and format data that follows no rule, such as lookup tables, may be generated from an MIT-licensed source and listed with its notice in THIRD-PARTY.md.

Facts over guesses #

When a specification states something about a model, cite where it comes from (reference code file:line, model card, config). Mark anything not yet verified UNVERIFIED.

Unsafe code #

Rust code is safe by default, and unsafe code stays in small modules with a safe API on top. The crates that parse untrusted input (server, tokenizer, core, sampling, policy, adapters, the CLI and the model and engine crates) forbid(unsafe_code), as does every other crate without unsafe. Request data still reaches unsafe code and kernels through the engines: token ids, images, and the expert routing that a prompt steers. The soundness of an engine therefore rests on the checks at its submit and forward and on the bounds of the indices that a kernel or a raw-pointer loop uses.

unsafe is allowed for these uses and no others:

  • FFI: the CUDA driver and kernel launches;
  • mmap, and pinned or registered host memory;
  • SIMD intrinsics;
  • shared-memory parallelism through raw pointers.

SIMD code follows three rules:

  • It lives in a simd module of its crate.
  • Each SIMD kernel has a scalar twin, and a test compares the two: bit for bit where the code manipulates bits (quant decoding, scale unpacking), within a stated tolerance for plain float reductions.
  • New kernels are safe #[target_feature] functions, so that only the dispatch call and the raw loads and stores are unsafe.

Parallelism through raw pointers follows four rules:

  • The workspace has one pool module, and a new pool is a change to that module rather than a second one.
  • A raw-pointer pool is justified by a benchmark against a safe baseline (std::thread::scope or rayon), stated in the commit that adds it.
  • Its soundness does not depend on public fields. A panic in a worker poisons the step, and no later phase of that step runs.
  • It has Miri or loom tests where they can run.

Outside those modules, unsafe impl Send and unsafe impl Sync are not allowed, and neither get_unchecked nor unchecked arithmetic is used for speed unless the commit states the measured gain. Data-dependent indices (expert ids, top-k outputs, positions) are bounds-checked before they reach unsafe code or a kernel.

The workspace lints enforce part of this: unsafe_op_in_unsafe_fn and clippy::undocumented_unsafe_blocks are deny for every crate, so each unsafe block and unsafe impl carries a // SAFETY: comment that states why it is sound. Setting FIRECREST_SCALAR=1 makes every SIMD dispatch run the scalar twin (Miri always does), so a test run can cover both paths on one machine. The switch is for testing only and makes no difference to the GPU paths' safety. Code that predates these rules is brought in line when it is changed.

Kernel and host change together #

A change to a layout, an offset or an ABI constant shared by a kernel and its Rust side lands in one commit, both sides at once.

Workspace membership #

The root Cargo.toml lists members explicitly. A new crate needs its Cargo.toml and src/lib.rs (or main.rs) before it is added to members, and to [workspace.dependencies] if other crates depend on it.

No machine-specific material #

Code, comments, tests, error messages and docs contain no hostnames, no absolute paths of a particular machine and no names of private services. Tools take paths from arguments or environment variables. scripts/check-public.sh lists what remains; a machine may keep a list of its own private names in the untracked scripts/check-public.local.

Shared GPUs #

When several people or agents share a machine, GPU work is serialized by whatever means the machine uses. Tests and tools read FIRECREST_GPU (0, 1 or all: the GPUs they may use; two-GPU tests skip unless it is all) and FIRECREST_FUNC=1 (a small-footprint functional run: few expert slots, a small context, no timing).

Generated files #

Generators of code or data committed to the tree are written in Rust: a generator in xtask/ (cargo xtask gen), a build script, or a proc-macro where it removes repetition. Python stays for the oracles and fixture writers that run a Python reference implementation (engines/*/oracle, crates/firecrest-tokenizer/tools/ref, bench/). A generator reads only inputs pinned in the tree (xtask/data/) or crates pinned to an exact version in xtask/Cargo.toml, so every machine produces the same bytes. Each generated file states the command that wrote it and the version of its data. cargo xtask gen --check lists the generated files that differ from their generator's output, and cargo test --workspace runs that check. xtask/README.md lists the generators.

Docs and site stay current #

A change that alters behaviour, options, defaults, setup steps, APIs, file formats or measured numbers updates the documents that state them in the same commit: README.md, docs/, the crate and engine READMEs, and the project site in site/ (plain static files; site/README.md lists every fact the site states and the document it comes from). Check links with a local server before committing a site change.

No working notes in the tree #

Progress notes, hill-climb and experiment logs (PLAN.md, ledgers, PERF.md-style diaries), status files, hand-offs to the next agent, TODO lists and benchmark receipts are never committed here. They belong outside the checkout. The tree keeps the outcome: the code, and the documents above updated to state the current behaviour and the measured numbers, with the command that reproduces them.

Measured numbers #

A speed, memory or quality number in a document is stated with the means to reproduce it: the command line, the commit it was built from, and the hardware (GPU, CPU, host RAM) it ran on. Prefill and decode speeds come from firecrest bench ("Measuring speed" in docs/operations.md), with its command line next to the number. Its JSON line records the commit, the model file's digest and the hardware. Engine-specific tools (kernel_bench, bench_decode, bench_prefill, bench_gemm and the like) stay development tools, and a number from one of them states that tool's command. Numbers written before firecrest bench existed keep their own commands until they are measured again. A number that a later change makes stale is updated or removed in that change, as "Docs and site stay current" requires. A document states the result and the command; the run's raw output is a benchmark receipt and stays out of the tree.

Commit messages #

area: what the commit does

Why, and what was measured or tested, wrapped at 72 columns.
  • The subject is area: summary: imperative mood, lower case after the colon, no trailing period, at most 72 characters. The body is optional for small changes.

  • area names the part of the tree the commit changes:

    Area Path
    server, quant, gguf, cli, ... crates/firecrest-NAME (the crate name without firecrest-)
    cuda crates/firecrest-cuda (driver bindings)
    kernels kernels/ and crates/firecrest-kernels
    deepseek-v4.1, deepseek-v4.1/gpu, ... engines/deepseek-v4.1/ as a whole, or one of its crates
    inkling, inkling/draft, ... engines/inkling/ as a whole, or one of its crates
    smollm3, smollm3/gpu, ... engines/smollm3/ as a whole, or one of its crates
    qwen3.6, qwen3.6/gpu, ... engines/qwen3.6/ as a whole, or one of its crates
    docs, bench, ui, site, scripts, brand, skills, xtask docs/, bench/, ui/, site/, scripts/, assets/brand/, .agents/skills/, xtask/
    workspace the root Cargo.toml, toolchain and changes across the whole tree
  • A commit that spans areas takes the area it is mainly about; a kernel change with its host side takes the Rust side's area. Two areas may be joined with a comma (kernels, deepseek-v4.1/gpu: ...); more than two means workspace or a split commit.

  • Documentation updated alongside code (see "Docs and site stay current") takes the code's area.

Examples: kernels: fuse the MXFP4 dequant into the expert GEMV, deepseek-v4.1/prefill: chunk attention at 4096 tokens, server: reject tool calls with duplicate ids.

Checks before a pull request #

  • cargo test --workspace passes, which includes cargo xtask gen --check.
  • cargo clippy --all-targets -- -D warnings passes on each touched crate (-p NAME).
  • The touched Rust files are formatted (cargo fmt -p NAME, or rustfmt --edition 2024 FILE for single files; the tree as a whole is not yet formatted).
  • scripts/check-public.sh reports nothing.
  • Each changed kernel file passes the standalone nvcc check in "Building and testing".
  • A change to SIMD code passes its crate's tests a second time with FIRECREST_SCALAR=1.
  • A changed site passes a link check against a local server.

Agents and authorship #

Agents identify the text they write. An agent that writes an issue, a pull request, a review or a comment on the forge opens it with an italic line naming its model and its part in the text, for example *Reviewed by Claude Opus 5.5.*, *Written by Claude Opus 5.5.* or *Written by Claude Opus 5.5, edited by a maintainer.*. A commit written by an agent states the model in its body in the same form or in a Co-Authored-By trailer.