"""simple docstring"""





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  _snake_case		(    __snake_case ):







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.config_class(**self.inputs_dict)
											self.parent.assertTrue(hasattr(_A      ,    """tf_padding"""))
											self.parent.assertTrue(hasattr(_A      ,    """depth_multiplier"""))




class  _snake_case		:







				"""simple docstring"""






				def __init__(      self							:							Tuple      ,    _A							:							Dict      ,    _A							:							Union[str, Any]=1_3      ,    _A							:							str=3      ,    _A							:							Tuple=3_2      ,    _A							:							Any=0.25      ,    _A							:							List[Any]=8      ,    _A							:							str=8      ,    _A							:							List[Any]=6      ,    _A							:							Any=3_2      ,    _A							:							Tuple=True      ,    _A							:							List[Any]=True      ,    _A							:							int=True      ,    _A							:							Optional[Any]="relu6"      ,    _A							:							Tuple=1_2_8_0      ,    _A							:							Union[str, Any]=0.1      ,    _A							:							Dict=0.02      ,    _A							:							Tuple=True      ,    _A							:							Dict=True      ,    _A							:							Optional[int]=1_0      ,    _A							:							List[str]=None      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    parent
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    batch_size
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    num_channels
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    image_size
											_SCREAMING_SNAKE_CASE		:						str			  =    depth_multiplier
											_SCREAMING_SNAKE_CASE		:						Any			  =    depth_divisible_by
											_SCREAMING_SNAKE_CASE		:						Dict			  =    min_depth
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    expand_ratio
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    tf_padding
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    output_stride
											_SCREAMING_SNAKE_CASE		:						str			  =    first_layer_is_expansion
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    finegrained_output
											_SCREAMING_SNAKE_CASE		:						int			  =    hidden_act
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier)
											_SCREAMING_SNAKE_CASE		:						Any			  =    classifier_dropout_prob
											_SCREAMING_SNAKE_CASE		:						str			  =    use_labels
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    is_training
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    num_labels
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    initializer_range
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    scope





				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    None
											_SCREAMING_SNAKE_CASE		:						Any			  =    None
											if self.use_labels:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    ids_tensor([self.batch_size]      ,    self.num_labels)
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    ids_tensor([self.batch_size, self.image_size, self.image_size]      ,    self.num_labels)

											_SCREAMING_SNAKE_CASE		:						int			  =    self.get_config()

											return config, pixel_values, labels, pixel_labels





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											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 		_lowerCAmelCase		(      self							:							int      ,    _A							:							Tuple      ,    _A							:							Optional[int]      ,    _A							:							List[str]      ,    _A							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    MobileNetVaModel(config=_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    model(_A)
											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 		_lowerCAmelCase		(      self							:							str      ,    _A							:							List[str]      ,    _A							:							Dict      ,    _A							:							Optional[int]      ,    _A							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Tuple			  =    self.num_labels
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    MobileNetVaForImageClassification(_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model(_A      ,    labels=_A)
											self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels))





				def 		_lowerCAmelCase		(      self							:							Optional[Any]      ,    _A							:							Union[str, Any]      ,    _A							:							str      ,    _A							:							str      ,    _A							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    self.num_labels
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    MobileNetVaForSemanticSegmentation(_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						Any			  =    model(_A)
											self.parent.assertEqual(
											    result.logits.shape      ,    (
											        self.batch_size,
											        self.num_labels,
											        self.image_size // self.output_stride,
											        self.image_size // self.output_stride,
											    )      ,    )
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model(_A      ,    labels=_A)
											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 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    config_and_inputs
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    {"""pixel_values""": pixel_values}
											return config, inputs_dict




@require_torch
class  _snake_case		(    __snake_case   ,       __snake_case   ,       unittest.TestCase ):







				"""simple docstring"""






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

				a			=						False
				a			=						False
				a			=						False
				a			=						False





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    MobileNetVaModelTester(self)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    MobileNetVaConfigTester(self      ,    config_class=_A      ,    has_text_modality=_A)





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											self.config_tester.run_common_tests()





				@unittest.skip(reason="""MobileNetV2 does not use inputs_embeds""")
				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											pass





				@unittest.skip(reason="""MobileNetV2 does not support input and output embeddings""")
				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											pass





				@unittest.skip(reason="""MobileNetV2 does not output attentions""")
				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											pass





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Dict			  =    self.model_tester.prepare_config_and_inputs_for_common()

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

																		_SCREAMING_SNAKE_CASE		:						Any			  =    ["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]      ,    _A)





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											def check_hidden_states_output(_A							:							Optional[Any]      ,    _A							:							Optional[int]      ,    _A							:							Dict):
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model_class(_A)
																		model.to(_A)
																		model.eval()

																		with torch.no_grad():
																									_SCREAMING_SNAKE_CASE		:						str			  =    model(**self._prepare_for_class(_A      ,    _A))

																		_SCREAMING_SNAKE_CASE		:						int			  =    outputs.hidden_states

																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    1_6
																		self.assertEqual(len(_A)      ,    _A)

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    True
																		check_hidden_states_output(_A      ,    _A      ,    _A)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    True

																		check_hidden_states_output(_A      ,    _A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A)





				def 		_lowerCAmelCase		(      self							:							Union[str, Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*_A)





				@slow
				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_SCREAMING_SNAKE_CASE		:						str			  =    MobileNetVaModel.from_pretrained(_A)
																		self.assertIsNotNone(_A)



def      lowerCamelCase_()->    Tuple:
							_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""  )
							return image




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







				"""simple docstring"""






				@cached_property
				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											return (
											    MobileNetVaImageProcessor.from_pretrained("""google/mobilenet_v2_1.0_224""") if is_vision_available() else None
											)





				@slow
				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224""").to(_A)

											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    self.default_image_processor
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    prepare_img()
											_SCREAMING_SNAKE_CASE		:						int			  =    image_processor(images=_A      ,    return_tensors="""pt""").to(_A)

											# forward pass
											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    model(**_A)

											# verify the logits
											_SCREAMING_SNAKE_CASE		:						Any			  =    torch.Size((1, 1_0_0_1))
											self.assertEqual(outputs.logits.shape      ,    _A)

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    torch.tensor([0.2_445, -1.1_993, 0.1_905]).to(_A)

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





				@slow
				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513""")
											_SCREAMING_SNAKE_CASE		:						str			  =    model.to(_A)

											_SCREAMING_SNAKE_CASE		:						Dict			  =    MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513""")

											_SCREAMING_SNAKE_CASE		:						Dict			  =    prepare_img()
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    image_processor(images=_A      ,    return_tensors="""pt""").to(_A)

											# forward pass
											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model(**_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    outputs.logits

											# verify the logits
											_SCREAMING_SNAKE_CASE		:						Dict			  =    torch.Size((1, 2_1, 6_5, 6_5))
											self.assertEqual(logits.shape      ,    _A)

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    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=_A      ,    )

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



"""simple docstring"""





import functools
import operator

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


lowerCAmelCase_     =	logging.get_logger(__name__)

lowerCAmelCase_     =	{
    '''asapp/sew-tiny-100k''': '''https://huggingface.co/asapp/sew-tiny-100k/resolve/main/config.json''',
    # See all SEW models at https://huggingface.co/models?filter=sew
}




class  _snake_case		(    __snake_case ):







				"""simple docstring"""






				a			=						"sew"





				def __init__(      self							:							List[Any]      ,    _A							:							Tuple=3_2      ,    _A							:							str=7_6_8      ,    _A							:							Dict=1_2      ,    _A							:							Tuple=1_2      ,    _A							:							Optional[Any]=3_0_7_2      ,    _A							:							List[str]=2      ,    _A							:							Dict="gelu"      ,    _A							:							Union[str, Any]=0.1      ,    _A							:							Optional[int]=0.1      ,    _A							:							Optional[int]=0.1      ,    _A							:							Optional[int]=0.0      ,    _A							:							str=0.1      ,    _A							:							Tuple=0.1      ,    _A							:							Optional[int]=0.02      ,    _A							:							Dict=1e-5      ,    _A							:							str="group"      ,    _A							:							Tuple="gelu"      ,    _A							:							Union[str, Any]=(6_4, 1_2_8, 1_2_8, 1_2_8, 1_2_8, 2_5_6, 2_5_6, 2_5_6, 2_5_6, 5_1_2, 5_1_2, 5_1_2, 5_1_2)      ,    _A							:							Optional[Any]=(5, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1)      ,    _A							:							Any=(1_0, 3, 1, 3, 1, 3, 1, 3, 1, 2, 1, 2, 1)      ,    _A							:							Tuple=False      ,    _A							:							Tuple=1_2_8      ,    _A							:							int=1_6      ,    _A							:							Union[str, Any]=True      ,    _A							:							Optional[Any]=0.05      ,    _A							:							List[Any]=1_0      ,    _A							:							Union[str, Any]=2      ,    _A							:							Tuple=0.0      ,    _A							:							Union[str, Any]=1_0      ,    _A							:							Optional[int]=0      ,    _A							:							Union[str, Any]="mean"      ,    _A							:							Optional[int]=False      ,    _A							:							List[Any]=False      ,    _A							:							int=2_5_6      ,    _A							:							str=0      ,    _A							:							Optional[int]=1      ,    _A							:							List[Any]=2      ,    **_A							:							Dict      ,    ):



											"""simple docstring"""







											super().__init__(**_A      ,    pad_token_id=_A      ,    bos_token_id=_A      ,    eos_token_id=_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    hidden_size
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    feat_extract_norm
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    feat_extract_activation
											_SCREAMING_SNAKE_CASE		:						Dict			  =    list(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    list(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    list(_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    conv_bias
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    num_conv_pos_embeddings
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    num_conv_pos_embedding_groups
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    len(self.conv_dim)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    num_hidden_layers
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    intermediate_size
											_SCREAMING_SNAKE_CASE		:						str			  =    squeeze_factor
											_SCREAMING_SNAKE_CASE		:						Dict			  =    hidden_act
											_SCREAMING_SNAKE_CASE		:						str			  =    num_attention_heads
											_SCREAMING_SNAKE_CASE		:						Dict			  =    hidden_dropout
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    attention_dropout
											_SCREAMING_SNAKE_CASE		:						int			  =    activation_dropout
											_SCREAMING_SNAKE_CASE		:						Any			  =    feat_proj_dropout
											_SCREAMING_SNAKE_CASE		:						str			  =    final_dropout
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    layerdrop
											_SCREAMING_SNAKE_CASE		:						Any			  =    layer_norm_eps
											_SCREAMING_SNAKE_CASE		:						int			  =    initializer_range
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    vocab_size

											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)`,"""
																		    f"""but is `len(config.conv_dim) = {len(self.conv_dim)}`, `len(config.conv_stride)"""
																		    f"""= {len(self.conv_stride)}`, `len(config.conv_kernel) = {len(self.conv_kernel)}`.""")

											# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    apply_spec_augment
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    mask_time_prob
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    mask_time_length
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    mask_time_min_masks
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    mask_feature_prob
											_SCREAMING_SNAKE_CASE		:						int			  =    mask_feature_length
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    mask_feature_min_masks

											# ctc loss
											_SCREAMING_SNAKE_CASE		:						int			  =    ctc_loss_reduction
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    ctc_zero_infinity

											# sequence classification
											_SCREAMING_SNAKE_CASE		:						Dict			  =    use_weighted_layer_sum
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    classifier_proj_size





				@property
				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											return functools.reduce(operator.mul      ,    self.conv_stride      ,    1)


