apr-cli 0.4.15

CLI tool for APR model inspection, debugging, and operations
Documentation
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

    // ========================================================================
    // BenchConfig Tests
    // ========================================================================

    #[test]
    fn test_bench_config_default() {
        let config = BenchConfig::default();
        assert_eq!(config.warmup, 3);
        assert_eq!(config.iterations, 5);
        assert_eq!(config.max_tokens, 32);
    }

    #[test]
    fn test_bench_config_default_prompt() {
        let config = BenchConfig::default();
        assert_eq!(config.prompt, "What is 2+2?");
    }

    #[test]
    fn test_bench_config_custom_values() {
        let config = BenchConfig {
            warmup: 5,
            iterations: 10,
            max_tokens: 64,
            prompt: "Custom prompt".to_string(),
            quiet: false,
        };
        assert_eq!(config.warmup, 5);
        assert_eq!(config.iterations, 10);
        assert_eq!(config.max_tokens, 64);
        assert_eq!(config.prompt, "Custom prompt");
    }

    // ========================================================================
    // BenchResult Tests
    // ========================================================================

    #[test]
    fn test_bench_result_pass() {
        let result = BenchResult {
            total_tokens: 100,
            total_time: Duration::from_secs(5),
            tokens_per_second: 20.0,
            time_to_first_token: Duration::from_millis(50),
            iteration_times: vec![Duration::from_secs(1); 5],
            mean_time: Duration::from_secs(1),
            median_time: Duration::from_secs(1),
            std_dev: Duration::from_millis(10),
            passed: true,
        };

        assert!(result.passed);
        assert!(result.tokens_per_second >= 10.0);
    }

    #[test]
    fn test_bench_result_fail() {
        let result = BenchResult {
            total_tokens: 50,
            total_time: Duration::from_secs(10),
            tokens_per_second: 5.0, // Below threshold
            time_to_first_token: Duration::from_millis(100),
            iteration_times: vec![Duration::from_secs(2); 5],
            mean_time: Duration::from_secs(2),
            median_time: Duration::from_secs(2),
            std_dev: Duration::from_millis(50),
            passed: false,
        };

        assert!(!result.passed);
        assert!(result.tokens_per_second < 10.0);
    }

    #[test]
    fn test_bench_result_excellent_throughput() {
        let result = BenchResult {
            total_tokens: 1000,
            total_time: Duration::from_secs(1),
            tokens_per_second: 1000.0, // Excellent
            time_to_first_token: Duration::from_millis(5),
            iteration_times: vec![Duration::from_millis(200); 5],
            mean_time: Duration::from_millis(200),
            median_time: Duration::from_millis(200),
            std_dev: Duration::from_millis(5),
            passed: true,
        };

        assert!(result.passed);
        assert!(result.tokens_per_second >= 100.0); // A+ grade threshold
    }

    #[test]
    fn test_bench_result_threshold_boundary() {
        // Exactly at threshold
        let result = BenchResult {
            total_tokens: 100,
            total_time: Duration::from_secs(10),
            tokens_per_second: 10.0, // Exactly at threshold
            time_to_first_token: Duration::from_millis(100),
            iteration_times: vec![Duration::from_secs(2); 5],
            mean_time: Duration::from_secs(2),
            median_time: Duration::from_secs(2),
            std_dev: Duration::from_millis(50),
            passed: true,
        };

        assert!(result.passed);
        assert!(result.tokens_per_second == 10.0);
    }

    #[test]
    fn test_bench_result_just_below_threshold() {
        let result = BenchResult {
            total_tokens: 99,
            total_time: Duration::from_secs(10),
            tokens_per_second: 9.9, // Just below threshold
            time_to_first_token: Duration::from_millis(100),
            iteration_times: vec![Duration::from_secs(2); 5],
            mean_time: Duration::from_secs(2),
            median_time: Duration::from_secs(2),
            std_dev: Duration::from_millis(50),
            passed: false,
        };

        assert!(!result.passed);
        assert!(result.tokens_per_second < 10.0);
    }

    // ========================================================================
    // Run Command Tests (no inference feature)
    // ========================================================================

    #[test]
    fn test_run_file_not_found() {
        let result = run(
            Path::new("/nonexistent/model.gguf"),
            3,     // warmup
            5,     // iterations
            32,    // max_tokens
            None,  // prompt
            false, // fast
            None,  // brick
            false, // json
        );
        // Should fail - file doesn't exist
        assert!(result.is_err());
    }

    #[test]
    fn test_run_is_directory() {
        let dir = tempdir().expect("create temp dir");
        let result = run(
            dir.path(),
            3,     // warmup
            5,     // iterations
            32,    // max_tokens
            None,  // prompt
            false, // fast
            None,  // brick
            false, // json
        );
        // Should fail - it's a directory not a file
        assert!(result.is_err());
    }

    #[test]
    fn test_run_invalid_brick_name() {
        // Create a dummy file so path validation passes
        let file = NamedTempFile::with_suffix(".gguf").expect("create temp file");
        let result = run(
            file.path(),
            3,                         // warmup
            5,                         // iterations
            32,                        // max_tokens
            None,                      // prompt
            false,                     // fast
            Some("invalid_brick_xyz"), // invalid brick name
            false,                     // json
        );
        // Should fail - either no inference feature or invalid brick
        assert!(result.is_err());
    }

    #[test]
    fn test_run_with_custom_prompt() {
        let mut file = NamedTempFile::with_suffix(".gguf").expect("create temp file");
        file.write_all(b"not a real gguf").expect("write");
        let result = run(
            file.path(),
            1,  // warmup
            1,  // iterations
            16, // max_tokens
            Some("Custom test prompt"),
            false, // fast
            None,  // brick
            false, // json
        );
        // Will fail since it's not a real model, but tests the path
        assert!(result.is_err());
    }

    #[test]
    fn test_run_with_various_extensions() {
        // Test .apr extension
        let mut file_apr = NamedTempFile::with_suffix(".apr").expect("create temp file");
        file_apr.write_all(b"not a real apr").expect("write");
        let result = run(file_apr.path(), 1, 1, 16, None, false, None, false);
        assert!(result.is_err()); // Will fail - invalid format

        // Test .safetensors extension
        let mut file_st = NamedTempFile::with_suffix(".safetensors").expect("create temp file");
        file_st.write_all(b"not a real safetensors").expect("write");
        let result = run(file_st.path(), 1, 1, 16, None, false, None, false);
        assert!(result.is_err()); // Will fail - invalid format
    }

    // ========================================================================
    // Brick Benchmark Tests (when inference feature enabled)
    // ========================================================================

    #[cfg(feature = "inference")]
    #[test]
    fn test_brick_benchmark_rms_norm() {
        // Create a dummy file
        let file = NamedTempFile::with_suffix(".gguf").expect("create temp file");
        let _result = run(file.path(), 1, 1, 16, None, false, Some("rms_norm"), false);
        // May pass or fail depending on implementation, but should not panic
        // The important thing is the brick name is recognized
    }

    #[cfg(feature = "inference")]
    #[test]
    fn test_brick_benchmark_invalid_name() {
        let file = NamedTempFile::with_suffix(".gguf").expect("create temp file");
        let result = run(
            file.path(),
            1,
            1,
            16,
            None,
            false,
            Some("nonexistent_brick"),
            false,
        );
        assert!(result.is_err());
        // Error message should mention unknown brick type
    }

    // ========================================================================
    // Additional BenchConfig Tests
    // ========================================================================

    #[test]
    fn test_bench_config_zero_iterations() {
        let config = BenchConfig {
            warmup: 0,
            iterations: 0,
            max_tokens: 0,
            prompt: String::new(),
            quiet: false,
        };
        assert_eq!(config.warmup, 0);
        assert_eq!(config.iterations, 0);
        assert_eq!(config.max_tokens, 0);
        assert!(config.prompt.is_empty());
    }

    #[test]
    fn test_bench_config_large_values() {
        let config = BenchConfig {
            warmup: 1000,
            iterations: 10000,
            max_tokens: 4096,
            prompt: "x".repeat(10000),
            quiet: false,
        };
        assert_eq!(config.warmup, 1000);
        assert_eq!(config.iterations, 10000);
        assert_eq!(config.max_tokens, 4096);
        assert_eq!(config.prompt.len(), 10000);
    }

    #[test]
    fn test_bench_config_unicode_prompt() {
        let config = BenchConfig {
            warmup: 1,
            iterations: 1,
            max_tokens: 32,
            prompt: "日本語テスト 🎉 émojis".to_string(),
            quiet: false,
        };
        assert!(config.prompt.contains('æ—¥'));
        assert!(config.prompt.contains('🎉'));
    }

    // ========================================================================
    // Additional BenchResult Tests
    // ========================================================================

    #[test]
    fn test_bench_result_zero_tokens() {
        let result = BenchResult {
            total_tokens: 0,
            total_time: Duration::from_secs(1),
            tokens_per_second: 0.0,
            time_to_first_token: Duration::from_millis(0),
            iteration_times: vec![],
            mean_time: Duration::from_secs(0),
            median_time: Duration::from_secs(0),
            std_dev: Duration::from_secs(0),
            passed: false,
        };

        assert_eq!(result.total_tokens, 0);
        assert_eq!(result.tokens_per_second, 0.0);
        assert!(!result.passed);
    }

    #[test]
    fn test_bench_result_single_iteration() {
        let result = BenchResult {
            total_tokens: 10,
            total_time: Duration::from_millis(500),
            tokens_per_second: 20.0,
            time_to_first_token: Duration::from_millis(50),
            iteration_times: vec![Duration::from_millis(500)],
            mean_time: Duration::from_millis(500),
            median_time: Duration::from_millis(500),
            std_dev: Duration::from_millis(0),
            passed: true,
        };

        assert_eq!(result.iteration_times.len(), 1);
        assert_eq!(result.mean_time, result.median_time);
    }

    #[test]
    fn test_bench_result_high_variance() {
        let result = BenchResult {
            total_tokens: 100,
            total_time: Duration::from_secs(10),
            tokens_per_second: 10.0,
            time_to_first_token: Duration::from_millis(100),
            iteration_times: vec![
                Duration::from_millis(100),
                Duration::from_secs(5),
                Duration::from_millis(200),
            ],
            mean_time: Duration::from_millis(1767),
            median_time: Duration::from_millis(200),
            std_dev: Duration::from_secs(2), // High variance
            passed: true,
        };

        // Mean and median are very different due to outlier
        assert!(result.mean_time > result.median_time);
    }

    #[test]
    fn test_bench_result_fast_ttft() {
        let result = BenchResult {
            total_tokens: 100,
            total_time: Duration::from_secs(1),
            tokens_per_second: 100.0,
            time_to_first_token: Duration::from_micros(500), // 0.5ms
            iteration_times: vec![Duration::from_millis(200); 5],
            mean_time: Duration::from_millis(200),
            median_time: Duration::from_millis(200),
            std_dev: Duration::from_millis(1),
            passed: true,
        };

        assert!(result.time_to_first_token < Duration::from_millis(1));
    }

    #[test]
    fn test_bench_result_slow_ttft() {
        let result = BenchResult {
            total_tokens: 100,
            total_time: Duration::from_secs(10),
            tokens_per_second: 10.0,
            time_to_first_token: Duration::from_secs(5), // 5s - very slow
            iteration_times: vec![Duration::from_secs(2); 5],
            mean_time: Duration::from_secs(2),
            median_time: Duration::from_secs(2),
            std_dev: Duration::from_millis(10),
            passed: true,
        };

        assert!(result.time_to_first_token >= Duration::from_secs(5));
    }

    // ========================================================================
    // Edge Case Tests
    // ========================================================================

    #[test]
    fn test_run_empty_file() {
        let file = NamedTempFile::with_suffix(".gguf").expect("create temp file");
        // File is empty - should fail validation
        let result = run(file.path(), 1, 1, 16, None, false, None, false);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_unknown_extension() {
        let mut file = NamedTempFile::with_suffix(".xyz").expect("create temp file");
        file.write_all(b"some content").expect("write");
        let result = run(file.path(), 1, 1, 16, None, false, None, false);
        assert!(result.is_err());
    }

    #[test]
    fn test_run_no_extension() {
        let mut file = NamedTempFile::new().expect("create temp file");
        file.write_all(b"some content").expect("write");
        let result = run(file.path(), 1, 1, 16, None, false, None, false);
        assert!(result.is_err());
    }

    #[test]
    fn test_bench_result_iterations_consistency() {
        // Ensure iteration_times length matches what would be expected
        let times = vec![Duration::from_millis(100); 10];
        let result = BenchResult {
            total_tokens: 320,
            total_time: Duration::from_secs(1),
            tokens_per_second: 320.0,
            time_to_first_token: Duration::from_millis(10),
            iteration_times: times.clone(),
            mean_time: Duration::from_millis(100),
            median_time: Duration::from_millis(100),
            std_dev: Duration::from_millis(0),
            passed: true,
        };

        assert_eq!(result.iteration_times.len(), 10);
    }