Skip to content

fix: model.transcribe() is not thread-safe — freeze/unfreeze race on _frozen_grad_map #1

Closed

Description

@beastoin

Current State

model.transcribe() is not thread-safe. Concurrent calls from a multi-threaded serving framework (e.g. FastAPI + uvicorn) cause race conditions that result in:

  • ValueError: Cannot unfreeze partially without first freezing the module
  • CUDA stream capture errors
  • Corrupted _frozen_grad_map state
  • Intermittent incorrect transcription results

Production impact: 33 concurrent requests → 2,103 HTTP 500 errors in our Parakeet TDT service.

Current workaround: threading.Semaphore(1) serializing all inference — kills throughput.

Root Cause

ASRTranscriptionMixin._transcribe_on_begin() calls encoder.freeze(), decoder.freeze(), joint.freeze() which mutate shared _frozen_grad_map dicts on each module. _transcribe_on_end() calls .unfreeze(partial=True) which reads and deletes those dicts. Under concurrent access, thread A's unfreeze races with thread B's freeze.

The freeze/unfreeze is redundant:

  1. transcribe() is already decorated with @torch.inference_mode() — disables autograd, making requires_grad=False unnecessary
  2. _transcribe_on_begin() already calls self.eval() — handles BatchNorm/Dropout correctly
  3. RNNT's transcribe() adds @torch.no_grad() on top of that

The freeze/unfreeze predates the @inference_mode() decorator and was never updated.

Additional issue: transcribe_generator() is NOT decorated with @inference_mode(), unlike transcribe().

Affected Code (4 locations)

File What's frozen
nemo/collections/asr/parts/mixins/transcription.py (ASRTranscriptionMixin) encoder, decoder, joint
nemo/collections/asr/models/hybrid_rnnt_ctc_models.py ctc_decoder
nemo/collections/asr/models/aed_multitask_models.py transf_decoder
nemo/collections/asr/models/transformer_bpe_models.py transf_decoder

Requirements

  1. Remove all freeze/unfreeze calls from the transcription path
  2. Add @torch.inference_mode() to transcribe_generator()
  3. Keep self.eval() / self.train(mode=original) (these are needed for BatchNorm/Dropout)
  4. Add concurrent transcription regression tests
  5. Existing tests must pass with no regressions

Remaining Lower-Priority Issues (not in scope)

These shared mutations still exist but don't crash — they cause minor non-determinism:

  • preprocessor.featurizer.dither — mutated to 0.0 then restored
  • preprocessor.featurizer.pad_to — mutated to 0 then restored
  • logging.set_verbosity() — global state change

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions