Disable transformer when super stressed

This commit is contained in:
Aleksandras Kostarevas 2024-04-29 20:58:37 -04:00
parent ecaf9df340
commit 21800d37e2
2 changed files with 17 additions and 0 deletions

View File

@ -381,6 +381,7 @@ class LatinIME : InputMethodService(), LifecycleOwner, ViewModelStoreOwner, Save
override fun onStartInput(attribute: EditorInfo?, restarting: Boolean) {
super.onStartInput(attribute, restarting)
latinIMELegacy.onStartInput(attribute, restarting)
languageModelFacilitator.onStartInput()
}
override fun onStartInputView(info: EditorInfo?, restarting: Boolean) {

View File

@ -133,6 +133,9 @@ public class LanguageModelFacilitator(
public fun hasPendingUpdate(): Boolean =
computationSemaphore.availablePermits == 0
private var numConsecutiveTimeouts = 0
private var transformerDisabled = false
public fun blockUntilComplete() {
runBlocking {
try {
@ -145,8 +148,14 @@ public class LanguageModelFacilitator(
}
}
numConsecutiveTimeouts = 0
} catch(e: TimeoutCancellationException) {
Log.d("LanguageModelFacilitator", "Failed to complete prediction within 1000ms!")
numConsecutiveTimeouts += 1
if(numConsecutiveTimeouts > 5) {
transformerDisabled = true
Log.w("LanguageModelFacilitator", "Temporarily disabling transformer due to continuous timeouts")
}
}
}
}
@ -171,6 +180,8 @@ public class LanguageModelFacilitator(
}
private suspend fun runLanguageModel(values: PredictionInputValues): ArrayList<SuggestedWordInfo>? {
if(transformerDisabled) return null
val locale = dictionaryFacilitator.locale ?: return null
if (languageModel == null || (languageModel?.locale?.language != locale.language)) {
Log.d(
@ -602,4 +613,9 @@ public class LanguageModelFacilitator(
assert(historyLog.isEmpty())
loadHistoryLogBackup(context, historyLog)
}
public fun onStartInput() {
transformerDisabled = false
numConsecutiveTimeouts = 0
}
}