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
pub(crate) use super::*;
pub(crate) use crate::format::test_factory::{
    build_pygmy_apr, build_pygmy_apr_f16, build_pygmy_apr_q4, build_pygmy_apr_q8,
    build_pygmy_apr_with_config, build_pygmy_safetensors, build_pygmy_safetensors_with_config,
    PygmyConfig,
};

// ========================================================================
// Format Detection Tests
// ========================================================================

#[test]
fn test_detect_format_v2() {
    assert_eq!(detect_format(&MAGIC_APR2), Some("v2"));
    assert_eq!(detect_format(&MAGIC_APR0), Some("v2"));
}

#[test]
fn test_detect_format_v1() {
    assert_eq!(detect_format(&MAGIC_APRN), Some("v1"));
    assert_eq!(detect_format(&MAGIC_APR1), Some("v1"));
}

#[test]
fn test_detect_format_invalid() {
    assert_eq!(detect_format(&[0x00, 0x00, 0x00, 0x00]), None);
    assert_eq!(detect_format(&[0xFF, 0xFF, 0xFF, 0xFF]), None);
    assert_eq!(detect_format(b"GGUF"), None);
}

#[test]
fn test_is_valid_apr_magic() {
    assert!(is_valid_apr_magic(&MAGIC_APR2));
    assert!(is_valid_apr_magic(&MAGIC_APR0));
    assert!(is_valid_apr_magic(&MAGIC_APRN));
    assert!(!is_valid_apr_magic(b"GGUF"));
    assert!(!is_valid_apr_magic(&[0x00; 4]));
}

// ========================================================================
// TensorListOptions Tests
// ========================================================================

#[test]
fn test_options_default() {
    let opts = TensorListOptions::default();
    assert!(!opts.compute_stats);
    assert!(opts.filter.is_none());
    // Default limit is usize::MAX (effectively unlimited)
    assert_eq!(opts.limit, usize::MAX);
}

#[test]
fn test_options_builder() {
    let opts = TensorListOptions::new()
        .with_stats()
        .with_filter("weight")
        .with_limit(10);

    assert!(opts.compute_stats);
    assert_eq!(opts.filter, Some("weight".to_string()));
    assert_eq!(opts.limit, 10);
}

// ========================================================================
// Pygmy APR v2 Tests (TOOL-APR-001 Fix)
// ========================================================================

#[test]
fn test_list_tensors_pygmy_apr_default() {
    let apr_bytes = build_pygmy_apr();
    let result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("list tensors");

    assert_eq!(result.format_version, "v2");
    assert!(result.tensor_count > 0, "Expected at least one tensor");
    assert!(result.total_size_bytes > 0);

    // Check we got tensor names from the index (could be various naming conventions)
    let names: Vec<_> = result.tensors.iter().map(|t| t.name.as_str()).collect();

    // Pygmy uses "model." prefix
    let has_model_tensors = names.iter().any(|n| n.starts_with("model."));
    let has_lm_head = names.iter().any(|n| n.contains("lm_head"));

    assert!(
        has_model_tensors || has_lm_head,
        "Expected model tensors, got: {:?}",
        names
    );
}

#[test]
fn test_list_tensors_pygmy_apr_with_filter() {
    let apr_bytes = build_pygmy_apr();
    let opts = TensorListOptions::new().with_filter("self_attn");
    let result = list_tensors_from_bytes(&apr_bytes, opts).expect("list tensors");

    // All returned tensors should match filter
    for tensor in &result.tensors {
        assert!(
            tensor.name.contains("self_attn"),
            "Expected tensor {} to contain 'self_attn'",
            tensor.name
        );
    }
}

#[test]
fn test_list_tensors_pygmy_apr_with_limit() {
    // Use llama_style which has many tensors (>10)
    let apr_bytes = build_pygmy_apr_with_config(PygmyConfig::llama_style());

    // First, get total count without limit
    let full_result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("full list");
    let total_tensors = full_result.tensor_count;

    // GH-195 FIX: Verify model has more than our test limit
    assert!(
        total_tensors > 3,
        "Test requires model with >3 tensors, got {total_tensors}"
    );

    // Now apply limit
    let opts = TensorListOptions::new().with_limit(3);
    let limited_result = list_tensors_from_bytes(&apr_bytes, opts).expect("limited list");

    // P2 FIX: Use exact assertions, not tautological >= checks
    assert_eq!(
        limited_result.tensors.len(),
        3,
        "tensors.len() should equal the limit when total > limit"
    );
    assert_eq!(
        limited_result.tensor_count, total_tensors,
        "tensor_count must reflect TRUE total ({total_tensors}), not truncated length"
    );
}

#[test]
fn test_list_tensors_pygmy_apr_with_stats() {
    let apr_bytes = build_pygmy_apr();
    let opts = TensorListOptions::new().with_stats().with_limit(5);
    let result = list_tensors_from_bytes(&apr_bytes, opts).expect("list tensors");

    // Check at least one tensor has stats
    let has_stats = result
        .tensors
        .iter()
        .any(|t| t.mean.is_some() && t.std.is_some() && t.nan_count.is_some());
    assert!(has_stats, "Expected at least one tensor to have stats");
}

#[test]
fn test_list_tensors_pygmy_apr_f16() {
    let apr_bytes = build_pygmy_apr_f16();
    let result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("list tensors");

    // F16 tensors should be detected
    let f16_tensors: Vec<_> = result.tensors.iter().filter(|t| t.dtype == "f16").collect();
    assert!(!f16_tensors.is_empty(), "Expected F16 tensors");
}

#[test]
fn test_list_tensors_pygmy_apr_q8() {
    let apr_bytes = build_pygmy_apr_q8();
    let result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("list tensors");

    // Should have at least some tensors (mix of Q8 and F32)
    assert!(!result.tensors.is_empty(), "Expected tensors in Q8 model");

    // Q8 model contains both F32 (embedding) and Q8 (attention) tensors
    let dtypes: Vec<_> = result.tensors.iter().map(|t| t.dtype.as_str()).collect();
    assert!(
        dtypes.iter().any(|d| *d == "q8" || *d == "f32"),
        "Expected Q8 or F32 tensors, got: {:?}",
        dtypes
    );
}

#[test]
fn test_list_tensors_pygmy_apr_q4() {
    let apr_bytes = build_pygmy_apr_q4();
    let result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("list tensors");

    // Should have at least some tensors (mix of Q4 and F32)
    assert!(!result.tensors.is_empty(), "Expected tensors in Q4 model");

    // Q4 model contains both F32 (embedding) and Q4 (attention) tensors
    let dtypes: Vec<_> = result.tensors.iter().map(|t| t.dtype.as_str()).collect();
    assert!(
        dtypes.iter().any(|d| *d == "q4" || *d == "f32"),
        "Expected Q4 or F32 tensors, got: {:?}",
        dtypes
    );
}

#[test]
fn test_list_tensors_pygmy_apr_minimal() {
    let apr_bytes = build_pygmy_apr_with_config(PygmyConfig::minimal());
    let result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("list tensors");

    // Minimal config has embedding only
    assert!(result.tensor_count >= 1);
}

#[test]
fn test_list_tensors_pygmy_apr_llama_style() {
    let apr_bytes = build_pygmy_apr_with_config(PygmyConfig::llama_style());
    let result =
        list_tensors_from_bytes(&apr_bytes, TensorListOptions::default()).expect("list tensors");

    // LLaMA style has many tensors (at least some)
    assert!(
        result.tensor_count >= 1,
        "Expected at least 1 tensor, got {}",
        result.tensor_count
    );

    // Should have tensors with various components
    let has_any_tensor = !result.tensors.is_empty();
    assert!(has_any_tensor);
}

// ========================================================================
// Error Handling Tests
// ========================================================================

#[test]
fn test_list_tensors_empty_data() {
    let result = list_tensors_from_bytes(&[], TensorListOptions::default());
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.to_string().contains("too small"));
}

#[test]
fn test_list_tensors_invalid_magic() {
    let data = [0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00];
    let result = list_tensors_from_bytes(&data, TensorListOptions::default());
    assert!(result.is_err());
    let err = result.unwrap_err();
    assert!(err.to_string().contains("Unknown model format"));
}

#[test]
fn test_list_tensors_truncated_v2() {
    // Valid v2 magic but truncated file
    let mut data = vec![0x41, 0x50, 0x52, 0x00]; // "APR\0"
    data.extend_from_slice(&[0u8; 10]); // Not enough for header

    let result = list_tensors_from_bytes(&data, TensorListOptions::default());
    assert!(result.is_err());
}

// ========================================================================
// Statistics Tests
// ========================================================================

#[test]
fn test_compute_stats_normal_values() {
    let mut info = TensorInfo {
        name: "test".to_string(),
        shape: vec![4],
        dtype: "f32".to_string(),
        size_bytes: 16,
        mean: None,
        std: None,
        min: None,
        max: None,
        nan_count: None,
        inf_count: None,
    };

    let data = vec![1.0, 2.0, 3.0, 4.0];
    compute_tensor_stats(&mut info, &data);

    assert_eq!(info.mean, Some(2.5));
    assert!(info.std.expect("test value") > 1.0 && info.std.expect("test value") < 1.2);
    assert_eq!(info.min, Some(1.0));
    assert_eq!(info.max, Some(4.0));
    assert_eq!(info.nan_count, Some(0));
    assert_eq!(info.inf_count, Some(0));
}

#[test]
fn test_compute_stats_with_nan() {
    let mut info = TensorInfo {
        name: "test".to_string(),
        shape: vec![3],
        dtype: "f32".to_string(),
        size_bytes: 12,
        mean: None,
        std: None,
        min: None,
        max: None,
        nan_count: None,
        inf_count: None,
    };

    let data = vec![1.0, f32::NAN, 2.0];
    compute_tensor_stats(&mut info, &data);

    assert_eq!(info.nan_count, Some(1));
    assert_eq!(info.inf_count, Some(0));
    // Mean should be computed from valid values only
    assert_eq!(info.mean, Some(1.5));
}

#[test]
fn test_compute_stats_with_inf() {
    let mut info = TensorInfo {
        name: "test".to_string(),
        shape: vec![3],
        dtype: "f32".to_string(),
        size_bytes: 12,
        mean: None,
        std: None,
        min: None,
        max: None,
        nan_count: None,
        inf_count: None,
    };

    let data = vec![1.0, f32::INFINITY, 2.0];
    compute_tensor_stats(&mut info, &data);

    assert_eq!(info.nan_count, Some(0));
    assert_eq!(info.inf_count, Some(1));
}

#[test]
fn test_compute_stats_empty() {
    let mut info = TensorInfo {
        name: "test".to_string(),
        shape: vec![0],
        dtype: "f32".to_string(),
        size_bytes: 0,
        mean: None,
        std: None,
        min: None,
        max: None,
        nan_count: None,
        inf_count: None,
    };

    compute_tensor_stats(&mut info, &[]);

    assert!(info.mean.is_none());
    assert!(info.std.is_none());
}

// ========================================================================
// Format Size Tests
// ========================================================================

#[test]
fn test_format_size_bytes() {
    assert_eq!(format_size(500), "500 B");
    assert_eq!(format_size(0), "0 B");
}

#[test]
fn test_format_size_kb() {
    assert_eq!(format_size(1024), "1.0 KB");
    assert_eq!(format_size(2048), "2.0 KB");
}

#[test]
fn test_format_size_mb() {
    assert_eq!(format_size(1024 * 1024), "1.0 MB");
    assert_eq!(format_size(100 * 1024 * 1024), "100.0 MB");
}

#[test]
fn test_format_size_gb() {
    assert_eq!(format_size(1024 * 1024 * 1024), "1.0 GB");
    assert_eq!(format_size(5 * 1024 * 1024 * 1024), "5.0 GB");
}

// ========================================================================
// TensorInfo Tests
// ========================================================================

#[test]
fn test_tensor_info_clone() {
    let info = TensorInfo {
        name: "test".to_string(),
        shape: vec![10, 20],
        dtype: "f32".to_string(),
        size_bytes: 800,
        mean: Some(0.5),
        std: Some(0.1),
        min: Some(0.0),
        max: Some(1.0),
        nan_count: Some(0),
        inf_count: Some(0),
    };

    let cloned = info.clone();
    assert_eq!(cloned.name, info.name);
    assert_eq!(cloned.shape, info.shape);
    assert_eq!(cloned.dtype, info.dtype);
}

#[test]
fn test_tensor_list_result_clone() {
    let result = TensorListResult {
        file: "test.apr".to_string(),
        format_version: "v2".to_string(),
        tensor_count: 5,
        total_size_bytes: 1000,
        tensors: vec![],
    };

    let cloned = result.clone();
    assert_eq!(cloned.file, result.file);
    assert_eq!(cloned.format_version, result.format_version);
}

#[path = "tensors_tests_workflow.rs"]
mod tensors_tests_workflow;
#[path = "tensors_tests_stats.rs"]
mod tensors_tests_stats;
#[path = "tensors_tests_dtype_convert.rs"]
mod tensors_tests_dtype_convert;