aprender-core 0.31.2

Next-generation machine learning library in pure Rust
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
/// Test result for a single criterion
struct TestResult {
    name: &'static str,
    passed: bool,
    details: Option<String>,
    points: u32,
    max_points: u32,
}

impl TestResult {
    fn pass(name: &'static str, points: u32, details: String) -> Self {
        Self {
            name,
            passed: true,
            details: Some(details),
            points,
            max_points: points,
        }
    }

    fn fail(name: &'static str, max_points: u32, details: String) -> Self {
        Self {
            name,
            passed: false,
            details: Some(details),
            points: 0,
            max_points,
        }
    }

    fn skip(name: &'static str, max_points: u32, reason: String) -> Self {
        Self {
            name,
            passed: true,
            details: Some(format!("SKIP: {}", reason)),
            points: 0,
            max_points,
        }
    }
}

/// Results for a complete matrix cell
struct CellResult {
    cell: MatrixCell,
    tests: Vec<TestResult>,
    total_points: u32,
    max_points: u32,
    elapsed: std::time::Duration,
}

impl CellResult {
    fn passed(&self) -> bool {
        self.tests.iter().all(|t| t.passed)
    }
}

/// Configuration
struct Config {
    apr_binary: PathBuf,
    trace_level: TraceLevel,
    /// Test class for same-model comparison (PMAT-SHOWCASE-METHODOLOGY-001)
    test_class: TestClass,
    min_cpu_tps: f64,
    min_gpu_tps: f64,
    /// Lower threshold for float32 models (SafeTensors) which are slower than quantized
    min_gpu_tps_float32: f64,
    verbose: bool,
    // Model URIs (HuggingFace or local paths) - apr downloads automatically
    gguf_model: String,
    safetensors_model: String,
    apr_model: String,
    /// Compare against Ollama as groundtruth (PMAT-SHOWCASE-METHODOLOGY-001 Section 5)
    with_ollama: bool,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            apr_binary: find_apr_binary(),
            trace_level: TraceLevel::None,
            test_class: TestClass::Quantized, // Default to Class A (faster, recommended)
            // 1.5B model thresholds (PMAT-SHOWCASE-METHODOLOGY-001)
            // Word-based tok/s estimation has ~20% variance, so use conservative thresholds
            // Actual performance: CPU ~5-10 tok/s, GPU ~7-15 tok/s (quantized)
            min_cpu_tps: 5.0,         // 1.5B on CPU is slow (~5-10 tok/s observed)
            min_gpu_tps: 5.0,         // Conservative threshold for GPU (actual ~7-10 tok/s)
            min_gpu_tps_float32: 5.0, // SafeTensors F32 is memory-bound
            verbose: false,
            gguf_model: default_model_for_format(Format::Gguf),
            safetensors_model: default_model_for_format(Format::SafeTensors),
            apr_model: default_model_for_format(Format::Apr),
            with_ollama: false,
        }
    }
}

fn find_apr_binary() -> PathBuf {
    // GH-301: Use CARGO_TARGET_DIR if set, then standard target/, then PATH
    let mut candidates: Vec<PathBuf> = Vec::new();
    if let Ok(target_dir) = std::env::var("CARGO_TARGET_DIR") {
        candidates.push(PathBuf::from(&target_dir).join("release").join("apr"));
        candidates.push(PathBuf::from(&target_dir).join("debug").join("apr"));
    }
    candidates.push(PathBuf::from("target/release/apr"));
    candidates.push(PathBuf::from("target/debug/apr"));
    for path in candidates {
        if path.exists() {
            return path;
        }
    }
    PathBuf::from("apr")
}

/// Canonical GGUF model - the SINGLE SOURCE OF TRUTH for quantized comparisons
/// All APR Q4_K tests MUST use this exact model converted to APR format.
const CANONICAL_GGUF: &str =
    "hf://Qwen/Qwen2.5-Coder-1.5B-Instruct-GGUF/qwen2.5-coder-1.5b-instruct-q4_k_m.gguf";

/// Canonical SafeTensors model - for full precision (Class B) comparisons
const CANONICAL_SAFETENSORS: &str = "hf://Qwen/Qwen2.5-Coder-1.5B-Instruct";

/// Returns HuggingFace URI or local path for model format.
///
/// CRITICAL (PMAT-SHOWCASE-METHODOLOGY-001):
/// - Class A (Quantized): GGUF and APR use the SAME Q4_K_M weights
/// - Class B (Full Precision): SafeTensors and APR use the SAME F32 weights
/// - NEVER compare different quantizations!
fn default_model_for_format(format: Format) -> String {
    match format {
        // GGUF Q4_K_M: Canonical quantized model from HuggingFace
        Format::Gguf => CANONICAL_GGUF.to_string(),
        // SafeTensors F32: Full precision for Class B comparisons
        Format::SafeTensors => CANONICAL_SAFETENSORS.to_string(),
        // APR: Use GGUF source for Class A (quantized) - same weights!
        // For Class B (full precision), user must specify --apr with SafeTensors source
        // Default to GGUF source since Class A is the primary focus
        Format::Apr => CANONICAL_GGUF.to_string(),
    }
}

fn gpu_available() -> bool {
    Command::new("nvidia-smi")
        .stdout(Stdio::null())
        .stderr(Stdio::null())
        .status()
        .map(|s| s.success())
        .unwrap_or(false)
}

/// Wait for child process with timeout using polling (PMAT-QA-PROTOCOL-001 ยง7.6)
///
/// A hung process is a test FAILURE, not a process state. This function:
/// 1. Polls child.try_wait() in a loop
/// 2. Kills the process if timeout exceeded
/// 3. Returns explicit error on timeout
fn wait_with_timeout(
    child: &mut Child,
    timeout: Duration,
) -> Result<std::process::ExitStatus, String> {
    let start = Instant::now();
    let poll_interval = Duration::from_millis(100);

    loop {
        match child.try_wait() {
            Ok(Some(status)) => return Ok(status),
            Ok(None) => {
                // Still running - check timeout
                if start.elapsed() >= timeout {
                    // Kill the hung process
                    let _ = child.kill();
                    let _ = child.wait(); // Reap zombie
                    return Err(format!(
                        "HANG: Process killed after {}s timeout (PMAT-QA-PROTOCOL-001 violation)",
                        timeout.as_secs()
                    ));
                }
                std::thread::sleep(poll_interval);
            }
            Err(e) => return Err(format!("Process error: {}", e)),
        }
    }
}

fn run_apr(config: &Config, args: &[&str]) -> Result<String, String> {
    run_apr_with_timeout(config, args, DEFAULT_TIMEOUT)
}

fn run_apr_with_timeout(
    config: &Config,
    args: &[&str],
    timeout: Duration,
) -> Result<String, String> {
    let mut cmd = if config.apr_binary.to_string_lossy() == "cargo" {
        let mut c = Command::new("cargo");
        c.args(["run", "-p", "apr-cli", "--release", "--"]);
        c.args(args);
        c
    } else {
        let mut c = Command::new(&config.apr_binary);
        c.args(args);
        c
    };

    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());

    if config.verbose {
        eprintln!("{}DEBUG: {:?}{}", CYAN, cmd, NC);
    }

    // Spawn instead of output() to enable timeout handling
    let mut child = match cmd.spawn() {
        Ok(child) => child,
        Err(e) => return Err(format!("Failed to spawn: {}", e)),
    };

    // Wait with timeout - hung processes are test failures
    let status = wait_with_timeout(&mut child, timeout)?;

    // Read output after process completes
    let mut stdout_str = String::new();
    let mut stderr_str = String::new();

    if let Some(mut stdout) = child.stdout.take() {
        let _ = stdout.read_to_string(&mut stdout_str);
    }
    if let Some(mut stderr) = child.stderr.take() {
        let _ = stderr.read_to_string(&mut stderr_str);
    }

    if status.success() {
        Ok(format!("{}{}", stdout_str, stderr_str))
    } else {
        Err(format!("Exit {}: {}{}", status, stdout_str, stderr_str))
    }
}

/// Run `apr chat` with input piped to stdin (PMAT-QA-PROTOCOL-001 ยง7.4)
///
/// Chat mode is tested by piping a prompt to stdin and capturing stdout.
/// This catches hangs that only occur in interactive mode.
fn run_chat_test(
    config: &Config,
    model: &str,
    prompt: &str,
    backend: Backend,
    with_trace: bool,
    timeout: Duration,
) -> Result<String, String> {
    use std::io::Write;

    let mut args: Vec<&str> = vec!["chat", model];
    if let Some(flag) = backend.flag() {
        args.push(flag);
    }
    if with_trace {
        args.push("--trace");
    }

    let mut cmd = if config.apr_binary.to_string_lossy() == "cargo" {
        let mut c = Command::new("cargo");
        c.args(["run", "-p", "apr-cli", "--release", "--"]);
        c.args(&args);
        c
    } else {
        let mut c = Command::new(&config.apr_binary);
        c.args(&args);
        c
    };

    cmd.stdin(Stdio::piped())
        .stdout(Stdio::piped())
        .stderr(Stdio::piped());

    if config.verbose {
        eprintln!("{}DEBUG (chat): {:?}{}", CYAN, cmd, NC);
    }

    let mut child = match cmd.spawn() {
        Ok(child) => child,
        Err(e) => return Err(format!("Failed to spawn chat: {}", e)),
    };

    // Register process for SIGINT cleanup (PMAT-098-PF)
    let pid = child.id();
    register_process(pid);

    // Write prompt to stdin, then close it to signal EOF
    if let Some(mut stdin) = child.stdin.take() {
        let _ = writeln!(stdin, "{}", prompt);
        // stdin is dropped here, closing the pipe
    }

    // Wait with timeout (this handles kill on timeout)
    let status = wait_with_timeout(&mut child, timeout);

    // Unregister after process is reaped (success or timeout)
    unregister_process(pid);

    let status = status?;

    // Read output
    let mut stdout_str = String::new();
    let mut stderr_str = String::new();

    if let Some(mut stdout) = child.stdout.take() {
        let _ = stdout.read_to_string(&mut stdout_str);
    }
    if let Some(mut stderr) = child.stderr.take() {
        let _ = stderr.read_to_string(&mut stderr_str);
    }

    if status.success() {
        Ok(format!("{}{}", stdout_str, stderr_str))
    } else {
        Err(format!(
            "Chat exit {}: {}{}",
            status, stdout_str, stderr_str
        ))
    }
}

/// Poll server health endpoint until ready or timeout (PMAT-QA-PROTOCOL-001 ยง7.4)
fn wait_for_server_ready(server_guard: &mut ProcessGuard, port: u16) -> Result<(), String> {
    let start = Instant::now();
    let server_timeout = Duration::from_secs(30);
    let health_url = format!("http://127.0.0.1:{port}/health");

    loop {
        if start.elapsed() >= server_timeout {
            return Err("Server startup timeout (30s)".to_string());
        }
        if let Some(child) = server_guard.child_mut() {
            if let Ok(Some(status)) = child.try_wait() {
                return Err(format!("Server exited early: {status}"));
            }
        }
        let health_check = Command::new("curl")
            .args(["-s", "-o", "/dev/null", "-w", "%{http_code}", &health_url])
            .output();
        if let Ok(output) = health_check {
            let code = String::from_utf8_lossy(&output.stdout);
            if code.trim() == "200" {
                return Ok(());
            }
        }
        std::thread::sleep(Duration::from_millis(500));
    }
}

/// Run `apr serve` test with HTTP request (PMAT-QA-PROTOCOL-001 ยง7.4)
///
/// 1. Start apr serve on a random port
/// 2. Wait for server ready
/// 3. Send curl request
/// 4. Capture response
/// 5. Kill server
fn run_serve_test(
    config: &Config,
    model: &str,
    prompt: &str,
    backend: Backend,
    with_trace: bool,
    timeout: Duration,
) -> Result<String, String> {
    use std::net::TcpListener;

    // Find an available port
    let port = TcpListener::bind("127.0.0.1:0")
        .map(|l| {
            l.local_addr()
                .expect("bound listener must have local addr")
                .port()
        })
        .unwrap_or(18080);

    let port_str = port.to_string();
    let mut args: Vec<&str> = vec!["serve", model, "--port", &port_str];
    if let Some(flag) = backend.flag() {
        args.push(flag);
    }

    let mut cmd = if config.apr_binary.to_string_lossy() == "cargo" {
        let mut c = Command::new("cargo");
        c.args(["run", "-p", "apr-cli", "--release", "--"]);
        c.args(&args);
        c
    } else {
        let mut c = Command::new(&config.apr_binary);
        c.args(&args);
        c
    };

    cmd.stdout(Stdio::piped()).stderr(Stdio::piped());

    if config.verbose {
        eprintln!("{}DEBUG (serve): {:?} on port {}{}", CYAN, cmd, port, NC);
    }

    // Wrap server in ProcessGuard for SIGINT safety (PMAT-098-PF)
    // This ensures the server is killed even if:
    // 1. The user presses Ctrl+C
    // 2. The test panics
    // 3. An early return occurs
    let mut server_guard = match cmd.spawn() {
        Ok(child) => ProcessGuard::new(child),
        Err(e) => return Err(format!("Failed to spawn serve: {}", e)),
    };

    // Wait for server to be ready (poll health endpoint)
    wait_for_server_ready(&mut server_guard, port)?;

    // Build request body
    let body = format!(
        r#"{{"model":"test","messages":[{{"role":"user","content":"{}"}}],"max_tokens":10}}"#,
        prompt
    );

    // Send chat completion request
    let url = format!("http://127.0.0.1:{}/v1/chat/completions", port);
    let mut curl_args = vec![
        "-s",
        "-X",
        "POST",
        &url,
        "-H",
        "Content-Type: application/json",
        "-d",
        &body,
    ];

    if with_trace {
        curl_args.extend(["-H", "X-Trace-Level: layer"]);
    }

    let request_start = Instant::now();
    let response = Command::new("curl")
        .args(&curl_args)
        .output()
        .map_err(|e| format!("curl failed: {}", e))?;

    // Check timeout
    if request_start.elapsed() >= timeout {
        // ProcessGuard::drop will kill the server
        return Err(format!("Request timeout ({}s)", timeout.as_secs()));
    }

    // Explicitly kill server before returning (ProcessGuard::drop would do this too)
    server_guard.kill_and_wait();

    if response.status.success() {
        Ok(String::from_utf8_lossy(&response.stdout).to_string())
    } else {
        Err(format!(
            "curl error: {}",
            String::from_utf8_lossy(&response.stderr)
        ))
    }
}