aprender-core 0.29.1

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
460
461
462
463
464
465
466
467
468
469
470
pub(crate) use super::*;

// Mock module for testing
pub(super) struct MockModule {
    weights: Tensor,
}

impl MockModule {
    fn new(data: &[f32], shape: &[usize]) -> Self {
        Self {
            weights: Tensor::new(data, shape),
        }
    }
}

impl Module for MockModule {
    fn forward(&self, input: &Tensor) -> Tensor {
        input.clone()
    }

    fn parameters(&self) -> Vec<&Tensor> {
        vec![&self.weights]
    }
}

// Empty module for testing edge case
pub(super) struct EmptyModule;

impl Module for EmptyModule {
    fn forward(&self, input: &Tensor) -> Tensor {
        input.clone()
    }

    fn parameters(&self) -> Vec<&Tensor> {
        vec![]
    }
}

// ==========================================================================
// FALSIFICATION: L1 importance equals absolute weight (spec item 31)
// ==========================================================================
#[test]
fn test_magnitude_l1_equals_abs_weight() {
    let importance = MagnitudeImportance::l1();
    let weights = Tensor::new(&[1.0, -2.0, 3.0, -4.0], &[4]);

    let scores = importance.compute_for_weights(&weights).unwrap();
    let expected = &[1.0, 2.0, 3.0, 4.0]; // |w|

    assert_eq!(
        scores.data(),
        expected,
        "MAG-01 FALSIFIED: L1 importance should equal |w|"
    );
}

#[test]
fn test_magnitude_l1_zero_weights() {
    let importance = MagnitudeImportance::l1();
    let weights = Tensor::new(&[0.0, 0.0, 0.0], &[3]);

    let scores = importance.compute_for_weights(&weights).unwrap();
    let expected = &[0.0, 0.0, 0.0];

    assert_eq!(
        scores.data(),
        expected,
        "MAG-02 FALSIFIED: L1 of zeros should be zeros"
    );
}

// ==========================================================================
// FALSIFICATION: L2 importance equals weight squared (spec item 32)
// ==========================================================================
#[test]
fn test_magnitude_l2_equals_weight_squared() {
    let importance = MagnitudeImportance::l2();
    let weights = Tensor::new(&[1.0, -2.0, 3.0, -4.0], &[4]);

    let scores = importance.compute_for_weights(&weights).unwrap();
    let expected = &[1.0, 4.0, 9.0, 16.0]; // w²

    assert_eq!(
        scores.data(),
        expected,
        "MAG-03 FALSIFIED: L2 importance should equal w²"
    );
}

#[test]
fn test_magnitude_l2_zero_weights() {
    let importance = MagnitudeImportance::l2();
    let weights = Tensor::new(&[0.0, 0.0, 0.0], &[3]);

    let scores = importance.compute_for_weights(&weights).unwrap();

    for &v in scores.data() {
        assert!(
            v == 0.0,
            "MAG-04 FALSIFIED: L2 of zeros should be zeros, got {}",
            v
        );
    }
}

// ==========================================================================
// FALSIFICATION: Importance is always non-negative (spec item 47)
// ==========================================================================
#[test]
fn test_magnitude_importance_always_non_negative() {
    let l1 = MagnitudeImportance::l1();
    let l2 = MagnitudeImportance::l2();

    // Test with various negative weights
    let weights = Tensor::new(&[-5.0, -0.001, -100.0, -0.0], &[4]);

    let l1_scores = l1.compute_for_weights(&weights).unwrap();
    let l2_scores = l2.compute_for_weights(&weights).unwrap();

    for &v in l1_scores.data() {
        assert!(
            v >= 0.0,
            "MAG-05 FALSIFIED: L1 importance should be non-negative, got {}",
            v
        );
    }

    for &v in l2_scores.data() {
        assert!(
            v >= 0.0,
            "MAG-05 FALSIFIED: L2 importance should be non-negative, got {}",
            v
        );
    }
}

// ==========================================================================
// FALSIFICATION: All zeros produces no NaN (spec item 1)
// ==========================================================================
#[test]
fn test_magnitude_all_zeros_no_nan() {
    let l1 = MagnitudeImportance::l1();
    let l2 = MagnitudeImportance::l2();

    let weights = Tensor::new(&[0.0, 0.0, 0.0, 0.0], &[4]);

    let l1_scores = l1.compute_for_weights(&weights).unwrap();
    let l2_scores = l2.compute_for_weights(&weights).unwrap();

    for &v in l1_scores.data() {
        assert!(
            !v.is_nan(),
            "MAG-06 FALSIFIED: L1 should not produce NaN on zeros"
        );
    }

    for &v in l2_scores.data() {
        assert!(
            !v.is_nan(),
            "MAG-06 FALSIFIED: L2 should not produce NaN on zeros"
        );
    }
}

// ==========================================================================
// FALSIFICATION: NaN weights detected (Jidoka)
// ==========================================================================
#[test]
fn test_magnitude_detects_nan_weights() {
    let importance = MagnitudeImportance::l2();
    let weights = Tensor::new(&[1.0, f32::NAN, 3.0], &[3]);

    let result = importance.compute_for_weights(&weights);

    assert!(
        result.is_err(),
        "MAG-07 FALSIFIED: NaN weights should be detected"
    );
    match result.unwrap_err() {
        PruningError::NumericalInstability { method, details } => {
            assert_eq!(method, "magnitude_l2");
            assert!(details.contains("NaN"));
        }
        _ => panic!("MAG-07 FALSIFIED: Expected NumericalInstability error"),
    }
}

#[test]
fn test_magnitude_detects_inf_weights() {
    let importance = MagnitudeImportance::l1();
    let weights = Tensor::new(&[1.0, f32::INFINITY, 3.0], &[3]);

    let result = importance.compute_for_weights(&weights);

    assert!(
        result.is_err(),
        "MAG-08 FALSIFIED: Inf weights should be detected"
    );
    match result.unwrap_err() {
        PruningError::NumericalInstability { method, details } => {
            assert_eq!(method, "magnitude_l1");
            assert!(details.contains("Inf"));
        }
        _ => panic!("MAG-08 FALSIFIED: Expected NumericalInstability error"),
    }
}

#[test]
fn test_magnitude_detects_neg_inf_weights() {
    let importance = MagnitudeImportance::l2();
    let weights = Tensor::new(&[1.0, f32::NEG_INFINITY, 3.0], &[3]);

    let result = importance.compute_for_weights(&weights);

    assert!(
        result.is_err(),
        "MAG-09 FALSIFIED: -Inf weights should be detected"
    );
}

// ==========================================================================
// FALSIFICATION: Does not require calibration
// ==========================================================================
#[test]
fn test_magnitude_does_not_require_calibration() {
    let l1 = MagnitudeImportance::l1();
    let l2 = MagnitudeImportance::l2();

    assert!(
        !l1.requires_calibration(),
        "MAG-10 FALSIFIED: L1 should not require calibration"
    );
    assert!(
        !l2.requires_calibration(),
        "MAG-10 FALSIFIED: L2 should not require calibration"
    );
}

// ==========================================================================
// FALSIFICATION: Module integration via Importance trait
// ==========================================================================
#[test]
fn test_magnitude_compute_via_trait() {
    let module = MockModule::new(&[1.0, -2.0, 3.0, -4.0], &[2, 2]);
    let importance = MagnitudeImportance::l2();

    let scores = importance.compute(&module, None).unwrap();

    assert_eq!(scores.method, "magnitude_l2");
    assert_eq!(scores.shape(), &[2, 2]);
    assert_eq!(scores.values.data(), &[1.0, 4.0, 9.0, 16.0]);
}

#[test]
fn test_magnitude_empty_module_error() {
    let module = EmptyModule;
    let importance = MagnitudeImportance::l1();

    let result = importance.compute(&module, None);

    assert!(
        result.is_err(),
        "MAG-11 FALSIFIED: empty module should error"
    );
    match result.unwrap_err() {
        PruningError::NoParameters { .. } => (),
        _ => panic!("MAG-11 FALSIFIED: Expected NoParameters error"),
    }
}

// ==========================================================================
// FALSIFICATION: Name method
// ==========================================================================
#[test]
fn test_magnitude_name() {
    let l1 = MagnitudeImportance::l1();
    let l2 = MagnitudeImportance::l2();

    assert_eq!(l1.name(), "magnitude_l1", "MAG-12 FALSIFIED: wrong L1 name");
    assert_eq!(l2.name(), "magnitude_l2", "MAG-12 FALSIFIED: wrong L2 name");
}

// ==========================================================================
// FALSIFICATION: Norm getter
// ==========================================================================
#[test]
fn test_magnitude_norm_getter() {
    let l1 = MagnitudeImportance::l1();
    let l2 = MagnitudeImportance::l2();

    assert_eq!(l1.norm(), NormType::L1);
    assert_eq!(l2.norm(), NormType::L2);
}

// ==========================================================================
// FALSIFICATION: with_norm constructor
// ==========================================================================
#[test]
fn test_magnitude_with_norm() {
    let l1 = MagnitudeImportance::with_norm(NormType::L1);
    let l2 = MagnitudeImportance::with_norm(NormType::L2);

    assert_eq!(l1.norm(), NormType::L1);
    assert_eq!(l2.norm(), NormType::L2);
}

// ==========================================================================
// FALSIFICATION: Shape preserved
// ==========================================================================
#[test]
fn test_magnitude_preserves_shape() {
    let importance = MagnitudeImportance::l2();

    // 2D
    let weights_2d = Tensor::new(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]);
    let scores_2d = importance.compute_for_weights(&weights_2d).unwrap();
    assert_eq!(
        scores_2d.shape(),
        &[2, 3],
        "MAG-13 FALSIFIED: 2D shape should be preserved"
    );

    // 1D
    let weights_1d = Tensor::new(&[1.0, 2.0, 3.0], &[3]);
    let scores_1d = importance.compute_for_weights(&weights_1d).unwrap();
    assert_eq!(
        scores_1d.shape(),
        &[3],
        "MAG-13 FALSIFIED: 1D shape should be preserved"
    );
}

// ==========================================================================
// FALSIFICATION: Very small weights handled correctly
// ==========================================================================
#[test]
fn test_magnitude_small_weights() {
    let importance = MagnitudeImportance::l2();
    let weights = Tensor::new(&[1e-10, 1e-20, 1e-30], &[3]);

    let scores = importance.compute_for_weights(&weights).unwrap();

    // L2 of very small numbers should still be >= 0 and not NaN
    for &v in scores.data() {
        assert!(
            v >= 0.0,
            "MAG-14 FALSIFIED: small weight score should be >= 0"
        );
        assert!(
            !v.is_nan(),
            "MAG-14 FALSIFIED: small weight score should not be NaN"
        );
    }
}

// ==========================================================================
// FALSIFICATION: Large weights handled correctly
// ==========================================================================
#[test]
fn test_magnitude_large_weights() {
    let importance = MagnitudeImportance::l1();
    let weights = Tensor::new(&[1e10, 1e20, 1e30], &[3]);

    let scores = importance.compute_for_weights(&weights).unwrap();

    assert!(
        (scores.data()[0] - 1e10).abs() < 1e5,
        "MAG-15 FALSIFIED: large weight L1 should be preserved"
    );
}

// ==========================================================================
// FALSIFICATION: Clone and Debug
// ==========================================================================
#[test]
fn test_magnitude_clone() {
    let orig = MagnitudeImportance::l2();
    let cloned = orig.clone();

    assert_eq!(orig.norm(), cloned.norm());
}

#[test]
fn test_magnitude_debug() {
    let importance = MagnitudeImportance::l1();
    let debug = format!("{:?}", importance);
    assert!(debug.contains("MagnitudeImportance"));
}

#[test]
fn test_norm_type_eq() {
    assert_eq!(NormType::L1, NormType::L1);
    assert_eq!(NormType::L2, NormType::L2);
    assert_ne!(NormType::L1, NormType::L2);
}

// ==========================================================================
// FALSIFICATION: NormType Copy and Clone
// ==========================================================================
#[test]
fn test_norm_type_copy() {
    let norm = NormType::L1;
    let copied = norm;
    assert_eq!(norm, copied);
}

#[test]
fn test_norm_type_clone() {
    let norm = NormType::L2;
    let cloned = norm.clone();
    assert_eq!(norm, cloned);
}

#[test]
fn test_norm_type_debug() {
    let l1 = NormType::L1;
    let l2 = NormType::L2;
    let debug_l1 = format!("{:?}", l1);
    let debug_l2 = format!("{:?}", l2);
    assert!(debug_l1.contains("L1"));
    assert!(debug_l2.contains("L2"));
}

// ==========================================================================
// FALSIFICATION: L1 formula with very precise values
// ==========================================================================
#[test]
fn test_magnitude_l1_precise() {
    let importance = MagnitudeImportance::l1();
    let weights = Tensor::new(&[-0.5, 0.5], &[2]);

    let scores = importance.compute_for_weights(&weights).unwrap();

    assert!(
        (scores.data()[0] - 0.5).abs() < 1e-6,
        "MAG-16 FALSIFIED: L1 of -0.5 should be 0.5"
    );
    assert!(
        (scores.data()[1] - 0.5).abs() < 1e-6,
        "MAG-16 FALSIFIED: L1 of 0.5 should be 0.5"
    );
}

// ==========================================================================
// FALSIFICATION: L2 formula with very precise values
// ==========================================================================
#[test]
fn test_magnitude_l2_precise() {
    let importance = MagnitudeImportance::l2();
    let weights = Tensor::new(&[0.5, -0.5, 2.0, -2.0], &[4]);

    let scores = importance.compute_for_weights(&weights).unwrap();

    assert!(
        (scores.data()[0] - 0.25).abs() < 1e-6,
        "MAG-17 FALSIFIED: L2 of 0.5 should be 0.25"
    );
    assert!(
        (scores.data()[1] - 0.25).abs() < 1e-6,
        "MAG-17 FALSIFIED: L2 of -0.5 should be 0.25"
    );
    assert!(
        (scores.data()[2] - 4.0).abs() < 1e-6,
        "MAG-17 FALSIFIED: L2 of 2.0 should be 4.0"
    );
    assert!(
        (scores.data()[3] - 4.0).abs() < 1e-6,
        "MAG-17 FALSIFIED: L2 of -2.0 should be 4.0"
    );
}