Files
leleka/crates/context/tests/context_lift.rs
T

161 lines
6.0 KiB
Rust

//! The context-pin lift as pure arithmetic: the policy's effective configuration drives the same cell
//! geometry the driver and runtime allocate from. The driver and runtime consume `QsaGeometry` through their
//! serving context (linux-only crates); everything they size is a function of the values asserted here.
use leleka_context::context_policy::{
CHARTER_PROFILES, ContextRequest, PolicyError, ROPE_N_ROT, resolve_context,
};
use leleka_context::geometry::{MAX_CELLS, QsaGeometry, TRAINED_CELLS};
use leleka_context::memory_plan::KvFormat;
use leleka_context::rope_scaling::{RopeScalingType, build_rope_table};
const GIB: u64 = 1 << 30;
fn geometry_of(
slots: u64,
ctx: u64,
vram: u64,
) -> (
leleka_context::context_policy::EffectiveContext,
QsaGeometry,
) {
let e = resolve_context(&ContextRequest::new(slots, ctx, KvFormat::Int8, vram)).unwrap();
let g = QsaGeometry::new(e.ctx_per_slot as usize).unwrap();
(e, g)
}
#[test]
fn default_one_slot_at_trained_context_is_unchanged() {
let (e, g) = geometry_of(1, 262_144, 32 * GIB);
assert!(!e.rope_active);
assert_eq!(e.rope.kernel_args(64), Default::default());
assert_eq!(g, QsaGeometry::TRAINED);
assert_eq!(g.cells(), TRAINED_CELLS);
assert_eq!(g.mapped_host_bytes(), Ok(276_825_088));
assert_eq!(g.stage_bytes(), Ok(277_086_212));
assert_eq!(g.pooled_rows(), 65_537);
assert_eq!(g.max_blocks(), 65_538);
assert_eq!(g.page_table_bytes(), 262_144);
}
#[test]
fn two_slots_of_256k_size_each_slot_like_the_default_without_scaling() {
let (e, g) = geometry_of(2, 262_144, 32 * GIB);
assert!(!e.rope_active);
assert_eq!(e.total_cells, 524_288);
assert_eq!(g, QsaGeometry::TRAINED, "per-slot buffers do not grow");
assert_eq!(e.rope_table_rows, TRAINED_CELLS as u64);
assert_eq!(
e.rope_table_bytes,
g.rope_table_bytes(ROPE_N_ROT).unwrap() as u64
);
}
#[test]
fn one_slot_of_512k_with_yarn_2_scales_every_buffer_formula() {
let (e, g) = geometry_of(1, 524_288, 32 * GIB);
let t = QsaGeometry::TRAINED;
assert!(e.rope_active);
assert_eq!(e.rope.kind, RopeScalingType::YaRN);
assert_eq!(e.rope.factor, 2.0);
assert_eq!(g.cells(), MAX_CELLS);
assert_eq!(g.pages(), 2 * t.pages());
assert_eq!(g.page_table_bytes(), 2 * t.page_table_bytes());
assert_eq!(g.pooled_rows(), 2 * t.pages() + 1);
assert_eq!(g.max_blocks(), 2 * t.pages() + 2);
assert_eq!(
g.host_payload_bytes().unwrap(),
2 * t.host_payload_bytes().unwrap()
);
assert_eq!(
g.mapped_host_bytes().unwrap(),
2 * t.host_payload_bytes().unwrap() + 1024
);
for (a, b) in g.stage_runs().unwrap().iter().zip(t.stage_runs().unwrap()) {
assert_eq!(*a, 2 * b);
}
assert_eq!(g.canonical_metadata_bytes().unwrap(), 131_072 * 160 + 4096);
assert_eq!(
e.rope_table_bytes,
g.rope_table_bytes(ROPE_N_ROT).unwrap() as u64
);
assert_eq!(e.rope_table_rows as usize, g.cells());
// the planner and the geometry agree on the total cell count
assert_eq!(e.total_cells as usize, g.cells());
}
#[test]
fn charter_profiles_all_resolve_and_total_at_most_512k_cells() {
for (slots, ctx) in CHARTER_PROFILES {
let (e, g) = geometry_of(slots, ctx, 32 * GIB);
assert_eq!(g.cells() as u64, ctx);
assert_eq!(e.rope_active, ctx > TRAINED_CELLS as u64);
assert!(e.total_cells <= MAX_CELLS as u64);
}
}
#[test]
fn planner_refusals() {
// KV does not close on the default 5.943 GB pool
let small = resolve_context(&ContextRequest::new(
1,
524_288,
KvFormat::Int8,
5_943_000_000,
))
.unwrap_err();
assert!(small.is_planner_rejection());
// two slots of 512K are past the charter and past the supported bound
assert!(matches!(
resolve_context(&ContextRequest::new(2, 524_292, KvFormat::Int8, 64 * GIB)),
Err(PolicyError::ContextTooLarge { .. })
));
// a context the geometry cannot represent is refused by the geometry too
assert!(QsaGeometry::new(MAX_CELLS + 4).is_err());
assert!(QsaGeometry::new(262_145).is_err());
// no scaling past the trained length is refused by the setup rule
let mut r = ContextRequest::new(1, 524_288, KvFormat::Int8, 32 * GIB);
r.rope_scaling = Some(RopeScalingType::None);
assert!(matches!(resolve_context(&r), Err(PolicyError::Setup(_))));
}
#[test]
fn boundary_positions_262143_262144_524287_524288() {
let t = QsaGeometry::TRAINED;
let m = QsaGeometry::new(MAX_CELLS).unwrap();
// `admits_end(n)` is "n tokens fit": the last valid position is n - 1
assert!(t.admits_end(262_143 + 1));
assert!(!t.admits_end(262_144 + 1));
assert!(
m.admits_end(262_144 + 1),
"262144 is a valid position at 512K"
);
assert!(m.admits_end(524_287 + 1));
assert!(!m.admits_end(524_288 + 1));
// YaRN 2 table: rows 262143..524287 exist, are finite, and differ from the unscaled rotation
let (e, _) = geometry_of(1, 524_288, 32 * GIB);
let scaled = build_rope_table(ROPE_N_ROT, &e.rope, MAX_CELLS).unwrap();
assert_eq!(scaled.n_pos, MAX_CELLS);
for pos in [262_143usize, 262_144, 524_287] {
assert!(
scaled
.row_cos(pos)
.iter()
.chain(scaled.row_sin(pos))
.all(|v| v.is_finite())
);
}
let (d, _) = geometry_of(1, 262_144, 32 * GIB);
let plain = build_rope_table(ROPE_N_ROT, &d.rope, TRAINED_CELLS).unwrap();
assert_eq!(plain.n_pos, TRAINED_CELLS);
// row 0 is angle 0: cos is the YaRN magnitude scale (1 + 0.1 ln 2), sin is 0; the unscaled table has 1 / 0
let ms = e.rope.mscale() as f32;
assert!(ms > 1.0);
assert!(scaled.row_cos(0).iter().all(|c| (*c - ms).abs() < 1e-6));
assert!(scaled.row_sin(0).iter().all(|v| *v == 0.0));
assert!(plain.row_cos(0).iter().all(|c| *c == 1.0));
// the scaled table is not the unscaled one at the last trained position
assert_ne!(scaled.row_sin(262_143), plain.row_sin(262_143));
}