use memra_engine::Engine;
use memra_engine::forward::argmax;
use memra_engine::glm_spec::{Glm5SpecKnobs, Glm5SpecSession};
use memra_engine::hybrid::HybridModel;
use memra_engine::model::GpuTensor;
use memra_engine::spec::{PEN_WINDOW_MAX, SpecSampling};
use memra_gguf::GgmlType;
use memra_gguf::config::{HfConfig, ModelConfig};
use memra_gguf::model_plan::ModelPlan;
use memra_gguf::source::{TensorSource, TensorView};
use memra_gguf::tensor_contract::{
CheckpointDialect, ContractOptions, LayerTensor, MtpTensor, OutputHead, TensorContract,
TensorId, TensorMatch,
};
use memra_reference::{ReferenceTensor, ReferenceWeights, deterministic_fixture};
use memra_sampling::{Sampler, SamplerConfig};
use sha2::{Digest, Sha256};
use std::borrow::Cow;
use std::cell::RefCell;
use std::collections::{BTreeMap, BTreeSet};
use std::path::{Path, PathBuf};
use std::rc::Rc;
const HIDDEN: usize = 128;
const VOCAB: u32 = 32;
const PROMPT: usize = 24;
const BLOCK: usize = 8;
const K: usize = BLOCK - 1;
fn gpu_guard() -> std::sync::MutexGuard<'static, ()> {
static GPU: std::sync::Mutex<()> = std::sync::Mutex::new(());
GPU.lock().unwrap_or_else(|poisoned| poisoned.into_inner())
}
fn force_true_f32() {
static ONCE: std::sync::Once = std::sync::Once::new();
ONCE.call_once(|| {
if std::env::var("NVIDIA_TF32_OVERRIDE").as_deref() != Ok("0") {
unsafe { std::env::set_var("NVIDIA_TF32_OVERRIDE", "0") };
}
});
}
fn mini_config_json() -> String {
r#"{
"model_type": "glm5_next_text",
"num_hidden_layers": 4,
"num_nextn_predict_layers": 1,
"hidden_size": 128,
"intermediate_size": 64,
"vocab_size": 32,
"max_position_embeddings": 512,
"rms_norm_eps": 1e-05,
"hidden_act": "silu",
"swiglu_limit": 10.0,
"tie_word_embeddings": true,
"hc_mult": 4,
"hc_eps": 1e-06,
"hc_sinkhorn_iters": 20,
"mhc": true,
"layer_types": ["linear_attention", "deepseek_sparse_attention",
"linear_attention", "deepseek_sparse_attention"],
"mlp_layer_types": ["dense", "sparse", "sparse", "sparse"],
"first_k_dense_replace": 1,
"indexer_types": ["full", "full", "full", "full"],
"linear_attn_config": {
"num_heads": 1,
"head_dim": 128,
"short_conv_kernel_size": 4,
"gate_lower_bound": -5.0,
"kda_layers": [0, 2],
"full_attn_layers": [1, 3]
},
"num_attention_heads": 2,
"num_key_value_heads": 2,
"q_lora_rank": 16,
"kv_lora_rank": 16,
"qk_head_dim": 16,
"qk_nope_head_dim": 16,
"qk_rope_head_dim": 0,
"v_head_dim": 16,
"mla_use_nope": true,
"index_n_heads": 1,
"index_head_dim": 8,
"index_topk": 8,
"index_kpool": 4,
"index_kpool_always_select_tail": true,
"index_kpool_compress": true,
"indexer_rope_interleave": true,
"index_share_for_mtp_iteration": true,
"n_routed_experts": 4,
"num_experts_per_tok": 2,
"moe_intermediate_size": 64,
"n_shared_experts": 1,
"scoring_func": "sigmoid",
"topk_method": "noaux_tc",
"routed_scaling_factor": 2.5,
"norm_topk_prob": true,
"n_group": 1,
"topk_group": 1,
"head_dim": 0,
"attention_bias": false,
"moe_router_dtype": "float32",
"dtype": "bfloat16"
}"#
.to_string()
}
fn mini_config() -> ModelConfig {
ModelConfig::from_hf(&HfConfig::parse(&mini_config_json()))
}
fn mini_plan(config: &ModelConfig) -> ModelPlan {
memra_gguf::model_packs::for_config(config)
.expect("glm5_next model pack matches the mini config")
.compile_plan(config)
.expect("mini glm5_next plan compiles")
}
fn varied(len: usize, seed: u64, spread: f32) -> Vec<f32> {
(0..len)
.map(|i| {
let x = (i as u64)
.wrapping_mul(6364136223846793005)
.wrapping_add(seed)
.rotate_left(17) as f64
/ u64::MAX as f64;
1.0 + spread * (x as f32 - 0.5)
})
.collect()
}
fn centered(len: usize, seed: u64, spread: f32) -> Vec<f32> {
varied(len, seed, spread)
.into_iter()
.map(|v| v - 1.0)
.collect()
}
fn fixture_weights(plan: &ModelPlan) -> ReferenceWeights {
let mut weights = deterministic_fixture(plan)
.expect("deterministic glm5 hc+mtp fixture")
.weights;
for (tensor, seed) in [
(MtpTensor::EmbeddingNorm, 0xE0_12u64),
(MtpTensor::HiddenNorm, 0x40_77),
(MtpTensor::OutputNorm, 0x5EAD),
] {
weights.insert(
TensorId::Mtp { depth: 0, tensor },
ReferenceTensor::new(vec![HIDDEN], varied(HIDDEN, seed, 0.8)).unwrap(),
);
}
weights
}
struct OwnedTensor {
bytes: Vec<u8>,
ne: Vec<u64>,
ggml_type: GgmlType,
}
struct FixtureSource {
config: ModelConfig,
tensors: BTreeMap<String, OwnedTensor>,
}
impl TensorSource for FixtureSource {
fn config(&self) -> ModelConfig {
self.config.clone()
}
fn find(&self, name: &str) -> Option<TensorView<'_>> {
let t = self.tensors.get(name)?;
Some(TensorView {
bytes: Cow::Borrowed(&t.bytes),
ggml_type: t.ggml_type,
ne: t.ne.clone(),
})
}
}
fn is_expert_bank(id: &TensorId) -> bool {
matches!(
id,
TensorId::Layer {
tensor: LayerTensor::MoeExpertGateBank
| LayerTensor::MoeExpertUpBank
| LayerTensor::MoeExpertDownBank,
..
}
)
}
fn fixture_source(config: &ModelConfig, plan: &ModelPlan) -> FixtureSource {
let weights = fixture_weights(plan);
let contract = TensorContract::for_plan(
plan,
CheckpointDialect::Gguf,
ContractOptions {
output_head: OutputHead::TiedToEmbedding,
},
)
.expect("contract for the mini glm5_next hc+mtp plan");
let mut tensors = BTreeMap::new();
for req in contract
.requirements
.iter()
.filter(|r| r.required || weights.contains_key(&r.id))
{
let tensor = weights
.get(&req.id)
.unwrap_or_else(|| panic!("reference fixture is missing {:?}", req.id));
let elements: usize = req.shape.iter().map(|&d| d as usize).product();
assert_eq!(
elements,
tensor.data.len(),
"shape mismatch for {:?}",
req.id
);
let (bytes, ggml_type) = if is_expert_bank(&req.id) {
(
memra_gguf::nvfp4_repack::f32_to_q8_0(&tensor.data),
GgmlType::Q8_0,
)
} else {
(
tensor.data.iter().flat_map(|v| v.to_le_bytes()).collect(),
GgmlType::F32,
)
};
let names = match req.match_mode {
TensorMatch::OneOf => &req.names[..1],
TensorMatch::All => req.names.as_slice(),
};
for name in names {
tensors.insert(
name.clone(),
OwnedTensor {
bytes: bytes.clone(),
ne: req.shape.clone(),
ggml_type,
},
);
}
}
FixtureSource {
config: config.clone(),
tensors,
}
}
fn tokens(n: usize, seed: u64) -> Vec<u32> {
let mut s = seed | 1;
(0..n)
.map(|_| {
s = s
.wrapping_mul(6_364_136_223_846_793_005)
.wrapping_add(1_442_695_040_888_963_407);
((s >> 33) as u32) % VOCAB
})
.collect()
}
fn f32_to_bf16_bytes(v: f32) -> [u8; 2] {
let bits = v.to_bits();
let rounded = bits.wrapping_add(0x7FFF + ((bits >> 16) & 1));
(((rounded >> 16) & 0xFFFF) as u16).to_le_bytes()
}
fn write_safetensors(path: &Path, tensors: &[(String, Vec<usize>, Vec<f32>)]) {
let mut header = String::from("{");
let mut data: Vec<u8> = Vec::new();
for (i, (name, shape, vals)) in tensors.iter().enumerate() {
let elements: usize = shape.iter().product();
assert_eq!(elements, vals.len(), "drafter fixture shape for {name}");
let start = data.len();
for v in vals {
data.extend_from_slice(&f32_to_bf16_bytes(*v));
}
let end = data.len();
if i > 0 {
header.push(',');
}
let dims: Vec<String> = shape.iter().map(|d| d.to_string()).collect();
header.push_str(&format!(
"\"{name}\":{{\"dtype\":\"BF16\",\"shape\":[{}],\"data_offsets\":[{start},{end}]}}",
dims.join(",")
));
}
header.push('}');
let hb = header.as_bytes();
let mut out = Vec::with_capacity(8 + hb.len() + data.len());
out.extend_from_slice(&(hb.len() as u64).to_le_bytes());
out.extend_from_slice(hb);
out.extend_from_slice(&data);
std::fs::write(path, out).expect("write drafter safetensors");
}
const DRAFT_LAYERS: usize = 2;
const DRAFT_NH: usize = 2;
const DRAFT_NKV: usize = 1;
const DRAFT_HD: usize = 32;
const DRAFT_FF: usize = 64;
const DRAFT_RANK: usize = 8;
const DRAFT_TOPK: usize = 4;
const DRAFT_GROUP: usize = 16;
const DRAFT_CONV_K: usize = 2;
fn write_mini_drafter(tag: &str) -> PathBuf {
let dir = std::env::temp_dir().join(format!("glm5-dflash-mini-{}-{tag}", std::process::id()));
std::fs::create_dir_all(&dir).expect("drafter fixture dir");
let config = format!(
r#"{{
"architectures": ["DFlash2DraftModel"],
"hidden_size": {HIDDEN},
"num_attention_heads": {DRAFT_NH},
"num_key_value_heads": {DRAFT_NKV},
"head_dim": {DRAFT_HD},
"intermediate_size": {DRAFT_FF},
"num_hidden_layers": {DRAFT_LAYERS},
"rms_norm_eps": 1e-06,
"sliding_window": 2048,
"is_causal": false,
"layer_types": ["sliding_attention", "sliding_attention"],
"rope_parameters": {{"rope_type": "default", "rope_theta": 1000000.0}},
"dflash_config": {{
"block_size": {BLOCK},
"mask_token_id": 4,
"target_layer_ids": [0, 1, 2],
"selector_rank": {DRAFT_RANK},
"selector_top_k": {DRAFT_TOPK},
"conv_kernel_size": {DRAFT_CONV_K},
"conv_group_size": {DRAFT_GROUP}
}}
}}"#
);
std::fs::write(dir.join("config.json"), config).expect("drafter config.json");
let h = HIDDEN;
let groups = h / DRAFT_GROUP;
let mut seed = 0x0DF1_A500_u64;
let mut next = |len: usize, spread: f32, norm: bool| -> Vec<f32> {
seed = seed.wrapping_add(0x9E37_79B9);
if norm {
varied(len, seed, spread)
} else {
centered(len, seed, spread)
}
};
let mut tensors: Vec<(String, Vec<usize>, Vec<f32>)> = Vec::new();
for i in 0..DRAFT_LAYERS {
let p = |s: &str| format!("layers.{i}.{s}");
tensors.push((
p("self_attn.q_proj.weight"),
vec![DRAFT_NH * DRAFT_HD, h],
next(DRAFT_NH * DRAFT_HD * h, 0.3, false),
));
tensors.push((
p("self_attn.k_proj.weight"),
vec![DRAFT_NKV * DRAFT_HD, h],
next(DRAFT_NKV * DRAFT_HD * h, 0.3, false),
));
tensors.push((
p("self_attn.v_proj.weight"),
vec![DRAFT_NKV * DRAFT_HD, h],
next(DRAFT_NKV * DRAFT_HD * h, 0.3, false),
));
tensors.push((
p("self_attn.o_proj.weight"),
vec![h, DRAFT_NH * DRAFT_HD],
next(h * DRAFT_NH * DRAFT_HD, 0.3, false),
));
tensors.push((
p("self_attn.q_norm.weight"),
vec![DRAFT_HD],
next(DRAFT_HD, 0.4, true),
));
tensors.push((
p("self_attn.k_norm.weight"),
vec![DRAFT_HD],
next(DRAFT_HD, 0.4, true),
));
tensors.push((p("input_layernorm.weight"), vec![h], next(h, 0.4, true)));
tensors.push((
p("post_attention_layernorm.weight"),
vec![h],
next(h, 0.4, true),
));
tensors.push((
p("mlp.gate_proj.weight"),
vec![DRAFT_FF, h],
next(DRAFT_FF * h, 0.3, false),
));
tensors.push((
p("mlp.up_proj.weight"),
vec![DRAFT_FF, h],
next(DRAFT_FF * h, 0.3, false),
));
tensors.push((
p("mlp.down_proj.weight"),
vec![h, DRAFT_FF],
next(h * DRAFT_FF, 0.3, false),
));
for conv in ["attention_conv", "mlp_conv"] {
tensors.push((
p(&format!("{conv}.base_kernel")),
vec![2, DRAFT_CONV_K, h],
next(2 * DRAFT_CONV_K * h, 0.4, false),
));
tensors.push((
p(&format!("{conv}.kernel_projection.weight")),
vec![2 * DRAFT_CONV_K * groups, h],
next(2 * DRAFT_CONV_K * groups * h, 0.3, false),
));
}
}
let n_taps = 3usize;
tensors.push((
"fc.weight".into(),
vec![h, n_taps * h],
next(h * n_taps * h, 0.3, false),
));
tensors.push(("hidden_norm.weight".into(), vec![h], next(h, 0.4, true)));
tensors.push(("norm.weight".into(), vec![h], next(h, 0.4, true)));
tensors.push((
"candidate_selector.hidden_projection.weight".into(),
vec![DRAFT_RANK, h],
next(DRAFT_RANK * h, 0.3, false),
));
tensors.push((
"candidate_selector.predecessor_codebook".into(),
vec![VOCAB as usize, DRAFT_RANK],
next(VOCAB as usize * DRAFT_RANK, 1.6, false),
));
tensors.push((
"candidate_selector.successor_codebook".into(),
vec![VOCAB as usize, DRAFT_RANK],
next(VOCAB as usize * DRAFT_RANK, 1.6, false),
));
write_safetensors(&dir.join("model.safetensors"), &tensors);
dir
}
fn write_ranks_fixture(tag: &str, ranks: &[u32]) -> PathBuf {
let path = std::env::temp_dir().join(format!(
"glm5-dflash-ranks-{}-{tag}.txt",
std::process::id()
));
let text: String = ranks.iter().map(|t| format!("{t}\n")).collect();
std::fs::write(&path, text).expect("write ranks fixture");
path
}
fn sha16_of(path: &Path) -> String {
let bytes = std::fs::read(path).expect("read ranks fixture");
Sha256::digest(&bytes)
.iter()
.take(8)
.map(|b| format!("{b:02x}"))
.collect()
}
struct Harness {
engine: Engine,
model: HybridModel,
plan: ModelPlan,
drafter_dir: PathBuf,
ranks_path: Option<PathBuf>,
}
impl Drop for Harness {
fn drop(&mut self) {
std::fs::remove_dir_all(&self.drafter_dir).ok();
if let Some(p) = self.ranks_path.as_ref() {
std::fs::remove_file(p).ok();
}
unsafe {
std::env::remove_var("MEMRA_GLM5_DFLASH");
std::env::remove_var("MEMRA_FRSPEC_TRIM");
}
}
}
impl Harness {
fn new(tag: &str) -> Self {
Self::build(tag, None)
}
fn with_trim(tag: &str, ranks: &[u32]) -> Self {
let path = write_ranks_fixture(tag, ranks);
Self::build(tag, Some(path))
}
fn build(tag: &str, ranks_path: Option<PathBuf>) -> Self {
force_true_f32();
let drafter_dir = write_mini_drafter(tag);
unsafe {
match ranks_path.as_ref() {
Some(p) => std::env::set_var("MEMRA_FRSPEC_TRIM", p),
None => std::env::remove_var("MEMRA_FRSPEC_TRIM"),
}
std::env::remove_var("MEMRA_GLM5_MTP"); std::env::remove_var("MEMRA_GLM5_DFLASH_GATE_RED");
std::env::set_var("MEMRA_GLM5_DFLASH", &drafter_dir);
}
let config = mini_config();
let plan = mini_plan(&config);
let source = fixture_source(&config, &plan);
let engine = Engine::new(0).expect("CUDA engine on device 0");
let model = HybridModel::load_from_source(&engine, &source)
.expect("mini glm5 loads with the DFlash2 drafter");
assert!(model.hyper.is_some(), "hc trunk expected");
assert!(
model.mtp.is_none(),
"the q38 pattern: the native MTP head must NOT load on the dflash route"
);
assert!(
model.glm5_dflash.is_some(),
"MEMRA_GLM5_DFLASH must attach the drafter"
);
assert_eq!(
model.dflash_trim.is_some(),
ranks_path.is_some(),
"the draft-head slab loads iff MEMRA_FRSPEC_TRIM names a ranks file"
);
Self {
engine,
model,
plan,
drafter_dir,
ranks_path,
}
}
fn fresh_primed(
&self,
prompt: &[u32],
max_ctx: usize,
) -> (memra_engine::cache::Cache, Vec<f32>) {
let mut cache = memra_engine::cache::Cache::new_planned(
&self.engine,
&self.model.cfg,
&self.plan,
max_ctx,
)
.expect("cache for the mini glm5 model");
let (logits, _seed, _hiddens) = self
.model
.prime_cache(&self.engine, prompt, &mut cache, 0)
.expect("hc prime");
(cache, logits)
}
}
fn plain_tape(h: &Harness, prompt: &[u32], max_new: usize) -> Vec<u32> {
let (mut cache, logits) = h.fresh_primed(prompt, prompt.len() + max_new + 16);
let mut tape = Vec::with_capacity(max_new);
tape.push(argmax(&logits) as u32);
while tape.len() < max_new {
let ll = h
.model
.decode_step(&h.engine, *tape.last().unwrap(), &mut cache)
.expect("plain decode step");
tape.push(argmax(&ll) as u32);
}
tape
}
fn drive_bursts(
h: &Harness,
sess: &mut Glm5SpecSession,
prompt: &[u32],
k: usize,
total: usize,
burst_target: usize,
eos: &[u32],
) -> (Vec<u32>, usize, usize, usize) {
let mut tape: Vec<u32> = Vec::new();
let mut drafted = 0usize;
let mut accepted = 0usize;
let mut bursts = 0usize;
while tape.len() < total && !sess.finished() {
let room = (total - tape.len()).min(burst_target);
let (burst, d, a) = h
.model
.glm5_spec_session_burst(&h.engine, sess, room, k, eos)
.expect("glm5 dflash spec session burst");
if burst.is_empty() {
break;
}
bursts += 1;
drafted += d;
accepted += a;
tape.extend(burst);
assert_eq!(
sess.pos(),
sess.committed.len(),
"cache rows != committed tokens at a burst boundary"
);
let mut expect: Vec<u32> = prompt.to_vec();
expect.extend_from_slice(&tape[..tape.len() - 1]);
assert_eq!(
sess.committed, expect,
"committed must be prompt + served tape minus the live anchor"
);
}
(tape, drafted, accepted, bursts)
}
fn drive_gated(
h: &Harness,
sess: &mut Glm5SpecSession,
prompt: &[u32],
k: usize,
total: usize,
burst_target: usize,
knobs: &mut Glm5SpecKnobs<'_>,
) -> (Vec<u32>, usize, usize, usize) {
let mut tape: Vec<u32> = Vec::new();
let mut drafted = 0usize;
let mut accepted = 0usize;
let mut bursts = 0usize;
while tape.len() < total && !sess.finished() {
let room = (total - tape.len()).min(burst_target);
let (burst, d, a) = h
.model
.glm5_spec_session_burst_gated(&h.engine, sess, room, k, &[], knobs)
.expect("glm5 dflash gated spec session burst");
if burst.is_empty() {
break;
}
bursts += 1;
drafted += d;
accepted += a;
tape.extend(burst);
assert_eq!(sess.pos(), sess.committed.len());
let mut expect: Vec<u32> = prompt.to_vec();
expect.extend_from_slice(&tape[..tape.len() - 1]);
assert_eq!(sess.committed, expect);
}
(tape, drafted, accepted, bursts)
}
fn census_knob(census: &Rc<RefCell<Vec<u32>>>) -> impl FnMut(usize, usize, u32) -> u32 + use<> {
let c = Rc::clone(census);
move |_round: usize, _ki: usize, d: u32| -> u32 {
c.borrow_mut().push(d);
d
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_served_bursts_greedy_tape_matches_plain_decode_k1_to_7() {
let _gpu = gpu_guard();
let h = Harness::new("g1");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let tape = plain_tape(&h, &prompt, max_new);
for k in 1..=K {
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("glm5 dflash spec session");
let (out, drafted, accepted, bursts) =
drive_bursts(&h, &mut sess, &prompt, k, max_new, 3, &[]);
assert_eq!(
&out[..max_new],
&tape[..],
"K={k}: dflash served-burst tape diverged from plain greedy \
({accepted}/{drafted} over {bursts} bursts) — the draft source may only move \
acceptance, never output"
);
assert!(
bursts >= max_new / (k + 2),
"K={k}: the drive never actually split into bursts ({bursts})"
);
println!(
"gate 1 PASS K={k}: dflash served bursts byte-identical over {bursts} bursts, \
{accepted}/{drafted} accepted"
);
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_forced_rejection_partial_accepts_stay_byte_identical() {
let _gpu = gpu_guard();
let h = Harness::new("g2");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let k = K;
let tape = plain_tape(&h, &prompt, max_new + k + 2);
let tape_for_override = tape.clone();
let committed_before = move |round: usize| -> usize {
let mut c = 1usize;
for r in 0..round {
c += (r % k) + 1;
}
c
};
let mut over = move |round: usize, ki: usize, _drafted: u32| -> u32 {
let j_target = round % k;
let cursor = committed_before(round);
let pos = cursor + ki;
let correct = if pos < tape_for_override.len() {
tape_for_override[pos]
} else {
0
};
if ki < j_target {
correct
} else {
(correct + 1) % VOCAB
}
};
let mut knobs = Glm5SpecKnobs {
draft_override: Some(&mut over),
..Default::default()
};
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("glm5 dflash spec session");
let mut out: Vec<u32> = Vec::new();
let mut bursts = 0usize;
let mut drafted = 0usize;
let mut accepted = 0usize;
while out.len() < max_new && !sess.finished() {
let room = (max_new - out.len()).min(2);
let (burst, d, a) = h
.model
.glm5_spec_session_burst_gated(&h.engine, &mut sess, room, k, &[], &mut knobs)
.expect("forced-rejection served burst");
if burst.is_empty() {
break;
}
bursts += 1;
drafted += d;
accepted += a;
out.extend(burst);
assert_eq!(sess.pos(), sess.committed.len());
}
assert_eq!(
&out[..max_new],
&tape[..max_new],
"forced-rejection dflash bursts diverged from plain greedy"
);
let (mut exp_drafted, mut exp_accepted, mut committed, mut round) = (0usize, 0, 0, 0);
while committed < max_new {
let j = round % k;
exp_drafted += k;
exp_accepted += j;
committed += j + 1;
round += 1;
}
assert_eq!(
(drafted, accepted),
(exp_drafted, exp_accepted),
"the j-sweep must accept exactly j drafts per round (a vacuous sweep means the \
dflash accept mapping is dead)"
);
assert!(accepted > 0, "the sweep never exercised a real accept");
println!(
"gate 2 PASS: forced-rejection j-sweep byte-identical over {bursts} bursts, \
schedule-exact {accepted}/{drafted} accepted"
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_tap_shift_red_arm_moves_drafts_never_the_tape() {
let _gpu = gpu_guard();
let h = Harness::new("g3");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let tape = plain_tape(&h, &prompt, max_new);
let k = K;
let run = |red: bool| -> (Vec<u32>, Vec<u32>, usize, usize) {
unsafe {
if red {
std::env::set_var("MEMRA_GLM5_DFLASH_GATE_RED", "tap-shift");
} else {
std::env::remove_var("MEMRA_GLM5_DFLASH_GATE_RED");
}
}
let mut drafts: Vec<u32> = Vec::new();
let mut out: Vec<u32> = Vec::new();
let mut drafted = 0usize;
let mut accepted = 0usize;
{
let mut rec = |_round: usize, _ki: usize, d: u32| -> u32 {
drafts.push(d);
d
};
let mut knobs = Glm5SpecKnobs {
draft_override: Some(&mut rec),
..Default::default()
};
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("glm5 dflash spec session");
while out.len() < max_new && !sess.finished() {
let (burst, d, a) = h
.model
.glm5_spec_session_burst_gated(&h.engine, &mut sess, 3, k, &[], &mut knobs)
.expect("burst");
if burst.is_empty() {
break;
}
drafted += d;
accepted += a;
out.extend(burst);
}
}
unsafe { std::env::remove_var("MEMRA_GLM5_DFLASH_GATE_RED") };
(out, drafts, drafted, accepted)
};
let (out_green, drafts_green, d_g, a_g) = run(false);
let (out_red, drafts_red, d_r, a_r) = run(true);
assert_eq!(
&out_green[..max_new],
&tape[..],
"green arm tape must match plain greedy"
);
assert_eq!(
&out_red[..max_new],
&tape[..],
"RED ARM TAPE DIVERGED: wrong drafter features must be invisible in the output — \
only acceptance may move (the exactness seam is verify, not the drafter)"
);
assert_ne!(
drafts_green, drafts_red,
"tap-shift did not change the draft stream — the feature seam is DEAD (the drafter \
is not consuming the tapped trunk features)"
);
let acc_g = a_g as f64 / d_g.max(1) as f64;
let acc_r = a_r as f64 / d_r.max(1) as f64;
assert!(
acc_r <= acc_g + 1e-9,
"wrong features IMPROVED acceptance ({acc_r:.3} > {acc_g:.3}) — the tap layers are \
mislabeled"
);
println!(
"gate 3 PASS: tape byte-identical both arms; drafts diverged; acceptance green \
{a_g}/{d_g}={acc_g:.3} vs red {a_r}/{d_r}={acc_r:.3} (collapse magnitude lands on \
the box with the real artifact — probe band 0.73 acc@1)"
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_rollback_disabled_bites() {
let _gpu = gpu_guard();
let h = Harness::new("g4");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let tape = plain_tape(&h, &prompt, max_new);
let mut over = |_round: usize, ki: usize, drafted: u32| -> u32 {
if ki == 0 {
(drafted + 1) % VOCAB
} else {
drafted
}
};
let mut knobs = Glm5SpecKnobs {
draft_override: Some(&mut over),
disable_rollback: true,
..Default::default()
};
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + K + 8, None)
.expect("glm5 dflash spec session");
let mut out: Vec<u32> = Vec::new();
let mut failed: Option<String> = None;
while out.len() < max_new && !sess.finished() {
match h
.model
.glm5_spec_session_burst_gated(&h.engine, &mut sess, 3, K, &[], &mut knobs)
{
Ok((burst, _d, _a)) => {
if burst.is_empty() {
break;
}
out.extend(burst);
}
Err(err) => {
failed = Some(err.to_string());
break;
}
}
}
match failed {
Some(err) => println!("gate 4 RED bites: dflash burst failed loudly: {err}"),
None => {
assert_ne!(
&out[..max_new.min(out.len())],
&tape[..max_new.min(out.len())],
"rollback disabled + forced rejections still produced the plain tape — the \
red arm went blind on the dflash source"
);
println!("gate 4 RED bites: dflash tape diverged with rollback disabled");
}
}
}
fn sampled_cfg(seed: u64) -> SpecSampling {
SpecSampling {
temp: 0.9,
seed,
top_k: 0,
top_p: 1.0,
min_p: 0.0,
penalty_last_n: 0,
penalty_repeat: 1.0,
penalty_freq: 0.0,
penalty_present: 0.0,
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_sampled_twin_is_deterministic_and_burst_split_invariant() {
let _gpu = gpu_guard();
let h = Harness::new("g5");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 24usize;
let k = 3usize;
let ctx = prompt.len() + max_new + k + 8;
let run = |seed: u64, burst_target: usize| -> Vec<u32> {
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, Some(sampled_cfg(seed)))
.expect("sampled glm5 dflash spec session");
let (tape, _d, _a, _b) =
drive_bursts(&h, &mut sess, &prompt, k, max_new, burst_target, &[]);
tape[..max_new.min(tape.len())].to_vec()
};
let a = run(42, 3);
let b = run(42, 3);
assert_eq!(a, b, "same seed, same burst split: reproducible");
let c = run(42, max_new);
assert_eq!(
a, c,
"burst-split invariance: the selector's draws and accept uniforms ride the \
session's Philox counters, so the split must not change the stream"
);
let d = run(43, 3);
assert_ne!(a, d, "a different seed must change the sampled tape");
println!("gate 5 PASS: dflash sampled twin deterministic, split-invariant, seed-sensitive");
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_eos_finishes_the_session() {
let _gpu = gpu_guard();
let h = Harness::new("g6");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let tape = plain_tape(&h, &prompt, max_new);
let eos = [tape[6]];
let first_eos = tape.iter().position(|t| eos.contains(t)).unwrap();
let k = 3usize;
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("glm5 dflash spec session");
let mut out: Vec<u32> = Vec::new();
while out.len() < max_new && !sess.finished() {
let (burst, _d, _a) = h
.model
.glm5_spec_session_burst(&h.engine, &mut sess, 3, k, &eos)
.expect("burst");
if burst.is_empty() {
break;
}
out.extend(burst);
}
assert!(sess.finished(), "EOS must finish the session");
let cut = out
.iter()
.position(|t| eos.contains(t))
.expect("EOS emitted");
assert_eq!(
&out[..=cut],
&tape[..=first_eos],
"the public prefix through EOS must match plain greedy"
);
let (again, d2, a2) = h
.model
.glm5_spec_session_burst(&h.engine, &mut sess, 8, k, &eos)
.expect("post-EOS burst");
assert!(
again.is_empty() && d2 == 0 && a2 == 0,
"a finished session must emit nothing"
);
println!("gate 6 PASS: EOS at pos {first_eos} finished the dflash session");
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_k_past_the_block_refuses_loudly() {
let _gpu = gpu_guard();
let h = Harness::new("g7");
let prompt = tokens(PROMPT, 0xA11CE);
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + 40, None)
.expect("glm5 dflash spec session");
let err = h
.model
.glm5_spec_session_burst(&h.engine, &mut sess, 8, BLOCK, &[])
.expect_err("K == block_size must refuse");
assert!(
err.to_string().contains("DFlash2 drafter's block"),
"refusal must name the drafter block bound, got: {err}"
);
println!("gate 7 PASS: K={BLOCK} refused loudly ({err})");
}
#[test]
#[ignore = "receipt-gate child body; spawned by gpu_draft_source_selection_matrix"]
fn helper_emit_dflash_receipts() {
let _gpu = gpu_guard();
force_true_f32();
let with_mtp = std::env::var("MEMRA_GLM5_MTP").as_deref() == Ok("1");
let config = mini_config();
let plan = mini_plan(&config);
let source = fixture_source(&config, &plan);
let engine = Engine::new(0).expect("CUDA engine on device 0");
let model = HybridModel::load_from_source(&engine, &source).expect("mini glm5 loads per env");
let (free, total) = engine.ctx().mem_get_info().expect("mem_get_info");
eprintln!(
"[helper] vram-used-at-ready-mib={} mtp={} dflash={}",
(total - free) >> 20,
with_mtp,
model.glm5_dflash.is_some()
);
if memra_engine::glm_spec::glm5_spec_on()
&& (model.mtp.is_some() || model.glm5_dflash.is_some())
{
let prompt = tokens(PROMPT, 0xA11CE);
let mut sess = model
.glm5_spec_session_new(&engine, &prompt, prompt.len() + 40, None)
.expect("glm5 spec session");
let (burst, d, a) = model
.glm5_spec_session_burst(&engine, &mut sess, 8, 3, &[])
.expect("burst");
eprintln!("[helper] burst={} drafted={d} accepted={a}", burst.len());
eprintln!(
"[helper] trim_rounds={} rounds={}",
sess.rank_trimmed_rounds, sess.rounds
);
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_draft_source_selection_matrix() {
let _gpu = gpu_guard();
let drafter_dir = write_mini_drafter("matrix");
let run_child = |glm5_spec: Option<&str>,
glm5_mtp: Option<&str>,
dflash: Option<&Path>,
trim: Option<&Path>|
-> (bool, String) {
let exe = std::env::current_exe().expect("test binary path");
let mut cmd = std::process::Command::new(exe);
cmd.args([
"helper_emit_dflash_receipts",
"--exact",
"--ignored",
"--nocapture",
"--test-threads=1",
]);
cmd.env_remove("MEMRA_GLM5_SPEC");
cmd.env_remove("MEMRA_GLM5_MTP");
cmd.env_remove("MEMRA_GLM5_DFLASH");
cmd.env_remove("MEMRA_GLM5_DFLASH_GATE_RED");
cmd.env_remove("MEMRA_FRSPEC_TRIM");
cmd.env("NVIDIA_TF32_OVERRIDE", "0");
if let Some(v) = glm5_spec {
cmd.env("MEMRA_GLM5_SPEC", v);
}
if let Some(v) = glm5_mtp {
cmd.env("MEMRA_GLM5_MTP", v);
}
if let Some(dir) = dflash {
cmd.env("MEMRA_GLM5_DFLASH", dir);
}
if let Some(path) = trim {
cmd.env("MEMRA_FRSPEC_TRIM", path);
}
let out = cmd.output().expect("spawn receipt child");
(
out.status.success(),
String::from_utf8_lossy(&out.stderr).into_owned(),
)
};
let run_ok = |glm5_spec: Option<&str>,
glm5_mtp: Option<&str>,
dflash: Option<&Path>,
trim: Option<&Path>|
-> String {
let (ok, log) = run_child(glm5_spec, glm5_mtp, dflash, trim);
assert!(
ok,
"receipt child failed (spec={glm5_spec:?} mtp={glm5_mtp:?} dflash={} trim={}):\n{log}",
dflash.is_some(),
trim.is_some()
);
log
};
let vram_mib = |log: &str| -> u64 {
log.lines()
.find_map(|l| l.strip_prefix("[helper] vram-used-at-ready-mib="))
.and_then(|rest| rest.split_whitespace().next())
.and_then(|v| v.parse().ok())
.expect("vram line")
};
let log = run_ok(Some("1"), None, Some(&drafter_dir), None);
assert!(
log.contains("draft source = dflash2 @ "),
"dflash2 selection receipt missing:\n{log}"
);
assert!(
log.contains("native MTP head NOT loaded"),
"the head-not-loaded note is the VRAM receipt:\n{log}"
);
assert!(
log.contains("draft head FULL target vocab"),
"no ranks file = the full-head note stays:\n{log}"
);
assert!(
!log.contains("RANK-TRIMMED"),
"no ranks file must never print a trim receipt:\n{log}"
);
assert!(
log.contains("[helper] burst="),
"dflash child never burst:\n{log}"
);
let vram_dflash = vram_mib(&log);
let log = run_ok(Some("1"), Some("1"), Some(&drafter_dir), None);
assert!(
log.contains("draft source = dflash2 @ ") && log.contains("ALSO loaded"),
"both-armed selection must state dflash2 wins:\n{log}"
);
let log = run_ok(Some("1"), Some("1"), None, None);
assert!(
log.contains("[glm5-spec] draft source = native-mtp"),
"native-mtp selection receipt missing:\n{log}"
);
let vram_mtp = vram_mib(&log);
let log = run_ok(Some("1"), None, None, None);
assert!(
log.contains("[glm5-spec] MEMRA_GLM5_SPEC=1 but no MTP head loaded"),
"fail-closed warn missing:\n{log}"
);
let ranks: Vec<u32> = (0..VOCAB).rev().filter(|t| *t != 4 && *t != 9).collect();
let n_ranks = ranks.len();
let ranks_path = write_ranks_fixture("matrix-ok", &ranks);
let sha16 = sha16_of(&ranks_path);
let log = run_ok(Some("1"), None, Some(&drafter_dir), Some(&ranks_path));
let armed = format!("draft head RANK-TRIMMED n_ranks={n_ranks} src={sha16}");
assert!(
log.contains("[glm5-spec] serve route ARMED: draft source = dflash2 @ ")
&& log.contains(&armed),
"ARMED line must carry `{armed}`:\n{log}"
);
assert!(
!log.contains("FULL target vocab"),
"a loaded slab must retire the full-head note:\n{log}"
);
assert!(
log.contains(&format!(
"[frspec-trim] glm5 DFlash2 draft-head slab: {n_ranks} rows of {VOCAB} gathered \
from main output.weight"
)) && log.contains(&format!("src={sha16}")),
"slab build receipt missing:\n{log}"
);
assert!(
log.contains(&format!(
"[glm5-spec] draft head RANK-TRIMMED n_ranks={n_ranks} src={sha16}"
)),
"per-session engagement line missing:\n{log}"
);
assert!(
log.contains("[helper] burst=") && log.contains("[helper] trim_rounds="),
"trimmed dflash child never burst / never counted:\n{log}"
);
let trim_rounds: usize = log
.lines()
.find_map(|l| l.strip_prefix("[helper] trim_rounds="))
.and_then(|rest| rest.split_whitespace().next())
.and_then(|v| v.parse().ok())
.expect("trim_rounds line");
assert!(
trim_rounds > 0,
"the counter must count trimmed rounds:\n{log}"
);
let log = run_ok(Some("1"), Some("1"), Some(&drafter_dir), Some(&ranks_path));
assert!(
log.contains(&armed) && log.contains("ALSO loaded"),
"MtpHead-preferred shape must still print the RANK-TRIMMED note:\n{log}"
);
assert!(
log.contains("[frspec-trim] self-trimmed head:")
&& !log.contains("glm5 DFlash2 draft-head slab"),
"with a target-head-trimmed MtpHead loaded no second slab may be built:\n{log}"
);
let mut oob = ranks.clone();
oob[3] = VOCAB;
let oob_path = write_ranks_fixture("matrix-oob", &oob);
let oob_sha = sha16_of(&oob_path);
let (ok, log) = run_child(Some("1"), None, Some(&drafter_dir), Some(&oob_path));
assert!(
!ok && log.contains(&format!("token id {VOCAB} >= head rows {VOCAB}"))
&& log.contains(&format!("sha16={oob_sha}")),
"an out-of-vocab ranks file must refuse the boot by name (ok={ok}):\n{log}"
);
assert!(
!log.contains("RANK-TRIMMED") && !log.contains("[helper] burst="),
"a refused ranks file must never reach a receipt or a burst:\n{log}"
);
let (ok, log) = run_child(Some("1"), Some("1"), Some(&drafter_dir), Some(&oob_path));
assert!(
!ok && log.contains(&format!("token id {VOCAB} >= head rows {VOCAB}"))
&& log.contains(&format!("sha16={oob_sha}")),
"the both-loaded shape must refuse an out-of-vocab ranks file by name (ok={ok}):\n{log}"
);
assert!(
!log.contains("RANK-TRIMMED") && !log.contains("[helper] burst="),
"a refused ranks file must never reach a receipt or a burst (both loaded):\n{log}"
);
let mut dup = ranks.clone();
dup[5] = dup[6];
let dup_path = write_ranks_fixture("matrix-dup", &dup);
let (ok, log) = run_child(Some("1"), None, Some(&drafter_dir), Some(&dup_path));
assert!(
!ok && log.contains(&format!("token id {} appears more than once", dup[6])),
"a duplicated id must refuse the boot (ok={ok}):\n{log}"
);
let bad_path = std::env::temp_dir().join(format!(
"glm5-dflash-ranks-{}-matrix-bad.txt",
std::process::id()
));
std::fs::write(&bad_path, "31\n30\nid\n29\n").expect("write bad ranks fixture");
let (ok, log) = run_child(Some("1"), None, Some(&drafter_dir), Some(&bad_path));
assert!(
!ok && log.contains("line 3 is not a token id"),
"a non-numeric ranks line must refuse the boot (ok={ok}):\n{log}"
);
for (dfl, trim) in [
(None, None),
(Some(drafter_dir.as_path()), None),
(Some(drafter_dir.as_path()), Some(ranks_path.as_path())),
] {
let log = run_ok(None, None, dfl, trim);
assert!(
!log.contains("[glm5-spec]"),
"MEMRA_GLM5_SPEC off (dflash={} trim={}) must print no [glm5-spec] line:\n{log}",
dfl.is_some(),
trim.is_some()
);
}
for p in [&ranks_path, &oob_path, &dup_path, &bad_path] {
std::fs::remove_file(p).ok();
}
std::fs::remove_dir_all(&drafter_dir).ok();
println!(
"gate 8 PASS: selection matrix green+red incl. the RANK-TRIMMED arms (slab receipt, \
MtpHead-preferred, 3 boot refusals on both shapes); VRAM-at-ready mini-fixture: dflash-boot \
{vram_dflash} MiB vs mtp-boot {vram_mtp} MiB (delta {} MiB — mini scale; the box \
three-way window banks the real-artifact delta)",
vram_dflash as i64 - vram_mtp as i64
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_rank_trimmed_head_moves_acceptance_never_the_tape() {
let _gpu = gpu_guard();
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let census_ks = [3usize, K];
let (tape, untrimmed) = {
let h = Harness::new("g11u");
let tape = plain_tape(&h, &prompt, max_new);
let mut per_k: Vec<(usize, Vec<u32>, usize, usize)> = Vec::new();
for &k in &census_ks {
let census = Rc::new(RefCell::new(Vec::<u32>::new()));
let mut record = census_knob(&census);
let mut knobs = Glm5SpecKnobs {
draft_override: Some(&mut record),
..Default::default()
};
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("untrimmed glm5 dflash spec session");
let (out, drafted, accepted, _) =
drive_gated(&h, &mut sess, &prompt, k, max_new, 3, &mut knobs);
assert_eq!(&out[..max_new], &tape[..], "K={k}: untrimmed tape != plain");
assert_eq!(
sess.rank_trimmed_rounds, 0,
"K={k}: no trim loaded, the counter must stay 0"
);
let ids = census.borrow().clone();
assert_eq!(ids.len(), drafted, "K={k}: the census must see every draft");
per_k.push((k, ids, drafted, accepted));
}
(tape, per_k)
};
let mut freq = BTreeMap::<u32, usize>::new();
for (_, ids, _, _) in &untrimmed {
for &t in ids {
*freq.entry(t).or_default() += 1;
}
}
assert!(
freq.len() >= 3,
"degenerate fixture: only {} distinct drafted ids",
freq.len()
);
let mut by_freq: Vec<(u32, usize)> = freq.iter().map(|(&t, &n)| (t, n)).collect();
by_freq.sort_by(|a, b| b.1.cmp(&a.1).then(a.0.cmp(&b.0)));
let excluded: BTreeSet<u32> = by_freq.iter().take(2).map(|(t, _)| *t).collect();
let ranks: Vec<u32> = (0..VOCAB).rev().filter(|t| !excluded.contains(t)).collect();
let n_ranks = ranks.len();
assert!(n_ranks >= DRAFT_TOPK && n_ranks < VOCAB as usize);
let ranks_set: BTreeSet<u32> = ranks.iter().copied().collect();
let h = Harness::with_trim("g11t", &ranks);
let slab = h.model.dflash_trim.as_ref().expect("the draft-head slab");
assert_eq!(
slab.d2t, ranks,
"slab d2t must be the ranks file, in rank order"
);
let sha16 = sha16_of(h.ranks_path.as_ref().unwrap());
assert_eq!(slab.src_sha16, sha16);
assert_eq!(h.model.frspec_src_sha16.as_deref(), Some(sha16.as_str()));
assert!(
h.model
.glm5_dflash_trim()
.is_some_and(|(_, d2t)| d2t == ranks.as_slice()),
"the round's trim resolution must select the slab"
);
let (full, slab_rows) = match (&h.model.output, &slab.head) {
(GpuTensor::Float { data: f, ne: fne }, GpuTensor::Float { data: sl, ne: sne }) => {
assert_eq!(fne, &vec![HIDDEN as u64, VOCAB as u64]);
assert_eq!(sne, &vec![HIDDEN as u64, n_ranks as u64]);
(
h.engine.dtoh(f).expect("head dtoh"),
h.engine.dtoh(sl).expect("slab dtoh"),
)
}
_ => panic!("the mini fixture's head is F32 on both sides"),
};
let bits = |v: &[f32]| v.iter().map(|x| x.to_bits()).collect::<Vec<u32>>();
let head_row = |t: u32| bits(&full[t as usize * HIDDEN..(t as usize + 1) * HIDDEN]);
for (r, &t) in ranks.iter().enumerate() {
assert_eq!(
bits(&slab_rows[r * HIDDEN..(r + 1) * HIDDEN]),
head_row(t),
"slab row {r} != head row {t}"
);
}
assert_ne!(head_row(ranks[0]), head_row(ranks[1]));
assert_ne!(bits(&slab_rows[..HIDDEN]), head_row(ranks[1]));
let global_before = memra_engine::glm_spec::glm5_rank_trimmed_draft_rounds();
let mut trimmed_census: Vec<(usize, Vec<u32>)> = Vec::new();
for k in 1..=K {
let census = Rc::new(RefCell::new(Vec::<u32>::new()));
let mut record = census_knob(&census);
let mut knobs = Glm5SpecKnobs {
draft_override: Some(&mut record),
..Default::default()
};
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("trimmed glm5 dflash spec session");
let (out, drafted, accepted, bursts) =
drive_gated(&h, &mut sess, &prompt, k, max_new, 3, &mut knobs);
assert_eq!(
&out[..max_new],
&tape[..],
"K={k}: RANK-TRIMMED tape diverged from plain greedy ({accepted}/{drafted} over \
{bursts} bursts), the slab may only move acceptance, never output"
);
let ids = census.borrow().clone();
assert_eq!(ids.len(), drafted, "K={k}: the census must see every draft");
assert!(
ids.iter().all(|t| ranks_set.contains(t)),
"K={k}: a drafted id lies outside the ranks set: {ids:?}"
);
assert!(sess.rounds > 0);
assert_eq!(
sess.rank_trimmed_rounds, sess.rounds,
"K={k}: every round drafted through the slab must be counted"
);
if let Some((_, uids, ud, ua)) = untrimmed.iter().find(|(uk, ..)| *uk == k) {
assert!(
uids.iter().any(|t| excluded.contains(t)),
"K={k}: the untrimmed census never drafted an excluded id, the ranks set \
does not bind and the identity above is vacuous"
);
println!(
"gate 13 K={k}: tape identical; acceptance trimmed {accepted}/{drafted} vs \
untrimmed {ua}/{ud} (free to move; n_ranks={n_ranks}, excluded {excluded:?})"
);
}
trimmed_census.push((k, ids));
}
assert!(
memra_engine::glm_spec::glm5_rank_trimmed_draft_rounds() > global_before,
"the process-wide counter must move"
);
{
let k = K;
let census = Rc::new(RefCell::new(Vec::<u32>::new()));
let mut record = census_knob(&census);
let mut knobs = Glm5SpecKnobs {
draft_override: Some(&mut record),
skip_d2t_remap: true,
..Default::default()
};
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, None)
.expect("red-arm glm5 dflash spec session");
let (out, drafted, accepted, _) =
drive_gated(&h, &mut sess, &prompt, k, max_new, 3, &mut knobs);
assert_eq!(
&out[..max_new],
&tape[..],
"remap-skipped red arm: the tape must STILL be plain (verify arbitrates)"
);
let ids = census.borrow().clone();
let (_, remapped) = trimmed_census
.iter()
.find(|(tk, _)| *tk == k)
.expect("K census");
assert_ne!(
&ids, remapped,
"skipping the d2t remap must change WHICH ids get drafted (the remap is live)"
);
assert_eq!(sess.rank_trimmed_rounds, sess.rounds);
println!(
"gate 13 RED PASS: remap skipped -> tape identical, drafted sequence moved \
({accepted}/{drafted} accepted)"
);
}
println!("gate 13 PASS: RANK-TRIMMED slab n_ranks={n_ranks} src={sha16}");
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_confidence_gate_truncates_drafts_never_the_tape() {
let _gpu = gpu_guard();
let h = Harness::new("g10");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let k = 3usize;
let ctx = prompt.len() + max_new + k + 8;
let tape = plain_tape(&h, &prompt, max_new);
let drive =
|pmin: Option<(f32, bool)>, sampling: Option<SpecSampling>| -> (Vec<u32>, usize, usize) {
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, sampling)
.expect("glm5 dflash spec session");
let mut knobs = Glm5SpecKnobs {
pmin_override: pmin,
..Default::default()
};
let mut out: Vec<u32> = Vec::new();
let (mut drafted, mut accepted) = (0usize, 0usize);
while out.len() < max_new && !sess.finished() {
let room = (max_new - out.len()).min(3);
let (burst, d, a) = h
.model
.glm5_spec_session_burst_gated(&h.engine, &mut sess, room, k, &[], &mut knobs)
.expect("gated burst");
if burst.is_empty() {
break;
}
drafted += d;
accepted += a;
out.extend(burst);
}
(out, drafted, accepted)
};
let (out_off, drafted_off, _) = drive(None, None);
assert_eq!(&out_off[..max_new], &tape[..], "gate-off arm diverged");
assert!(
drafted_off > 0,
"gate-off arm drafted nothing — fixture defect"
);
let (out, drafted, accepted) = drive(Some((1.1, true)), None);
assert_eq!(
&out[..max_new],
&tape[..],
"PMIN0 zero-draft rounds must stay byte-identical (each round IS a plain step)"
);
assert_eq!(
(drafted, accepted),
(0, 0),
"p_min=1.1 + PMIN0 must truncate EVERY proposal to zero drafts"
);
let (out, drafted, _) = drive(Some((1.1, false)), None);
assert_eq!(&out[..max_new], &tape[..], "slot-0 survivor arm diverged");
assert!(
drafted > 0 && drafted < drafted_off,
"without PMIN0 exactly slot 0 rides per round: got {drafted} vs gate-off {drafted_off}"
);
let (sa, da, _) = drive(Some((1.1, true)), Some(sampled_cfg(42)));
let (sb, db, _) = drive(Some((1.1, true)), Some(sampled_cfg(42)));
assert_eq!(sa, sb, "sampled zero-draft rounds must be deterministic");
assert_eq!(
(da, db),
(0, 0),
"sampled arms must also draft zero at p_min=1.1"
);
assert!(
sa.len() >= max_new,
"sampled zero-draft session stalled at {} of {max_new}",
sa.len()
);
println!(
"gate 10 PASS: dflash tau-slot gate truncates drafts (0 with PMIN0, {drafted} \
slot-0 survivors vs {drafted_off} gate-off), tape byte-identical on every arm"
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_restored_session_bytes_match_plain_decode_and_cold_acceptance() {
let _gpu = gpu_guard();
let h = Harness::new("g11");
let prompt = tokens(PROMPT, 0xA11CE);
let split = PROMPT - BLOCK; let (prefix, suffix) = prompt.split_at(split);
let max_new = 20usize;
let k = 3usize;
let ctx = prompt.len() + max_new + K + 8;
let tape_plain = plain_tape(&h, &prompt, max_new);
let mut cold = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("cold spec session");
let (tape_cold, drafted_cold, accepted_cold, _) =
drive_bursts(&h, &mut cold, &prompt, k, max_new, 7, &[]);
assert_eq!(
tape_cold,
tape_plain[..tape_cold.len()],
"cold spec tape must match plain decode (gate 1's bar, re-anchored here)"
);
let mut donor = h
.model
.glm5_spec_session_new(&h.engine, prefix, ctx, None)
.expect("donor spec session over the prefix");
let (_donor_burst, donor_drafted, _, _) = drive_bursts(&h, &mut donor, prefix, k, 4, 4, &[]);
assert!(donor_drafted > 0, "the donor must have drafted (kv filled)");
let tail = donor
.export_draft_tail(&h.engine, prefix.len())
.expect("drafter tail export at the boundary");
let dr = h.model.glm5_dflash.as_ref().expect("drafter attached");
let dkv = memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("drafter KV rebuilt from the tail");
drop(donor);
let (boundary_cache, boundary_logits) = h.fresh_primed(prefix, ctx);
{
assert!(
std::env::var("MEMRA_GLM5_SPEC_FULLCOVER").is_err(),
"gate 11 pins the DISARMED posture; run gate 13 for the armed one"
);
let (c2, _) = h.fresh_primed(prefix, ctx);
let dkv_red =
memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("red-arm dkv");
assert!(
h.model
.glm5_spec_session_from_restored(
&h.engine,
c2,
prefix,
&[],
&boundary_logits,
dkv_red,
ctx,
None,
)
.is_err(),
"RED: an empty suffix must refuse while MEMRA_GLM5_SPEC_FULLCOVER is unset"
);
let (c3, _) = h.fresh_primed(prefix, ctx);
let dkv_red2 =
memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("red-arm dkv 2");
assert!(
h.model
.glm5_spec_session_from_restored(
&h.engine,
c3,
&prefix[..prefix.len() - 1],
suffix,
&boundary_logits,
dkv_red2,
ctx,
None,
)
.is_err(),
"RED: cache.pos != restored prefix must refuse"
);
}
let mut restored = h
.model
.glm5_spec_session_from_restored(
&h.engine,
boundary_cache,
prefix,
suffix,
&boundary_logits,
dkv,
ctx,
None,
)
.expect("restored spec session");
let (tape, drafted, accepted, _bursts) =
drive_bursts(&h, &mut restored, &prompt, k, max_new, 7, &[]);
assert!(drafted > 0, "the restored session must actually draft");
assert_eq!(
tape,
tape_plain[..tape.len()],
"restored spec tape must be BYTE-IDENTICAL to plain decode"
);
assert_eq!(
tape.len(),
tape_cold.len(),
"restored and cold sessions must serve the same tape length"
);
assert_eq!(
(drafted, accepted),
(drafted_cold, accepted_cold),
"restored-session drafter context must be byte-equivalent to the cold session's"
);
println!(
"gate 11 PASS: restored session == plain bytes over {} tokens, acceptance {} / {} \
identical to cold",
tape.len(),
accepted,
drafted,
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_restored_continuation_is_bit_identical_to_the_split_prime_cold_twin() {
let _gpu = gpu_guard();
let h = Harness::new("g12");
let prompt = tokens(PROMPT, 0xC1B7);
let split = PROMPT - BLOCK;
let (prefix, suffix) = prompt.split_at(split);
let max_new = 20usize;
let k = 3usize;
let ctx = prompt.len() + max_new + K + 8;
let (mut cache_mono, logits_mono) = h.fresh_primed(&prompt, ctx);
let mut tape_mono = Vec::with_capacity(max_new);
tape_mono.push(argmax(&logits_mono) as u32);
while tape_mono.len() < max_new {
let ll = h
.model
.decode_step(&h.engine, *tape_mono.last().unwrap(), &mut cache_mono)
.expect("mono decode step");
tape_mono.push(argmax(&ll) as u32);
}
let (mut cache_split, _boundary_logits) = h.fresh_primed(prefix, ctx);
let (logits_split, _seed, _hiddens) = h
.model
.prime_cache(&h.engine, suffix, &mut cache_split, 0)
.expect("suffix continuation prime");
let mut tape_split = Vec::with_capacity(max_new);
tape_split.push(argmax(&logits_split) as u32);
while tape_split.len() < max_new {
let ll = h
.model
.decode_step(&h.engine, *tape_split.last().unwrap(), &mut cache_split)
.expect("split decode step");
tape_split.push(argmax(&ll) as u32);
}
assert_eq!(logits_mono.len(), logits_split.len(), "logit widths match");
assert!(
logits_mono
.iter()
.chain(logits_split.iter())
.all(|v| v.is_finite()),
"non-finite anchor logits — the band below cannot see NaN (f32::max drops it)"
);
let mut diff_bits = 0usize;
let mut max_delta = 0f32;
for (a, b) in logits_mono.iter().zip(logits_split.iter()) {
if a.to_bits() != b.to_bits() {
diff_bits += 1;
max_delta = max_delta.max((a - b).abs());
}
}
assert!(
max_delta <= 1e-3,
"mono-vs-split anchor logits moved {max_delta:.3e} — beyond the chunked-prime class"
);
let mut donor = h
.model
.glm5_spec_session_new(&h.engine, prefix, ctx, None)
.expect("donor spec session over the prefix");
let (_burst, donor_drafted, _, _) = drive_bursts(&h, &mut donor, prefix, k, 4, 4, &[]);
assert!(donor_drafted > 0, "the donor must have drafted (kv filled)");
let tail = donor
.export_draft_tail(&h.engine, prefix.len())
.expect("drafter tail export at the boundary");
let dr = h.model.glm5_dflash.as_ref().expect("drafter attached");
let dkv = memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("drafter KV rebuilt from the tail");
drop(donor);
let (boundary_cache, bl) = h.fresh_primed(prefix, ctx);
let mut restored = h
.model
.glm5_spec_session_from_restored(
&h.engine,
boundary_cache,
prefix,
suffix,
&bl,
dkv,
ctx,
None,
)
.expect("restored spec session");
let (tape_spec, drafted, _accepted, _bursts) =
drive_bursts(&h, &mut restored, &prompt, k, max_new, 7, &[]);
assert!(drafted > 0, "the restored session must actually draft");
assert_eq!(
tape_spec,
tape_split[..tape_spec.len()],
"restored spec tape must be BYTE-IDENTICAL to the split-prime cold twin"
);
println!(
"gate 12 PASS: restored == split twin over {} tokens; mono-vs-split anchor logits: \
{} of {} values differ, max |delta| {:.3e}; mono tape == split tape: {}",
tape_spec.len(),
diff_bits,
logits_mono.len(),
max_delta,
tape_mono == tape_split,
);
}
struct FullCoverArm;
impl FullCoverArm {
fn arm() -> Self {
unsafe { std::env::set_var("MEMRA_GLM5_SPEC_FULLCOVER", "1") };
Self
}
}
impl Drop for FullCoverArm {
fn drop(&mut self) {
unsafe { std::env::remove_var("MEMRA_GLM5_SPEC_FULLCOVER") };
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_full_cover_restore_bytes_match_plain_decode_and_cold_acceptance() {
let _gpu = gpu_guard();
let h = Harness::new("g13");
let prompt = tokens(PROMPT, 0xF00C);
let max_new = 20usize;
let k = 3usize;
let ctx = prompt.len() + max_new + K + 8;
let tape_plain = plain_tape(&h, &prompt, max_new);
let mut cold = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("cold spec session");
let (tape_cold, drafted_cold, accepted_cold, _) =
drive_bursts(&h, &mut cold, &prompt, k, max_new, 7, &[]);
assert_eq!(
tape_cold,
tape_plain[..tape_cold.len()],
"cold spec tape must match plain decode"
);
let mut donor = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("donor spec session over the whole prompt");
let (_donor_burst, donor_drafted, _, _) = drive_bursts(&h, &mut donor, &prompt, k, 4, 4, &[]);
assert!(donor_drafted > 0, "the donor must have drafted (kv filled)");
let tail = donor
.export_draft_tail(&h.engine, prompt.len())
.expect("drafter tail export at the prompt boundary");
let dr = h.model.glm5_dflash.as_ref().expect("drafter attached");
drop(donor);
let (boundary_cache, boundary_logits) = h.fresh_primed(&prompt, ctx);
let dkv = memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("drafter KV rebuilt from the tail");
let _arm = FullCoverArm::arm();
{
let (c2, _) = h.fresh_primed(&prompt, ctx);
let dkv_red =
memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("red-arm dkv");
assert!(
h.model
.glm5_spec_session_from_restored(
&h.engine,
c2,
&prompt,
&[],
&[],
dkv_red,
ctx,
None,
)
.is_err(),
"RED: a full-cover restore without the entry's boundary logits must refuse"
);
}
let mut restored = h
.model
.glm5_spec_session_from_restored(
&h.engine,
boundary_cache,
&prompt,
&[],
&boundary_logits,
dkv,
ctx,
None,
)
.expect("full-cover restored spec session");
let (tape, drafted, accepted, _bursts) =
drive_bursts(&h, &mut restored, &prompt, k, max_new, 7, &[]);
assert!(
drafted > 0,
"the full-cover restored session must actually draft"
);
assert_eq!(
tape,
tape_plain[..tape.len()],
"full-cover restored spec tape must be BYTE-IDENTICAL to plain decode"
);
assert_eq!(
tape.len(),
tape_cold.len(),
"full-cover restored and cold sessions must serve the same tape length"
);
assert_eq!(
(drafted, accepted),
(drafted_cold, accepted_cold),
"full-cover restored drafter context must be byte-equivalent to the cold session's"
);
let seed = 0x5EEDu64;
let mut cold_sampled = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, Some(sampled_cfg(seed)))
.expect("cold sampled spec session");
let (tape_cold_sampled, _d, _a, _b) =
drive_bursts(&h, &mut cold_sampled, &prompt, k, max_new, 7, &[]);
let (cache_sampled, logits_sampled) = h.fresh_primed(&prompt, ctx);
let dkv_sampled =
memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("sampled-arm drafter KV rebuilt from the tail");
let mut restored_sampled = h
.model
.glm5_spec_session_from_restored(
&h.engine,
cache_sampled,
&prompt,
&[],
&logits_sampled,
dkv_sampled,
ctx,
Some(sampled_cfg(seed)),
)
.expect("full-cover restored sampled spec session");
let (tape_restored_sampled, drafted_s, _a_s, _b_s) =
drive_bursts(&h, &mut restored_sampled, &prompt, k, max_new, 7, &[]);
assert!(
drafted_s > 0,
"the sampled full-cover restored session must actually draft"
);
assert_eq!(
tape_restored_sampled, tape_cold_sampled,
"at one seed the full-cover restored sampled tape must equal the COLD sampled \
tape byte for byte: the anchor is drawn from the entry's boundary row at Philox \
counter 0, exactly as the cold session draws from its prime's row"
);
println!(
"gate 13 PASS: full-cover restore == plain bytes over {} tokens, acceptance {} / {} \
identical to cold; sampled twin == cold sampled over {} tokens at seed {seed:#x}",
tape.len(),
accepted,
drafted,
tape_restored_sampled.len(),
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_streamed_burst_slices_concat_to_the_unhooked_burst() {
let _gpu = gpu_guard();
let h = Harness::new("g14");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let k = 3usize;
let tape = plain_tape(&h, &prompt, max_new);
for (arm, sampling) in [
("greedy", None),
("sampled", Some(sampled_cfg(0x5EED_0B20))),
] {
let mut plain_sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, sampling)
.expect("glm5 dflash spec session (un-hooked twin)");
let (plain_burst, pd, pa) = h
.model
.glm5_spec_session_burst(&h.engine, &mut plain_sess, max_new, k, &[])
.expect("un-hooked burst");
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, prompt.len() + max_new + k + 8, sampling)
.expect("glm5 dflash spec session (streamed)");
let mut slices: Vec<Vec<u32>> = Vec::new();
let (burst, d, a) = h
.model
.glm5_spec_session_burst_streamed(&h.engine, &mut sess, max_new, k, &[], &mut |s| {
slices.push(s.to_vec())
})
.expect("streamed burst");
let concat: Vec<u32> = slices.iter().flatten().copied().collect();
assert_eq!(
concat, burst,
"{arm}: the hook's slices must concatenate to the returned burst"
);
assert_eq!(
slices.first().map(|s| s.as_slice()),
Some(&burst[..1]),
"{arm}: the first slice must be the prime's anchor alone"
);
assert!(
slices.iter().all(|s| !s.is_empty()),
"{arm}: the hook never sees an empty slice"
);
assert_eq!(
slices.len(),
1 + sess.rounds,
"{arm}: one slice per round plus the anchor slice"
);
assert_eq!(
(burst.clone(), d, a),
(plain_burst.clone(), pd, pa),
"{arm}: the streamed burst must be byte-identical to the un-hooked twin \
(tokens AND counters)"
);
if arm == "greedy" {
assert_eq!(
&burst[..max_new],
&tape[..],
"greedy streamed burst diverged from plain decode"
);
}
assert_eq!(sess.pos(), sess.committed.len());
println!(
"gate 14 PASS ({arm}): {} slices over {} rounds concatenate to the {}-token burst, \
byte-identical to the un-hooked twin ({a}/{d} accepted)",
slices.len(),
sess.rounds,
burst.len()
);
}
}
struct EnvArm(&'static str);
impl EnvArm {
fn set(name: &'static str, value: &str) -> Self {
unsafe { std::env::set_var(name, value) };
Self(name)
}
}
impl Drop for EnvArm {
fn drop(&mut self) {
unsafe { std::env::remove_var(self.0) };
}
}
type Gate15Arm = (Vec<u32>, usize, usize, Vec<Vec<f32>>, Vec<Vec<f32>>);
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_chunked_drafter_prime_kv_matches_eager_ingest() {
let _gpu = gpu_guard();
let _host_taps = EnvArm::set("MEMRA_GLM5_DRAFT_TAPS_DEVICE", "0");
let h = Harness::new("g15");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let k = 3usize;
let ctx = prompt.len() + max_new + k + 8;
let tape = plain_tape(&h, &prompt, max_new);
let cfg = &h
.model
.glm5_dflash
.as_ref()
.expect("drafter attached")
.draft
.cfg;
let row_floats = cfg.n_kv * cfg.head_dim;
let kv_after_one_round = |sess: &mut Glm5SpecSession| -> (Vec<Vec<f32>>, Vec<Vec<f32>>) {
let (_burst, _d, _a) = h
.model
.glm5_spec_session_burst(&h.engine, sess, 1, k, &[])
.expect("one round");
assert!(
sess.draft_kv_len().expect("dflash session") >= prompt.len(),
"the drafter KV must cover the prompt after round 1"
);
sess.draft_kv_rows_host(&h.engine, prompt.len(), row_floats)
.expect("dflash session exports its KV rows")
};
let (k_eager, v_eager) = {
unsafe { std::env::remove_var("MEMRA_GLM5_DRAFT_PRIME_V2") };
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("eager session");
assert_eq!(
sess.draft_kv_len(),
Some(prompt.len()),
"the eager arm ingests the prompt AT CREATION by default (before the anchor is \
emitted; MEMRA_GLM5_DRAFT_PRIME_LAZY=1 restores the round-1 placement)"
);
kv_after_one_round(&mut sess)
};
let (k_v2, v_v2) = {
let _v2 = EnvArm::set("MEMRA_GLM5_DRAFT_PRIME_V2", "1");
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("chunked-prime session");
assert_eq!(
sess.draft_kv_len(),
Some(prompt.len()),
"the chunked arm must have ingested the whole prompt at creation"
);
kv_after_one_round(&mut sess)
};
let bits = |planes: &[Vec<f32>]| -> Vec<Vec<u32>> {
planes
.iter()
.map(|p| p.iter().map(|f| f.to_bits()).collect())
.collect()
};
assert_eq!(
bits(&k_v2),
bits(&k_eager),
"one-chunk prompt: the chunked drafter prime's K planes must be bit-identical to \
the eager ingest's (same GEMM, same M, only the data movement changed)"
);
assert_eq!(
bits(&v_v2),
bits(&v_eager),
"one-chunk prompt: V planes must be bit-identical"
);
let _chunk = EnvArm::set("MEMRA_PRIME_CHUNK", "16");
let run = |v2: bool| -> Gate15Arm {
let arm = v2.then(|| EnvArm::set("MEMRA_GLM5_DRAFT_PRIME_V2", "1"));
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("session");
drop(arm);
let (kk, vv) = kv_after_one_round(&mut sess);
let (mut out, mut d, mut a) = (Vec::new(), 0usize, 0usize);
while out.len() < max_new && !sess.finished() {
let (b, bd, ba) = h
.model
.glm5_spec_session_burst(&h.engine, &mut sess, 4, k, &[])
.expect("burst");
if b.is_empty() {
break;
}
out.extend(b);
d += bd;
a += ba;
}
(out, d, a, kk, vv)
};
let (out_e, d_e, a_e, k_e2, v_e2) = run(false);
let (out_v, d_v, a_v, k_v3, v_v3) = run(true);
assert_eq!(
&out_e[..max_new],
&tape[..],
"eager arm, chunked trunk: tape == plain"
);
assert_eq!(&out_v[..max_new], &tape[..], "chunked arm: tape == plain");
let maxdiff = |a: &[Vec<f32>], b: &[Vec<f32>]| -> f32 {
a.iter()
.zip(b)
.flat_map(|(x, y)| x.iter().zip(y).map(|(p, q)| (p - q).abs()))
.fold(0f32, f32::max)
};
let (kd, vd) = (maxdiff(&k_v3, &k_e2), maxdiff(&v_v3, &v_e2));
println!(
"gate 15 PASS: one-chunk KV bit-identical; multi-chunk (PRIME_CHUNK=16) tape == plain \
on both arms; KV max-abs-diff k={kd:e} v={vd:e}; acceptance eager {a_e}/{d_e} vs \
chunked {a_v}/{d_v}"
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_dflash_device_resident_drafter_prime_kv_matches_eager_ingest() {
let _gpu = gpu_guard();
let h = Harness::new("g16");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 20usize;
let k = 3usize;
let ctx = prompt.len() + max_new + k + 8;
let tape = plain_tape(&h, &prompt, max_new);
let cfg = &h
.model
.glm5_dflash
.as_ref()
.expect("drafter attached")
.draft
.cfg;
let row_floats = cfg.n_kv * cfg.head_dim;
let bits = |planes: &[Vec<f32>]| -> Vec<Vec<u32>> {
planes
.iter()
.map(|p| p.iter().map(|f| f.to_bits()).collect())
.collect()
};
let maxdiff = |a: &[Vec<f32>], b: &[Vec<f32>]| -> f32 {
a.iter()
.zip(b)
.flat_map(|(x, y)| x.iter().zip(y).map(|(p, q)| (p - q).abs()))
.fold(0f32, f32::max)
};
let run = |device: bool| -> Gate15Arm {
unsafe { std::env::remove_var("MEMRA_GLM5_DRAFT_PRIME_V2") };
let arm = EnvArm::set(
"MEMRA_GLM5_DRAFT_TAPS_DEVICE",
if device { "1" } else { "0" },
);
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, None)
.expect("session");
drop(arm);
assert_eq!(
sess.draft_kv_len(),
Some(prompt.len()),
"device={device}: the drafter KV must cover the prompt at creation"
);
let (kk, vv) = sess
.draft_kv_rows_host(&h.engine, prompt.len(), row_floats)
.expect("dflash session exports its KV rows");
let (mut out, mut d, mut a) = (Vec::new(), 0usize, 0usize);
while out.len() < max_new && !sess.finished() {
let (b, bd, ba) = h
.model
.glm5_spec_session_burst(&h.engine, &mut sess, 4, k, &[])
.expect("burst");
if b.is_empty() {
break;
}
out.extend(b);
d += bd;
a += ba;
}
(out, d, a, kk, vv)
};
let (out_e, d_e, a_e, k_e, v_e) = run(false);
let (out_d, d_d, a_d, k_d, v_d) = run(true);
assert_eq!(
bits(&k_d),
bits(&k_e),
"one range: device-resident K planes != eager"
);
assert_eq!(
bits(&v_d),
bits(&v_e),
"one range: device-resident V planes != eager"
);
assert_eq!(&out_e[..max_new], &tape[..], "eager arm: tape == plain");
assert_eq!(&out_d[..max_new], &tape[..], "device arm: tape == plain");
assert_eq!(
out_d, out_e,
"GREEDY TAPE IDENTITY, host taps vs device taps (the default flip's exactness \
receipt): the served tapes must be byte-identical"
);
assert_eq!(
(d_d, a_d),
(d_e, a_e),
"one range: identical KV must give identical acceptance"
);
let _chunk = EnvArm::set("MEMRA_PRIME_CHUNK", "16");
let (out_e2, d_e2, a_e2, k_e2, v_e2) = run(false);
let (out_d2, d_d2, a_d2, k_d2, v_d2) = run(true);
assert_eq!(
&out_e2[..max_new],
&tape[..],
"eager arm, chunked trunk: tape == plain"
);
assert_eq!(
&out_d2[..max_new],
&tape[..],
"device arm, chunked trunk: tape == plain"
);
assert_eq!(
out_d2, out_e2,
"two ranges: greedy tape identity host taps vs device taps"
);
println!(
"gate 16 PASS: one-range KV bit-identical ({a_e}/{d_e} accepted both arms); two-range \
(PRIME_CHUNK=16) tape == plain on both arms, KV max-abs-diff k={:e} v={:e}, \
acceptance eager {a_e2}/{d_e2} vs device {a_d2}/{d_d2}",
maxdiff(&k_d2, &k_e2),
maxdiff(&v_d2, &v_e2)
);
}
struct PenaltyArm;
impl PenaltyArm {
fn arm() -> Self {
unsafe { std::env::set_var("MEMRA_SPEC_PENALTY", "1") };
Self
}
}
impl Drop for PenaltyArm {
fn drop(&mut self) {
unsafe { std::env::remove_var("MEMRA_SPEC_PENALTY") };
}
}
fn penalty_cfg(temperature: f32, seed: u64) -> SamplerConfig {
SamplerConfig {
temperature,
penalty_last_n: PEN_WINDOW_MAX,
penalty_repeat: 1.3,
penalty_freq: 0.35,
penalty_present: 0.2,
seed,
..SamplerConfig::default()
}
}
fn spec_sampling_of(cfg: &SamplerConfig) -> SpecSampling {
SpecSampling {
temp: cfg.temperature,
seed: cfg.seed,
top_k: cfg.top_k as i32,
top_p: cfg.top_p,
min_p: cfg.min_p,
penalty_last_n: cfg.penalty_last_n,
penalty_repeat: cfg.penalty_repeat,
penalty_freq: cfg.penalty_freq,
penalty_present: cfg.penalty_present,
}
}
fn plain_tape_sampler(
h: &Harness,
prompt: &[u32],
max_new: usize,
cfg: &SamplerConfig,
) -> Vec<u32> {
let (mut cache, logits) = h.fresh_primed(prompt, prompt.len() + max_new + 16);
let mut sampler = Sampler::new(cfg.clone());
for &t in prompt {
sampler.accept(t);
}
let mut tape = Vec::with_capacity(max_new);
let first = sampler.sample(&logits);
sampler.accept(first);
tape.push(first);
while tape.len() < max_new {
let ll = h
.model
.decode_step(&h.engine, *tape.last().unwrap(), &mut cache)
.expect("plain decode step");
let t = sampler.sample(&ll);
sampler.accept(t);
tape.push(t);
}
tape
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_penalized_greedy_spec_tape_matches_the_plain_penalized_sampler() {
let _gpu = gpu_guard();
let h = Harness::new("g15");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 24usize;
let cfg = penalty_cfg(0.0, 0);
let tape_pen = plain_tape_sampler(&h, &prompt, max_new, &cfg);
let tape_raw = plain_tape(&h, &prompt, max_new);
assert_ne!(
tape_pen, tape_raw,
"the penalties must visibly move the plain tape at this scale, or the identity \
below proves nothing"
);
let ctx = prompt.len() + max_new + K + 8;
assert!(
std::env::var("MEMRA_SPEC_PENALTY").is_err(),
"gate 17 pins the DARK posture first; the arm is thrown below"
);
let dark = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, Some(spec_sampling_of(&cfg)));
assert!(
dark.is_err(),
"RED: a penalized session must refuse while MEMRA_SPEC_PENALTY is unset"
);
let why = dark.err().map(|e| e.to_string()).unwrap_or_default();
assert!(
why.contains("MEMRA_SPEC_PENALTY"),
"the refusal must name the door: {why}"
);
let _arm = PenaltyArm::arm();
for k in 1..=K {
let mut sess = h
.model
.glm5_spec_session_new(
&h.engine,
&prompt,
prompt.len() + max_new + k + 8,
Some(spec_sampling_of(&cfg)),
)
.expect("penalized greedy glm5 dflash spec session");
let (out, drafted, accepted, bursts) =
drive_bursts(&h, &mut sess, &prompt, k, max_new, 3, &[]);
assert_eq!(
&out[..max_new],
&tape_pen[..],
"K={k}: penalized greedy spec tape diverged from the plain penalized sampler \
({accepted}/{drafted} over {bursts} bursts)"
);
assert!(
drafted > 0,
"K={k}: the penalized session must actually draft"
);
println!(
"gate 17 PASS K={k}: penalized greedy spec == plain penalized sampler over \
{bursts} bursts, {accepted}/{drafted} accepted"
);
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_device_penalties_are_bit_identical_to_the_host_sampler() {
let _gpu = gpu_guard();
force_true_f32();
let e = Engine::new(0).expect("CUDA engine on device 0");
let n = 1000usize; let nrow = 5usize; let mut state = 0x5EEDu64;
let mut next = || {
state ^= state << 13;
state ^= state >> 7;
state ^= state << 17;
state
};
let rows: Vec<f32> = (0..nrow * n)
.map(|_| ((next() % 20_001) as f32 - 10_000.0) / 997.0)
.collect();
let hist0: Vec<u32> = (0..200).map(|_| (next() % 40) as u32).collect();
let drafts: Vec<u32> = (0..nrow - 1).map(|_| (next() % 40) as u32).collect();
let classes: [(usize, f32, f32, f32); 6] = [
(PEN_WINDOW_MAX, 1.3, 0.0, 0.0),
(PEN_WINDOW_MAX, 1.0, 0.35, 0.0),
(PEN_WINDOW_MAX, 1.0, 0.0, 0.2),
(PEN_WINDOW_MAX, 1.3, 0.35, 0.2),
(PEN_WINDOW_MAX, 0.8, -0.1, -0.05),
(64, 1.3, 0.35, 0.2),
];
for (last_n, rep, freq, present) in classes {
let cfg = SamplerConfig {
penalty_last_n: last_n,
penalty_repeat: rep,
penalty_freq: freq,
penalty_present: present,
..SamplerConfig::default()
};
let mut expect: Vec<f32> = Vec::with_capacity(nrow * n);
for r in 0..nrow {
let mut s = Sampler::new(cfg.clone());
for &t in hist0.iter().chain(drafts[..r].iter()) {
s.accept(t);
}
expect.extend(s.penalized_logits(&rows[r * n..(r + 1) * n]));
}
let win = last_n.min(PEN_WINDOW_MAX);
let w0 = hist0.len().saturating_sub(win);
let mut hist: Vec<u32> = hist0[w0..].to_vec();
let n_win = hist.len();
hist.extend_from_slice(&drafts);
let hd = e.htod_u32_v(&hist).expect("hist");
let mut buf = e.htod(&rows).expect("rows");
e.penalize_logits_rows_inc(&mut buf, &hd, n_win, rep, freq, present, n, nrow, win)
.expect("penalize_logits_rows_inc");
let got = e.dtoh(&buf).expect("dtoh");
let bad = got
.iter()
.zip(&expect)
.filter(|(a, b)| a.to_bits() != b.to_bits())
.count();
assert_eq!(
bad,
0,
"rows_inc (last_n={last_n} rep={rep} freq={freq} present={present}): {bad} of \
{} logits differ from the host sampler's bytes",
got.len()
);
let touched = got
.iter()
.zip(&rows)
.filter(|(a, b)| a.to_bits() != b.to_bits())
.count();
assert!(touched > 0, "the class must actually penalize something");
let hd0 = e.htod_u32_v(&hist[..n_win]).expect("hist0");
let mut col = e.htod(&rows[..n]).expect("row 0");
e.penalize_logits(&mut col, &hd0, n_win, rep, freq, present, n)
.expect("penalize_logits");
let got0 = e.dtoh(&col).expect("dtoh");
let bad0 = got0
.iter()
.zip(&expect[..n])
.filter(|(a, b)| a.to_bits() != b.to_bits())
.count();
assert_eq!(
bad0, 0,
"penalize_logits (last_n={last_n} rep={rep} freq={freq} present={present}): \
{bad0} of {n} logits differ from the host sampler's bytes"
);
println!(
"gate 18 PASS last_n={last_n} rep={rep} freq={freq} present={present}: \
{touched} penalized logits over {nrow} rows, all bit-identical to the host"
);
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_penalized_sampled_twin_is_deterministic_split_invariant_and_engaged() {
let _gpu = gpu_guard();
let h = Harness::new("g17");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 24usize;
let k = 3usize;
let ctx = prompt.len() + max_new + k + 8;
let _arm = PenaltyArm::arm();
let run = |cfg: SamplerConfig, burst_target: usize| -> Vec<u32> {
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, Some(spec_sampling_of(&cfg)))
.expect("penalized sampled glm5 dflash spec session");
let (tape, d, _a, _b) = drive_bursts(&h, &mut sess, &prompt, k, max_new, burst_target, &[]);
assert!(d > 0, "the sampled penalized session must draft");
tape[..max_new.min(tape.len())].to_vec()
};
let a = run(penalty_cfg(0.9, 42), 3);
let b = run(penalty_cfg(0.9, 42), 3);
assert_eq!(a, b, "same seed, same burst split: reproducible");
let c = run(penalty_cfg(0.9, 42), max_new);
assert_eq!(
a, c,
"burst-split invariance: the accept uniforms and every draw ride the session's \
Philox counters, so the split must not change the stream"
);
let d = run(penalty_cfg(0.9, 43), 3);
assert_ne!(a, d, "a different seed must change the sampled tape");
let unpen = run(
SamplerConfig {
temperature: 0.9,
seed: 42,
..SamplerConfig::default()
},
3,
);
assert_ne!(
a, unpen,
"the penalties must move the sampled target (same seed, same counters, different p)"
);
println!(
"gate 19 PASS: penalized sampled twin deterministic, split-invariant, seed-sensitive, \
and distinct from the unpenalized same-seed tape"
);
}
struct ClipOff;
impl ClipOff {
fn arm() -> Self {
unsafe { std::env::set_var("MEMRA_DFLASH2_SDPA_CLIP", "0") };
Self
}
}
impl Drop for ClipOff {
fn drop(&mut self) {
unsafe { std::env::remove_var("MEMRA_DFLASH2_SDPA_CLIP") };
}
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_cold_drafter_restore_bytes_match_plain_decode_and_republishes_a_floor_tail() {
let _gpu = gpu_guard();
let h = Harness::new("g18");
let prompt = tokens(PROMPT, 0xA11CE);
let split = PROMPT - BLOCK;
let (prefix, suffix) = prompt.split_at(split);
let max_new = 40usize;
let k = 3usize;
let ctx = prompt.len() + max_new + K + 8;
let dr = h.model.glm5_dflash.as_ref().expect("drafter attached");
let tape_plain = plain_tape(&h, &prompt, max_new);
{
let _clip_off = ClipOff::arm();
assert!(
memra_engine::dflash::DflashKv::new_cold_at(&h.engine, &dr.draft.cfg, ctx, split)
.is_err(),
"RED: a cold drafter must refuse under MEMRA_DFLASH2_SDPA_CLIP=0"
);
}
let (boundary_cache, boundary_logits) = h.fresh_primed(prefix, ctx);
let dkv = memra_engine::dflash::DflashKv::new_cold_at(&h.engine, &dr.draft.cfg, ctx, split)
.expect("cold drafter at the restored boundary");
assert_eq!(
dkv.len, split,
"the cold drafter sits at the restored boundary"
);
assert_eq!(dkv.floor(), split, "and owns no rows below it");
let mut restored = h
.model
.glm5_spec_session_from_restored(
&h.engine,
boundary_cache,
prefix,
suffix,
&boundary_logits,
dkv,
ctx,
None,
)
.expect("restored spec session with a cold drafter");
let n1 = 12usize;
let (tape1, drafted1, accepted1, _) = drive_bursts(&h, &mut restored, &prompt, k, n1, 5, &[]);
assert!(drafted1 > 0, "the cold-drafter session must actually draft");
assert_eq!(
tape1,
tape_plain[..tape1.len()],
"cold-drafter restored tape must be BYTE-IDENTICAL to plain decode (the drafter \
can only move acceptance)"
);
let upto = prompt.len();
let tail = restored
.export_draft_tail(&h.engine, upto)
.expect("tail export from the cold-drafter session");
assert_eq!(tail.floor, split, "the tail carries its exporter's floor");
assert!(
tail.base >= split && tail.base + tail.rows == upto,
"the tail covers [{}, {upto}) and nothing below the floor {split}",
tail.base
);
assert!(
tail.rows < dr.draft.cfg.sliding_window,
"at this scale the tail is SHORT (window {}): the floor is what admits it",
dr.draft.cfg.sliding_window
);
drop(restored);
let j0 = 0usize; let committed2: Vec<u32> = prompt.to_vec();
let suffix2 = &tape_plain[j0..j0 + BLOCK];
let dkv2 = memra_engine::dflash::DflashKv::from_tail(&h.engine, &dr.draft.cfg, ctx, &tail)
.expect("a floor-bearing tail imports");
assert_eq!(
dkv2.floor(),
tail.base,
"the import inherits the floor at the tail's base"
);
assert_eq!(dkv2.len, upto);
let (cache2, logits2) = h.fresh_primed(&committed2, ctx);
let prompt2: Vec<u32> = committed2
.iter()
.copied()
.chain(suffix2.iter().copied())
.collect();
let mut restored2 = h
.model
.glm5_spec_session_from_restored(
&h.engine,
cache2,
&committed2,
suffix2,
&logits2,
dkv2,
ctx,
None,
)
.expect("session restored from the floor-bearing tail");
let n2 = 12usize;
let (tape2, drafted2, accepted2, _) = drive_bursts(&h, &mut restored2, &prompt2, k, n2, 5, &[]);
assert!(drafted2 > 0, "the re-restored session must draft");
let expect2 = &tape_plain[j0 + BLOCK..j0 + BLOCK + tape2.len()];
assert_eq!(
tape2, expect2,
"the continuation from the floor-bearing tail must be plain decode's continuation"
);
println!(
"gate 20 PASS: cold-drafter restore == plain bytes ({accepted1}/{drafted1} accepted), \
floor tail [{}, {upto}) re-imports with floor {} and continues byte-identical \
({accepted2}/{drafted2} accepted)",
tail.base, tail.base
);
}
#[test]
#[ignore = "needs a CUDA device, run under flock /tmp/memra-5090.lock"]
fn gpu_penalized_greedy_session_refuses_demotion() {
let _gpu = gpu_guard();
let h = Harness::new("g19");
let prompt = tokens(PROMPT, 0xA11CE);
let max_new = 12usize;
let cfg = penalty_cfg(0.0, 0);
let ctx = prompt.len() + max_new + K + 8;
let _arm = PenaltyArm::arm();
let mut sess = h
.model
.glm5_spec_session_new(&h.engine, &prompt, ctx, Some(spec_sampling_of(&cfg)))
.expect("penalized greedy glm5 dflash spec session");
let _ = drive_bursts(&h, &mut sess, &prompt, K, max_new, 3, &[]);
assert!(
!sess.demote_eligible(),
"a penalized greedy session must NOT be demotion-eligible: the flush's plain \
argmax carries no penalty pass and would silently drop the request's penalties"
);
let err = match h.model.glm5_spec_into_demoted(&h.engine, sess) {
Ok(_) => panic!(
"penalized greedy demote must refuse loudly, not silently emit an unpenalized \
token"
),
Err(err) => err,
};
assert!(
err.to_string().contains("sampled") || err.to_string().to_lowercase().contains("penal"),
"the refusal should name why a penalized session stays on spec, got: {err}"
);
println!(
"gate 21 PASS: penalized greedy session refuses demotion by name (demote_eligible() \
reads pen as well as sampling)"
);
}