(very WIP!) Fast LLM inference in Rust & CUDA doll.tngl.io/firecrest

unsafe: rules for where unsafe is allowed, and reducing what we have #11

Open do.llth.ing
Open opened by and assigned to do.llth.ing

Written by Claude Opus 5.5.

Where the unsafe is #

At the tip of #5 there are about 1,070 unsafe blocks, functions and impls:

Crates Sites Mostly
dsv41/gpu, dsv41/prefill, inkling/gpu, qwen3.6/gpu, smollm3/gpu about 670 kernel launches and device pointers
firecrest-cuda 214 driver bindings
firecrest-quant 151 about 80 SIMD; about 40 thread pools that share buffers through raw pointers (pool.rs, and #5's workers, kpool); hostmem
others about 35 mmap, pinned memory, vision SIMD

The crates that handle untrusted input already forbid(unsafe_code): server, tokenizer, core, sampling, policy, adapters, cli and the model/engine crates. That covers only their own code. Request data (token ids, images, and the MoE routing a prompt steers) still reaches unsafe code and kernels through the engines. So the checks at each engine's submit/forward, and the indices a kernel or a raw-pointer loop uses, are where safety is actually decided.

AGENTS.md currently allows unsafe for FFI, SIMD intrinsics and mmap or pinned memory. The pools fit none of these. In #5 they had the one soundness bug the review found: after a worker panics, the other threads can reach aliased &mut views from safe code, because the invariant depends on pub fields.

Proposal #

Rules (AGENTS.md, or CONTRIBUTING.md, #12):

  1. Allowed uses, and only these:
    • FFI and kernel launches;
    • mmap and pinned or registered host memory;
    • SIMD intrinsics;
    • shared-memory parallelism.
  2. SIMD:
    • Lives in simd/ modules.
    • Has a scalar twin, tested against it: bit-identical where the code manipulates bits (quant decode, scale unpacking); within a stated tolerance for plain float reductions.
    • Uses safe #[target_feature] functions (current stable Rust) so that only the dispatch call and raw loads/stores are unsafe.
  3. Parallelism through raw pointers:
    • One pool module for the whole workspace.
    • Justified by a benchmark against a safe baseline (std::thread::scope, rayon).
    • Its safety may not depend on public fields.
    • A panic in a worker poisons the step, and no later phase runs.
    • Miri or loom tests where feasible.
  4. Everywhere else: no unsafe impl Send/Sync outside those modules, and no get_unchecked or unchecked arithmetic for speed without a measured gain stated in the commit.
  5. Data-dependent indices (expert ids, top-k outputs, positions) are bounds-checked before they reach unsafe code or a kernel.

Tooling:

  • Workspace lints: unsafe_op_in_unsafe_fn = "deny" and clippy::undocumented_unsafe_blocks = "deny", now set per crate.
  • forbid(unsafe_code) in every crate that has no unsafe. engines/smollm3/engine lacks it today.
  • A switch (feature or environment variable) that forces the scalar paths, so CI and Miri can run both. This is for testing, not a security feature: the GPU path can't be made safe by a flag.

Reduction work:

  • Shrink the SIMD unsafe with safe target_feature functions.
  • Merge workers/kpool and pool.rs.
  • Generate the kernel launch wrappers (#10). They are most of the remaining sites.
Sign up or Sign in to add to the discussion.