All projects

Atlas

A Rust inference engine that runs DeepSeek-V4-Flash, a 149 GiB language model, on a single 24 GB AMD graphics card.

A recording of atlas-cli chat generating text on one RX 7900 XTX. Open the recording file (asciicast v2).

Recorded on one RX 7900 XTX (24 GB) with 64 GB RAM. Real time, idle pauses capped at 1 s.

What it is

  • An engine built from scratch in Rust to run DeepSeek-V4-Flash, one of the large open language models, on one consumer AMD graphics card.
  • The model’s weights take about 149 GiB and the card has 24 GB, so Atlas keeps the parts it needs on the GPU and moves the rest between system memory and the SSD as it generates.
  • You chat with it in the terminal, and it answers at about five tokens a second once warm.
  • On the real weights, its output for a token matches a separate reference implementation bit for bit, and that reference shares no code with the engine.

The problem

DeepSeek-V4-Flash is a sparse Mixture-of-Experts model. Each of its 43 layers has 256 routed experts, and only six of them are chosen for each token. That sparsity is what makes it possible to run at all on small hardware: most of the model sits idle at any moment, but you never know in advance which parts the next token will need.

The routed weights come to about 11,000 groups of 13.4 MB. Only a fraction of them fit on the GPU and in 64 GB of host memory at once, so the rest has to come off NVMe while the model is generating. The work was making that fast without changing a single bit of what the model computes.

How I built it

  • I split the engine into a Cargo workspace: a runtime for scheduling, residency, eviction, the KV cache and the forward pass, a backend-neutral trait layer, a ROCm backend, a scalar CPU backend, a checkpoint reader, the DeepSeek adapter, a tokenizer and a chat CLI.
  • The GPU kernels are written in HIP for AMD ROCm. HIP compiles with floating-point contraction on by default, which fuses a multiply and an add into one FMA even through explicit rounding helpers, so I build with it off and every operation rounds exactly as the CPU reference does.
  • The backend layer bundles every operation the model needs behind one trait, with a single wait in place of a synchronize call per operation, so the runtime, not each kernel call, decides when the host waits for the device.
  • Expert weights live in three tiers, GPU, RAM and NVMe, with explicit residency states. The library refuses to evict by default; the chat CLI opts into least-recently-used eviction, a decision recorded in an ADR.
  • Device and host budgets are sized from the model plan and the memory actually free when the chat starts. Video memory the desktop already holds is read from the amdgpu driver in sysfs, because HIP’s own query only subtracts the calling process’s allocations, and overcommitting the card made the driver spill Atlas buffers into system memory.
  • Sampling is greedy by default, with seeded temperature, top-k and top-p for reproducible variety.
  • The code holds itself to hard rules: no unsafe outside the ROCm crate, no unwrap, expect or panic in production code, and a CI check that keeps every production file small.

The interesting parts

Paging experts across GPU, RAM and NVMe

Each routed expert group has separate host and device residency states, so a RAM copy survives while its GPU transfer is in flight. Every expert a token selects is protected until the step runs, so a step that cannot fit fails with a capacity error instead of thrashing. My first rule for freeing RAM dropped the host copies of groups already on the GPU, since they looked redundant. They were exactly the groups the GPU was about to evict, so GPU churn turned into disk reads. Plain least-recently-used across both tiers cut checkpoint loads from about 57 to 31 per token, and victims now come from an ordered index instead of a scan over 11,000 groups.

Bit-identical output as the gate for every speed-up

The oracle is an independent scalar implementation that depends on no other Atlas crate. On the real weights, a single forward position produces logits whose FNV-1a fingerprint is identical on the GPU, the CPU backend and the oracle under the checkpoint’s quantization policy. I used that as a hard rule: an optimization was accepted only if it kept the floating-point reduction order, left the fingerprints unchanged and produced the identical 96-token output text. Parallel reads, indexed eviction and deferred synchronization all passed that gate on the way to about 180 ms per token.

Waiting on the GPU only when the host needs a result

On ROCm, freeing device memory implicitly waits for the GPU, so releasing intermediates as the forward pass goes would put back every wait I removed. The chat runs each token in a window: per-launch waits are skipped, freed activations go on a retiring list and are released only after a barrier, and the host waits only where it must read a result (router scores, indexer scores and the final logits) and once at the end. Every error path still runs the barrier before returning. With the same 96 tokens out, a token went from about 243 ms to about 190.

Direct I/O without unsafe code

A buffered read of one expert into a fresh buffer took about 5 ms. O_DIRECT alone was slower, because faulting in fresh pages dominated; into a page-aligned buffer that was already touched it took about 2 ms, so Atlas adopted both together. Safe Rust cannot allocate aligned memory, so each buffer is an ordinary Vec<u8> with a page-aligned window inside it, recycled through a bounded pool. The O_DIRECT descriptor is opened through /proc/self/fd, which names the already open file rather than resolving its path again. Eight reader threads fetch the selected experts of each routed step ahead, and if direct reads are refused it falls back to buffered reads.

How it runs

  • It runs as atlas-cli chat, a terminal UI, on one RX 7900 XTX with 24 GB of VRAM and 64 GB of host RAM.
  • Once caches are warm it generates a token in about 180 ms, roughly 5.5 tokens a second.
  • CI runs on CPU only: cargo fmt, the file-size check, clippy with warnings as errors, and the full workspace test suite.
  • The CPU backend keeps a journal and supports fault injection, so the runtime is tested in depth without a GPU.
  • A separate test sweep runs against the real checkpoint on the GPU, including multi-token generation.