fix: retry mismatched translation batches in halves (#276)
This commit was merged in pull request #276.
This commit is contained in:
@@ -35,6 +35,13 @@ use crate::srt::Cue;
|
||||
/// [`Backend::batch_budget`].
|
||||
pub const BATCH_CHAR_BUDGET: usize = 4_096;
|
||||
|
||||
/// How many times a batch whose reply came back mismatched is halved and
|
||||
/// sent again before the translation fails. Four halvings take a full batch
|
||||
/// down to a sixteenth, small enough that a local model keeps count, while
|
||||
/// a backend that mangles everything costs at most 31 requests per batch
|
||||
/// rather than one per cue.
|
||||
const MAX_SPLITS: u32 = 4;
|
||||
|
||||
/// The name a translation backend answers to in settings and the UI.
|
||||
///
|
||||
/// A string rather than an enum for the same reason as
|
||||
@@ -322,9 +329,11 @@ impl Error {
|
||||
///
|
||||
/// Chunks the cues into batches within [`Backend::batch_budget`], sends them
|
||||
/// in order, validates every reply against its batch, and reassembles the
|
||||
/// translated text onto the original timings. All or nothing: any failed
|
||||
/// batch fails the whole call, so a partially translated subtitle never
|
||||
/// exists (DESIGN.md §15).
|
||||
/// translated text onto the original timings. A batch whose reply does not
|
||||
/// match is never accepted; it is split in half and each half sent again, up
|
||||
/// to [`MAX_SPLITS`] times, since smaller models lose count on long batches.
|
||||
/// All or nothing: any batch still failing fails the whole call, so a
|
||||
/// partially translated subtitle never exists (DESIGN.md §15).
|
||||
///
|
||||
/// # Errors
|
||||
///
|
||||
@@ -346,8 +355,7 @@ pub async fn translate(
|
||||
|
||||
let mut texts = Vec::with_capacity(cues.len());
|
||||
for batch in batches(cues, source, target, backend.batch_budget()) {
|
||||
let reply = backend.translate(&batch).await?;
|
||||
validate(&batch, &reply, &backend.id())?;
|
||||
let reply = translate_splitting(backend, batch).await?;
|
||||
texts.extend(reply.into_iter().map(|cue| cue.text));
|
||||
}
|
||||
|
||||
@@ -362,6 +370,54 @@ pub async fn translate(
|
||||
.collect())
|
||||
}
|
||||
|
||||
/// Translate one batch, halving it on a mismatched reply. Halves are worked
|
||||
/// front to back so the replies come out in cue order.
|
||||
async fn translate_splitting(backend: &dyn Backend, batch: Batch) -> Result<Vec<TranslatedCue>> {
|
||||
let mut out = Vec::with_capacity(batch.cues.len());
|
||||
let mut pending = std::collections::VecDeque::from([(batch, 0)]);
|
||||
while let Some((batch, splits)) = pending.pop_front() {
|
||||
let reply = backend.translate(&batch).await?;
|
||||
match validate(&batch, &reply, &backend.id()) {
|
||||
Ok(()) => out.extend(reply),
|
||||
Err(error @ Error::CueMismatch { .. })
|
||||
if batch.cues.len() > 1 && splits < MAX_SPLITS =>
|
||||
{
|
||||
tracing::info!(
|
||||
%error,
|
||||
cues = batch.cues.len(),
|
||||
"translation batch mismatched, retrying in halves"
|
||||
);
|
||||
let (first, second) = halves(batch);
|
||||
pending.push_front((second, splits + 1));
|
||||
pending.push_front((first, splits + 1));
|
||||
}
|
||||
Err(error) => return Err(error),
|
||||
}
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
fn halves(batch: Batch) -> (Batch, Batch) {
|
||||
let Batch {
|
||||
source,
|
||||
target,
|
||||
mut cues,
|
||||
} = batch;
|
||||
let second = cues.split_off(cues.len() / 2);
|
||||
(
|
||||
Batch {
|
||||
source: source.clone(),
|
||||
target: target.clone(),
|
||||
cues,
|
||||
},
|
||||
Batch {
|
||||
source,
|
||||
target,
|
||||
cues: second,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Chunk cues into batches of at most `budget` characters of text.
|
||||
///
|
||||
/// Order preserved, numbering 1-based and continuous across batches. A single
|
||||
@@ -450,6 +506,8 @@ mod tests {
|
||||
calls: AtomicUsize,
|
||||
drop_last_cue: bool,
|
||||
renumber_from: Option<usize>,
|
||||
/// Answers with one cue too many for any batch larger than this.
|
||||
pad_above: Option<usize>,
|
||||
}
|
||||
|
||||
impl Backend for StubBackend {
|
||||
@@ -479,6 +537,12 @@ mod tests {
|
||||
if self.drop_last_cue {
|
||||
reply.pop();
|
||||
}
|
||||
if self.pad_above.is_some_and(|limit| batch.cues.len() > limit) {
|
||||
reply.push(TranslatedCue {
|
||||
number: 0,
|
||||
text: "EXTRA".to_owned(),
|
||||
});
|
||||
}
|
||||
Ok(reply)
|
||||
})
|
||||
}
|
||||
@@ -580,6 +644,56 @@ mod tests {
|
||||
assert!(matches!(error, Error::CueMismatch { .. }));
|
||||
}
|
||||
|
||||
/// A long batch the backend loses count on is halved until the halves
|
||||
/// come back whole, and the cues still land in order.
|
||||
#[tokio::test]
|
||||
async fn a_mismatched_batch_is_retried_in_halves() {
|
||||
let backend = StubBackend {
|
||||
pad_above: Some(2),
|
||||
..StubBackend::default()
|
||||
};
|
||||
|
||||
let translated = translate(
|
||||
&backend,
|
||||
&cues(&["a", "b", "c", "d", "e", "f", "g", "h"]),
|
||||
&Language::Other("en".to_owned()),
|
||||
&Language::PortuguesePortugal,
|
||||
)
|
||||
.await
|
||||
.expect("the halves answer");
|
||||
|
||||
// 8 fails, two 4s fail, four 2s answer.
|
||||
assert_eq!(backend.calls.load(Ordering::SeqCst), 7);
|
||||
let texts: Vec<&str> = translated.iter().map(|cue| cue.text.as_str()).collect();
|
||||
assert_eq!(texts, ["A", "B", "C", "D", "E", "F", "G", "H"]);
|
||||
}
|
||||
|
||||
/// Halving stops after `MAX_SPLITS`, so a backend that mangles every
|
||||
/// reply fails the translation instead of falling to one cue a request.
|
||||
#[tokio::test]
|
||||
async fn halving_gives_up_after_the_split_limit() {
|
||||
let backend = StubBackend {
|
||||
pad_above: Some(0),
|
||||
..StubBackend::default()
|
||||
};
|
||||
let texts: Vec<String> = (0..64).map(|n| n.to_string()).collect();
|
||||
let refs: Vec<&str> = texts.iter().map(String::as_str).collect();
|
||||
|
||||
let error = translate(
|
||||
&backend,
|
||||
&cues(&refs),
|
||||
&Language::Other("en".to_owned()),
|
||||
&Language::PortuguesePortugal,
|
||||
)
|
||||
.await
|
||||
.expect_err("every reply is padded");
|
||||
|
||||
assert!(matches!(error, Error::CueMismatch { .. }));
|
||||
// One request each at 64, 32, 16, 8 and 4 cues, then the first
|
||||
// 4-cue half fails for good.
|
||||
assert_eq!(backend.calls.load(Ordering::SeqCst), 5);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn renumbered_cues_fail_the_whole_translation() {
|
||||
let backend = StubBackend {
|
||||
|
||||
Reference in New Issue
Block a user