
import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
	from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import MobileNetVaImageProcessor







class 			A_  (     __lowerCamelCase  ):




	'''simple docstring'''


	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.config_class(**self.inputs_dict			)
		self.parent.assertTrue(hasattr(snake_case						,       'tf_padding'			)			)
		self.parent.assertTrue(hasattr(snake_case						,       'depth_multiplier'			)			)







class 			A_  :




	'''simple docstring'''


	def __init__(			self						,       snake_case						,       snake_case=13						,       snake_case=3						,       snake_case=32						,       snake_case=0.25						,       snake_case=8						,       snake_case=8						,       snake_case=6						,       snake_case=32						,       snake_case=True						,       snake_case=True						,       snake_case=True						,       snake_case="relu6"						,       snake_case=1280						,       snake_case=0.1						,       snake_case=0.02						,       snake_case=True						,       snake_case=True						,       snake_case=10						,       snake_case=None						,       ):
		lowercase	    =       parent
		lowercase	    =       batch_size
		lowercase	    =       num_channels
		lowercase	    =       image_size
		lowercase	    =       depth_multiplier
		lowercase	    =       depth_divisible_by
		lowercase	    =       min_depth
		lowercase	    =       expand_ratio
		lowercase	    =       tf_padding
		lowercase	    =       output_stride
		lowercase	    =       first_layer_is_expansion
		lowercase	    =       finegrained_output
		lowercase	    =       hidden_act
		lowercase	    =       last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier			)
		lowercase	    =       classifier_dropout_prob
		lowercase	    =       use_labels
		lowercase	    =       is_training
		lowercase	    =       num_labels
		lowercase	    =       initializer_range
		lowercase	    =       scope





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

		lowercase	    =       None
		lowercase	    =       None
		if self.use_labels:
			lowercase	    =       ids_tensor([self.batch_size]						,       self.num_labels			)
			lowercase	    =       ids_tensor([self.batch_size, self.image_size, self.image_size]						,       self.num_labels			)

		lowercase	    =       self.get_config()

		return config, pixel_values, labels, pixel_labels





	def       SCREAMING_SNAKE_CASE__		(			self			):
		return MobileNetVaConfig(
		    num_channels=self.num_channels						,       image_size=self.image_size						,       depth_multiplier=self.depth_multiplier						,       depth_divisible_by=self.depth_divisible_by						,       min_depth=self.min_depth						,       expand_ratio=self.expand_ratio						,       output_stride=self.output_stride						,       first_layer_is_expansion=self.first_layer_is_expansion						,       finegrained_output=self.finegrained_output						,       hidden_act=self.hidden_act						,       tf_padding=self.tf_padding						,       classifier_dropout_prob=self.classifier_dropout_prob						,       initializer_range=self.initializer_range						,       )





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       MobileNetVaModel(config=snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(
		    result.last_hidden_state.shape						,       (
		        self.batch_size,
		        self.last_hidden_size,
		        self.image_size // self.output_stride,
		        self.image_size // self.output_stride,
		    )						,       )
		self.parent.assertEqual(
		    result.pooler_output.shape						,       (self.batch_size, self.last_hidden_size)						,       )





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       self.num_labels
		lowercase	    =       MobileNetVaForImageClassification(snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case						,       labels=snake_case			)
		self.parent.assertEqual(result.logits.shape						,       (self.batch_size, self.num_labels)			)





	def       SCREAMING_SNAKE_CASE__		(			self						,       snake_case						,       snake_case						,       snake_case						,       snake_case			):
		lowercase	    =       self.num_labels
		lowercase	    =       MobileNetVaForSemanticSegmentation(snake_case			)
		model.to(snake_case			)
		model.eval()
		lowercase	    =       model(snake_case			)
		self.parent.assertEqual(
		    result.logits.shape						,       (
		        self.batch_size,
		        self.num_labels,
		        self.image_size // self.output_stride,
		        self.image_size // self.output_stride,
		    )						,       )
		lowercase	    =       model(snake_case						,       labels=snake_case			)
		self.parent.assertEqual(
		    result.logits.shape						,       (
		        self.batch_size,
		        self.num_labels,
		        self.image_size // self.output_stride,
		        self.image_size // self.output_stride,
		    )						,       )





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.prepare_config_and_inputs()
		lowercase    ,   lowercase    ,   lowercase    ,   lowercase	    =       config_and_inputs
		lowercase	    =       {'pixel_values': pixel_values}
		return config, inputs_dict







@require_torch
class 			A_  (     __lowerCamelCase ,      __lowerCamelCase ,      unittest.TestCase  ):




	'''simple docstring'''


	_UpperCamelCase	:	Tuple									=			(
	    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
	    if is_torch_available()
	    else ()
	)
	_UpperCamelCase	:	Any									=			(
	    {
	        """feature-extraction""": MobileNetVaModel,
	        """image-classification""": MobileNetVaForImageClassification,
	        """image-segmentation""": MobileNetVaForSemanticSegmentation,
	    }
	    if is_torch_available()
	    else {}
	)

	_UpperCamelCase	:	List[str]									=			False
	_UpperCamelCase	:	Optional[int]									=			False
	_UpperCamelCase	:	Union[str, Any]									=			False
	_UpperCamelCase	:	Optional[Any]									=			False





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       MobileNetVaModelTester(self			)
		lowercase	    =       MobileNetVaConfigTester(self						,       config_class=snake_case						,       has_text_modality=snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		self.config_tester.run_common_tests()





	@unittest.skip(reason='MobileNetV2 does not use inputs_embeds'			)
	def       SCREAMING_SNAKE_CASE__		(			self			):
		pass





	@unittest.skip(reason='MobileNetV2 does not support input and output embeddings'			)
	def       SCREAMING_SNAKE_CASE__		(			self			):
		pass





	@unittest.skip(reason='MobileNetV2 does not output attentions'			)
	def       SCREAMING_SNAKE_CASE__		(			self			):
		pass





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase    ,   lowercase	    =       self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			lowercase	    =       model_class(snake_case			)
			lowercase	    =       inspect.signature(model.forward			)
			# signature.parameters is an OrderedDict => so arg_names order is deterministic
			lowercase	    =       [*signature.parameters.keys()]

			lowercase	    =       ['pixel_values']
			self.assertListEqual(arg_names[:1]						,       snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		def check_hidden_states_output(snake_case						,       snake_case						,       snake_case			):
			lowercase	    =       model_class(snake_case			)
			model.to(snake_case			)
			model.eval()

			with torch.no_grad():
				lowercase	    =       model(**self._prepare_for_class(snake_case						,       snake_case			)			)

			lowercase	    =       outputs.hidden_states

			lowercase	    =       16
			self.assertEqual(len(snake_case			)						,       snake_case			)

		lowercase    ,   lowercase	    =       self.model_tester.prepare_config_and_inputs_for_common()

		for model_class in self.all_model_classes:
			lowercase	    =       True
			check_hidden_states_output(snake_case						,       snake_case						,       snake_case			)

			# check that output_hidden_states also work using config
			del inputs_dict["output_hidden_states"]
			lowercase	    =       True

			check_hidden_states_output(snake_case						,       snake_case						,       snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_image_classification(*snake_case			)





	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_semantic_segmentation(*snake_case			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
			lowercase	    =       MobileNetVaModel.from_pretrained(snake_case			)
			self.assertIsNotNone(snake_case			)






def 				UpperCAmelCase_						(				):
	lowercase	    =       Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'				)
	return image







@require_torch
@require_vision
class 			A_  (     unittest.TestCase  ):




	'''simple docstring'''


	@cached_property
	def       SCREAMING_SNAKE_CASE__		(			self			):
		return (
		    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v2_1.0_224'			) if is_vision_available() else None
		)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v2_1.0_224'			).to(snake_case			)

		lowercase	    =       self.default_image_processor
		lowercase	    =       prepare_img()
		lowercase	    =       image_processor(images=snake_case						,       return_tensors='pt'			).to(snake_case			)

		# forward pass
		with torch.no_grad():
			lowercase	    =       model(**snake_case			)

		# verify the logits
		lowercase	    =       torch.Size((1, 1001)			)
		self.assertEqual(outputs.logits.shape						,       snake_case			)

		lowercase	    =       torch.tensor([0.2_445, -1.1_993, 0.1_905]			).to(snake_case			)

		self.assertTrue(torch.allclose(outputs.logits[0, :3]						,       snake_case						,       atol=1E-4			)			)





	@slow
	def       SCREAMING_SNAKE_CASE__		(			self			):
		lowercase	    =       MobileNetVaForSemanticSegmentation.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'			)
		lowercase	    =       model.to(snake_case			)

		lowercase	    =       MobileNetVaImageProcessor.from_pretrained('google/deeplabv3_mobilenet_v2_1.0_513'			)

		lowercase	    =       prepare_img()
		lowercase	    =       image_processor(images=snake_case						,       return_tensors='pt'			).to(snake_case			)

		# forward pass
		with torch.no_grad():
			lowercase	    =       model(**snake_case			)
		lowercase	    =       outputs.logits

		# verify the logits
		lowercase	    =       torch.Size((1, 21, 65, 65)			)
		self.assertEqual(logits.shape						,       snake_case			)

		lowercase	    =       torch.tensor(
		    [
		        [[17.5_790, 17.7_581, 18.3_355], [18.3_257, 18.4_230, 18.8_973], [18.6_169, 18.8_650, 19.2_187]],
		        [[-2.1_595, -2.0_977, -2.3_741], [-2.4_226, -2.3_028, -2.6_835], [-2.7_819, -2.5_991, -2.7_706]],
		        [[4.2_058, 4.8_317, 4.7_638], [4.4_136, 5.0_361, 4.9_383], [4.5_028, 4.9_644, 4.8_734]],
		    ]						,       device=snake_case						,       )

		self.assertTrue(torch.allclose(logits[0, :3, :3, :3]						,       snake_case						,       atol=1E-4			)			)





'''simple docstring'''
import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__SCREAMING_SNAKE_CASE					:				Dict									=		logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE					:				int									=		{
    '''facebook/wav2vec2-base-960h''': '''https://huggingface.co/facebook/wav2vec2-base-960h/resolve/main/config.json''',
    # See all Wav2Vec2 models at https://huggingface.co/models?filter=wav2vec2
}






class 					__lowerCAmelCase   (   lowercase						):

						"""simple docstring"""






						_UpperCAmelCase		:       Dict   						="wav2vec2"






						def __init__(  self							:      str ,  lowerCAmelCase							:      Dict=32 ,  lowerCAmelCase							:      List[Any]=7_68 ,  lowerCAmelCase							:      List[str]=12 ,  lowerCAmelCase							:      List[str]=12 ,  lowerCAmelCase							:      List[Any]=30_72 ,  lowerCAmelCase							:      Optional[Any]="gelu" ,  lowerCAmelCase							:      Optional[int]=0.1 ,  lowerCAmelCase							:      Optional[int]=0.1 ,  lowerCAmelCase							:      List[Any]=0.1 ,  lowerCAmelCase							:      Dict=0.0 ,  lowerCAmelCase							:      Union[str, Any]=0.0 ,  lowerCAmelCase							:      str=0.1 ,  lowerCAmelCase							:      List[str]=0.1 ,  lowerCAmelCase							:      Dict=0.0_2 ,  lowerCAmelCase							:      List[str]=1e-5 ,  lowerCAmelCase							:      Optional[Any]="group" ,  lowerCAmelCase							:      List[Any]="gelu" ,  lowerCAmelCase							:      int=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12) ,  lowerCAmelCase							:      Union[str, Any]=(5, 2, 2, 2, 2, 2, 2) ,  lowerCAmelCase							:      Any=(10, 3, 3, 3, 3, 2, 2) ,  lowerCAmelCase							:      str=False ,  lowerCAmelCase							:      Tuple=1_28 ,  lowerCAmelCase							:      Union[str, Any]=16 ,  lowerCAmelCase							:      List[str]=False ,  lowerCAmelCase							:      Union[str, Any]=True ,  lowerCAmelCase							:      Any=0.0_5 ,  lowerCAmelCase							:      Any=10 ,  lowerCAmelCase							:      Dict=2 ,  lowerCAmelCase							:      List[Any]=0.0 ,  lowerCAmelCase							:      int=10 ,  lowerCAmelCase							:      List[str]=0 ,  lowerCAmelCase							:      Tuple=3_20 ,  lowerCAmelCase							:      List[str]=2 ,  lowerCAmelCase							:      int=0.1 ,  lowerCAmelCase							:      List[Any]=1_00 ,  lowerCAmelCase							:      List[str]=2_56 ,  lowerCAmelCase							:      int=2_56 ,  lowerCAmelCase							:      Optional[Any]=0.1 ,  lowerCAmelCase							:      str="sum" ,  lowerCAmelCase							:      List[Any]=False ,  lowerCAmelCase							:      List[str]=False ,  lowerCAmelCase							:      Tuple=2_56 ,  lowerCAmelCase							:      str=(5_12, 5_12, 5_12, 5_12, 15_00) ,  lowerCAmelCase							:      List[str]=(5, 3, 3, 1, 1) ,  lowerCAmelCase							:      Tuple=(1, 2, 3, 1, 1) ,  lowerCAmelCase							:      Optional[Any]=5_12 ,  lowerCAmelCase							:      Any=0 ,  lowerCAmelCase							:      str=1 ,  lowerCAmelCase							:      Any=2 ,  lowerCAmelCase							:      Any=False ,  lowerCAmelCase							:      List[Any]=3 ,  lowerCAmelCase							:      Any=2 ,  lowerCAmelCase							:      List[str]=3 ,  lowerCAmelCase							:      int=None ,  lowerCAmelCase							:      Optional[int]=None ,  **lowerCAmelCase							:      Union[str, Any] ,  ):
													super().__init__(**lowerCAmelCase ,  pad_token_id=lowerCAmelCase ,  bos_token_id=lowerCAmelCase ,  eos_token_id=lowerCAmelCase	)
													A_			    =		hidden_size
													A_			    =		feat_extract_norm
													A_			    =		feat_extract_activation
													A_			    =		list(lowerCAmelCase	)
													A_			    =		list(lowerCAmelCase	)
													A_			    =		list(lowerCAmelCase	)
													A_			    =		conv_bias
													A_			    =		num_conv_pos_embeddings
													A_			    =		num_conv_pos_embedding_groups
													A_			    =		len(self.conv_dim	)
													A_			    =		num_hidden_layers
													A_			    =		intermediate_size
													A_			    =		hidden_act
													A_			    =		num_attention_heads
													A_			    =		hidden_dropout
													A_			    =		attention_dropout
													A_			    =		activation_dropout
													A_			    =		feat_proj_dropout
													A_			    =		final_dropout
													A_			    =		layerdrop
													A_			    =		layer_norm_eps
													A_			    =		initializer_range
													A_			    =		vocab_size
													A_			    =		do_stable_layer_norm
													A_			    =		use_weighted_layer_sum

													if (
													    (len(self.conv_stride	) != self.num_feat_extract_layers)
													    or (len(self.conv_kernel	) != self.num_feat_extract_layers)
													    or (len(self.conv_dim	) != self.num_feat_extract_layers)
													):
																				raise ValueError(
																				    "Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =="
																				    " `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ="
																				    F" {len(self.conv_dim	)}`, `len(config.conv_stride) = {len(self.conv_stride	)}`,"
																				    F" `len(config.conv_kernel) = {len(self.conv_kernel	)}`."	)

													# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
													A_			    =		apply_spec_augment
													A_			    =		mask_time_prob
													A_			    =		mask_time_length
													A_			    =		mask_time_min_masks
													A_			    =		mask_feature_prob
													A_			    =		mask_feature_length
													A_			    =		mask_feature_min_masks

													# parameters for pretraining with codevector quantized representations
													A_			    =		num_codevectors_per_group
													A_			    =		num_codevector_groups
													A_			    =		contrastive_logits_temperature
													A_			    =		feat_quantizer_dropout
													A_			    =		num_negatives
													A_			    =		codevector_dim
													A_			    =		proj_codevector_dim
													A_			    =		diversity_loss_weight

													# ctc loss
													A_			    =		ctc_loss_reduction
													A_			    =		ctc_zero_infinity

													# adapter
													A_			    =		add_adapter
													A_			    =		adapter_kernel_size
													A_			    =		adapter_stride
													A_			    =		num_adapter_layers
													A_			    =		output_hidden_size or hidden_size
													A_			    =		adapter_attn_dim

													# SequenceClassification-specific parameter. Feel free to ignore for other classes.
													A_			    =		classifier_proj_size

													# XVector-specific parameters. Feel free to ignore for other classes.
													A_			    =		list(lowerCAmelCase	)
													A_			    =		list(lowerCAmelCase	)
													A_			    =		list(lowerCAmelCase	)
													A_			    =		xvector_output_dim






						@property
						def 				_UpperCAmelCase       (  self							:      Tuple	):
													return functools.reduce(operator.mul ,  self.conv_stride ,  1	)





