



"""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, MobileNetVaModel
							from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import MobileNetVaImageProcessor






class      UpperCAmelCase__		(      UpperCAmelCase_     ):



		def 					A_		( self  :    Dict				)			->						List[Any]:






									'''simple docstring'''

									A         =					self.config_class(**self.inputs_dict				)
									self.parent.assertTrue(hasattr(_lowercase      ,							'tf_padding'				)				)
									self.parent.assertTrue(hasattr(_lowercase      ,							'depth_multiplier'				)				)






class      UpperCAmelCase__		:



		def __init__( self  :    int      ,							snake_case  :    List[str]      ,							snake_case  :    Tuple=13      ,							snake_case  :    Dict=3      ,							snake_case  :    List[Any]=32      ,							snake_case  :    str=0.25      ,							snake_case  :    Dict=8      ,							snake_case  :    Tuple=True      ,							snake_case  :    List[str]=1_024      ,							snake_case  :    List[Any]=32      ,							snake_case  :    Any="relu6"      ,							snake_case  :    List[str]=0.1      ,							snake_case  :    List[str]=0.02      ,							snake_case  :    Any=True      ,							snake_case  :    Tuple=True      ,							snake_case  :    List[Any]=10      ,							snake_case  :    str=None      ,							)			->						Any:






									'''simple docstring'''

									A         =					parent
									A         =					batch_size
									A         =					num_channels
									A         =					image_size
									A         =					depth_multiplier
									A         =					min_depth
									A         =					tf_padding
									A         =					int(last_hidden_size * depth_multiplier				)
									A         =					output_stride
									A         =					hidden_act
									A         =					classifier_dropout_prob
									A         =					use_labels
									A         =					is_training
									A         =					num_labels
									A         =					initializer_range
									A         =					scope



		def 					A_		( self  :    int				)			->						Any:






									'''simple docstring'''

									A         =					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

									A         =					self.get_config()

									return config, pixel_values, labels, pixel_labels



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






									'''simple docstring'''

									return MobileNetVaConfig(
									    num_channels=self.num_channels      ,							image_size=self.image_size      ,							depth_multiplier=self.depth_multiplier      ,							min_depth=self.min_depth      ,							tf_padding=self.tf_padding      ,							hidden_act=self.hidden_act      ,							classifier_dropout_prob=self.classifier_dropout_prob      ,							initializer_range=self.initializer_range      ,							)



		def 					A_		( self  :    Optional[int]      ,							snake_case  :    Optional[int]      ,							snake_case  :    Any      ,							snake_case  :    List[Any]      ,							snake_case  :    Dict				)			->						Tuple:






									'''simple docstring'''

									A         =					MobileNetVaModel(config=_lowercase				)
									model.to(_lowercase				)
									model.eval()
									A         =					model(_lowercase				)
									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,
									    )      ,							)



		def 					A_		( self  :    Union[str, Any]      ,							snake_case  :    List[str]      ,							snake_case  :    str      ,							snake_case  :    Optional[int]      ,							snake_case  :    int				)			->						Optional[Any]:






									'''simple docstring'''

									A         =					self.num_labels
									A         =					MobileNetVaForImageClassification(_lowercase				)
									model.to(_lowercase				)
									model.eval()
									A         =					model(_lowercase      ,							labels=_lowercase				)
									self.parent.assertEqual(result.logits.shape      ,							(self.batch_size, self.num_labels)				)



		def 					A_		( self  :    Dict				)			->						Tuple:






									'''simple docstring'''

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






@require_torch
class      UpperCAmelCase__		(      UpperCAmelCase_   ,UpperCAmelCase_   ,unittest.TestCase     ):
		lowerCAmelCase_       :	Tuple 							=     (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
		lowerCAmelCase_       :	Union[str, Any] 							=     (
		    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
		    if is_torch_available()
		    else {}
		)

		lowerCAmelCase_       :	str 							=     False
		lowerCAmelCase_       :	List[str] 							=     False
		lowerCAmelCase_       :	int 							=     False
		lowerCAmelCase_       :	int 							=     False



		def 					A_		( self  :    Optional[Any]				)			->						Dict:






									'''simple docstring'''

									A         =					MobileNetVaModelTester(self				)
									A         =					MobileNetVaConfigTester(self      ,							config_class=_lowercase      ,							has_text_modality=_lowercase				)



		def 					A_		( self  :    str				)			->						Optional[Any]:






									'''simple docstring'''

									self.config_tester.run_common_tests()



		@unittest.skip(reason='MobileNetV1 does not use inputs_embeds'				)
		def 					A_		( self  :    int				)			->						Any:






									'''simple docstring'''

									pass



		@unittest.skip(reason='MobileNetV1 does not support input and output embeddings'				)
		def 					A_		( self  :    Any				)			->						Tuple:






									'''simple docstring'''

									pass



		@unittest.skip(reason='MobileNetV1 does not output attentions'				)
		def 					A_		( self  :    Any				)			->						str:






									'''simple docstring'''

									pass



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






									'''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 					A_		( self  :    Any				)			->						Any:






									'''simple docstring'''

									A         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase				)



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






									'''simple docstring'''

									def check_hidden_states_output(snake_case  :    Tuple      ,							snake_case  :    Optional[Any]      ,							snake_case  :    Tuple				):
																A         =					model_class(_lowercase				)
																model.to(_lowercase				)
																model.eval()

																with torch.no_grad():
																							A         =					model(**self._prepare_for_class(_lowercase      ,							_lowercase				)				)

																A         =					outputs.hidden_states

																A         =					26
																self.assertEqual(len(_lowercase				)      ,							_lowercase				)

									A      ,  A         =					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
																A         =					True
																check_hidden_states_output(_lowercase      ,							_lowercase      ,							_lowercase				)

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

																check_hidden_states_output(_lowercase      ,							_lowercase      ,							_lowercase				)



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






									'''simple docstring'''

									A         =					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*_lowercase				)



		@slow
		def 					A_		( self  :    Dict				)			->						str:






									'''simple docstring'''

									for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																A         =					MobileNetVaModel.from_pretrained(_lowercase				)
																self.assertIsNotNone(_lowercase				)



def   lowerCAmelCase__			(     ) ->							Optional[Any]:
							A         =					Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'			)
							return image






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



		@cached_property
		def 					A_		( self  :    Union[str, Any]				)			->						Union[str, Any]:






									'''simple docstring'''

									return (
									    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v1_1.0_224'				) if is_vision_available() else None
									)



		@slow
		def 					A_		( self  :    List[Any]				)			->						str:






									'''simple docstring'''

									A         =					MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v1_1.0_224'				).to(_lowercase				)

									A         =					self.default_image_processor
									A         =					prepare_img()
									A         =					image_processor(images=_lowercase      ,							return_tensors='pt'				).to(_lowercase				)

									# forward pass
									with torch.no_grad():
																A         =					model(**_lowercase				)

									# verify the logits
									A         =					torch.Size((1, 1_001)				)
									self.assertEqual(outputs.logits.shape      ,							_lowercase				)

									A         =					torch.tensor([-4.1739, -1.1233, 3.1205]				).to(_lowercase				)

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








"""simple docstring"""







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


A    =			logging.get_logger(__name__)

A    =			{
    'google/fnet-base': 'https://huggingface.co/google/fnet-base/resolve/main/config.json',
    'google/fnet-large': 'https://huggingface.co/google/fnet-large/resolve/main/config.json'
    # See all FNet models at https://huggingface.co/models?filter=fnet
}






class      UpperCAmelCase__		(      UpperCamelCase     ):
		lowerCAmelCase_       :	List[Any] 							=     """fnet"""



		def __init__( self  :    Tuple      ,							snake_case  :    Optional[int]=32_000      ,							snake_case  :    Optional[Any]=768      ,							snake_case  :    Union[str, Any]=12      ,							snake_case  :    List[str]=3_072      ,							snake_case  :    str="gelu_new"      ,							snake_case  :    Any=0.1      ,							snake_case  :    List[str]=512      ,							snake_case  :    Any=4      ,							snake_case  :    str=0.02      ,							snake_case  :    Union[str, Any]=1E-1_2      ,							snake_case  :    Optional[Any]=False      ,							snake_case  :    List[Any]=512      ,							snake_case  :    int=3      ,							snake_case  :    Any=1      ,							snake_case  :    Union[str, Any]=2      ,							**snake_case  :    Union[str, Any]      ,							)			->						Dict:






									'''simple docstring'''

									super().__init__(pad_token_id=snake_case      ,							bos_token_id=snake_case      ,							eos_token_id=snake_case      ,							**snake_case				)

									A         =					vocab_size
									A         =					max_position_embeddings
									A         =					hidden_size
									A         =					num_hidden_layers
									A         =					intermediate_size
									A         =					hidden_act
									A         =					hidden_dropout_prob
									A         =					initializer_range
									A         =					type_vocab_size
									A         =					layer_norm_eps
									A         =					use_tpu_fourier_optimizations
									A         =					tpu_short_seq_length



