Skip to content

Cache ParakeetModule across transcription calls to avoid repea…#223

Open
psiddh wants to merge 3 commits intometa-pytorch:mainfrom
psiddh:main
Open

Cache ParakeetModule across transcription calls to avoid repea…#223
psiddh wants to merge 3 commits intometa-pytorch:mainfrom
psiddh:main

Conversation

@psiddh
Copy link
Contributor

@psiddh psiddh commented Mar 12, 2026

…ted model loading

Summary: Currently, runParakeetOnWavFile() creates a new ParakeetModule and calls .close() on every transcription request (L235-251). This means the model is loaded from disk on every single inference call, and the reported latency includes model load time. This change caches the ParakeetModule at the class level and only recreates it when the user changes model settings.

…ted model loading

Summary: Currently, runParakeetOnWavFile() creates a new ParakeetModule and calls .close()
on every transcription request (L235-251). This means the model is loaded from disk on
every single inference call, and the reported latency includes model load time.
This change caches the ParakeetModule at the class level and only
recreates it when the user changes model settings.
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Mar 12, 2026
@psiddh psiddh changed the title Title: Cache ParakeetModule across transcription calls to avoid repea… Cache ParakeetModule across transcription calls to avoid repea… Mar 12, 2026
@psiddh psiddh requested a review from Copilot March 12, 2026 07:40
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR improves transcription latency in the Android Parakeet demo by caching a single ParakeetModule instance across transcription calls, instead of loading/closing the model for every request.

Changes:

  • Add a cached ParakeetModule plus bookkeeping for which model/tokenizer/data paths are currently loaded.
  • Introduce getOrCreateModule() to reuse the existing module unless model settings changed.
  • Close the cached module in onDestroy().

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +224 to +231
private fun getOrCreateModule(settings: ModelSettings): ParakeetModule {
val dataPath = settings.dataPath.ifBlank { null }
if (parakeetModule != null &&
loadedModelPath == settings.modelPath &&
loadedTokenizerPath == settings.tokenizerPath &&
loadedDataPath == dataPath
) {
return parakeetModule!!
Copy link

Copilot AI Mar 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getOrCreateModule() reads/writes parakeetModule and the loaded*Path fields without any synchronization. Since runParakeetOnWavFile() can be invoked from different threads (UI thread via runParakeet() and a background Thread via runParakeetFromFile()), this can race with module creation/close and/or concurrent transcribe() calls. Consider guarding module access with a lock/single-thread executor, and ensure only one transcription can run at a time across all entry points.

Copilot uses AI. Check for mistakes.
psiddh and others added 2 commits March 12, 2026 00:48
…akeetapp/MainActivity.kt

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
…akeetapp/MainActivity.kt

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants