Files
leleka/crates/context/src/memory_plan.rs
T

133 lines
4.9 KiB
Rust

//! Memory-admission arithmetic: literal port of `include/strata/plan/plan.hpp` and the pool derivation in
//! `src/plan/plan_main.cpp` (the `strata-plan` CLI), plus the per-KV-format byte costs the Original defines in
//! `src/core/layer.cpp::kv_pool_bytes`, `kernels/kv_q8.hpp` and `kernels/kv_q4.hpp`.
//!
//! What is literal (golden-tested against the `strata-plan` binary built from the Original):
//! `Geometry`, `Costs`, `state_bytes`, `kv_bytes_per_token_int8`, `make_plan` (INT8 KV), `vram_pool_bytes`,
//! `cli_pool`, `Plan::to_text`, the `DoesNotClose` messages.
//!
//! What is an extension and is labelled as such: `make_plan_with_format` (the same arithmetic with another KV
//! format's per-cell bytes; the per-cell bytes are golden-tested against the Original's own `kv_pool_bytes`),
//! `engine_resident_estimate` (hand-derived from `qsa_state_bytes`; NOT used for admission), and
//! `slots`-aware helpers in `context_policy`.
//!
//! Known quirks of the Original that this port keeps on purpose (so answers are identical to `strata-plan`):
//! - the planner counts the indexer key store as `key_heads * key_dim / qsa_block` = 32 bytes per token and
//! layer, whereas `layer.cpp` allocates an fp32 pooled store (128 bytes per token and layer);
//! - the CLI subtracts the dense and embedding bytes from the free VRAM to form the pool and `make_plan`
//! subtracts them from that pool again (a conservative double count when both are non-zero);
//! - a derived pool of 0 silently becomes the 5.943 GB default pool.
//!
//! Overflow: the Original uses wrapping u64 arithmetic; this port uses checked arithmetic and reports
//! [`PlanError::Overflow`] instead (identical for every non-overflowing input).
//!
//! Pure functions, no I/O, no GPU. No performance or quality claim is made by this module.
use std::fmt;
/// `Geometry` of the real Qwen3.8-Flash-Next artifact (plan.hpp).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Geometry {
pub n_layers: u64,
pub n_qsa_layers: u64,
pub n_kv_heads: u64,
pub head_dim: u64,
pub kv_group: u64,
pub qsa_block: u64,
pub indexer_q_heads: u64,
pub indexer_key_heads: u64,
pub indexer_key_dim: u64,
pub ssm_state_size: u64,
pub ssm_v_heads: u64,
pub ssm_d_conv: u64,
pub ssm_conv_channels: u64,
}
/// Byte costs (plan.hpp `Costs`).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Costs {
pub expert_blob: u64,
pub dense_bytes: u64,
pub embd_bytes: u64,
pub mtp_bytes: u64,
pub workspace_bytes: u64,
pub state_bytes: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum PlanError {
/// plan.hpp `DoesNotClose`; the payload is the Original's exact `what()` text.
DoesNotClose(String),
/// Checked arithmetic overflowed (the Original would wrap silently).
Overflow,
Geometry(&'static str),
}
type R<T> = Result<T, PlanError>;
// ---- KV formats ------------------------------------------------------------------------------
/// KV cache storage formats of the Original (`--kv`). `Int8Keyless` is the experimental
/// `STRATA_KEYLESS_ROUTING` mode (`g_without_key`): half of INT8's bytes, no K store; it is not a default
/// and carries its own quality gate (charter: Value-Only Cache).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KvFormat {
Fp16,
Int8,
Q4_0,
K8V4,
Int8Keyless,
}
/// `KV_Q8_GROUP` (kv_q8.hpp) and `QK4_0`/`sizeof(block_q4_0)` (kv_q4.hpp).
pub const KV_Q8_GROUP: u64 = 64;
pub const QK4_0: u64 = 32;
pub const BLOCK_Q4_0_BYTES: u64 = 18;
/// The part of `QsaShapes` that sizes the KV pools.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct KvShapes {
pub n_head_kv: u64,
pub head_dim: u64,
/// KV page granule in cells (`qsa_real_shapes().page_size` = 4).
pub page_size: u64,
}
// ---- the plan --------------------------------------------------------------------------------
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Plan {
pub max_context: u64,
pub kv_bytes: u64,
pub state_bytes: u64,
pub cache_bytes: u64,
pub cache_slots: u64,
pub vram_budget: u64,
pub vram_used: u64,
pub dram_experts: u64,
pub notes: Vec<String>,
}
// ---- EXTENSION: advisory estimate of what the engine allocates ---------------------------------
/// Hand-derived from `layer.cpp::qsa_state_bytes` for a fully resident state (no `--kv-resident`
/// streaming). NOT golden-tested against the Original as a whole (only its `kv_pool_bytes` part is) and NOT
/// used for admission; it exists to show how far plan.hpp's indexer term is from the fp32 pooled store.
/// Excludes the step/pos state and 16-byte alignment (KiB scale).
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct EngineResidentEstimate {
pub kv_pools: u64,
pub page_tables: u64,
pub indexer_stores: u64,
pub rope_tables: u64,
pub total: u64,
}
#[cfg(test)]
mod tests;
mod geometry_sizes;
pub use geometry_sizes::*;
mod per_token_plan;
pub use per_token_plan::*;