Skip to content

Commit 4b3a3d7

Browse files
authored
pipelines: defer eager model imports in text_to_audio and token_classification (#50001)
pipelines: defer eager model imports to avoid spurious test dependencies `token_classification.py` imported `BasicTokenizer` at module level, and `text_to_audio.py` imported `SpeechT5HifiGan` inside the `if is_torch_available()` block — both at import time, unconditionally. This caused every test file that loads any pipeline (via PipelineTesterMixin → test_pipeline_mixin.py → pipeline source files) to record a transitive dependency on `modeling_bert_legacy` and `modeling_speecht5`, even when those models are completely unrelated to the test being run. Move both imports to the call site where they are first needed (`__init__` for `BasicTokenizer`, the spectrogram-model branch of `__init__` for `SpeechT5HifiGan`), so they are only executed when the relevant pipeline class is actually instantiated. Co-authored-by: ydshieh <ydshieh@users.noreply.github.com>
1 parent 3fe9133 commit 4b3a3d7

2 files changed

Lines changed: 7 additions & 7 deletions

File tree

‎src/transformers/pipelines/text_to_audio.py‎

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727
import torch
2828

2929
from ..models.auto.modeling_auto import MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING
30-
from ..models.speecht5.modeling_speecht5 import SpeechT5HifiGan
3130

3231
DEFAULT_VOCODER_ID = "microsoft/speecht5_hifigan"
3332

@@ -122,11 +121,11 @@ def __init__(self, *args, vocoder=None, sampling_rate=None, noise_scheduler=None
122121

123122
self.vocoder = None
124123
if self.model.__class__ in MODEL_FOR_TEXT_TO_SPECTROGRAM_MAPPING.values():
125-
self.vocoder = (
126-
SpeechT5HifiGan.from_pretrained(DEFAULT_VOCODER_ID).to(self.model.device)
127-
if vocoder is None
128-
else vocoder
129-
)
124+
if vocoder is None:
125+
from ..models.speecht5.modeling_speecht5 import SpeechT5HifiGan
126+
127+
vocoder = SpeechT5HifiGan.from_pretrained(DEFAULT_VOCODER_ID).to(self.model.device)
128+
self.vocoder = vocoder
130129

131130
if self.model.config.model_type in ["musicgen", "speecht5"]:
132131
# MusicGen and SpeechT5 expect to use their tokenizer instead

‎src/transformers/pipelines/token_classification.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
import numpy as np
66

7-
from ..models.bert.tokenization_bert_legacy import BasicTokenizer
87
from ..utils import (
98
ExplicitEnum,
109
add_end_docstrings,
@@ -138,6 +137,8 @@ def __init__(self, args_parser=TokenClassificationArgumentHandler(), **kwargs):
138137

139138
self.check_model_type(MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING_NAMES)
140139

140+
from ..models.bert.tokenization_bert_legacy import BasicTokenizer
141+
141142
self._basic_tokenizer = BasicTokenizer(do_lower_case=False)
142143
self._args_parser = args_parser
143144

0 commit comments

Comments
 (0)