pub(crate) use super::*;
#[test]
fn j1_qwen2_config_valid() {
let config = Qwen2Config::qwen2_0_5b_instruct();
assert_eq!(config.hidden_size, 896);
assert_eq!(config.num_attention_heads, 14);
assert_eq!(config.num_kv_heads, 2);
assert_eq!(config.num_layers, 24);
assert_eq!(config.vocab_size, 151936);
}
#[test]
fn j2_int4_quantization_size() {
let config = Qwen2Config::qwen2_0_5b_instruct();
let int4_size = config.model_size_int4();
assert!(
int4_size < 400 * 1024 * 1024,
"INT4 size: {} bytes",
int4_size
);
}
#[test]
fn j3_perplexity_degradation() {
let checker = PerplexityChecker::new(10.0);
assert!(checker.is_acceptable(11.5));
assert!(checker.is_acceptable(11.0));
assert!(!checker.is_acceptable(12.0));
let degradation = checker.degradation_pct(11.5);
assert!((degradation - 15.0).abs() < 0.1);
}
#[test]
fn j4_wasm_compatible_config() {
let config = Qwen2Config::qwen2_0_5b_instruct();
let int4_size = config.model_size_int4();
assert!(int4_size < 4 * 1024 * 1024 * 1024);
}
#[test]
fn j5_load_time_target() {
let metrics = DemoMetrics {
load_time_ms: 4500,
first_token_ms: 1500,
tokens_per_sec: 20.0,
peak_memory_bytes: 400 * 1024 * 1024,
tokens_generated: 100,
};
assert!(metrics.load_time_ms < 5000);
assert!(metrics.meets_targets());
}
#[test]
fn j6_first_token_latency() {
let metrics = DemoMetrics {
load_time_ms: 3000,
first_token_ms: 1800,
tokens_per_sec: 15.0,
peak_memory_bytes: 450 * 1024 * 1024,
tokens_generated: 50,
};
assert!(metrics.first_token_ms < 2000);
}
#[test]
fn j7_streaming_throughput() {
let metrics = DemoMetrics {
load_time_ms: 3000,
first_token_ms: 1500,
tokens_per_sec: 18.5,
peak_memory_bytes: 400 * 1024 * 1024,
tokens_generated: 100,
};
assert!(metrics.tokens_per_sec >= 15.0);
}
#[test]
fn j8_memory_usage() {
let config = Qwen2Config::qwen2_0_5b_instruct();
let model_size = config.model_size_int4();
let kv_cache = config.kv_cache_size(2048);
let total = model_size + kv_cache;
assert!(total < 512 * 1024 * 1024, "Total: {} bytes", total);
}
#[test]
fn j9_simd_speedup_design() {
let simd_lanes = 4; let expected_speedup = simd_lanes as f64 * 0.6; assert!(expected_speedup >= 2.0);
}
#[test]
fn j10_chrome_compatibility() {
let compat = BrowserCompatibility::default();
assert!(compat.supports_chrome(120));
assert!(compat.supports_chrome(121));
assert!(!compat.supports_chrome(119));
}
#[test]
fn j11_firefox_compatibility() {
let compat = BrowserCompatibility::default();
assert!(compat.supports_firefox(120));
assert!(compat.supports_firefox(125));
assert!(!compat.supports_firefox(115));
}
#[test]
fn j12_safari_compatibility() {
let compat = BrowserCompatibility::default();
assert!(compat.supports_safari(17));
assert!(compat.supports_safari(18));
assert!(!compat.supports_safari(16));
}
#[test]
fn j13_tokenizer_config() {
let tokenizer = Qwen2Tokenizer::new();
assert_eq!(tokenizer.vocab_size, 151936);
assert_eq!(tokenizer.special_tokens.eos_id, 151645);
}
#[test]
fn j14_special_tokens() {
let tokenizer = Qwen2Tokenizer::new();
assert!(tokenizer.is_special(tokenizer.special_tokens.bos_id));
assert!(tokenizer.is_special(tokenizer.special_tokens.eos_id));
assert!(tokenizer.is_special(tokenizer.special_tokens.im_start_id));
assert!(tokenizer.is_special(tokenizer.special_tokens.im_end_id));
assert!(!tokenizer.is_special(100));
}
#[test]
fn j15_eos_detection() {
let tokenizer = Qwen2Tokenizer::new();
assert!(tokenizer.is_eos(tokenizer.special_tokens.eos_id));
assert!(tokenizer.is_eos(tokenizer.special_tokens.im_end_id));
assert!(!tokenizer.is_eos(100)); }
#[test]
fn test_quantization_compression() {
assert_eq!(QuantizationType::Int4.compression_ratio(), 8.0);
assert_eq!(QuantizationType::Int8.compression_ratio(), 4.0);
assert_eq!(QuantizationType::Fp16.compression_ratio(), 2.0);
assert_eq!(QuantizationType::Fp32.compression_ratio(), 1.0);
}
#[test]
fn test_instruction_format() {
let tokenizer = Qwen2Tokenizer::new();
let formatted = tokenizer.format_instruction("Hello, how are you?");
assert!(formatted.contains("<|im_start|>user"));
assert!(formatted.contains("Hello, how are you?"));
assert!(formatted.contains("<|im_end|>"));
assert!(formatted.contains("<|im_start|>assistant"));
}
#[test]
fn test_kv_cache_scaling() {
let config = Qwen2Config::qwen2_0_5b_instruct();
let cache_512 = config.kv_cache_size(512);
let cache_1024 = config.kv_cache_size(1024);
assert!((cache_1024 as f64 / cache_512 as f64 - 2.0).abs() < 0.01);
}
#[test]
fn test_demo_metrics_fail_cases() {
let slow_load = DemoMetrics {
load_time_ms: 6000,
first_token_ms: 1000,
tokens_per_sec: 20.0,
peak_memory_bytes: 400 * 1024 * 1024,
tokens_generated: 100,
};
assert!(!slow_load.meets_targets());
let slow_first = DemoMetrics {
load_time_ms: 3000,
first_token_ms: 3000,
tokens_per_sec: 20.0,
peak_memory_bytes: 400 * 1024 * 1024,
tokens_generated: 100,
};
assert!(!slow_first.meets_targets());
let slow_throughput = DemoMetrics {
load_time_ms: 3000,
first_token_ms: 1000,
tokens_per_sec: 10.0,
peak_memory_bytes: 400 * 1024 * 1024,
tokens_generated: 100,
};
assert!(!slow_throughput.meets_targets());
let high_memory = DemoMetrics {
load_time_ms: 3000,
first_token_ms: 1000,
tokens_per_sec: 20.0,
peak_memory_bytes: 600 * 1024 * 1024,
tokens_generated: 100,
};
assert!(!high_memory.meets_targets());
}
fn assert_tokens_within_vocab(tokens: &SpecialTokens, vocab_size: u32, family: &str) {
assert!(
tokens.eos_id < vocab_size,
"FALSIFY: {family} EOS ({}) >= vocab_size ({vocab_size})",
tokens.eos_id
);
if tokens.bos_id > 0 {
assert!(
tokens.bos_id < vocab_size,
"FALSIFY: {family} BOS ({}) >= vocab_size ({vocab_size})",
tokens.bos_id
);
}
if tokens.pad_id > 0 {
assert!(
tokens.pad_id < vocab_size,
"FALSIFY: {family} PAD ({}) >= vocab_size ({vocab_size})",
tokens.pad_id
);
}
if tokens.im_start_id > 0 {
assert!(
tokens.im_start_id < vocab_size,
"FALSIFY: {family} im_start ({}) >= vocab_size ({vocab_size})",
tokens.im_start_id
);
}
if tokens.im_end_id > 0 {
assert!(
tokens.im_end_id < vocab_size,
"FALSIFY: {family} im_end ({}) >= vocab_size ({vocab_size})",
tokens.im_end_id
);
}
}
#[test]
fn falsify_qwen2_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::qwen2();
assert_eq!(
tokens.bos_id, 151_643,
"FALSIFY: Qwen2 BOS mismatch vs YAML"
);
assert_eq!(
tokens.eos_id, 151_645,
"FALSIFY: Qwen2 EOS mismatch vs YAML"
);
assert_eq!(
tokens.pad_id, 151_643,
"FALSIFY: Qwen2 PAD mismatch vs YAML"
);
assert_eq!(
tokens.im_start_id, 151_644,
"FALSIFY: Qwen2 im_start mismatch vs YAML"
);
assert_eq!(
tokens.im_end_id, 151_645,
"FALSIFY: Qwen2 im_end mismatch vs YAML"
);
assert_tokens_within_vocab(&tokens, 151_936, "qwen2");
}
#[test]
fn falsify_qwen3_5_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::qwen3_5();
assert_eq!(
tokens.eos_id, 248_044,
"FALSIFY: Qwen3.5 EOS mismatch vs YAML"
);
assert_tokens_within_vocab(&tokens, 248_320, "qwen3_5");
}
#[test]
fn falsify_llama_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::llama();
assert_eq!(
tokens.bos_id, 128_000,
"FALSIFY: LLaMA BOS mismatch vs YAML"
);
assert_eq!(
tokens.eos_id, 128_001,
"FALSIFY: LLaMA EOS mismatch vs YAML"
);
assert_eq!(
tokens.pad_id, 128_001,
"FALSIFY: LLaMA PAD mismatch vs YAML"
);
assert_tokens_within_vocab(&tokens, 128_256, "llama");
}
#[test]
fn falsify_mistral_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::mistral();
assert_eq!(tokens.bos_id, 1, "FALSIFY: Mistral BOS mismatch vs YAML");
assert_eq!(tokens.eos_id, 2, "FALSIFY: Mistral EOS mismatch vs YAML");
assert_tokens_within_vocab(&tokens, 32_000, "mistral");
}
#[test]
fn falsify_gemma_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::gemma();
assert_eq!(tokens.bos_id, 2, "FALSIFY: Gemma BOS mismatch vs YAML");
assert_eq!(tokens.eos_id, 1, "FALSIFY: Gemma EOS mismatch vs YAML");
assert_eq!(tokens.pad_id, 0, "FALSIFY: Gemma PAD mismatch vs YAML");
assert_tokens_within_vocab(&tokens, 256_000, "gemma");
}
#[test]
fn falsify_deepseek_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::deepseek();
assert_eq!(tokens.bos_id, 0, "FALSIFY: DeepSeek BOS mismatch vs YAML");
assert_eq!(tokens.eos_id, 1, "FALSIFY: DeepSeek EOS mismatch vs YAML");
assert_tokens_within_vocab(&tokens, 129_280, "deepseek");
}
#[test]
fn falsify_phi3_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::phi3();
assert_eq!(tokens.bos_id, 1, "FALSIFY: Phi-3 BOS mismatch vs YAML");
assert_eq!(tokens.eos_id, 32_000, "FALSIFY: Phi-3 EOS mismatch vs YAML");
assert_eq!(tokens.pad_id, 32_000, "FALSIFY: Phi-3 PAD mismatch vs YAML");
assert_tokens_within_vocab(&tokens, 32_064, "phi3");
}
#[test]
fn falsify_phi2_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::phi2();
assert_eq!(tokens.eos_id, 50_256, "FALSIFY: Phi-2 EOS mismatch vs YAML");
assert_eq!(tokens.pad_id, 50_256, "FALSIFY: Phi-2 PAD mismatch vs YAML");
assert_tokens_within_vocab(&tokens, 51_200, "phi2");
}
#[test]
fn falsify_gpt2_special_tokens_match_yaml_registry() {
let tokens = SpecialTokens::gpt2();
assert_eq!(tokens.eos_id, 50_256, "FALSIFY: GPT-2 EOS mismatch vs YAML");
assert_eq!(tokens.pad_id, 50_256, "FALSIFY: GPT-2 PAD mismatch vs YAML");
assert_tokens_within_vocab(&tokens, 50_257, "gpt2");
}
#[test]
fn falsify_from_architecture_maps_all_yaml_families() {
let mappings = [
("qwen2", 151_645_u32), ("qwen3", 151_645), ("qwen3moe", 151_645), ("qwen3_5", 248_044),
("llama", 128_001),
("mistral", 2),
("gemma", 1),
("gemma2", 1), ("deepseek", 1),
("deepseek2", 1), ("phi3", 32_000),
("phi", 50_256), ("phi2", 50_256),
("gpt2", 50_256),
];
for (arch, expected_eos) in mappings {
let tokens = SpecialTokens::from_architecture(arch)
.unwrap_or_else(|| panic!("FALSIFY: from_architecture('{arch}') returned None"));
assert_eq!(
tokens.eos_id, expected_eos,
"FALSIFY: from_architecture('{arch}') EOS mismatch"
);
}
}
#[test]
fn falsify_from_architecture_unknown_returns_none() {
assert!(SpecialTokens::from_architecture("unknown").is_none());
assert!(SpecialTokens::from_architecture("mamba").is_none());
assert!(SpecialTokens::from_architecture("").is_none());
}