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
pub(crate) use super::*;
pub(crate) use crate::pruning::calibration::ActivationStats;

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

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

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

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

// ==========================================================================
// FALSIFICATION: Wanda requires calibration (spec item 52)
// ==========================================================================
#[test]
fn test_wanda_requires_calibration() {
    let wanda = WandaImportance::new("layer0");
    assert!(
        wanda.requires_calibration(),
        "WND-01 FALSIFIED: Wanda must require calibration"
    );
}

#[test]
fn test_wanda_errors_without_calibration() {
    let module = MockLinear::new(&[1.0, 2.0, 3.0, 4.0], 2, 2);
    let wanda = WandaImportance::new("layer0");

    // No context provided
    let result = wanda.compute(&module, None);

    assert!(
        result.is_err(),
        "WND-02 FALSIFIED: Should error without calibration"
    );
    match result.unwrap_err() {
        PruningError::CalibrationRequired { method } => {
            assert_eq!(method, "wanda");
        }
        _ => panic!("WND-02 FALSIFIED: Expected CalibrationRequired error"),
    }
}

#[test]
fn test_wanda_errors_missing_layer_stats() {
    let module = MockLinear::new(&[1.0, 2.0, 3.0, 4.0], 2, 2);
    let wanda = WandaImportance::new("nonexistent_layer");

    // Context exists but doesn't have stats for this layer
    let ctx = CalibrationContext::new("test".to_string());
    let result = wanda.compute(&module, Some(&ctx));

    assert!(
        result.is_err(),
        "WND-03 FALSIFIED: Should error on missing layer stats"
    );
    match result.unwrap_err() {
        PruningError::MissingActivationStats { layer } => {
            assert_eq!(layer, "nonexistent_layer");
        }
        _ => panic!("WND-03 FALSIFIED: Expected MissingActivationStats error"),
    }
}

// ==========================================================================
// FALSIFICATION: Zero activations handled (spec item 9)
// ==========================================================================
#[test]
fn test_wanda_zero_activations_handled() {
    let wanda = WandaImportance::new("layer0");

    // Weights [2, 2]
    let weights = Tensor::new(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
    // Activation norms with a zero
    let norms = Tensor::new(&[0.0, 1.0], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms);
    assert!(
        result.is_ok(),
        "WND-04 FALSIFIED: Should handle zero activations"
    );

    let importance = result.unwrap();
    for &v in importance.data() {
        assert!(
            !v.is_nan(),
            "WND-04 FALSIFIED: Zero activations should not produce NaN"
        );
        assert!(
            v.is_finite(),
            "WND-04 FALSIFIED: Zero activations should not produce Inf"
        );
    }
}

// ==========================================================================
// FALSIFICATION: Importance is always non-negative (spec item 47)
// ==========================================================================
#[test]
fn test_wanda_importance_non_negative() {
    let wanda = WandaImportance::new("layer0");

    // Mixed positive and negative weights
    let weights = Tensor::new(&[-1.0, 2.0, -3.0, 4.0], &[2, 2]);
    let norms = Tensor::new(&[1.0, 2.0], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms);
    assert!(result.is_ok());

    let importance = result.unwrap();
    for &v in importance.data() {
        assert!(
            v >= 0.0,
            "WND-05 FALSIFIED: Wanda importance should be non-negative, got {}",
            v
        );
    }
}

// ==========================================================================
// FALSIFICATION: Wanda formula correctness
// ==========================================================================
#[test]
fn test_wanda_formula_correctness() {
    let wanda = WandaImportance::new("layer0").with_eps(0.0);

    // Weights [2, 2]: [[1, 2], [3, 4]]
    let weights = Tensor::new(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
    // Activation norms: [4.0, 9.0] -> sqrt = [2.0, 3.0]
    let norms = Tensor::new(&[4.0, 9.0], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms).unwrap();
    let data = result.data();

    // importance[0,0] = |1| * sqrt(4) = 1 * 2 = 2.0
    // importance[0,1] = |2| * sqrt(9) = 2 * 3 = 6.0
    // importance[1,0] = |3| * sqrt(4) = 3 * 2 = 6.0
    // importance[1,1] = |4| * sqrt(9) = 4 * 3 = 12.0
    assert!(
        (data[0] - 2.0).abs() < 1e-6,
        "WND-06 FALSIFIED: importance[0,0] should be 2.0, got {}",
        data[0]
    );
    assert!(
        (data[1] - 6.0).abs() < 1e-6,
        "WND-06 FALSIFIED: importance[0,1] should be 6.0, got {}",
        data[1]
    );
    assert!(
        (data[2] - 6.0).abs() < 1e-6,
        "WND-06 FALSIFIED: importance[1,0] should be 6.0, got {}",
        data[2]
    );
    assert!(
        (data[3] - 12.0).abs() < 1e-6,
        "WND-06 FALSIFIED: importance[1,1] should be 12.0, got {}",
        data[3]
    );
}

#[test]
fn test_wanda_with_negative_weights() {
    let wanda = WandaImportance::new("layer0").with_eps(0.0);

    // Negative weights
    let weights = Tensor::new(&[-2.0, -3.0], &[1, 2]);
    let norms = Tensor::new(&[1.0, 4.0], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms).unwrap();
    let data = result.data();

    // importance[0,0] = |-2| * sqrt(1) = 2 * 1 = 2.0
    // importance[0,1] = |-3| * sqrt(4) = 3 * 2 = 6.0
    assert!(
        (data[0] - 2.0).abs() < 1e-6,
        "WND-07 FALSIFIED: should use absolute weight"
    );
    assert!(
        (data[1] - 6.0).abs() < 1e-6,
        "WND-07 FALSIFIED: should use absolute weight"
    );
}

// ==========================================================================
// FALSIFICATION: Shape validation
// ==========================================================================
#[test]
fn test_wanda_shape_mismatch() {
    let wanda = WandaImportance::new("layer0");

    // Weights [2, 3] but norms [2] (should be [3])
    let weights = Tensor::new(&[1.0, 2.0, 3.0, 4.0, 5.0, 6.0], &[2, 3]);
    let norms = Tensor::new(&[1.0, 2.0], &[2]); // Wrong size!

    let result = wanda.compute_from_tensors(&weights, &norms);

    assert!(
        result.is_err(),
        "WND-08 FALSIFIED: Should detect shape mismatch"
    );
    match result.unwrap_err() {
        PruningError::ShapeMismatch { expected, got } => {
            assert_eq!(expected, vec![3]); // in_features = 3
            assert_eq!(got, vec![2]);
        }
        _ => panic!("WND-08 FALSIFIED: Expected ShapeMismatch error"),
    }
}

#[test]
fn test_wanda_1d_weights_rejected() {
    let wanda = WandaImportance::new("layer0");

    let weights = Tensor::new(&[1.0, 2.0, 3.0], &[3]); // 1D, not 2D
    let norms = Tensor::new(&[1.0, 2.0, 3.0], &[3]);

    let result = wanda.compute_from_tensors(&weights, &norms);

    assert!(
        result.is_err(),
        "WND-09 FALSIFIED: Should reject 1D weights"
    );
}

// ==========================================================================
// FALSIFICATION: NaN/Inf detection (Jidoka)
// ==========================================================================
#[test]
fn test_wanda_detects_nan_weights() {
    let wanda = WandaImportance::new("layer0");

    let weights = Tensor::new(&[1.0, f32::NAN, 3.0, 4.0], &[2, 2]);
    let norms = Tensor::new(&[1.0, 2.0], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms);

    assert!(
        result.is_err(),
        "WND-10 FALSIFIED: Should detect NaN weights"
    );
    match result.unwrap_err() {
        PruningError::NumericalInstability { method, details } => {
            assert_eq!(method, "wanda");
            assert!(details.contains("NaN"));
        }
        _ => panic!("WND-10 FALSIFIED: Expected NumericalInstability error"),
    }
}

#[test]
fn test_wanda_detects_nan_norms() {
    let wanda = WandaImportance::new("layer0");

    let weights = Tensor::new(&[1.0, 2.0, 3.0, 4.0], &[2, 2]);
    let norms = Tensor::new(&[1.0, f32::NAN], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms);

    assert!(result.is_err(), "WND-11 FALSIFIED: Should detect NaN norms");
}

#[test]
fn test_wanda_detects_inf_weights() {
    let wanda = WandaImportance::new("layer0");

    let weights = Tensor::new(&[1.0, f32::INFINITY, 3.0, 4.0], &[2, 2]);
    let norms = Tensor::new(&[1.0, 2.0], &[2]);

    let result = wanda.compute_from_tensors(&weights, &norms);

    assert!(
        result.is_err(),
        "WND-12 FALSIFIED: Should detect Inf weights"
    );
}

// ==========================================================================
// FALSIFICATION: Integration with Module
// ==========================================================================
#[test]
fn test_wanda_compute_via_trait() {
    // Create module
    let module = MockLinear::new(&[1.0, 2.0, 3.0, 4.0], 2, 2);

    // Create calibration context with stats
    let mut ctx = CalibrationContext::new("test".to_string());
    let mut stats = ActivationStats::new(2);
    // Update stats to have some norms
    stats.update(&Tensor::new(&[2.0, 3.0], &[2]));
    ctx.add_layer_stats("layer0".to_string(), stats);

    // Compute via trait
    let wanda = WandaImportance::new("layer0");
    let result = wanda.compute(&module, Some(&ctx));

    assert!(
        result.is_ok(),
        "WND-13 FALSIFIED: Should compute successfully"
    );
    let scores = result.unwrap();
    assert_eq!(scores.method, "wanda");
    assert_eq!(scores.shape(), &[2, 2]);
}

// ==========================================================================
// FALSIFICATION: Name method
// ==========================================================================
#[test]
fn test_wanda_name() {
    let wanda = WandaImportance::new("layer0");
    assert_eq!(wanda.name(), "wanda", "WND-14 FALSIFIED: wrong name");
}

// ==========================================================================
// FALSIFICATION: Getters
// ==========================================================================
#[test]
fn test_wanda_layer_name_getter() {
    let wanda = WandaImportance::new("model.layer.0.mlp");
    assert_eq!(wanda.layer_name(), "model.layer.0.mlp");
}

#[test]
fn test_wanda_pattern_getter() {
    let wanda = WandaImportance::new("layer0");
    assert_eq!(wanda.pattern(), None);

    let wanda = wanda.with_pattern(SparsityPattern::NM { n: 2, m: 4 });
    assert_eq!(wanda.pattern(), Some(SparsityPattern::NM { n: 2, m: 4 }));
}

// ==========================================================================
// FALSIFICATION: Builder pattern
// ==========================================================================
#[test]
fn test_wanda_with_pattern() {
    let wanda = WandaImportance::new("layer0").with_pattern(SparsityPattern::NM { n: 2, m: 4 });

    assert_eq!(wanda.pattern(), Some(SparsityPattern::NM { n: 2, m: 4 }));
}

#[test]
fn test_wanda_with_eps() {
    let wanda = WandaImportance::new("layer0").with_eps(1e-10);

    // Verify it's used by testing with zero activations
    let weights = Tensor::new(&[1.0], &[1, 1]);
    let norms = Tensor::new(&[0.0], &[1]);

    let result = wanda.compute_from_tensors(&weights, &norms).unwrap();
    let data = result.data();

    // With eps=1e-10, sqrt(eps) = 1e-5
    // importance = |1.0| * 1e-5 = 1e-5
    assert!(
        (data[0] - 1e-5).abs() < 1e-8,
        "WND-15 FALSIFIED: custom eps should be used"
    );
}

// ==========================================================================
// FALSIFICATION: Clone and Debug
// ==========================================================================
#[test]
fn test_wanda_clone() {
    let orig = WandaImportance::new("layer0").with_pattern(SparsityPattern::NM { n: 2, m: 4 });
    let cloned = orig.clone();

    assert_eq!(orig.layer_name(), cloned.layer_name());
    assert_eq!(orig.pattern(), cloned.pattern());
}

#[test]
fn test_wanda_debug() {
    let wanda = WandaImportance::new("layer0");
    let debug = format!("{:?}", wanda);
    assert!(debug.contains("WandaImportance"));
    assert!(debug.contains("layer0"));
}

// ==========================================================================
// FALSIFICATION: Empty module
// ==========================================================================
#[test]
fn test_wanda_empty_module() {
    struct EmptyModule;
    impl Module for EmptyModule {
        fn forward(&self, input: &Tensor) -> Tensor {
            input.clone()
        }
        fn parameters(&self) -> Vec<&Tensor> {
            vec![]
        }
    }

    let module = EmptyModule;
    let mut ctx = CalibrationContext::new("test".to_string());
    ctx.add_layer_stats("layer0".to_string(), ActivationStats::new(10));

    let wanda = WandaImportance::new("layer0");
    let result = wanda.compute(&module, Some(&ctx));

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

// ==========================================================================
// FALSIFICATION: Shape preserved
// ==========================================================================
#[test]
fn test_wanda_preserves_shape() {
    let wanda = WandaImportance::new("layer0");

    let weights = Tensor::new(&[1.0; 12], &[3, 4]);
    let norms = Tensor::new(&[1.0; 4], &[4]);

    let result = wanda.compute_from_tensors(&weights, &norms).unwrap();

    assert_eq!(
        result.shape(),
        &[3, 4],
        "WND-17 FALSIFIED: shape should be preserved"
    );
}

#[path = "wanda_tests_inf_detection.rs"]
mod wanda_tests_inf_detection;