referenceNll method
Negative log-likelihood per token of reference after prompt under
v: teacher-forced, the prompt in one pass and the reference tokens
before the last in another, scoring each captured final residual.
Implementation
Future<double> referenceNll(
String prompt,
List<int> reference,
ControlVector? v,
) async {
await _start(v);
await model.forward(_promptIds(prompt));
var total = -logProbability(model.logits, reference.first);
if (reference.length > 1) {
final h = model.config.hiddenSize;
final last = model.config.numLayers;
final rows = <Float32List>[];
await model.forward(
reference.sublist(0, reference.length - 1),
computeLogits: false,
capture: (slot, first, data, count) {
if (slot != last) return;
for (var t = 0; t < count; t++) {
rows.add(Float32List.fromList(data.sublist(t * h, (t + 1) * h)));
}
},
);
for (var t = 0; t < rows.length; t++) {
total -= logProbability(
await model.logitsOf(rows[t]),
reference[t + 1],
);
}
}
return total / reference.length;
}