Skip to content

Cache ParakeetModule across transcription calls to avoid repea…#223

Closed
psiddh wants to merge 0 commit into
meta-pytorch:mainfrom
psiddh:main
Closed

Cache ParakeetModule across transcription calls to avoid repea…#223
psiddh wants to merge 0 commit into
meta-pytorch:mainfrom
psiddh:main

Conversation

@psiddh

@psiddh psiddh commented Mar 12, 2026

Copy link
Copy Markdown
Contributor

…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

Copilot AI left a comment

Copy link
Copy Markdown

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!!

Copilot AI Mar 12, 2026

Copy link

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.
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