Skip to content

Commit

Permalink
make style and fix import (remove torchaudio dependency import)
Browse files Browse the repository at this point in the history
  • Loading branch information
ylacombe committed Sep 29, 2023
1 parent 605ed14 commit b16b5a9
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 68 deletions.
27 changes: 4 additions & 23 deletions src/transformers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@
"models.audio_spectrogram_transformer": [
"AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
"ASTConfig",
"ASTFeatureExtractor",
],
"models.auto": [
"ALL_PRETRAINED_CONFIG_ARCHIVE_MAP",
Expand Down Expand Up @@ -514,6 +515,7 @@
"models.speech_to_text": [
"SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP",
"Speech2TextConfig",
"Speech2TextFeatureExtractor",
"Speech2TextProcessor",
],
"models.speech_to_text_2": [
Expand Down Expand Up @@ -887,20 +889,6 @@
else:
_import_structure["convert_slow_tokenizer"] = ["SLOW_TO_FAST_CONVERTERS", "convert_slow_tokenizer"]

# Speech-specific objects
try:
if not is_speech_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
from .utils import dummy_speech_objects

_import_structure["utils.dummy_speech_objects"] = [
name for name in dir(dummy_speech_objects) if not name.startswith("_")
]
else:
_import_structure["models.audio_spectrogram_transformer"].append("ASTFeatureExtractor")
_import_structure["models.speech_to_text"].append("Speech2TextFeatureExtractor")

# Tensorflow-text-specific objects
try:
if not is_tensorflow_text_available():
Expand Down Expand Up @@ -4275,6 +4263,7 @@
from .models.audio_spectrogram_transformer import (
AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
ASTConfig,
ASTFeatureExtractor,
)
from .models.auto import (
ALL_PRETRAINED_CONFIG_ARCHIVE_MAP,
Expand Down Expand Up @@ -4624,6 +4613,7 @@
from .models.speech_to_text import (
SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP,
Speech2TextConfig,
Speech2TextFeatureExtractor,
Speech2TextProcessor,
)
from .models.speech_to_text_2 import (
Expand Down Expand Up @@ -4964,15 +4954,6 @@
else:
from .convert_slow_tokenizer import SLOW_TO_FAST_CONVERTERS, convert_slow_tokenizer

try:
if not is_speech_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
from .utils.dummy_speech_objects import *
else:
from .models.audio_spectrogram_transformer import ASTFeatureExtractor
from .models.speech_to_text import Speech2TextFeatureExtractor

try:
if not is_tensorflow_text_available():
raise OptionalDependencyNotAvailable()
Expand Down
21 changes: 4 additions & 17 deletions src/transformers/models/audio_spectrogram_transformer/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_speech_available, is_torch_available
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


_import_structure = {
"configuration_audio_spectrogram_transformer": [
"AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP",
"ASTConfig",
]
],
"feature_extraction_audio_spectrogram_transformer": ["ASTFeatureExtractor"],
}

try:
Expand All @@ -36,19 +37,13 @@
"ASTPreTrainedModel",
]

try:
if not is_speech_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["feature_extraction_audio_spectrogram_transformer"] = ["ASTFeatureExtractor"]

if TYPE_CHECKING:
from .configuration_audio_spectrogram_transformer import (
AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
ASTConfig,
)
from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor

try:
if not is_torch_available():
Expand All @@ -63,14 +58,6 @@
ASTPreTrainedModel,
)

try:
if not is_speech_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
import sys
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
from ...feature_extraction_utils import BatchFeature
from ...utils import TensorType, is_speech_available, logging


if is_speech_available():
import torchaudio.compliance.kaldi as ta_kaldi

Expand Down
19 changes: 2 additions & 17 deletions src/transformers/models/speech_to_text/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
OptionalDependencyNotAvailable,
_LazyModule,
is_sentencepiece_available,
is_speech_available,
is_tf_available,
is_torch_available,
)


_import_structure = {
"configuration_speech_to_text": ["SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2TextConfig"],
"feature_extraction_speech_to_text": ["Speech2TextFeatureExtractor"],
"processing_speech_to_text": ["Speech2TextProcessor"],
}

Expand All @@ -36,14 +36,6 @@
else:
_import_structure["tokenization_speech_to_text"] = ["Speech2TextTokenizer"]

try:
if not is_speech_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
_import_structure["feature_extraction_speech_to_text"] = ["Speech2TextFeatureExtractor"]

try:
if not is_tf_available():
raise OptionalDependencyNotAvailable()
Expand Down Expand Up @@ -73,6 +65,7 @@

if TYPE_CHECKING:
from .configuration_speech_to_text import SPEECH_TO_TEXT_PRETRAINED_CONFIG_ARCHIVE_MAP, Speech2TextConfig
from .feature_extraction_speech_to_text import Speech2TextFeatureExtractor
from .processing_speech_to_text import Speech2TextProcessor

try:
Expand All @@ -83,14 +76,6 @@
else:
from .tokenization_speech_to_text import Speech2TextTokenizer

try:
if not is_speech_available():
raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
pass
else:
from .feature_extraction_speech_to_text import Speech2TextFeatureExtractor

try:
if not is_tf_available():
raise OptionalDependencyNotAvailable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@

import numpy as np
import torch
import torchaudio.compliance.kaldi as ta_kaldi

from ...audio_utils import mel_filter_bank, spectrogram, window_function
from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, is_speech_available, logging


if is_speech_available():
import torchaudio.compliance.kaldi as ta_kaldi

logger = logging.get_logger(__name__)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ def test_call(self):
encoded_sequences_2 = feat_extract(np_speech_inputs, return_tensors="np").input_values
for enc_seq_1, enc_seq_2 in zip(encoded_sequences_1, encoded_sequences_2):
self.assertTrue(np.allclose(enc_seq_1, enc_seq_2, atol=1e-3))

@require_torch
def test_double_precision_pad(self):
import torch
Expand Down Expand Up @@ -174,7 +174,7 @@ def test_integration(self):
input_values = feature_extractor(input_speech, return_tensors="pt").input_values
self.assertEquals(input_values.shape, (1, 1024, 128))
self.assertTrue(torch.allclose(input_values[0, 0, :30], EXPECTED_INPUT_VALUES, atol=1e-4))

def test_feat_extract_from_and_save_pretrained(self):
feat_extract_first = self.feature_extraction_class(**self.feat_extract_dict)

Expand All @@ -201,16 +201,20 @@ def test_feat_extract_to_json_file(self):


@require_torch
@unittest.mock.patch("transformers.models.audio_spectrogram_transformer.feature_extraction_audio_spectrogram_transformer.is_speech_available", lambda: False)
@unittest.mock.patch(
"transformers.models.audio_spectrogram_transformer.feature_extraction_audio_spectrogram_transformer.is_speech_available",
lambda: False,
)
class ASTFeatureExtractionWithoutTorchaudioTest(SequenceFeatureExtractionTestMixin, unittest.TestCase):
feature_extraction_class = ASTFeatureExtractor

def setUp(self):
self.feat_extract_tester = ASTFeatureExtractionTester(self)

def test_using_audio_utils(self):
# Tests that it uses audio_utils instead of torchaudio
feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())

self.assertTrue(hasattr(feat_extract, "window"))
self.assertTrue(hasattr(feat_extract, "mel_filters"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@

import numpy as np

from transformers import Speech2TextFeatureExtractor
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


from transformers import Speech2TextFeatureExtractor

global_rng = random.Random()


Expand Down Expand Up @@ -306,7 +305,9 @@ def test_feat_extract_to_json_file(self):


@require_torch
@unittest.mock.patch("transformers.models.speech_to_text.feature_extraction_speech_to_text.is_speech_available", lambda: False)
@unittest.mock.patch(
"transformers.models.speech_to_text.feature_extraction_speech_to_text.is_speech_available", lambda: False
)
class Speech2TextFeatureExtractionWithoutTorchaudioTest(SequenceFeatureExtractionTestMixin, unittest.TestCase):
feature_extraction_class = Speech2TextFeatureExtractor

Expand All @@ -320,7 +321,7 @@ def _check_zero_mean_unit_variance(self, input_vector):
def test_using_audio_utils(self):
# Tests that it uses audio_utils instead of torchaudio
feat_extract = self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())

self.assertTrue(hasattr(feat_extract, "window"))
self.assertTrue(hasattr(feat_extract, "mel_filters"))

Expand Down
3 changes: 2 additions & 1 deletion tests/models/speech_to_text/test_processor_speech_to_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
from pathlib import Path
from shutil import copyfile

from transformers import Speech2TextTokenizer, Speech2TextFeatureExtractor, Speech2TextProcessor
from transformers import Speech2TextFeatureExtractor, Speech2TextProcessor, Speech2TextTokenizer
from transformers.models.speech_to_text.tokenization_speech_to_text import VOCAB_FILES_NAMES, save_json
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_torch, require_torchaudio
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_speech_to_text import floats_list


SAMPLE_SP = get_tests_dir("fixtures/test_sentencepiece.model")


Expand Down

0 comments on commit b16b5a9

Please sign in to comment.