



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     		=			()
			UpperCamelCase     		=			True




			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					__UpperCAmelCase       :	Optional[int]						=      self.config_class

					# test default config
					__UpperCAmelCase       :	Optional[int]						=      config_class()
					self.assertIsNotNone(a_					)
					__UpperCAmelCase       :	Optional[Any]						=      ['''stem'''] + [F'stage{idx}' for idx in range(1			,			len(config.depths					) + 1					)]
					self.assertEqual(config.stage_names			,			a_					)
					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
					__UpperCAmelCase       :	Any						=      config_class(out_features=a_			,			out_indices=a_					)
					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
					__UpperCAmelCase       :	List[Any]						=      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
					__UpperCAmelCase       :	int						=      config_class(out_features=['''stage1''', '''stage3''']					)
					self.assertEqual(config.out_features			,			['''stage1''', '''stage3''']					)
					self.assertEqual(config.out_indices			,			[1, 3]					)

					# Only out_indices set
					__UpperCAmelCase       :	str						=      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(a_					):
							__UpperCAmelCase       :	List[str]						=      config_class(out_features=['''stage1''', '''stage2''']			,			out_indices=[0, 2]					)




			def 					snake_case__	(  self				:					int					):
					'''simple docstring'''





					__UpperCAmelCase				,   __UpperCAmelCase       :	int						=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__UpperCAmelCase       :	List[str]						=      model_class(a_					)
							__UpperCAmelCase       :	Union[str, Any]						=      inspect.signature(model.forward					)
							# signature.parameters is an OrderedDict => so arg_names order is deterministic
							__UpperCAmelCase       :	Dict						=      [*signature.parameters.keys()]
							__UpperCAmelCase       :	str						=      ['''pixel_values''']
							self.assertListEqual(arg_names[:1]			,			a_					)




			def 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					__UpperCAmelCase				,   __UpperCAmelCase       :	Tuple						=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__UpperCAmelCase       :	str						=      model_class(a_					)
							self.assertEqual(len(model.channels					)			,			len(config.out_features					)					)
							__UpperCAmelCase       :	Dict						=      model.num_features
							__UpperCAmelCase       :	Union[str, Any]						=      [config.stage_names.index(a_					) for feat in config.out_features]
							__UpperCAmelCase       :	Dict						=      [num_features[idx] for idx in out_indices]
							self.assertListEqual(model.channels			,			a_					)

							__UpperCAmelCase       :	Tuple						=      copy.deepcopy(a_					)
							__UpperCAmelCase       :	Dict						=      None
							__UpperCAmelCase       :	Union[str, Any]						=      model_class(a_					)
							self.assertEqual(len(model.channels					)			,			1					)
							self.assertListEqual(model.channels			,			[num_features[-1]]					)

							__UpperCAmelCase       :	Dict						=      copy.deepcopy(a_					)
							__UpperCAmelCase       :	str						=      None
							__UpperCAmelCase       :	Tuple						=      model_class(a_					)
							self.assertEqual(len(model.channels					)			,			1					)
							self.assertListEqual(model.channels			,			[num_features[-1]]					)




			def 					snake_case__	(  self				:					List[Any]					):
					'''simple docstring'''





					__UpperCAmelCase				,   __UpperCAmelCase       :	str						=      self.model_tester.prepare_config_and_inputs_for_common()

					for model_class in self.all_model_classes:
							__UpperCAmelCase       :	List[Any]						=      model_class(a_					)
							model.to(a_					)
							model.eval()
							__UpperCAmelCase       :	List[str]						=      model(**a_					)

							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
							__UpperCAmelCase       :	Optional[int]						=      copy.deepcopy(a_					)
							__UpperCAmelCase       :	List[str]						=      None
							__UpperCAmelCase       :	Optional[Any]						=      model_class(a_					)
							model.to(a_					)
							model.eval()
							__UpperCAmelCase       :	Optional[Any]						=      model(**a_					)

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

							__UpperCAmelCase       :	Tuple						=      copy.deepcopy(a_					)
							__UpperCAmelCase       :	Dict						=      None
							__UpperCAmelCase       :	List[Any]						=      model_class(a_					)
							model.to(a_					)
							model.eval()
							__UpperCAmelCase       :	Tuple						=      model(**a_					)

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

							# Check backbone can be initialized with fresh weights
							__UpperCAmelCase       :	int						=      copy.deepcopy(a_					)
							__UpperCAmelCase       :	Tuple						=      False
							__UpperCAmelCase       :	Optional[Any]						=      model_class(a_					)
							model.to(a_					)
							model.eval()
							__UpperCAmelCase       :	int						=      model(**a_					)




			def 					snake_case__	(  self				:					Union[str, Any]					):
					'''simple docstring'''





					__UpperCAmelCase				,   __UpperCAmelCase       :	str						=      self.model_tester.prepare_config_and_inputs_for_common()

					for backbone_class in self.all_model_classes:
							__UpperCAmelCase       :	Dict						=      backbone_class(a_					)

							self.assertTrue(hasattr(a_			,			'''backbone_type'''					)					)
							self.assertTrue(hasattr(a_			,			'''stage_names'''					)					)
							self.assertTrue(hasattr(a_			,			'''num_features'''					)					)
							self.assertTrue(hasattr(a_			,			'''out_indices'''					)					)
							self.assertTrue(hasattr(a_			,			'''out_features'''					)					)
							self.assertTrue(hasattr(a_			,			'''out_feature_channels'''					)					)
							self.assertTrue(hasattr(a_			,			'''channels'''					)					)

							self.assertIsInstance(backbone.backbone_type			,			a_					)
							# 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 					snake_case__	(  self				:					List[str]					):
					'''simple docstring'''





					__UpperCAmelCase				,   __UpperCAmelCase       :	Tuple						=      self.model_tester.prepare_config_and_inputs_for_common()
					__UpperCAmelCase       :	Optional[Any]						=      inputs_dict['''pixel_values'''].shape[0]

					for backbone_class in self.all_model_classes:
							__UpperCAmelCase       :	List[Any]						=      backbone_class(a_					)
							backbone.to(a_					)
							backbone.eval()

							__UpperCAmelCase       :	Optional[int]						=      backbone(**a_					)

							# Test default outputs and verify feature maps
							self.assertIsInstance(outputs.feature_maps			,			a_					)
							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
							__UpperCAmelCase       :	str						=      backbone(**a_			,			output_hidden_states=a_					)
							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:
									__UpperCAmelCase       :	Optional[Any]						=      backbone(**a_			,			output_attentions=a_					)
									self.assertIsNotNone(outputs.attentions					)










from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_speech_available,
    is_torch_available,
)


__A       ={
    "configuration_speech_to_text_2": ["SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP", "Speech2Text2Config"],
    "processing_speech_to_text_2": ["Speech2Text2Processor"],
    "tokenization_speech_to_text_2": ["Speech2Text2Tokenizer"],
}


try:
				if not is_torch_available():
								raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
				pass
else:
				__A       =[
				    "SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST",
				    "Speech2Text2ForCausalLM",
				    "Speech2Text2PreTrainedModel",
				]


if TYPE_CHECKING:
				from .configuration_speech_to_text_a import SPEECH_TO_TEXT_2_PRETRAINED_CONFIG_ARCHIVE_MAP, SpeechaTextaConfig
				from .processing_speech_to_text_a import SpeechaTextaProcessor
				from .tokenization_speech_to_text_a import SpeechaTextaTokenizer

				try:
								if not is_torch_available():
												raise OptionalDependencyNotAvailable()
				except OptionalDependencyNotAvailable:
								pass
				else:
								from .modeling_speech_to_text_a import (
								    SPEECH_TO_TEXT_2_PRETRAINED_MODEL_ARCHIVE_LIST,
								    SpeechaTextaForCausalLM,
								    SpeechaTextaPreTrainedModel,
								)

else:
				import sys

				__A       =_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





