aprender-core 0.29.3

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
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//! Python to Rust Single-Shot Compile Benchmark (10 Levels)
//!
//! Canonical benchmark for code translation model evaluation.
//! Measures success rate by turn and finds smallest model meeting thresholds.
//!
//! # Levels
//! 1. Hello World
//! 2. Variables & Arithmetic
//! 3. Functions & Ownership
//! 4. Collections & Iterators
//! 5. Control Flow & Borrowing
//! 6. Error Handling (Result)
//! 7. OOP → Traits
//! 8. Concurrency (async/rayon)
//! 9. FFI/Unsafe
//! 10. Metaprogramming (proc macros)

use super::{Difficulty, EvalResult, Example, ExampleResult, ModelComparison};
use serde::{Deserialize, Serialize};
use std::fmt::Write;
use std::time::Duration;

/// Python→Rust benchmark level (1-10)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Py2RsLevel {
    /// Level 1: Hello World
    Hello = 1,
    /// Level 2: Variables & Arithmetic
    Variables = 2,
    /// Level 3: Functions & Ownership
    Functions = 3,
    /// Level 4: Collections & Iterators
    Collections = 4,
    /// Level 5: Control Flow & Borrowing
    ControlFlow = 5,
    /// Level 6: Error Handling
    ErrorHandling = 6,
    /// Level 7: OOP → Traits
    OopTraits = 7,
    /// Level 8: Concurrency
    Concurrency = 8,
    /// Level 9: FFI/Unsafe
    FfiUnsafe = 9,
    /// Level 10: Metaprogramming
    Metaprogramming = 10,
}

impl Py2RsLevel {
    /// Get all levels in order
    #[must_use]
    pub const fn all() -> [Self; 10] {
        [
            Self::Hello,
            Self::Variables,
            Self::Functions,
            Self::Collections,
            Self::ControlFlow,
            Self::ErrorHandling,
            Self::OopTraits,
            Self::Concurrency,
            Self::FfiUnsafe,
            Self::Metaprogramming,
        ]
    }

    /// Get level number (1-10)
    #[must_use]
    pub const fn number(&self) -> u8 {
        *self as u8
    }

    /// Get level name
    #[must_use]
    pub const fn name(&self) -> &'static str {
        match self {
            Self::Hello => "Hello",
            Self::Variables => "Variables",
            Self::Functions => "Functions",
            Self::Collections => "Collections",
            Self::ControlFlow => "ControlFlow",
            Self::ErrorHandling => "ErrorHandling",
            Self::OopTraits => "OOP→Traits",
            Self::Concurrency => "Concurrency",
            Self::FfiUnsafe => "FFI/Unsafe",
            Self::Metaprogramming => "Metaprogramming",
        }
    }

    /// Get difficulty for this level
    #[must_use]
    pub const fn difficulty(&self) -> Difficulty {
        match self {
            Self::Hello | Self::Variables => Difficulty::Trivial,
            Self::Functions | Self::Collections => Difficulty::Easy,
            Self::ControlFlow | Self::ErrorHandling => Difficulty::Medium,
            Self::OopTraits | Self::Concurrency => Difficulty::Hard,
            Self::FfiUnsafe | Self::Metaprogramming => Difficulty::Expert,
        }
    }

    /// Get scoring weight (higher levels worth more)
    #[must_use]
    pub const fn weight(&self) -> f32 {
        match self {
            Self::Hello => 1.0,
            Self::Variables => 1.5,
            Self::Functions => 2.0,
            Self::Collections => 3.0,
            Self::ControlFlow => 4.0,
            Self::ErrorHandling => 5.0,
            Self::OopTraits => 7.0,
            Self::Concurrency => 10.0,
            Self::FfiUnsafe => 15.0,
            Self::Metaprogramming => 20.0,
        }
    }

    /// Get canonical Python example
    #[must_use]
    pub fn python_example(&self) -> &'static str {
        match self {
            Self::Hello => r#"print("hello world")"#,
            Self::Variables => {
                r#"x = 42
y = x * 2 + 1
print(f"Result: {y}")"#
            }
            Self::Functions => {
                r"def factorial(n):
    if n <= 1:
        return 1
    return n * factorial(n - 1)"
            }
            Self::Collections => {
                r#"squares = [x**2 for x in range(10) if x % 2 == 0]
counts = {word: len(word) for word in ["hello", "world"]}"#
            }
            Self::ControlFlow => {
                r"def binary_search(arr, target):
    left, right = 0, len(arr) - 1
    while left <= right:
        mid = (left + right) // 2
        if arr[mid] == target:
            return mid
        elif arr[mid] < target:
            left = mid + 1
        else:
            right = mid - 1
    return -1"
            }
            Self::ErrorHandling => {
                r#"def read_config(path):
    try:
        with open(path) as f:
            return json.load(f)
    except FileNotFoundError:
        return {}
    except json.JSONDecodeError as e:
        raise ValueError(f"Invalid JSON: {e}")"#
            }
            Self::OopTraits => {
                r"class Shape:
    def area(self): raise NotImplementedError

class Circle(Shape):
    def __init__(self, radius): self.radius = radius
    def area(self): return 3.14159 * self.radius ** 2"
            }
            Self::Concurrency => {
                r"import asyncio
async def fetch_all(urls):
    async with aiohttp.ClientSession() as session:
        return await asyncio.gather(*[fetch(session, u) for u in urls])"
            }
            Self::FfiUnsafe => {
                r#"import ctypes
lib = ctypes.CDLL("libcrypto.so")
lib.SHA256_Init.argtypes = [ctypes.POINTER(SHA256_CTX)]"#
            }
            Self::Metaprogramming => {
                r"@dataclass
class Point:
    x: float
    y: float
    def distance(self, other):
        return ((self.x - other.x)**2 + (self.y - other.y)**2)**0.5"
            }
        }
    }
}

/// Score for Python→Rust benchmark
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Py2RsScore {
    /// Model identifier
    pub model_id: String,
    /// Highest level passed (1-10)
    pub max_level: u8,
    /// Levels passed on turn 1 (single-shot)
    pub single_shot_levels: Vec<u8>,
    /// Average turns per level (lower = better)
    pub avg_turns_by_level: [f32; 10],
    /// Composite score (0-100)
    pub composite: f32,
    /// Per-level results
    pub level_results: Vec<LevelResult>,
}

impl Py2RsScore {
    /// Create a new score
    #[must_use]
    pub fn new(model_id: impl Into<String>) -> Self {
        Self {
            model_id: model_id.into(),
            max_level: 0,
            single_shot_levels: Vec::new(),
            avg_turns_by_level: [0.0; 10],
            composite: 0.0,
            level_results: Vec::new(),
        }
    }

    /// Add a level result
    pub fn add_level(&mut self, result: LevelResult) {
        if result.passed {
            if result.level > self.max_level {
                self.max_level = result.level;
            }
            if result.turn == 1 {
                self.single_shot_levels.push(result.level);
            }
        }
        self.avg_turns_by_level[(result.level - 1) as usize] = result.turn as f32;
        self.level_results.push(result);
    }

    /// Compute composite score
    pub fn finalize(&mut self) {
        self.composite = self.compute_composite_score();
    }

    /// Compute weighted composite score (0-100)
    fn compute_composite_score(&self) -> f32 {
        let weights = [1.0, 1.5, 2.0, 3.0, 4.0, 5.0, 7.0, 10.0, 15.0, 20.0];
        let max_possible: f32 = weights.iter().sum(); // 68.5

        let earned: f32 = self
            .single_shot_levels
            .iter()
            .map(|&l| weights[(l - 1) as usize])
            .sum();

        (earned / max_possible) * 100.0
    }

    /// Get display symbol for level result
    #[must_use]
    pub fn level_symbol(&self, level: u8) -> char {
        if let Some(result) = self.level_results.iter().find(|r| r.level == level) {
            if result.passed {
                if result.turn == 1 {
                    '' // Pass Turn 1
                } else {
                    '' // Pass Turn 2+
                }
            } else {
                '' // Failed
            }
        } else {
            ''
        }
    }

    /// Format results as visual string
    #[must_use]
    pub fn visual_summary(&self) -> String {
        (1..=10).map(|l| self.level_symbol(l)).collect()
    }
}

/// Result for a single level
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LevelResult {
    /// Level number (1-10)
    pub level: u8,
    /// Level name
    pub name: String,
    /// Whether level was passed
    pub passed: bool,
    /// Turn on which it was solved (1-based)
    pub turn: u32,
    /// Error message if failed
    pub error: Option<String>,
    /// Latency
    pub latency: Duration,
}

impl LevelResult {
    /// Create a passed level result
    #[must_use]
    pub fn passed(level: Py2RsLevel, turn: u32, latency: Duration) -> Self {
        Self {
            level: level.number(),
            name: level.name().to_string(),
            passed: true,
            turn,
            error: None,
            latency,
        }
    }

    /// Create a failed level result
    #[must_use]
    pub fn failed(
        level: Py2RsLevel,
        attempts: u32,
        error: impl Into<String>,
        latency: Duration,
    ) -> Self {
        Self {
            level: level.number(),
            name: level.name().to_string(),
            passed: false,
            turn: attempts,
            error: Some(error.into()),
            latency,
        }
    }
}

/// Generate canonical `Py2Rs` examples
#[must_use]
pub fn generate_canonical_examples() -> Vec<Example> {
    Py2RsLevel::all()
        .iter()
        .map(|level| {
            Example::new(
                format!("py2rs-L{}", level.number()),
                level.python_example(),
                format!("Compile to valid Rust (Level {})", level.number()),
            )
            .with_difficulty(level.difficulty())
            .with_tags(vec![
                "py2rs".to_string(),
                format!("level-{}", level.number()),
                level.name().to_string(),
            ])
        })
        .collect()
}

/// Run `Py2Rs` benchmark on a model (mock implementation)
#[must_use]
pub fn run_benchmark(model_id: &str, max_turns: u32) -> Py2RsScore {
    let mut score = Py2RsScore::new(model_id);

    // Mock results - in real implementation, would run actual model
    for level in Py2RsLevel::all() {
        let (passed, turn) = mock_model_result(level, model_id);

        let result = if passed {
            LevelResult::passed(
                level,
                turn,
                Duration::from_millis(100 * u64::from(level.number())),
            )
        } else {
            LevelResult::failed(
                level,
                max_turns,
                "Compile error",
                Duration::from_millis(100 * u64::from(level.number()) * u64::from(max_turns)),
            )
        };

        score.add_level(result);
    }

    score.finalize();
    score
}

/// Mock model results (for testing)
fn mock_model_result(level: Py2RsLevel, model_id: &str) -> (bool, u32) {
    // Simulate different model capabilities
    let model_capability = match model_id {
        m if m.contains("16b") || m.contains("large") => 9,
        m if m.contains("6b") || m.contains("medium") => 7,
        m if m.contains("2b") || m.contains("small") => 5,
        _ => 6,
    };

    let level_num = level.number();

    if level_num <= model_capability {
        // Pass with 1-2 turns depending on difficulty
        let turn = if level_num <= model_capability - 2 {
            1
        } else {
            2
        };
        (true, turn)
    } else {
        (false, 5)
    }
}

/// Compare multiple models on `Py2Rs` benchmark
#[must_use]
pub fn compare_models(model_ids: &[(&str, u64)], max_turns: u32) -> ModelComparison {
    let mut comparison = ModelComparison::new("py2rs-canonical");

    for &(model_id, size) in model_ids {
        let score = run_benchmark(model_id, max_turns);

        // Convert Py2RsScore to EvalResult
        let mut result = EvalResult::new(model_id, "py2rs-canonical", size);

        for level_result in &score.level_results {
            let level = Py2RsLevel::all()[(level_result.level - 1) as usize];
            let example_result = if level_result.passed {
                ExampleResult::solved(
                    format!("L{}", level_result.level),
                    level.difficulty(),
                    level_result.turn,
                    vec![100; level_result.turn as usize],
                    vec![Duration::from_millis(50); level_result.turn as usize],
                )
            } else {
                ExampleResult::failed(
                    format!("L{}", level_result.level),
                    level.difficulty(),
                    max_turns,
                    {
                        #[allow(clippy::unwrap_or_default)]
                        level_result.error.clone().unwrap_or(String::new())
                    },
                    vec![100; max_turns as usize],
                    vec![Duration::from_millis(50); max_turns as usize],
                )
            };
            result.add_example(example_result);
        }

        result.finalize(max_turns);
        comparison.add_result(result);
    }

    comparison.compute_pareto_frontier();
    comparison.generate_recommendations();
    comparison
}

/// Format comparison as table
#[must_use]
pub fn format_comparison_table(_comparison: &ModelComparison, scores: &[Py2RsScore]) -> String {
    let mut output = String::new();

    output.push_str("┌────────────────────────────────────────────────────────────────┐\n");
    output.push_str("│ Benchmark: py2rs-canonical (10 levels)                         │\n");
    output.push_str("├──────────────┬───────┬────────────────────────────────┬────────┤\n");
    output.push_str("│ Model        │ Score │ Levels (● = T1, ◐ = T2+, ○ = fail) │ Max  │\n");
    output.push_str("├──────────────┼───────┼────────────────────────────────┼────────┤\n");

    for score in scores {
        let visual = score.visual_summary();
        let _ = writeln!(
            output,
            "│ {:12} │ {:5.1} │ {:30} │ L{:<4}",
            &score.model_id[..score.model_id.len().min(12)],
            score.composite,
            visual,
            score.max_level
        );
    }

    output.push_str("├──────────────┴───────┴────────────────────────────────┴────────┤\n");
    output.push_str("│ Legend: ● Pass Turn 1 | ◐ Pass Turn 2+ | ○ Failed              │\n");
    output.push_str("└────────────────────────────────────────────────────────────────┘\n");

    output
}

#[cfg(test)]
#[path = "py2rs_tests.rs"]
mod tests;