Expand description
CUDA inference lane — dense batch GEMM via persistent WMMA (mode=cuda).
§Goal
Prefer tensor-core dense matmul for batch prefill-shaped GEMMs when:
InferenceMode::CudaTcis active (prefer_tensor_core_gemm)- dims can be padded to multiples of 16
- a dense f32 weight matrix is available (f16 p64 expand, or one-shot dequant)
Weight rows are cached on the CUDA slab by content fingerprint so subsequent chunks do not re-upload the same matrix (no host thrash for repeated layers).
§Honest limits
- Not a fused Q4_K dequant-GEMV on-device (llama.cpp-class) — that remains M2b.
- Quantized weights must be dequantized once to dense f32 before first cache insert.
- Slab is finite (256 MiB); LRU-ish eviction of oldest entries when full.
Structs§
- Mega
Pass Layer Dims - Per-layer matmul dimensions resolved during cold plan construction.
- Mega
Pass Layer Weights - Per-layer weight references for the prepared CUDA decode pass.
Enums§
Constants§
- MAX_
DENSE_ ELEMS - Max f32 elements per densified matrix (~192 MiB) — covers 3B FFN (~25M) with headroom.
- Q8_
0_ BLOCK_ BYTES - Q8_
0_ BLOCK_ ELEMS
Traits§
- Mega
Pass Plan View - Immutable view of the cold-prepared weights and tensor metadata.
Functions§
- cache_
dense_ weight - Insert or refresh a dense weight matrix (row-major n_out × n_in).
- cache_
dense_ weight_ direct - Allocate a 2 MiB-aligned buffer and fill it via
fill— skips the intermediateVec<f32>thatcache_dense_weightrequires. The closure receives a row-majorn_out × n_inslice to write into directly. - clear_
weight_ cache - Clear weight cache (tests / model swap).
- dense_
weight_ cached - True if a dense weight for
keyis already in the host TC cache. - device_
kv_ ready - True when a permanent device KV arena is resident (P4 path eligible).
- ensure_
device_ kv_ cache - Reserve permanent device KV matching host
KvCacheLayoutf32 indices (P4). Call before heavy weight preload when possible so the slab still has room. Returns false if layout is invalid or the slab cannot hold the arena. - preload_
q4k_ soa_ weights - Bulk-preload Q4_K_SOA weight blobs into the multi-weight CUDA slab.
Call once at plan build so first-token decode does not thrash PCIe.
Each entry is
(raw_bytes, n_in, n_out). Returns how many newly resident. - preload_
resident_ blob - Upload an immutable prepared-plan blob to the permanent CUDA slab.
- prepare_
mega_ pass_ kernels - Compile/load every kernel that a prepared mega-pass may dispatch.
- q4k_
device_ weight_ count - How many Q4 SoA matrices are sticky-resident on CUDA.
- q4k_
weight_ resident - q8_
0_ gemv_ oracle_ into - Decode a row-major GGML Q8_0 matrix and multiply it by
x. - try_
cuda_ batch_ gemv - Batch dense GEMM: for each of
batchrows of hidden (n_in), compute out (n_out). - try_
cuda_ batch_ gemv_ cached - Like
try_cuda_batch_gemvbut uses a cached weight by fingerprint if present; otherwise cachesweightunderkeythen multiplies. - try_
cuda_ batch_ gemv_ cached_ only - Lookup-only: run GEMV if
keyis already densified in the cache. - try_
cuda_ mega_ pass - Run from a caller-provided f32 embedding. Kept as the portable compatibility entry point.
- try_
q4k_ soa_ attention_ device - P4 device attention: sticky-x QKV (no intermediate D2H) → RoPE → device KV write → GQA SDPA → O-proj GEMV → one residual-delta readback.
- try_
q4k_ soa_ ffn_ block - Full pre-norm FFN expansion+down on CUDA with one host→device x upload and one device→host readback (mid stays on-device between kernels).
- try_
q4k_ soa_ ffn_ block_ residual - Like
try_q4k_soa_ffn_blockbut fuses residual on device:out = residual + FFN(x)with a single host readback. - try_
q4k_ soa_ fused_ swiglu - Fused SwiGLU expansion on two sticky Q4_K SoA weights (T-A2):
out[i] = silu(gate_row_i · x) * (up_row_i · x). One kernel, onexupload. - try_
q4k_ soa_ gemv - On-device Q4_K SoA dequant-GEMV (type 112 /
.soa.p64). - try_
q4k_ soa_ qkv - Project Q, K, and V from the same activation with one sticky x upload and
one fused QKV kernel (shared act tile; GQA: K/V only for
row < n_kv). - try_
q8_ 0_ cuda_ gemv - Execute one native CUDA Q8_0 GEMV. This public runner is a differential/profiling boundary; prepared whole-model decode uses the same kernel with pre-resident views.
- warm_
cuda_ context - Ensure the multi-weight CUDA context exists (NVIDIA clocks / driver warm). Safe to call from portable paths — brings A2000 out of idle so wgpu resident decode sees production clocks (measured ~4× vs cold portable on 3B).
- weight_
cache_ len - How many weight matrices are currently cached.
- weight_
fingerprint - FNV-1a over weight bytes + dims for cache key.