漏句最后一里: 重试预算2次温度递进0.3→0.7(跳贪心死路)、MISS日志带attempt、Start预热翻译引擎消除冷加载期开头失败

This commit is contained in:
liwenhao2 2026-09-15 11:07:18 +08:00
parent c25f40927e
commit 8abad1eb78
1 changed files with 31 additions and 10 deletions

View File

@ -314,7 +314,15 @@ class MainActivity : AppCompatActivity() {
private val initialPending = ArrayList<Pair<Long, String>>() private val initialPending = ArrayList<Pair<Long, String>>()
private var initialBatchTimer: Runnable? = null private var initialBatchTimer: Runnable? = null
private val translatedSids = HashSet<Long>() // 每槽只翻一次 private val translatedSids = HashSet<Long>() // 每槽只翻一次
private val retriedSids = HashSet<Long>() // 失败仅重试一次 // 失败重试预算:每句最多 2 次,温度递进(0→0.3→0.7),贪心死路可被跳出。
// 仅在翻译线程读写,无并发。
private val translateAttempts = HashMap<Long, Int>()
private fun bumpAttempt(sid: Long): Int {
val n = (translateAttempts[sid] ?: 0) + 1
translateAttempts[sid] = n
return n
}
companion object { companion object {
private val SUMMARY_THREADS = 6 // 空闲时:大核全用 private val SUMMARY_THREADS = 6 // 空闲时:大核全用
@ -519,6 +527,15 @@ class MainActivity : AppCompatActivity() {
sessionTranscript?.close() sessionTranscript?.close()
sessionTranscript = SessionTranscript.begin(filesDir) sessionTranscript = SessionTranscript.begin(filesDir)
} }
// 预热翻译引擎:把模型文件读进 OS 页缓存后即释放(每批仍按防污染
// 协议重建),消除开头几批的冷加载失败
translateHandler.post {
val e = ensureTranslateEngine()
if (e != null) {
translateEngine = null
runCatching { e.release() }
}
}
recordingThread = thread(true) { recordingThread = thread(true) {
processSamples() processSamples()
} }
@ -2026,18 +2043,21 @@ class MainActivity : AppCompatActivity() {
} }
// 翻译线程执行:直出/编号批 → 逐条质检(空/复读/语言漂移)→ 主线程应用。 // 翻译线程执行:直出/编号批 → 逐条质检(空/复读/语言漂移)→ 主线程应用。
// 生成失败或语言漂移 → 带温度重试一次(每句仅一次,retriedSids 全部在本线程读写)。 // 生成失败或语言漂移 → 温度递进重试(每句最多 2 次,translateAttempts 仅在本线程读写)。
private fun runTranslateBatch(batch: List<Pair<Long, String>>, temp: Float) { private fun runTranslateBatch(batch: List<Pair<Long, String>>, temp: Float) {
val map = translateNumbered( val map = translateNumbered(
batch.map { Pair(it.first.toString(), it.second) }, temp batch.map { Pair(it.first.toString(), it.second) }, temp
) )
if (map == null || !plausibleTranslations(map.values)) { if (map == null || !plausibleTranslations(map.values)) {
Log.i(TAG, "MISS reason=genfail sids=" + val retry = ArrayList<Pair<Long, String>>()
batch.joinToString(",") { it.first.toString() }) for ((sid, src) in batch) {
if (temp == 0f) { val att = bumpAttempt(sid)
val retry = batch.filter { retriedSids.add(it.first) } Log.i(TAG, "MISS sid=" + sid + " reason=genfail attempt=" + att)
if (retry.isNotEmpty()) translateHandler.post { if (att <= 2) retry.add(Pair(sid, src))
runTranslateBatch(retry, 0.3f) }
if (retry.isNotEmpty()) {
translateHandler.post {
runTranslateBatch(retry, if (temp < 0.5f) 0.3f else 0.7f)
} }
} }
return return
@ -2052,8 +2072,9 @@ class MainActivity : AppCompatActivity() {
isEcho(src, tr, echoJaccardTh(src)) -> isEcho(src, tr, echoJaccardTh(src)) ->
Log.i(TAG, "MISS sid=" + sid + " reason=echo") Log.i(TAG, "MISS sid=" + sid + " reason=echo")
!targetLangOk(tr) -> { !targetLangOk(tr) -> {
Log.i(TAG, "MISS sid=" + sid + " reason=langdrift") val att = bumpAttempt(sid)
if (temp == 0f && retriedSids.add(sid)) drift.add(Pair(sid, src)) Log.i(TAG, "MISS sid=" + sid + " reason=langdrift attempt=" + att)
if (att <= 2) drift.add(Pair(sid, src))
} }
else -> good[sid] = tr else -> good[sid] = tr
} }