reference method

Future<Map<String, Object?>> reference(
  1. String name,
  2. List<EvalRow> rows
)

Reference likelihoods without and with the vector.

Implementation

Future<Map<String, Object?>> reference(
  String name,
  List<EvalRow> rows,
) async {
  final without = <double>[], withVector = <double>[], diff = <double>[];
  var tokens = 0;
  for (final (i, row) in rows.indexed) {
    final answer = row.reference;
    if (answer == null) {
      throw FormatException('$name row ${i + 1} has no reference');
    }
    final ids = tokenizer.encode(answer);
    if (ids.isEmpty) continue;
    final capped = ids.take(referenceTokens).toList();
    tokens += capped.length;
    final a = await referenceNll(row.prompt, capped, null);
    final b = await referenceNll(row.prompt, capped, vector);
    without.add(a);
    withVector.add(b);
    diff.add(b - a);
    _progress(name, i, rows.length);
  }
  double mean(List<double> v) => v.reduce((x, y) => x + y) / v.length;
  final (d, low, high) = bootstrapMean(diff);
  return {
    'kind': 'reference',
    'rows': without.length,
    'reference_tokens_cap': referenceTokens,
    'scored_tokens': tokens,
    'nll_without': mean(without),
    'nll_with': mean(withVector),
    'ppl_without': math.exp(mean(without)),
    'ppl_with': math.exp(mean(withVector)),
    'nll_diff': d,
    'nll_diff_ci95': [low, high],
    'rows_worse': diff.where((x) => x > 0).length,
  };
}