




import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType







@require_torch
class 		__UpperCAmelCase:





     """simple docstring"""



     __UpperCAmelCase						:			Union[str, Any]							=     ()
     __UpperCAmelCase						:			Tuple							=     True

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Tuple     )      ->      Tuple:






         """simple docstring"""
         a__  			=							self.config_class

         # test default config
         a__  			=							config_class()
         self.assertIsNotNone(__lowercase     )
         a__  			=							["""stem"""] + [f"""stage{idx}""" for idx in range(1    ,					len(config.depths     ) + 1     )]
         self.assertEqual(config.stage_names    ,					__lowercase     )
         self.assertTrue(set(config.out_features     ).issubset(set(config.stage_names     )     )     )

         # Test out_features and out_indices are correctly set
         # out_features and out_indices both None
         a__  			=							config_class(out_features=__lowercase    ,					out_indices=__lowercase     )
         self.assertEqual(config.out_features    ,					[config.stage_names[-1]]     )
         self.assertEqual(config.out_indices    ,					[len(config.stage_names     ) - 1]     )

         # out_features and out_indices both set
         a__  			=							config_class(out_features=["""stem""", """stage1"""]    ,					out_indices=[0, 1]     )
         self.assertEqual(config.out_features    ,					["""stem""", """stage1"""]     )
         self.assertEqual(config.out_indices    ,					[0, 1]     )

         # Only out_features set
         a__  			=							config_class(out_features=["""stage1""", """stage3"""]     )
         self.assertEqual(config.out_features    ,					["""stage1""", """stage3"""]     )
         self.assertEqual(config.out_indices    ,					[1, 3]     )

         # Only out_indices set
         a__  			=							config_class(out_indices=[0, 2]     )
         self.assertEqual(config.out_features    ,					[config.stage_names[0], config.stage_names[2]]     )
         self.assertEqual(config.out_indices    ,					[0, 2]     )

         # Error raised when out_indices do not correspond to out_features
         with self.assertRaises(__lowercase     ):
             a__  			=							config_class(out_features=["""stage1""", """stage2"""]    ,					out_indices=[0, 2]     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]     )      ->      Union[str, Any]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             a__  			=							model_class(__lowercase     )
             a__  			=							inspect.signature(model.forward     )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             a__  			=							[*signature.parameters.keys()]
             a__  			=							["""pixel_values"""]
             self.assertListEqual(arg_names[:1]    ,					__lowercase     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							List[str]     )      ->      Tuple:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             a__  			=							model_class(__lowercase     )
             self.assertEqual(len(model.channels     )    ,					len(config.out_features     )     )
             a__  			=							model.num_features
             a__  			=							[config.stage_names.index(__lowercase     ) for feat in config.out_features]
             a__  			=							[num_features[idx] for idx in out_indices]
             self.assertListEqual(model.channels    ,					__lowercase     )

             a__  			=							copy.deepcopy(__lowercase     )
             a__  			=							None
             a__  			=							model_class(__lowercase     )
             self.assertEqual(len(model.channels     )    ,					1     )
             self.assertListEqual(model.channels    ,					[num_features[-1]]     )

             a__  			=							copy.deepcopy(__lowercase     )
             a__  			=							None
             a__  			=							model_class(__lowercase     )
             self.assertEqual(len(model.channels     )    ,					1     )
             self.assertListEqual(model.channels    ,					[num_features[-1]]     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							str     )      ->      Optional[int]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             a__  			=							model_class(__lowercase     )
             model.to(__lowercase     )
             model.eval()
             a__  			=							model(**__lowercase     )

             self.assertEqual(len(result.feature_maps     )    ,					len(config.out_features     )     )
             self.assertEqual(len(model.channels     )    ,					len(config.out_features     )     )
             self.assertEqual(len(result.feature_maps     )    ,					len(config.out_indices     )     )
             self.assertEqual(len(model.channels     )    ,					len(config.out_indices     )     )

             # Check output of last stage is taken if out_features=None, out_indices=None
             a__  			=							copy.deepcopy(__lowercase     )
             a__  			=							None
             a__  			=							model_class(__lowercase     )
             model.to(__lowercase     )
             model.eval()
             a__  			=							model(**__lowercase     )

             self.assertEqual(len(result.feature_maps     )    ,					1     )
             self.assertEqual(len(model.channels     )    ,					1     )

             a__  			=							copy.deepcopy(__lowercase     )
             a__  			=							None
             a__  			=							model_class(__lowercase     )
             model.to(__lowercase     )
             model.eval()
             a__  			=							model(**__lowercase     )

             self.assertEqual(len(result.feature_maps     )    ,					1     )
             self.assertEqual(len(model.channels     )    ,					1     )

             # Check backbone can be initialized with fresh weights
             a__  			=							copy.deepcopy(__lowercase     )
             a__  			=							False
             a__  			=							model_class(__lowercase     )
             model.to(__lowercase     )
             model.eval()
             a__  			=							model(**__lowercase     )

     def 					__SCREAMING_SNAKE_CASE		(   self			:							str     )      ->      Union[str, Any]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()

         for backbone_class in self.all_model_classes:
             a__  			=							backbone_class(__lowercase     )

             self.assertTrue(hasattr(__lowercase    ,					"""backbone_type"""     )     )
             self.assertTrue(hasattr(__lowercase    ,					"""stage_names"""     )     )
             self.assertTrue(hasattr(__lowercase    ,					"""num_features"""     )     )
             self.assertTrue(hasattr(__lowercase    ,					"""out_indices"""     )     )
             self.assertTrue(hasattr(__lowercase    ,					"""out_features"""     )     )
             self.assertTrue(hasattr(__lowercase    ,					"""out_feature_channels"""     )     )
             self.assertTrue(hasattr(__lowercase    ,					"""channels"""     )     )

             self.assertIsInstance(backbone.backbone_type    ,					__lowercase     )
             # Verify num_features has been initialized in the backbone init
             self.assertIsNotNone(backbone.num_features     )
             self.assertTrue(len(backbone.channels     ) == len(backbone.out_indices     )     )
             self.assertTrue(len(backbone.stage_names     ) == len(backbone.num_features     )     )
             self.assertTrue(len(backbone.channels     ) <= len(backbone.num_features     )     )
             self.assertTrue(len(backbone.out_feature_channels     ) == len(backbone.stage_names     )     )






     def 					__SCREAMING_SNAKE_CASE		(   self			:							Optional[Any]     )      ->      Optional[Any]:






         """simple docstring"""
         a__			,		a__  			=							self.model_tester.prepare_config_and_inputs_for_common()
         a__  			=							inputs_dict["""pixel_values"""].shape[0]

         for backbone_class in self.all_model_classes:
             a__  			=							backbone_class(__lowercase     )
             backbone.to(__lowercase     )
             backbone.eval()

             a__  			=							backbone(**__lowercase     )

             # Test default outputs and verify feature maps
             self.assertIsInstance(outputs.feature_maps    ,					__lowercase     )
             self.assertTrue(len(outputs.feature_maps     ) == len(backbone.channels     )     )
             for feature_map, n_channels in zip(outputs.feature_maps    ,					backbone.channels     ):
                 self.assertTrue(feature_map.shape[:2]    ,					(batch_size, n_channels)     )
             self.assertIsNone(outputs.hidden_states     )
             self.assertIsNone(outputs.attentions     )

             # Test output_hidden_states=True
             a__  			=							backbone(**__lowercase    ,					output_hidden_states=__lowercase     )
             self.assertIsNotNone(outputs.hidden_states     )
             self.assertTrue(len(outputs.hidden_states     )    ,					len(backbone.stage_names     )     )
             for hidden_state, n_channels in zip(outputs.hidden_states    ,					backbone.channels     ):
                 self.assertTrue(hidden_state.shape[:2]    ,					(batch_size, n_channels)     )

             # Test output_attentions=True
             if self.has_attentions:
                 a__  			=							backbone(**__lowercase    ,					output_attentions=__lowercase     )
                 self.assertIsNotNone(outputs.attentions     )










'''simple docstring'''

import inspect
import json
import os
import tempfile
import unittest

from transformers.models.speech_to_text_a import SpeechaTextaTokenizer
from transformers.models.speech_to_text_a.tokenization_speech_to_text_a import VOCAB_FILES_NAMES

from ...test_tokenization_common import TokenizerTesterMixin


class    a__					(			__A				,    unittest.TestCase   ):







				"""simple docstring"""



				__UpperCamelCase     :		Any						=						SpeechaTextaTokenizer
				__UpperCamelCase     :		str						=						False




				def        _snake_case	(self		):
										super().setUp()

										__lowerCAmelCase											=					'''<s> <pad> </s> <unk> here@@ a couple of@@ words for the he@@ re@@ vocab'''.split(''' '''		)
										__lowerCAmelCase											=					['''he re</w> 123''', '''here a 1456''']
										__lowerCAmelCase											=					dict(zip(__lowercase							,      range(len(__lowercase		)		)		)		)

										__lowerCAmelCase											=					{'''pad_token''': '''<pad>''', '''unk_token''': '''<unk>''', '''bos_token''': '''<s>''', '''eos_token''': '''</s>'''}

										__lowerCAmelCase											=					tempfile.mkdtemp()
										__lowerCAmelCase											=					os.path.join(self.tmpdirname							,      VOCAB_FILES_NAMES['''vocab_file''']		)
										__lowerCAmelCase											=					os.path.join(self.tmpdirname							,      VOCAB_FILES_NAMES['''merges_file''']		)

										with open(self.vocab_file							,      '''w'''							,      encoding='''utf-8'''		) as fp:
																fp.write(json.dumps(__lowercase		) + '''\n'''		)

										with open(self.merges_file							,      '''w'''		) as fp:
																fp.write('''\n'''.join(__lowercase		)		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					list(self.get_tokenizer().get_vocab().keys()		)

										self.assertEqual(vocab_keys[0]							,      '''<s>'''		)
										self.assertEqual(vocab_keys[1]							,      '''<pad>'''		)
										self.assertEqual(vocab_keys[-1]							,      '''vocab'''		)
										self.assertEqual(len(__lowercase		)							,      14		)




				def        _snake_case	(self		):
										self.assertEqual(self.get_tokenizer().vocab_size							,      14		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					SpeechaTextaTokenizer.from_pretrained(self.tmpdirname		)

										# make sure @@ is correctly concatenated
										__lowerCAmelCase											=					[4, 6, 8, 7, 10]  # ["here@@", "couple", "words", "of@@", "the"]
										__lowerCAmelCase											=					tokenizer.decode(__lowercase		)

										self.assertTrue(output_string == '''herecouple words ofthe'''		)




				def        _snake_case	(self		):
										__lowerCAmelCase											=					SpeechaTextaTokenizer.from_pretrained(self.tmpdirname		)

										with tempfile.TemporaryDirectory() as tmp_dirname:
																tokenizer.save_pretrained(__lowercase		)
																os.remove(os.path.join(__lowercase							,      '''merges.txt'''		)		)

																# load tokenizer without merges file should not throw an error
																__lowerCAmelCase											=					SpeechaTextaTokenizer.from_pretrained(__lowercase		)

										with tempfile.TemporaryDirectory() as tmp_dirname:
																# save tokenizer and load again
																tokenizer.save_pretrained(__lowercase		)
																__lowerCAmelCase											=					SpeechaTextaTokenizer.from_pretrained(__lowercase		)

										self.assertIsNotNone(__lowercase		)




				def        _snake_case	(self		):
										if not self.test_slow_tokenizer:
																return

										__lowerCAmelCase											=					inspect.signature(self.tokenizer_class.__init__		)
										__lowerCAmelCase											=					self.get_tokenizer()

										for parameter_name, parameter in signature.parameters.items():
																if parameter.default != inspect.Parameter.empty and parameter_name != "merges_file":
																						self.assertIn(__lowercase							,      tokenizer.init_kwargs		)

