spark_storage/lib.rs
1// SPDX-License-Identifier: AGPL-3.0-only
2
3#![deny(warnings)]
4#![deny(clippy::all)]
5
6// Atlas spark-storage: high-speed NVMe-backed KV cache offload.
7//
8// Phase 0 of `--high-speed-swap` (see plan at
9// /workspace/.claude/plans/i-want-to-ensure-valiant-bunny.md): runtime probe
10// that decides whether the production backend should be cuFile/GDS or
11// io_uring + pinned-host bounce. Later phases add the predictor, scratch
12// pool, eviction, and I/O thread.
13//
14// Feature gating: every module that touches the CUDA driver (raw FFI in
15// `cuda_min`, the module/event helpers in `cuda_module`, anything that
16// holds a `DeviceBuffer`) is gated behind the `cuda` feature so the
17// crate compiles on Apple Silicon (`--no-default-features --features
18// metal`) where the high-speed-swap path won't be reachable anyway.
19
20#[cfg(feature = "cuda")]
21pub mod cuda_graph;
22#[cfg(feature = "cuda")]
23pub mod cuda_min;
24#[cfg(feature = "cuda")]
25pub mod cuda_module;
26
27// Re-export the module/event/launch helpers from their new home so existing
28// `use spark_storage::cuda_min::{CudaModule, CudaEvent, launch_kernel}` paths
29// keep working.
30#[cfg(feature = "cuda")]
31pub use cuda_module::{CudaEvent, CudaModule, launch_kernel};
32
33// Pure CPU-side modules — types, configs, references. Always compiled.
34pub mod attention_ref;
35pub mod cascade_policy;
36pub mod config;
37pub mod eviction;
38pub mod expert;
39pub mod expert_pack;
40pub mod expert_peer;
41pub mod group;
42pub mod kv_paging;
43pub mod model_dims;
44pub mod predictor_ref;
45pub mod projection;
46// The one-sided RDMA KV transport backend — a `StorageBackend` impl that
47// offloads/restores KV groups to a `cache_peer` blade over verbs. cuda (for the
48// pinned-host bounce + copy_h2d) + the verbs shim.
49#[cfg(all(feature = "cuda", atlas_rdma_verbs))]
50pub mod rdma_kv_backend;
51pub mod rdma_snapshot;
52pub mod snapshot_swap;
53// RDMA weight-staging peer (RO): serves a model's safetensors shards to the
54// weight loader over one-sided READ for fast model swaps. `manifest`/`wire` are
55// un-gated; `serve`/`shard` are `cfg(unix)` internally.
56pub mod weight_peer;
57
58// KV overflow blade (cache-peer): a passive remote-RAM RW tier for the
59// high-speed-swap KV cache, served over one-sided RDMA. `cache_peer` is the
60// server; `blade_cap` is the process-global commit ledger it (and the
61// weight/expert peers) reserve against — CUDA-free arithmetic, so it's
62// `#[allow(dead_code)]` on verbs-OFF builds where the handshake that consumes
63// it is compiled out.
64#[cfg(unix)]
65#[allow(dead_code)]
66pub(crate) mod blade_cap;
67pub mod cache_peer;
68
69// `ModelDims` is a plain POD struct (no GPU state) that
70// `spark-model`'s public surface threads through every layer's
71// forward signature; it must stay reachable on metal builds even
72// though the high-speed-swap orchestrator that consumes it is
73// CUDA-gated.
74pub use model_dims::ModelDims;
75
76// `layout` opens disk files with `O_DIRECT` and pre-allocates via
77// `posix_fallocate` — both Linux-specific. Only the cuda-side modules
78// (high_speed_swap, backend/io_uring, backend/posix) consume it.
79//
80// The gate is `all(cuda, unix)`, NOT `cuda` alone: gating on the feature was
81// only ever sufficient because cuda implied Linux. CUDA on Windows breaks that
82// assumption, and io_uring has no Windows analogue at all. The whole NVMe /
83// RDMA cold-tier stack below is therefore unix-only, and a Windows `spark`
84// binary is built without it rather than against an unvalidated IOCP port.
85#[cfg(feature = "cuda")]
86pub mod layout;
87
88// CUDA-only modules: each holds raw `cu*` FFI calls or a `DeviceBuffer`,
89// or transitively imports from the cuda_* modules above. Gated together
90// because separating them would just smear the boundary.
91#[cfg(feature = "cuda")]
92pub mod backend;
93// The tier micro-benchmark drives io_uring directly (submission queues, not
94// the StorageBackend trait), so it is Linux-only along with io_uring itself.
95#[cfg(all(feature = "cuda", target_os = "linux"))]
96pub mod bench;
97// T1 write-back cache composite (wraps any StorageBackend). cuda but not verbs.
98#[cfg(feature = "cuda")]
99pub mod cascade_backend;
100#[cfg(feature = "cuda")]
101pub mod expert_arena;
102#[cfg(feature = "cuda")]
103pub mod expert_tier;
104// NVMe-backed n-gram embedding row cache (LongCat / Qwen3.8-Flash-Next):
105// pinned GPU-addressable slots + host-side CLOCK eviction, same arena
106// primitive as the expert tier.
107#[cfg(feature = "cuda")]
108pub mod ngram_cache;
109#[cfg(feature = "cuda")]
110mod ngram_cache_fault;
111// RDMA expert staging needs rdma-core (libibverbs), which is Linux-only.
112#[cfg(all(feature = "cuda", unix))]
113pub mod expert_tier_rdma;
114#[cfg(feature = "cuda")]
115pub mod high_speed_swap;
116#[cfg(feature = "cuda")]
117pub mod predictor;
118// Capability probe for cuFile / GPUDirect Storage, which NVIDIA ships for
119// Linux only. There is nothing to probe on other platforms.
120#[cfg(all(feature = "cuda", target_os = "linux"))]
121pub mod probe;
122#[cfg(feature = "cuda")]
123pub mod scratch_pool;
124#[cfg(feature = "cuda")]
125pub mod tiled_attention;
126// RDMA weight loader — the cuda client of `weight_peer` that one-sided-READs a
127// model's tensors into a `spark_runtime::weights::WeightStore` for fast swaps.
128// RDMA-stage a PEFT adapter's A/B tensors straight into a resident LoRA pool
129// slot (reuses the weight_peer manifest + wire; landing byte-identical to the
130// disk pack).
131pub mod weight_lora_rdma;
132#[cfg(feature = "cuda")]
133pub mod weight_tier_rdma;
134
135#[cfg(feature = "cuda")]
136pub use backend::{PosixBackend, ReadRequest, StorageBackend};
137// io_uring is Linux-only; everything else in the tier is portable.
138#[cfg(all(feature = "cuda", target_os = "linux"))]
139pub use backend::IoUringBackend;
140pub use config::HighSpeedSwapConfig;
141pub use eviction::EvictionPolicy;
142pub use expert::{
143 ExpertKey, ExpertLayout, ExpertRecordHeader, ExpertRecordId, ExpertRecordSpec, Proj, ProjBytes,
144};
145#[cfg(feature = "cuda")]
146pub use expert_arena::ExpertArena;
147pub use expert_pack::{ExpertFileReader, ExpertFileWriter};
148pub use expert_pack::{ExpertIndex, ProjData, ProjView, pack_record, unpack_record};
149#[cfg(feature = "cuda")]
150pub use expert_tier::{
151 ArenaSlot, ExpertResidency, ExpertTier, PosixTier, TierKind, UmaArenaTier, open_tier,
152};
153#[cfg(all(feature = "cuda", unix))]
154pub use expert_tier_rdma::RdmaTier;
155#[cfg(feature = "cuda")]
156pub use high_speed_swap::{HighSpeedSwap, install_local, local_installed, with_local};
157#[cfg(all(feature = "cuda", atlas_rdma_verbs))]
158pub use kv_paging::KvPagingBackend;
159#[cfg(feature = "cuda")]
160pub use ngram_cache::NgramRowCache;
161#[cfg(all(feature = "cuda", atlas_rdma_verbs))]
162pub use rdma_kv_backend::RdmaKvBackend;
163pub use rdma_snapshot::RdmaSnapshotArena;
164
165/// `true` iff `atlas_rdma_verbs` was re-emitted for this crate by build.rs (the
166/// one-sided verbs shim lives in the CUDA-free `atlas-rdma` crate; `rustc-cfg`
167/// doesn't cross crates, so build.rs re-emits it off atlas-rdma's `links`
168/// metadata). `rdma_verbs_probe_tests` asserts it, so a silent cfg evaporation
169/// fails `cargo test -p spark-storage --lib` on verbs hosts instead of
170/// green-building with the gated modules compiled out.
171pub const fn rdma_verbs_enabled() -> bool {
172 cfg!(atlas_rdma_verbs)
173}
174
175#[cfg(test)]
176mod rdma_verbs_probe_tests;
177
178// Stub surface — same names as the real orchestrator above so spark-model's
179// call sites compile unchanged. `with_local` always returns None (orchestrator
180// absent), `local_installed` is false, and `install_local` bails — see
181// `stubs.rs` for rationale.
182//
183// Gated on the cuda feature alone: the orchestrator itself is now portable
184// (its backend is an alias -- io_uring on Linux, the positional-I/O backend
185// elsewhere), so a Windows CUDA build gets the REAL tier, not this stub.
186#[cfg(not(feature = "cuda"))]
187mod stubs;
188#[cfg(not(feature = "cuda"))]
189pub use stubs::{HighSpeedSwap, install_local, local_installed, with_local};
190
191#[cfg(feature = "cuda")]
192pub use predictor::{Predictor, PredictorDims};
193#[cfg(all(feature = "cuda", target_os = "linux"))]
194pub use probe::{Backend, ProbeConfig, ProbeResult, run_probe};
195pub use projection::{PredictorShape, build_projection};
196#[cfg(feature = "cuda")]
197pub use tiled_attention::{TiledAttention, TiledAttentionDims};
198#[cfg(feature = "cuda")]
199pub use weight_lora_rdma::RdmaLoraLoader;
200pub use weight_lora_rdma::{LoraAbKind, LoraLandTarget};
201pub use weight_peer::{WeightManifest, WeightTensorRecord};
202#[cfg(feature = "cuda")]
203pub use weight_tier_rdma::RdmaWeightLoader;