aprender-core 0.34.0

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
// SHIP-TWO-001 — `apr-model-diagnostics-v1` algorithm-level PARTIAL
// discharge for FALSIFY-DIAG-001..005.
//
// Contract: `contracts/apr-model-diagnostics-v1.yaml`.
// Spec: `docs/specifications/aprender-train/ship-two-models-spec.md`.
//
// ## What this file proves NOW (PARTIAL_ALGORITHM_LEVEL)
//
// Five model-diagnostics gates:
//
// - DIAG-001 (hex byte offset correctness): hex output at offset O for
//   N bytes byte-equals raw[O..O+N].
// - DIAG-002 (fingerprint format-independence): GGUF fingerprint ==
//   SafeTensors fingerprint for same model.
// - DIAG-003 (oracle never misidentifies unknown architecture):
//   randomized tensor names ⇒ FamilyDetection::Unknown.
// - DIAG-004 (compatibility check has no false positives): model size
//   > VRAM ⇒ incompatible.
// - DIAG-005 (NaN fault has actionable remediation): identifies layer
//   AND emits remediation hint.

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FamilyDetection {
    Known(u32),
    Unknown,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DiagVerdict {
    Pass,
    Fail,
}

// -----------------------------------------------------------------------------
// Verdict 1: DIAG-001 — hex byte offset correctness.
// -----------------------------------------------------------------------------

/// Pass iff `displayed_bytes == raw_bytes[offset..offset + displayed_bytes.len()]`.
#[must_use]
pub fn verdict_from_hex_byte_offset(
    raw_bytes: &[u8],
    offset: usize,
    displayed_bytes: &[u8],
) -> DiagVerdict {
    let end = offset.saturating_add(displayed_bytes.len());
    if end > raw_bytes.len() {
        return DiagVerdict::Fail;
    }
    if &raw_bytes[offset..end] == displayed_bytes {
        DiagVerdict::Pass
    } else {
        DiagVerdict::Fail
    }
}

// -----------------------------------------------------------------------------
// Verdict 2: DIAG-002 — fingerprint format-independence.
// -----------------------------------------------------------------------------

/// Pass iff `gguf_fingerprint == safetensors_fingerprint`.
#[must_use]
pub fn verdict_from_fingerprint_format_independent(
    gguf_fingerprint: &[u8; 32],
    safetensors_fingerprint: &[u8; 32],
) -> DiagVerdict {
    if gguf_fingerprint == safetensors_fingerprint {
        DiagVerdict::Pass
    } else {
        DiagVerdict::Fail
    }
}

// -----------------------------------------------------------------------------
// Verdict 3: DIAG-003 — oracle returns Unknown for randomized arch.
// -----------------------------------------------------------------------------

/// Pass iff `detection == FamilyDetection::Unknown` when input has no
/// recognizable architecture markers.
#[must_use]
pub fn verdict_from_oracle_unknown_for_random(detection: FamilyDetection) -> DiagVerdict {
    match detection {
        FamilyDetection::Unknown => DiagVerdict::Pass,
        FamilyDetection::Known(_) => DiagVerdict::Fail,
    }
}

// -----------------------------------------------------------------------------
// Verdict 4: DIAG-004 — compatibility check has no false positives.
// -----------------------------------------------------------------------------

/// Pass iff `is_compatible == false` when model_size_mb > vram_mb.
#[must_use]
pub fn verdict_from_compat_no_false_positive(
    model_size_mb: u64,
    vram_mb: u64,
    reported_compatible: bool,
) -> DiagVerdict {
    let actually_compatible = model_size_mb <= vram_mb;
    if actually_compatible == reported_compatible {
        DiagVerdict::Pass
    } else if !actually_compatible && reported_compatible {
        // FALSE POSITIVE: model exceeds VRAM but reported compatible.
        DiagVerdict::Fail
    } else {
        // false negative: incompatible reported when actually OK —
        // also Fail (under-reports capability).
        DiagVerdict::Fail
    }
}

// -----------------------------------------------------------------------------
// Verdict 5: DIAG-005 — NaN fault has actionable remediation.
// -----------------------------------------------------------------------------

/// `fault_layer_index` is the layer where NaN was detected (None if
/// not located). `remediation` is the hint string emitted.
///
/// Pass iff layer is identified AND remediation is non-empty.
#[must_use]
pub fn verdict_from_nan_fault_remediation(
    fault_layer_index: Option<usize>,
    remediation: &str,
) -> DiagVerdict {
    if fault_layer_index.is_none() {
        return DiagVerdict::Fail;
    }
    if remediation.trim().is_empty() {
        return DiagVerdict::Fail;
    }
    DiagVerdict::Pass
}

#[cfg(test)]
mod tests {
    use super::*;

    // -------------------------------------------------------------------------
    // Section 1: DIAG-001 — hex byte offset.
    // -------------------------------------------------------------------------
    #[test]
    fn diag001_pass_offset_matches() {
        let raw: Vec<u8> = (0..=255).collect();
        let displayed = &raw[64..96]; // 32 bytes at offset 64
        assert_eq!(
            verdict_from_hex_byte_offset(&raw, 64, displayed),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag001_pass_offset_zero() {
        let raw = vec![1_u8, 2, 3, 4];
        assert_eq!(
            verdict_from_hex_byte_offset(&raw, 0, &raw[0..4]),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag001_fail_displayed_corrupted() {
        let raw: Vec<u8> = (0..=10).collect();
        let displayed = vec![100_u8, 101, 102];
        assert_eq!(
            verdict_from_hex_byte_offset(&raw, 0, &displayed),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag001_fail_offset_out_of_range() {
        let raw = vec![1_u8, 2, 3];
        let displayed = vec![1_u8, 2, 3];
        assert_eq!(
            verdict_from_hex_byte_offset(&raw, 5, &displayed),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag001_fail_displayed_extends_past_eof() {
        let raw = vec![1_u8, 2];
        let displayed = vec![1_u8, 2, 3, 4];
        assert_eq!(
            verdict_from_hex_byte_offset(&raw, 0, &displayed),
            DiagVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 2: DIAG-002 — fingerprint format-independence.
    // -------------------------------------------------------------------------
    #[test]
    fn diag002_pass_identical() {
        let fp = [0xAB_u8; 32];
        assert_eq!(
            verdict_from_fingerprint_format_independent(&fp, &fp),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag002_fail_one_bit_differs() {
        let fp1 = [0xAB_u8; 32];
        let mut fp2 = fp1;
        fp2[15] ^= 0x01;
        assert_eq!(
            verdict_from_fingerprint_format_independent(&fp1, &fp2),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag002_fail_completely_different() {
        let fp1 = [0x00_u8; 32];
        let fp2 = [0xFF_u8; 32];
        assert_eq!(
            verdict_from_fingerprint_format_independent(&fp1, &fp2),
            DiagVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 3: DIAG-003 — oracle Unknown for random.
    // -------------------------------------------------------------------------
    #[test]
    fn diag003_pass_unknown() {
        assert_eq!(
            verdict_from_oracle_unknown_for_random(FamilyDetection::Unknown),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag003_fail_known_qwen2() {
        assert_eq!(
            verdict_from_oracle_unknown_for_random(FamilyDetection::Known(0xC0DE)),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag003_fail_known_llama() {
        assert_eq!(
            verdict_from_oracle_unknown_for_random(FamilyDetection::Known(0xBEEF)),
            DiagVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 4: DIAG-004 — compat no false positives.
    // -------------------------------------------------------------------------
    #[test]
    fn diag004_pass_fits_in_vram() {
        // 4GB model, 24GB VRAM, reported compatible.
        assert_eq!(
            verdict_from_compat_no_false_positive(4096, 24576, true),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag004_pass_exceeds_vram_correctly_incompatible() {
        // 30GB model, 24GB VRAM, correctly reported incompatible.
        assert_eq!(
            verdict_from_compat_no_false_positive(30000, 24576, false),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag004_fail_false_positive() {
        // The exact regression: model exceeds VRAM, reported compatible.
        assert_eq!(
            verdict_from_compat_no_false_positive(30000, 24576, true),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag004_fail_false_negative() {
        // Model fits but reported incompatible.
        assert_eq!(
            verdict_from_compat_no_false_positive(4096, 24576, false),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag004_pass_at_exact_vram() {
        assert_eq!(
            verdict_from_compat_no_false_positive(24576, 24576, true),
            DiagVerdict::Pass
        );
    }

    // -------------------------------------------------------------------------
    // Section 5: DIAG-005 — NaN fault remediation.
    // -------------------------------------------------------------------------
    #[test]
    fn diag005_pass_layer_and_remediation_present() {
        assert_eq!(
            verdict_from_nan_fault_remediation(
                Some(5),
                "Detected NaN at layer 5 attention. Remediation: re-run with --strict to reject; verify upstream training stability."
            ),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag005_pass_layer_zero_short_remediation() {
        assert_eq!(
            verdict_from_nan_fault_remediation(Some(0), "Re-quantize."),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn diag005_fail_layer_not_located() {
        assert_eq!(
            verdict_from_nan_fault_remediation(None, "Re-quantize."),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag005_fail_empty_remediation() {
        assert_eq!(
            verdict_from_nan_fault_remediation(Some(5), ""),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag005_fail_whitespace_only_remediation() {
        assert_eq!(
            verdict_from_nan_fault_remediation(Some(5), "   \n\t  "),
            DiagVerdict::Fail
        );
    }

    #[test]
    fn diag005_fail_both_missing() {
        assert_eq!(
            verdict_from_nan_fault_remediation(None, ""),
            DiagVerdict::Fail
        );
    }

    // -------------------------------------------------------------------------
    // Section 6: Realistic — full diagnostics pipeline.
    // -------------------------------------------------------------------------
    #[test]
    fn realistic_diag_post_fix_full_pipeline() {
        // Synthesize a diagnostics run on Qwen2.5-Coder-7B-Q4_K_M.
        let raw_header: Vec<u8> = vec![
            b'G', b'G', b'U', b'F', // magic
            0x03, 0x00, 0x00, 0x00, // version 3
        ];
        let displayed = &raw_header[0..4];
        // DIAG-001:
        assert_eq!(
            verdict_from_hex_byte_offset(&raw_header, 0, displayed),
            DiagVerdict::Pass
        );

        // DIAG-002 (fingerprint stability):
        let fp = [0xCA_u8; 32];
        assert_eq!(
            verdict_from_fingerprint_format_independent(&fp, &fp),
            DiagVerdict::Pass
        );

        // DIAG-003 (Qwen2.5 known, but on randomized model returns
        // Unknown):
        assert_eq!(
            verdict_from_oracle_unknown_for_random(FamilyDetection::Unknown),
            DiagVerdict::Pass
        );

        // DIAG-004 (4GB Q4 fits 24GB VRAM):
        assert_eq!(
            verdict_from_compat_no_false_positive(4096, 24576, true),
            DiagVerdict::Pass
        );

        // DIAG-005 (no NaN ⇒ no fault to report; this gate only
        // applies when NaN exists, so we test the in-fault path):
        assert_eq!(
            verdict_from_nan_fault_remediation(
                Some(5),
                "Re-train with gradient clipping enabled."
            ),
            DiagVerdict::Pass
        );
    }

    #[test]
    fn realistic_diag_pre_fix_all_5_failures() {
        // Pre-fix: hex bytes corrupted, fingerprints differ, oracle
        // misidentifies, compat says yes when no, no remediation.
        let raw = vec![1_u8, 2, 3, 4];
        let bad_displayed = vec![99_u8, 99, 99];

        assert_eq!(
            verdict_from_hex_byte_offset(&raw, 0, &bad_displayed),
            DiagVerdict::Fail
        );
        assert_eq!(
            verdict_from_fingerprint_format_independent(
                &[0x00_u8; 32],
                &[0xFF_u8; 32]
            ),
            DiagVerdict::Fail
        );
        assert_eq!(
            verdict_from_oracle_unknown_for_random(FamilyDetection::Known(0xC0DE)),
            DiagVerdict::Fail
        );
        assert_eq!(
            verdict_from_compat_no_false_positive(30000, 24576, true),
            DiagVerdict::Fail
        );
        assert_eq!(
            verdict_from_nan_fault_remediation(None, ""),
            DiagVerdict::Fail
        );
    }
}