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
//! Evaluation Harness for Standard Benchmarks (GH-454)
//!
//! Implements a lightweight evaluation framework compatible with the
//! lm-evaluation-harness approach. Supports:
//! - Multiple-choice tasks (HellaSwag, MMLU, ARC)
//! - Perplexity-based evaluation
//! - Log-likelihood scoring
//! - Metric aggregation and reporting
//!
//! # Design
//!
//! This module provides the scoring primitives and harness structure.
//! Actual benchmark datasets are loaded from JSONL files at runtime.

use crate::error::{AprenderError, Result};

// ============================================================================
// Task types
// ============================================================================

/// Evaluation task type.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TaskType {
    /// Multiple-choice: pick the best completion
    MultipleChoice,
    /// Perplexity: compute PPL on text
    Perplexity,
    /// Generation: compare generated text to reference
    Generation,
    /// Classification: accuracy on labeled examples
    Classification,
}

/// A single evaluation example.
#[derive(Debug, Clone)]
pub struct EvalExample {
    /// Context/prompt text
    pub context: String,
    /// Candidate completions (for multiple-choice)
    pub choices: Vec<String>,
    /// Correct answer index (for multiple-choice/classification)
    pub gold_idx: Option<usize>,
    /// Reference text (for generation/perplexity)
    pub reference: Option<String>,
}

/// An evaluation task (benchmark).
#[derive(Debug, Clone)]
pub struct EvalTask {
    /// Task name (e.g., "hellaswag", "mmlu_abstract_algebra")
    pub name: String,
    /// Task type
    pub task_type: TaskType,
    /// Examples in this task
    pub examples: Vec<EvalExample>,
    /// Number of few-shot examples to prepend
    pub num_fewshot: usize,
}

impl EvalTask {
    /// Create a new evaluation task.
    #[must_use]
    pub fn new(name: impl Into<String>, task_type: TaskType) -> Self {
        Self {
            name: name.into(),
            task_type,
            examples: Vec::new(),
            num_fewshot: 0,
        }
    }

    /// Add an example to the task.
    pub fn add_example(&mut self, example: EvalExample) {
        self.examples.push(example);
    }

    /// Set number of few-shot examples.
    #[must_use]
    pub fn with_fewshot(mut self, n: usize) -> Self {
        self.num_fewshot = n;
        self
    }

    /// Get number of examples.
    #[must_use]
    pub fn len(&self) -> usize {
        self.examples.len()
    }

    /// Check if task has no examples.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.examples.is_empty()
    }
}

// ============================================================================
// Scoring
// ============================================================================

/// Log-likelihood scores for candidate completions.
#[derive(Debug, Clone)]
pub struct LogLikelihoodScores {
    /// Per-choice log-likelihoods
    pub scores: Vec<f64>,
    /// Length-normalized scores (divide by token count)
    pub normalized_scores: Vec<f64>,
}

/// Score multiple-choice by selecting the highest log-likelihood completion.
///
/// Returns the index of the best completion and its score.
#[must_use]
pub fn score_multiple_choice(scores: &[f64], normalize: bool) -> (usize, f64) {
    if scores.is_empty() {
        return (0, f64::NEG_INFINITY);
    }

    scores
        .iter()
        .enumerate()
        .max_by(|(_, a), (_, b)| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal))
        .map(|(i, &s)| {
            let _ = normalize; // Used in normalized_scores path
            (i, s)
        })
        .unwrap_or((0, f64::NEG_INFINITY))
}

/// Compute perplexity from log-likelihood.
///
/// PPL = exp(-1/N * Σ log P(x_i))
#[must_use]
pub fn compute_perplexity(log_likelihood: f64, num_tokens: usize) -> f64 {
    if num_tokens == 0 {
        return f64::INFINITY;
    }
    (-log_likelihood / num_tokens as f64).exp()
}

/// Compute accuracy from predictions vs gold labels.
#[must_use]
pub fn compute_accuracy(predictions: &[usize], gold: &[usize]) -> f64 {
    if predictions.is_empty() || predictions.len() != gold.len() {
        return 0.0;
    }
    let correct = predictions
        .iter()
        .zip(gold.iter())
        .filter(|(&p, &g)| p == g)
        .count();
    correct as f64 / predictions.len() as f64
}

// ============================================================================
// Metrics
// ============================================================================

/// Evaluation metrics for a single task.
#[derive(Debug, Clone)]
pub struct TaskMetrics {
    /// Task name
    pub task_name: String,
    /// Task type
    pub task_type: TaskType,
    /// Number of examples evaluated
    pub num_examples: usize,
    /// Accuracy (for multiple-choice/classification)
    pub accuracy: Option<f64>,
    /// Perplexity (for perplexity tasks)
    pub perplexity: Option<f64>,
    /// Average log-likelihood
    pub avg_log_likelihood: Option<f64>,
    /// Per-example predictions (for multiple-choice)
    pub predictions: Vec<usize>,
}

/// Aggregate metrics across multiple tasks.
#[derive(Debug, Clone)]
pub struct EvalReport {
    /// Per-task metrics
    pub tasks: Vec<TaskMetrics>,
    /// Overall accuracy (macro average)
    pub macro_accuracy: f64,
    /// Number of tasks evaluated
    pub num_tasks: usize,
    /// Total examples evaluated
    pub total_examples: usize,
}

impl EvalReport {
    /// Compute aggregate report from task metrics.
    #[must_use]
    pub fn from_tasks(tasks: Vec<TaskMetrics>) -> Self {
        let num_tasks = tasks.len();
        let total_examples: usize = tasks.iter().map(|t| t.num_examples).sum();

        let accs: Vec<f64> = tasks.iter().filter_map(|t| t.accuracy).collect();
        let macro_accuracy = if accs.is_empty() {
            0.0
        } else {
            accs.iter().sum::<f64>() / accs.len() as f64
        };

        Self {
            tasks,
            macro_accuracy,
            num_tasks,
            total_examples,
        }
    }
}

// ============================================================================
// Harness
// ============================================================================

/// Evaluation harness configuration.
#[derive(Debug, Clone)]
pub struct HarnessConfig {
    /// Tasks to evaluate
    pub tasks: Vec<EvalTask>,
    /// Whether to use length normalization for scoring
    pub length_normalize: bool,
    /// Maximum number of examples per task (0 = all)
    pub max_examples: usize,
}

impl Default for HarnessConfig {
    fn default() -> Self {
        Self {
            tasks: Vec::new(),
            length_normalize: false,
            max_examples: 0,
        }
    }
}

/// Run evaluation harness with a scoring function.
///
/// The `score_fn` takes (context, completion) and returns the log-likelihood
/// of the completion given the context.
pub fn run_harness<F>(config: &HarnessConfig, score_fn: F) -> Result<EvalReport>
where
    F: Fn(&str, &str) -> f64,
{
    if config.tasks.is_empty() {
        return Err(AprenderError::FormatError {
            message: "No tasks to evaluate".to_string(),
        });
    }

    let mut all_metrics = Vec::new();

    for task in &config.tasks {
        let metrics = evaluate_task(task, &score_fn, config)?;
        all_metrics.push(metrics);
    }

    Ok(EvalReport::from_tasks(all_metrics))
}

/// Evaluate a single task.
fn evaluate_task<F>(task: &EvalTask, score_fn: &F, config: &HarnessConfig) -> Result<TaskMetrics>
where
    F: Fn(&str, &str) -> f64,
{
    let examples = if config.max_examples > 0 && config.max_examples < task.examples.len() {
        &task.examples[..config.max_examples]
    } else {
        &task.examples
    };

    match task.task_type {
        TaskType::MultipleChoice | TaskType::Classification => {
            evaluate_multiple_choice(task, examples, score_fn, config.length_normalize)
        }
        TaskType::Perplexity => evaluate_perplexity(task, examples, score_fn),
        TaskType::Generation => {
            // Generation evaluation requires comparing outputs — simplified here
            Ok(TaskMetrics {
                task_name: task.name.clone(),
                task_type: task.task_type,
                num_examples: examples.len(),
                accuracy: None,
                perplexity: None,
                avg_log_likelihood: None,
                predictions: Vec::new(),
            })
        }
    }
}

/// Evaluate multiple-choice task.
fn evaluate_multiple_choice<F>(
    task: &EvalTask,
    examples: &[EvalExample],
    score_fn: &F,
    _length_normalize: bool,
) -> Result<TaskMetrics>
where
    F: Fn(&str, &str) -> f64,
{
    let mut predictions = Vec::with_capacity(examples.len());
    let mut gold_labels = Vec::with_capacity(examples.len());

    for example in examples {
        let scores: Vec<f64> = example
            .choices
            .iter()
            .map(|choice| score_fn(&example.context, choice))
            .collect();

        let (pred_idx, _) = score_multiple_choice(&scores, false);
        predictions.push(pred_idx);

        if let Some(gold) = example.gold_idx {
            gold_labels.push(gold);
        }
    }

    let accuracy = if gold_labels.len() == predictions.len() {
        Some(compute_accuracy(&predictions, &gold_labels))
    } else {
        None
    };

    Ok(TaskMetrics {
        task_name: task.name.clone(),
        task_type: task.task_type,
        num_examples: examples.len(),
        accuracy,
        perplexity: None,
        avg_log_likelihood: None,
        predictions,
    })
}

/// Evaluate perplexity task.
fn evaluate_perplexity<F>(
    task: &EvalTask,
    examples: &[EvalExample],
    score_fn: &F,
) -> Result<TaskMetrics>
where
    F: Fn(&str, &str) -> f64,
{
    let mut total_ll = 0.0;
    let mut total_tokens = 0usize;

    for example in examples {
        let text = example.reference.as_deref().unwrap_or(&example.context);
        let ll = score_fn("", text);
        let tokens = text.split_whitespace().count().max(1);
        total_ll += ll;
        total_tokens += tokens;
    }

    let ppl = compute_perplexity(total_ll, total_tokens);
    let avg_ll = if examples.is_empty() {
        0.0
    } else {
        total_ll / examples.len() as f64
    };

    Ok(TaskMetrics {
        task_name: task.name.clone(),
        task_type: task.task_type,
        num_examples: examples.len(),
        accuracy: None,
        perplexity: Some(ppl),
        avg_log_likelihood: Some(avg_ll),
        predictions: Vec::new(),
    })
}

/// Create a mock HellaSwag-style task for testing.
#[must_use]
pub fn mock_hellaswag() -> EvalTask {
    let mut task = EvalTask::new("hellaswag", TaskType::MultipleChoice);

    task.add_example(EvalExample {
        context: "A person is making a sandwich. They".to_string(),
        choices: vec![
            " spread butter on the bread.".to_string(),
            " flew into space.".to_string(),
            " turned into a tree.".to_string(),
            " dissolved into nothing.".to_string(),
        ],
        gold_idx: Some(0),
        reference: None,
    });

    task.add_example(EvalExample {
        context: "The cat sat on the".to_string(),
        choices: vec![
            " ceiling fan.".to_string(),
            " mat and purred.".to_string(),
            " quantum vacuum.".to_string(),
            " surface of the sun.".to_string(),
        ],
        gold_idx: Some(1),
        reference: None,
    });

    task
}

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