




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  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				def     _A							(							self:   List[Any]		):
											SCREAMING_SNAKE_CASE_   	=    self.config_class(**self.inputs_dict		)
											self.parent.assertTrue(hasattr(_lowerCamelCase		,					'''tf_padding'''		)		)
											self.parent.assertTrue(hasattr(_lowerCamelCase		,					'''depth_multiplier'''		)		)






class  __magic_name__						:
				'''simple docstring'''







				def __init__(							self:   int		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   List[str]=13		,					_lowerCamelCase:   List[str]=3		,					_lowerCamelCase:   Tuple=32		,					_lowerCamelCase:   Tuple=0.25		,					_lowerCamelCase:   int=8		,					_lowerCamelCase:   Optional[Any]=8		,					_lowerCamelCase:   Optional[Any]=6		,					_lowerCamelCase:   Tuple=32		,					_lowerCamelCase:   Union[str, Any]=True		,					_lowerCamelCase:   List[str]=True		,					_lowerCamelCase:   List[Any]=True		,					_lowerCamelCase:   List[Any]="relu6"		,					_lowerCamelCase:   str=12_80		,					_lowerCamelCase:   Optional[int]=0.1		,					_lowerCamelCase:   Tuple=0.02		,					_lowerCamelCase:   List[Any]=True		,					_lowerCamelCase:   List[Any]=True		,					_lowerCamelCase:   int=10		,					_lowerCamelCase:   Any=None		,					):
											SCREAMING_SNAKE_CASE_   	=    parent
											SCREAMING_SNAKE_CASE_   	=    batch_size
											SCREAMING_SNAKE_CASE_   	=    num_channels
											SCREAMING_SNAKE_CASE_   	=    image_size
											SCREAMING_SNAKE_CASE_   	=    depth_multiplier
											SCREAMING_SNAKE_CASE_   	=    depth_divisible_by
											SCREAMING_SNAKE_CASE_   	=    min_depth
											SCREAMING_SNAKE_CASE_   	=    expand_ratio
											SCREAMING_SNAKE_CASE_   	=    tf_padding
											SCREAMING_SNAKE_CASE_   	=    output_stride
											SCREAMING_SNAKE_CASE_   	=    first_layer_is_expansion
											SCREAMING_SNAKE_CASE_   	=    finegrained_output
											SCREAMING_SNAKE_CASE_   	=    hidden_act
											SCREAMING_SNAKE_CASE_   	=    last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier		)
											SCREAMING_SNAKE_CASE_   	=    classifier_dropout_prob
											SCREAMING_SNAKE_CASE_   	=    use_labels
											SCREAMING_SNAKE_CASE_   	=    is_training
											SCREAMING_SNAKE_CASE_   	=    num_labels
											SCREAMING_SNAKE_CASE_   	=    initializer_range
											SCREAMING_SNAKE_CASE_   	=    scope


				def     _A							(							self:   Any		):
											SCREAMING_SNAKE_CASE_   	=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

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

											SCREAMING_SNAKE_CASE_   	=    self.get_config()

											return config, pixel_values, labels, pixel_labels


				def     _A							(							self:   int		):
											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     _A							(							self:   Optional[int]		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   str		,					_lowerCamelCase:   Dict		,					_lowerCamelCase:   List[str]		):
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaModel(config=_lowerCamelCase		)
											model.to(_lowerCamelCase		)
											model.eval()
											SCREAMING_SNAKE_CASE_   	=    model(_lowerCamelCase		)
											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     _A							(							self:   int		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Dict		):
											SCREAMING_SNAKE_CASE_   	=    self.num_labels
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaForImageClassification(_lowerCamelCase		)
											model.to(_lowerCamelCase		)
											model.eval()
											SCREAMING_SNAKE_CASE_   	=    model(_lowerCamelCase		,					labels=_lowerCamelCase		)
											self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels)		)


				def     _A							(							self:   Tuple		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   int		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   Optional[int]		):
											SCREAMING_SNAKE_CASE_   	=    self.num_labels
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaForSemanticSegmentation(_lowerCamelCase		)
											model.to(_lowerCamelCase		)
											model.eval()
											SCREAMING_SNAKE_CASE_   	=    model(_lowerCamelCase		)
											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_   	=    model(_lowerCamelCase		,					labels=_lowerCamelCase		)
											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     _A							(							self:   Dict		):
											SCREAMING_SNAKE_CASE_   	=    self.prepare_config_and_inputs()
											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    config_and_inputs
											SCREAMING_SNAKE_CASE_   	=    {'''pixel_values''': pixel_values}
											return config, inputs_dict






@require_torch
class  __magic_name__						(  __UpperCAmelCase    ,  __UpperCAmelCase    ,  unittest.TestCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Union[str, Any]	      =					(
				    (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE__					:							Tuple	      =					(
				    {
				        "feature-extraction": MobileNetVaModel,
				        "image-classification": MobileNetVaForImageClassification,
				        "image-segmentation": MobileNetVaForSemanticSegmentation,
				    }
				    if is_torch_available()
				    else {}
				)

				SCREAMING_SNAKE_CASE__					:							Tuple	      =					False
				SCREAMING_SNAKE_CASE__					:							Dict	      =					False
				SCREAMING_SNAKE_CASE__					:							int	      =					False
				SCREAMING_SNAKE_CASE__					:							List[str]	      =					False


				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaModelTester(self		)
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaConfigTester(self		,					config_class=_lowerCamelCase		,					has_text_modality=_lowerCamelCase		)


				def     _A							(							self:   Optional[Any]		):
											self.config_tester.run_common_tests()


				@unittest.skip(reason='''MobileNetV2 does not use inputs_embeds'''		)
				def     _A							(							self:   Union[str, Any]		):
											pass


				@unittest.skip(reason='''MobileNetV2 does not support input and output embeddings'''		)
				def     _A							(							self:   List[str]		):
											pass


				@unittest.skip(reason='''MobileNetV2 does not output attentions'''		)
				def     _A							(							self:   Optional[int]		):
											pass


				def     _A							(							self:   Dict		):
											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs_for_common()

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

																		SCREAMING_SNAKE_CASE_   	=    ['''pixel_values''']
																		self.assertListEqual(arg_names[:1]		,					_lowerCamelCase		)


				def     _A							(							self:   Tuple		):
											SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_lowerCamelCase		)


				def     _A							(							self:   Tuple		):
											def check_hidden_states_output(_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Optional[Any]		):
																		SCREAMING_SNAKE_CASE_   	=    model_class(_lowerCamelCase		)
																		model.to(_lowerCamelCase		)
																		model.eval()

																		with torch.no_grad():
																									SCREAMING_SNAKE_CASE_   	=    model(**self._prepare_for_class(_lowerCamelCase		,					_lowerCamelCase		)		)

																		SCREAMING_SNAKE_CASE_   	=    outputs.hidden_states

																		SCREAMING_SNAKE_CASE_   	=    16
																		self.assertEqual(len(_lowerCamelCase		)		,					_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
																		SCREAMING_SNAKE_CASE_   	=    True
																		check_hidden_states_output(_lowerCamelCase		,					_lowerCamelCase		,					_lowerCamelCase		)

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

																		check_hidden_states_output(_lowerCamelCase		,					_lowerCamelCase		,					_lowerCamelCase		)


				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase		)


				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCamelCase		)





				@slow
				def     _A							(							self:   List[Any]		):
											for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		SCREAMING_SNAKE_CASE_   	=    MobileNetVaModel.from_pretrained(_lowerCamelCase		)
																		self.assertIsNotNone(_lowerCamelCase		)






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






@require_torch
@require_vision
class  __magic_name__						(  unittest.TestCase):
				'''simple docstring'''







				@cached_property
				def     _A							(							self:   Union[str, Any]		):
											return (
											    MobileNetVaImageProcessor.from_pretrained('''google/mobilenet_v2_1.0_224'''		) if is_vision_available() else None
											)


				@slow
				def     _A							(							self:   Optional[int]		):
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaForImageClassification.from_pretrained('''google/mobilenet_v2_1.0_224'''		).to(_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    self.default_image_processor
											SCREAMING_SNAKE_CASE_   	=    prepare_img()
											SCREAMING_SNAKE_CASE_   	=    image_processor(images=_lowerCamelCase		,					return_tensors='''pt'''		).to(_lowerCamelCase		)

											# forward pass
											with torch.no_grad():
																		SCREAMING_SNAKE_CASE_   	=    model(**_lowerCamelCase		)

											# verify the logits
											SCREAMING_SNAKE_CASE_   	=    torch.Size((1, 10_01)		)
											self.assertEqual(outputs.logits.shape		,					_lowerCamelCase		)

											SCREAMING_SNAKE_CASE_   	=    torch.tensor([0.24_45, -1.19_93, 0.19_05]		).to(_lowerCamelCase		)

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





				@slow
				def     _A							(							self:   str		):
											SCREAMING_SNAKE_CASE_   	=    MobileNetVaForSemanticSegmentation.from_pretrained('''google/deeplabv3_mobilenet_v2_1.0_513'''		)
											SCREAMING_SNAKE_CASE_   	=    model.to(_lowerCamelCase		)

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

											SCREAMING_SNAKE_CASE_   	=    prepare_img()
											SCREAMING_SNAKE_CASE_   	=    image_processor(images=_lowerCamelCase		,					return_tensors='''pt'''		).to(_lowerCamelCase		)

											# forward pass
											with torch.no_grad():
																		SCREAMING_SNAKE_CASE_   	=    model(**_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    outputs.logits

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

											SCREAMING_SNAKE_CASE_   	=    torch.tensor(
											    [
											        [[17.57_90, 17.75_81, 18.33_55], [18.32_57, 18.42_30, 18.89_73], [18.61_69, 18.86_50, 19.21_87]],
											        [[-2.15_95, -2.09_77, -2.37_41], [-2.42_26, -2.30_28, -2.68_35], [-2.78_19, -2.59_91, -2.77_06]],
											        [[4.20_58, 4.83_17, 4.76_38], [4.41_36, 5.03_61, 4.93_83], [4.50_28, 4.96_44, 4.87_34]],
											    ]		,					device=_lowerCamelCase		,					)

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








import os
from shutil import copyfile
from typing import Any, Dict, List, Optional, Tuple

import sentencepiece as spm

from ...tokenization_utils import PreTrainedTokenizer
from ...utils import logging


__SCREAMING_SNAKE_CASE 						=logging.get_logger(__name__)


__SCREAMING_SNAKE_CASE 						="""▁"""

__SCREAMING_SNAKE_CASE 						={"""vocab_file""": """spiece.model"""}

__SCREAMING_SNAKE_CASE 						={
    """vocab_file""": {
        """google/reformer-crime-and-punishment""": (
            """https://huggingface.co/google/reformer-crime-and-punishment/resolve/main/spiece.model"""
        )
    }
}

__SCREAMING_SNAKE_CASE 						={
    """google/reformer-crime-and-punishment""": 524_288,
}






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Any	      =					VOCAB_FILES_NAMES
				SCREAMING_SNAKE_CASE__					:							Union[str, Any]	      =					PRETRAINED_VOCAB_FILES_MAP
				SCREAMING_SNAKE_CASE__					:							Optional[int]	      =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				SCREAMING_SNAKE_CASE__					:							Union[str, Any]	      =					["input_ids", "attention_mask"]


				def __init__(							self:   List[Any]		,					_lowerCamelCase:   List[Any]		,					_lowerCamelCase:   List[Any]="</s>"		,					_lowerCamelCase:   str="<unk>"		,					_lowerCamelCase:   Union[str, Any]=[]		,					_lowerCamelCase:   Optional[Dict[str, Any]] = None		,					**_lowerCamelCase:   List[str]		,					):
											SCREAMING_SNAKE_CASE_   	=    {} if sp_model_kwargs is None else sp_model_kwargs

											super().__init__(
											    eos_token=_lowerCamelCase		,					unk_token=_lowerCamelCase		,					additional_special_tokens=_lowerCamelCase		,					sp_model_kwargs=self.sp_model_kwargs		,					**_lowerCamelCase		,					)

											SCREAMING_SNAKE_CASE_   	=    vocab_file
											SCREAMING_SNAKE_CASE_   	=    spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(_lowerCamelCase		)


				@property
				def     _A							(							self:   Optional[int]		):
											return self.sp_model.get_piece_size()


				def     _A							(							self:   int		):
											SCREAMING_SNAKE_CASE_   	=    {self.convert_ids_to_tokens(_lowerCamelCase		): i for i in range(self.vocab_size		)}
											vocab.update(self.added_tokens_encoder		)
											return vocab


				def __getstate__(							self:   Tuple		):
											SCREAMING_SNAKE_CASE_   	=    self.__dict__.copy()
											SCREAMING_SNAKE_CASE_   	=    None
											return state


				def __setstate__(							self:   Dict		,					_lowerCamelCase:   Union[str, Any]		):
											SCREAMING_SNAKE_CASE_   	=    d

											# for backward compatibility
											if not hasattr(self		,					'''sp_model_kwargs'''		):
																		SCREAMING_SNAKE_CASE_   	=    {}

											SCREAMING_SNAKE_CASE_   	=    spm.SentencePieceProcessor(**self.sp_model_kwargs		)
											self.sp_model.Load(self.vocab_file		)


				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   str		):
											return self.sp_model.encode(_lowerCamelCase		,					out_type=_lowerCamelCase		)


				def     _A							(							self:   int		,					_lowerCamelCase:   Optional[int]		):
											return self.sp_model.piece_to_id(_lowerCamelCase		)


				def     _A							(							self:   Union[str, Any]		,					_lowerCamelCase:   Tuple		):
											if index < self.sp_model.get_piece_size():
																		SCREAMING_SNAKE_CASE_   	=    self.sp_model.IdToPiece(_lowerCamelCase		)
											return token


				def     _A							(							self:   List[str]		,					_lowerCamelCase:   int		):
											SCREAMING_SNAKE_CASE_   	=    []
											SCREAMING_SNAKE_CASE_   	=    ''''''
											for token in tokens:
																		# make sure that special tokens are not decoded using sentencepiece model
																		if token in self.all_special_tokens:
																									out_string += self.sp_model.decode(_lowerCamelCase		) + token
																									SCREAMING_SNAKE_CASE_   	=    []
																		else:
																									current_sub_tokens.append(_lowerCamelCase		)
											out_string += self.sp_model.decode(_lowerCamelCase		)
											return out_string.strip()





				def     _A							(							self:   Optional[int]		,					_lowerCamelCase:   str		,					_lowerCamelCase:   Optional[str] = None		):
											if not os.path.isdir(_lowerCamelCase		):
																		logger.error(f"Vocabulary path ({save_directory}) should be a directory"		)
																		return
											SCREAMING_SNAKE_CASE_   	=    os.path.join(
											    _lowerCamelCase		,					(filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']		)

											if os.path.abspath(self.vocab_file		) != os.path.abspath(_lowerCamelCase		) and os.path.isfile(self.vocab_file		):
																		copyfile(self.vocab_file		,					_lowerCamelCase		)
											elif not os.path.isfile(self.vocab_file		):
																		with open(_lowerCamelCase		,					'''wb'''		) as fi:
																									SCREAMING_SNAKE_CASE_   	=    self.sp_model.serialized_model_proto()
																									fi.write(_lowerCamelCase		)

											return (out_vocab_file,)


