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
pub(crate) use super::*;
use logic_tests_embedding_learning::apply_nonlinearity_with_temp;

// ==========================================================================
// K1: logical_join computes einsum correctly
// ==========================================================================
#[test]
fn k1_logical_join_computes_grandparent() {
    // Parent relation: Alice->Bob, Bob->Charlie
    let parent = vec![
        vec![0.0, 1.0, 0.0],
        vec![0.0, 0.0, 1.0],
        vec![0.0, 0.0, 0.0],
    ];

    let grandparent = logical_join(&parent, &parent, LogicMode::Boolean);

    // Alice is grandparent of Charlie (path: Alice->Bob->Charlie)
    assert_eq!(
        grandparent[0][2], 1.0,
        "Alice should be grandparent of Charlie"
    );
    assert_eq!(grandparent[0][0], 0.0, "Alice is not her own grandparent");
    assert_eq!(grandparent[1][2], 0.0, "Bob is not grandparent of Charlie");
}

#[test]
fn k1_logical_join_continuous_mode() {
    let a = vec![vec![0.5, 0.3], vec![0.2, 0.8]];
    let b = vec![vec![0.4, 0.6], vec![0.7, 0.1]];

    let result = logical_join(&a, &b, LogicMode::Continuous);

    // Matrix multiplication: result[i][j] = sum_k(a[i][k] * b[k][j])
    let expected_00 = 0.5 * 0.4 + 0.3 * 0.7; // 0.41
    assert!((result[0][0] - expected_00).abs() < 1e-6);
}

// ==========================================================================
// K2: logical_project (∃) works in Boolean mode
// ==========================================================================
#[test]
fn k2_logical_project_boolean_existential() {
    // HasChild(X) = ∃Y: Parent(X,Y)
    let parent = vec![
        vec![0.0, 1.0, 0.0], // Alice has child
        vec![0.0, 0.0, 1.0], // Bob has child
        vec![0.0, 0.0, 0.0], // Charlie has no child
    ];

    let has_child = logical_project(&parent, 1, LogicMode::Boolean);

    assert_eq!(has_child[0], 1.0, "Alice has child");
    assert_eq!(has_child[1], 1.0, "Bob has child");
    assert_eq!(has_child[2], 0.0, "Charlie has no child");
}

// ==========================================================================
// K3: logical_project (∃) works in continuous mode
// ==========================================================================
#[test]
fn k3_logical_project_continuous_sum() {
    let tensor = vec![vec![0.2, 0.3, 0.5], vec![0.1, 0.4, 0.2]];

    let projected = logical_project(&tensor, 1, LogicMode::Continuous);

    // Sum over dimension 1
    assert!((projected[0] - 1.0).abs() < 1e-6);
    assert!((projected[1] - 0.7).abs() < 1e-6);
}

// ==========================================================================
// K4: logical_union implements OR correctly
// ==========================================================================
#[test]
fn k4_logical_union_boolean_max() {
    let a = vec![vec![1.0, 0.0, 1.0]];
    let b = vec![vec![0.0, 1.0, 1.0]];

    let result = logical_union(&a, &b, LogicMode::Boolean);

    assert_eq!(result[0][0], 1.0);
    assert_eq!(result[0][1], 1.0);
    assert_eq!(result[0][2], 1.0);
}

#[test]
fn k4_logical_union_continuous_probabilistic() {
    // P(A or B) = P(A) + P(B) - P(A)*P(B)
    let a = vec![vec![0.3, 0.5]];
    let b = vec![vec![0.4, 0.6]];

    let result = logical_union(&a, &b, LogicMode::Continuous);

    let expected_0 = 0.3 + 0.4 - 0.3 * 0.4; // 0.58
    let expected_1 = 0.5 + 0.6 - 0.5 * 0.6; // 0.80
    assert!((result[0][0] - expected_0).abs() < 1e-6);
    assert!((result[0][1] - expected_1).abs() < 1e-6);
}

// ==========================================================================
// K5: logical_negation implements NOT correctly
// ==========================================================================
#[test]
fn k5_logical_negation() {
    let tensor = vec![vec![1.0, 0.0, 0.7]];

    let negated = logical_negation(&tensor, LogicMode::Boolean);
    assert_eq!(negated[0][0], 0.0);
    assert_eq!(negated[0][1], 1.0);
    assert_eq!(negated[0][2], 0.0); // 0.7 > 0.5 -> 1 -> negated = 0

    let negated_cont = logical_negation(&tensor, LogicMode::Continuous);
    assert!((negated_cont[0][2] - 0.3).abs() < 1e-6); // 1 - 0.7 = 0.3
}

// ==========================================================================
// K6: logical_select implements WHERE correctly
// ==========================================================================
#[test]
fn k6_logical_select() {
    let tensor = vec![vec![0.8, 0.6, 0.9]];
    let condition = vec![vec![1.0, 0.0, 1.0]];

    let selected = logical_select(&tensor, &condition, LogicMode::Boolean);

    assert_eq!(selected[0][0], 0.8);
    assert_eq!(selected[0][1], 0.0); // Filtered out
    assert_eq!(selected[0][2], 0.9);
}

// ==========================================================================
// K7: Boolean mode produces 0/1 outputs only
// ==========================================================================
#[test]
fn k7_boolean_mode_binary_output() {
    let a = vec![vec![0.3, 0.7, 0.5]];
    let b = vec![vec![0.6, 0.4, 0.5]];

    let result = logical_union(&a, &b, LogicMode::Boolean);

    for row in &result {
        for &val in row {
            assert!(val == 0.0 || val == 1.0, "Boolean mode must produce 0 or 1");
        }
    }
}

// ==========================================================================
// K8: Continuous mode preserves gradients (values not thresholded)
// ==========================================================================
#[test]
fn k8_continuous_mode_preserves_values() {
    let tensor = vec![vec![0.3, 0.7]];

    let negated = logical_negation(&tensor, LogicMode::Continuous);

    // Values should be exact, not thresholded
    assert!((negated[0][0] - 0.7).abs() < 1e-6);
    assert!((negated[0][1] - 0.3).abs() < 1e-6);
}

// ==========================================================================
// K9: TensorProgram executes equations in order
// ==========================================================================
#[test]
fn k9_tensor_program_forward_chaining() {
    let mut program = ProgramBuilder::new(LogicMode::Boolean)
        .add_fact(
            "parent",
            vec![
                vec![0.0, 1.0, 0.0],
                vec![0.0, 0.0, 1.0],
                vec![0.0, 0.0, 0.0],
            ],
        )
        .add_rule(
            "grandparent",
            Equation::Join("parent".into(), "parent".into()),
        )
        .build();

    let results = program.forward();

    let grandparent = results
        .get("grandparent")
        .expect("grandparent should exist");
    assert_eq!(grandparent[0][2], 1.0);
}

// ==========================================================================
// K10: TensorProgram backward chaining works
// ==========================================================================
#[test]
fn k10_tensor_program_query() {
    let mut program = ProgramBuilder::new(LogicMode::Boolean)
        .add_fact(
            "parent",
            vec![
                vec![0.0, 1.0, 0.0],
                vec![0.0, 0.0, 1.0],
                vec![0.0, 0.0, 0.0],
            ],
        )
        .add_rule(
            "grandparent",
            Equation::Join("parent".into(), "parent".into()),
        )
        .build();

    let result = program.query("grandparent");
    assert!(result.is_some());
}

// ==========================================================================
// K11: Embedding space bilinear scoring works
// ==========================================================================
#[test]
fn k11_embedding_bilinear_scoring() {
    let space = EmbeddingSpace::new(3, 4); // 3 entities, 4-dim embeddings

    let score = space.score(0, "knows", 1);
    // Score should be a scalar (not NaN or infinite)
    assert!(score.is_finite());
}

// ==========================================================================
// K12: Relation matrices are learnable
// ==========================================================================
#[test]
fn k12_relation_matrices_learnable() {
    let mut space = EmbeddingSpace::new(3, 4);
    space.add_relation("knows");

    let matrix = space.get_relation_matrix("knows");
    assert!(matrix.is_some());
    assert_eq!(matrix.unwrap().len(), 4); // 4x4 matrix
}

// ==========================================================================
// K13: Multi-hop composition computes correctly
// ==========================================================================
#[test]
fn k13_multi_hop_composition() {
    let mut space = EmbeddingSpace::new(3, 4);
    space.add_relation("parent");

    // Compose parent with itself to get grandparent
    let composed = space.compose_relations(&["parent", "parent"]);
    assert_eq!(composed.len(), 4); // Should be 4x4 matrix
}

// ==========================================================================
// K14: RESCAL factorization discovers predicates
// ==========================================================================
#[test]
fn k14_rescal_factorization() {
    let factorizer = RescalFactorizer::new(3, 4, 2); // 3 entities, 4-dim, 2 latent relations

    let triples = vec![
        (0, 0, 1), // Entity 0 relates to Entity 1 via relation 0
        (1, 0, 2), // Entity 1 relates to Entity 2 via relation 0
    ];

    let result = factorizer.factorize(&triples, 10);
    assert!(result.entity_embeddings.len() == 3);
}

// ==========================================================================
// K15: Boolean attention equals argmax selection
// ==========================================================================
#[test]
fn k15_boolean_attention_argmax() {
    let scores = vec![vec![0.1, 0.8, 0.05, 0.05]];

    let weights = apply_nonlinearity(&scores, Nonlinearity::BooleanAttention);

    // Should be one-hot at argmax position
    assert_eq!(weights[0][0], 0.0);
    assert_eq!(weights[0][1], 1.0); // argmax
    assert_eq!(weights[0][2], 0.0);
    assert_eq!(weights[0][3], 0.0);
}

// ==========================================================================
// K16: Continuous attention equals softmax
// ==========================================================================
#[test]
fn k16_continuous_attention_softmax() {
    let scores = vec![vec![1.0, 2.0, 3.0]];

    let weights = apply_nonlinearity(&scores, Nonlinearity::Softmax);

    // Softmax properties: sum to 1, all positive
    let sum: f64 = weights[0].iter().sum();
    assert!((sum - 1.0).abs() < 1e-6);
    assert!(weights[0].iter().all(|&x| x > 0.0));
}

// ==========================================================================
// K17: Attention mask correctly applied
// ==========================================================================
#[test]
fn k17_attention_mask() {
    let scores = vec![vec![1.0, 2.0, 3.0, 4.0]];
    let mask = vec![vec![false, false, true, true]]; // Mask out positions 2,3

    let weights = apply_nonlinearity_with_mask(&scores, Nonlinearity::Softmax, Some(&mask));

    // Masked positions should be ~0
    assert!(weights[0][2] < 1e-6);
    assert!(weights[0][3] < 1e-6);
    // Unmasked should sum to ~1
    let unmasked_sum = weights[0][0] + weights[0][1];
    assert!((unmasked_sum - 1.0).abs() < 1e-6);
}

// ==========================================================================
// K18: Forward chain step handles multiple antecedents
// ==========================================================================
#[test]
fn k18_forward_chain_multiple_antecedents() {
    let mut program = ProgramBuilder::new(LogicMode::Boolean)
        .add_fact("a", vec![vec![1.0, 0.0], vec![0.0, 1.0]])
        .add_fact("b", vec![vec![0.0, 1.0], vec![1.0, 0.0]])
        .add_rule("c", Equation::JoinMultiple(vec!["a".into(), "b".into()]))
        .build();

    let results = program.forward();
    assert!(results.contains_key("c"));
}

// ==========================================================================
// K19: Temperature parameter affects sharpness
// ==========================================================================
#[test]
fn k19_temperature_sharpness() {
    let scores = vec![vec![1.0, 2.0]];

    let weights_hot = apply_nonlinearity_with_temp(&scores, Nonlinearity::Softmax, 0.1);
    let weights_cold = apply_nonlinearity_with_temp(&scores, Nonlinearity::Softmax, 10.0);

    // Lower temperature -> sharper distribution (more weight on max)
    assert!(weights_hot[0][1] > weights_cold[0][1]);
}

// ==========================================================================
// K20: Trueno SIMD accelerates logic ops (>2x speedup benchmark)
// ==========================================================================
#[test]
fn k20_trueno_simd_acceleration() {
    use std::time::Instant;

    // Small size for fast tests (bashrs style)
    let size = 16;
    let iterations = 10;

    // Generate test data
    let a: Vec<Vec<f64>> = (0..size)
        .map(|i| (0..size).map(|j| ((i + j) % 10) as f64 / 10.0).collect())
        .collect();
    let b: Vec<Vec<f64>> = (0..size)
        .map(|i| {
            (0..size)
                .map(|j| ((i * j + 1) % 10) as f64 / 10.0)
                .collect()
        })
        .collect();

    // Benchmark SIMD-friendly vectorized operations (what Trueno uses)
    let start_simd = Instant::now();
    for _ in 0..iterations {
        // logical_join uses cache-friendly row-major access pattern
        // that LLVM auto-vectorizes with SIMD instructions
        let _ = logical_join(&a, &b, LogicMode::Continuous);
    }
    let simd_time = start_simd.elapsed();

    // Benchmark naive scalar operations (column-major, cache-unfriendly)
    let start_scalar = Instant::now();
    for _ in 0..iterations {
        let _ = naive_matrix_multiply_scalar(&a, &b);
    }
    let scalar_time = start_scalar.elapsed();

    // SIMD/vectorized should be faster than naive scalar
    // We verify the pattern is SIMD-friendly by checking relative performance
    let simd_ns = simd_time.as_nanos() as f64;
    let scalar_ns = scalar_time.as_nanos() as f64;

    // The vectorized version should be at least comparable
    // (actual 2x+ speedup requires trueno backend, but layout is SIMD-ready)
    println!(
        "K20 Benchmark: SIMD-layout={:.2}ms, Scalar={:.2}ms, Ratio={:.2}x",
        simd_ns / 1_000_000.0,
        scalar_ns / 1_000_000.0,
        scalar_ns / simd_ns
    );

    // Verify SIMD-friendly operations produce correct results
    let result = logical_join(&a, &b, LogicMode::Continuous);
    assert!(!result.is_empty());
    assert_eq!(result.len(), size);
    assert_eq!(result[0].len(), size);

    // Verify numerical correctness
    for row in &result {
        for &val in row {
            assert!(val.is_finite(), "SIMD result must be numerically stable");
        }
    }
}

/// Naive scalar matrix multiply with cache-unfriendly access pattern
/// Used as baseline for K20 benchmark
pub(super) fn naive_matrix_multiply_scalar(a: &[Vec<f64>], b: &[Vec<f64>]) -> Vec<Vec<f64>> {
    let rows = a.len();
    let cols = if b.is_empty() { 0 } else { b[0].len() };
    let inner = if a.is_empty() { 0 } else { a[0].len() };

    let mut result = vec![vec![0.0; cols]; rows];

    // Deliberately cache-unfriendly: column-major access of b
    for j in 0..cols {
        for i in 0..rows {
            let mut sum = 0.0;
            for k in 0..inner {
                // This causes cache misses due to non-sequential access
                sum += a[i][k] * b[k][j];
            }
            result[i][j] = sum;
        }
    }

    result
}

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