aprender-core 0.30.0

Next-generation machine learning library in pure Rust
// Section 14: Realizar Inference (F-REALIZE-*)
// All require model files
// =============================================================================

#[test]
fn f_realize_001_prefill_and_incremental_same_logits() {
    // F-REALIZE-001: Two runs with same prompt produce same first token (prefill consistency)
    let gguf = require_model!(gguf_model_path(), "GGUF model");
    let gguf_str = gguf.to_str().unwrap();
    let mut outputs = Vec::new();
    for _ in 0..2 {
        let (success, stdout, stderr) = run_apr(&[
            "run",
            gguf_str,
            "--prompt",
            "The capital of France is",
            "--max-tokens",
            "1",
            "--temperature",
            "0",
        ]);
        if !success {
            eprintln!("SKIP: apr run failed: {}", stderr);
            return;
        }
        outputs.push(stdout);
    }
    assert_eq!(
        outputs[0], outputs[1],
        "F-REALIZE-001: Prefill must be deterministic (same prompt → same first token)"
    );
}

#[test]
fn f_realize_002_gqa_attention_correct() {
    // F-REALIZE-002: `apr inspect` shows GQA configuration (num_kv_heads < num_heads)
    let gguf = require_model!(gguf_model_path(), "GGUF model");
    let (success, stdout, stderr) = run_apr(&["inspect", gguf.to_str().unwrap()]);
    if !success {
        eprintln!("SKIP: apr inspect failed: {}", stderr);
        return;
    }
    let combined = format!("{stdout}{stderr}");
    // Qwen2 0.5B uses GQA (num_kv_heads=2, num_heads=14)
    assert!(
        combined.contains("head") || combined.contains("attention") || combined.contains("gqa"),
        "F-REALIZE-002: apr inspect must show attention configuration"
    );
}

#[test]
fn f_realize_003_rope_applied_before_caching() {
    // F-REALIZE-003: Structural check — realizar has RoPE application code
    let realizar_dir = project_root()
        .parent()
        .expect("parent dir")
        .join("realizar")
        .join("src");
    if !realizar_dir.exists() {
        eprintln!("SKIP: realizar not found at sibling path");
        return;
    }
    let mut has_rope = false;
    for path in collect_rs_files(&realizar_dir) {
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        if content.contains("rope") || content.contains("RoPE") || content.contains("rotary") {
            has_rope = true;
            break;
        }
    }
    assert!(
        has_rope,
        "F-REALIZE-003: realizar must have RoPE implementation"
    );
}

#[test]
fn f_realize_004_chatml_template_applied() {
    // F-REALIZE-004: Structural check — ChatMLTemplate exists with im_start markers
    let chat_path = project_root()
        .join("src")
        .join("text")
        .join("chat_template")
        .join("mod.rs");
    let content = std::fs::read_to_string(&chat_path).expect("chat_template/mod.rs must exist");
    assert!(
        content.contains("ChatMLTemplate"),
        "F-REALIZE-004: ChatMLTemplate must exist"
    );
    assert!(
        content.contains("im_start") || content.contains("<|im_start|>"),
        "F-REALIZE-004: ChatML must use <|im_start|> markers"
    );
    assert!(
        content.contains("create_template"),
        "F-REALIZE-004: create_template factory must exist"
    );
}

#[test]
fn f_realize_005_chat_completions_returns_valid_response() {
    // F-REALIZE-005: Structural check — serve command has chat completions handler
    let serve_path = project_root()
        .join("crates")
        .join("apr-cli")
        .join("src")
        .join("commands")
        .join("serve");
    let mut has_chat_completions = false;
    if serve_path.is_dir() {
        for path in collect_rs_files(&serve_path) {
            let content = std::fs::read_to_string(&path).unwrap_or_default();
            if content.contains("chat") && content.contains("completions") {
                has_chat_completions = true;
                break;
            }
        }
    } else {
        // Try serve.rs
        let serve_file = serve_path.with_extension("rs");
        if serve_file.exists() {
            let content = std::fs::read_to_string(&serve_file).unwrap_or_default();
            has_chat_completions = content.contains("chat") && content.contains("completions");
        }
    }
    assert!(
        has_chat_completions,
        "F-REALIZE-005: serve command must have chat completions endpoint"
    );
}

#[test]
fn f_realize_006_circuit_breaker_trips_on_oom() {
    // F-REALIZE-006: Structural check — CircuitBreaker exists in federation/health.rs
    let health_path = project_root()
        .join("crates")
        .join("apr-cli")
        .join("src")
        .join("federation")
        .join("health.rs");
    let content = std::fs::read_to_string(&health_path).expect("health.rs must exist");
    assert!(
        content.contains("CircuitBreaker"),
        "F-REALIZE-006: CircuitBreaker struct must exist"
    );
    assert!(
        content.contains("CircuitBreakerState") || content.contains("circuit_breaker"),
        "F-REALIZE-006: Circuit breaker state management must exist"
    );
}

#[test]
fn f_realize_007_fused_q4k_matches_dequant_then_matmul() {
    // F-REALIZE-007: Structural check — fused Q4K kernel exists in realizar
    let realizar_dir = project_root()
        .parent()
        .expect("parent dir")
        .join("realizar")
        .join("src");
    if !realizar_dir.exists() {
        eprintln!("SKIP: realizar not found at sibling path");
        return;
    }
    let mut has_fused_q4k = false;
    for path in collect_rs_files(&realizar_dir) {
        let content = std::fs::read_to_string(&path).unwrap_or_default();
        if content.contains("fused") && content.contains("q4k") {
            has_fused_q4k = true;
            break;
        }
    }
    assert!(
        has_fused_q4k,
        "F-REALIZE-007: realizar must have fused Q4K kernel"
    );
}

#[test]
fn f_realize_008_swiglu_activation_for_qwen2() {
    // F-REALIZE-008: Structural check — MlpType::SwiGlu exists and qwen2 uses it
    let family_path = project_root()
        .join("src")
        .join("format")
        .join("model_family.rs");
    let content = std::fs::read_to_string(&family_path).expect("model_family.rs must exist");
    assert!(
        content.contains("SwiGlu"),
        "F-REALIZE-008: MlpType::SwiGlu variant must exist"
    );
    // Verify qwen2 YAML specifies swiglu
    let qwen2_yaml = project_root()
        .join("contracts")
        .join("model-families")
        .join("qwen2.yaml");
    let yaml_content = std::fs::read_to_string(&qwen2_yaml).expect("qwen2.yaml must exist");
    assert!(
        yaml_content.contains("swiglu") || yaml_content.contains("SwiGLU"),
        "F-REALIZE-008: Qwen2 YAML must specify SwiGLU activation"
    );
}

#[test]
fn f_realize_009_greedy_sampling_is_deterministic() {
    // F-REALIZE-009: Structural check — GreedyDecoder exists (deterministic by definition)
    let gen_path = project_root()
        .join("src")
        .join("nn")
        .join("generation")
        .join("mod.rs");
    let content = std::fs::read_to_string(&gen_path).expect("generation/mod.rs must exist");
    assert!(
        content.contains("GreedyDecoder"),
        "F-REALIZE-009: GreedyDecoder must exist"
    );
    // Greedy = argmax = deterministic. Verify no randomness in greedy path.
    // Check that there's a Decode trait or similar
    assert!(
        content.contains("fn decode")
            || content.contains("fn sample")
            || content.contains("fn generate"),
        "F-REALIZE-009: GreedyDecoder must have decode/sample/generate method"
    );
}

#[test]
fn f_realize_010_paged_attention_no_corruption_on_long_seq() {
    // F-REALIZE-010: Long-sequence generation produces readable output (no corruption)
    let gguf = require_model!(gguf_model_path(), "GGUF model");
    let gguf_str = gguf.to_str().unwrap();
    let (success, stdout, stderr) = run_apr(&[
        "run",
        gguf_str,
        "--prompt",
        "Write a short poem about the ocean.",
        "--max-tokens",
        "50",
        "--temperature",
        "0",
    ]);
    if !success {
        eprintln!("SKIP: apr run failed: {}", stderr);
        return;
    }
    // Output should not contain garbage replacement chars
    assert!(
        !stdout.contains('\u{FFFD}'),
        "F-REALIZE-010: Long sequence output must not contain U+FFFD replacement chars"
    );
    assert!(
        stdout.len() > 10,
        "F-REALIZE-010: Long sequence must produce substantial output (got {} bytes)",
        stdout.len()
    );
}

// =============================================================================