






'''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 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.config_class(**self.inputs_dict       )
								self.parent.assertTrue(hasattr(_lowerCAmelCase   ,"""tf_padding"""       )       )
								self.parent.assertTrue(hasattr(_lowerCAmelCase   ,"""depth_multiplier"""       )       )




class 		UpperCamelCase__    :




		'''simple docstring'''





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






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

								lowerCamelCase__         =  self.get_config()

								return config, pixel_values, labels, pixel_labels






		def 			UpperCamelCase_					(      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 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  MobileNetVaModel(config=_lowerCAmelCase       )
								model.to(_lowerCAmelCase       )
								model.eval()
								lowerCamelCase__         =  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 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  self.num_labels
								lowerCamelCase__         =  MobileNetVaForImageClassification(_lowerCAmelCase       )
								model.to(_lowerCAmelCase       )
								model.eval()
								lowerCamelCase__         =  model(_lowerCAmelCase   ,labels=_lowerCAmelCase       )
								self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
								lowerCamelCase__         =  self.num_labels
								lowerCamelCase__         =  MobileNetVaForSemanticSegmentation(_lowerCAmelCase       )
								model.to(_lowerCAmelCase       )
								model.eval()
								lowerCamelCase__         =  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,
								    )   ,)
								lowerCamelCase__         =  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 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.prepare_config_and_inputs()
								lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__       ,			lowerCamelCase__         =  config_and_inputs
								lowerCamelCase__         =  {"""pixel_values""": pixel_values}
								return config, inputs_dict




@require_torch
class 		UpperCamelCase__    (a   ,a   ,unittest.TestCase			):




		'''simple docstring'''





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

		_UpperCamelCase				  =					False
		_UpperCamelCase				  =					False
		_UpperCamelCase				  =					False
		_UpperCamelCase				  =					False






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  MobileNetVaModelTester(self       )
								lowerCamelCase__         =  MobileNetVaConfigTester(self   ,config_class=_lowerCAmelCase   ,has_text_modality=_lowerCAmelCase       )






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






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






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






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






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__       ,			lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs_for_common()

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

														lowerCamelCase__         =  ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								def check_hidden_states_output(_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       ):
														lowerCamelCase__         =  model_class(_lowerCAmelCase       )
														model.to(_lowerCAmelCase       )
														model.eval()

														with torch.no_grad():
																				lowerCamelCase__         =  model(**self._prepare_for_class(_lowerCAmelCase   ,_lowerCAmelCase       )       )

														lowerCamelCase__         =  outputs.hidden_states

														lowerCamelCase__         =  16
														self.assertEqual(len(_lowerCAmelCase       )   ,_lowerCAmelCase       )

								lowerCamelCase__       ,			lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
														lowerCamelCase__         =  True
														check_hidden_states_output(_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       )

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

														check_hidden_states_output(_lowerCAmelCase   ,_lowerCAmelCase   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_semantic_segmentation(*_lowerCAmelCase       )






		@slow
		def 			UpperCamelCase_					(      self       ):
								for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCamelCase__         =  MobileNetVaModel.from_pretrained(_lowerCAmelCase       )
														self.assertIsNotNone(_lowerCAmelCase       )



def 						A__			(				):
						lowerCamelCase__         =  Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""						)
						return image





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




		'''simple docstring'''





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






		@slow
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  MobileNetVaForImageClassification.from_pretrained("""google/mobilenet_v2_1.0_224"""       ).to(_lowerCAmelCase       )

								lowerCamelCase__         =  self.default_image_processor
								lowerCamelCase__         =  prepare_img()
								lowerCamelCase__         =  image_processor(images=_lowerCAmelCase   ,return_tensors="""pt"""       ).to(_lowerCAmelCase       )

								# forward pass
								with torch.no_grad():
														lowerCamelCase__         =  model(**_lowerCAmelCase       )

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

								lowerCamelCase__         =  torch.tensor([0.2445, -1.1993, 0.1905]       ).to(_lowerCAmelCase       )

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






		@slow
		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  MobileNetVaForSemanticSegmentation.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""       )
								lowerCamelCase__         =  model.to(_lowerCAmelCase       )

								lowerCamelCase__         =  MobileNetVaImageProcessor.from_pretrained("""google/deeplabv3_mobilenet_v2_1.0_513"""       )

								lowerCamelCase__         =  prepare_img()
								lowerCamelCase__         =  image_processor(images=_lowerCAmelCase   ,return_tensors="""pt"""       ).to(_lowerCAmelCase       )

								# forward pass
								with torch.no_grad():
														lowerCamelCase__         =  model(**_lowerCAmelCase       )
								lowerCamelCase__         =  outputs.logits

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

								lowerCamelCase__         =  torch.tensor(
								    [
								        [[17.5790, 17.7581, 18.3355], [18.3257, 18.4230, 18.8973], [18.6169, 18.8650, 19.2187]],
								        [[-2.1595, -2.0977, -2.3741], [-2.4226, -2.3028, -2.6835], [-2.7819, -2.5991, -2.7706]],
								        [[4.2058, 4.8317, 4.7638], [4.4136, 5.0361, 4.9383], [4.5028, 4.9644, 4.8734]],
								    ]   ,device=_lowerCAmelCase   ,)

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









'''simple docstring'''







import json
from typing import Iterator, List, Union

from tokenizers import AddedToken, Regex, Tokenizer, decoders, normalizers, pre_tokenizers, trainers
from tokenizers.implementations.base_tokenizer import BaseTokenizer
from tokenizers.models import Unigram
from tokenizers.processors import TemplateProcessing




class 		UpperCamelCase__    (a			):




		'''simple docstring'''





		def __init__(      self   ,_lowerCAmelCase = "▁"   ,_lowerCAmelCase = True   ,_lowerCAmelCase = "<unk>"   ,_lowerCAmelCase = "</s>"   ,_lowerCAmelCase = "<pad>"   ,):
								lowerCamelCase__         =  {
								    """pad""": {"""id""": 0, """token""": pad_token},
								    """eos""": {"""id""": 1, """token""": eos_token},
								    """unk""": {"""id""": 2, """token""": unk_token},
								}

								lowerCamelCase__         =  [None] * len(self.special_tokens       )
								for token_dict in self.special_tokens.values():
														lowerCamelCase__         =  token_dict["""token"""]

								lowerCamelCase__         =  Tokenizer(Unigram()       )

								lowerCamelCase__         =  normalizers.Sequence(
								    [
								        normalizers.Nmt(),
								        normalizers.NFKC(),
								        normalizers.Replace(Regex(""" {2,}"""       )   ,""" """       ),
								        normalizers.Lowercase(),
								    ]       )
								lowerCamelCase__         =  pre_tokenizers.Sequence(
								    [
								        pre_tokenizers.Metaspace(replacement=_lowerCAmelCase   ,add_prefix_space=_lowerCAmelCase       ),
								        pre_tokenizers.Digits(individual_digits=_lowerCAmelCase       ),
								        pre_tokenizers.Punctuation(),
								    ]       )
								lowerCamelCase__         =  decoders.Metaspace(replacement=_lowerCAmelCase   ,add_prefix_space=_lowerCAmelCase       )

								lowerCamelCase__         =  TemplateProcessing(
								    single=F'''$A {self.special_tokens["eos"]["token"]}'''   ,special_tokens=[(self.special_tokens["""eos"""]["""token"""], self.special_tokens["""eos"""]["""id"""])]   ,)

								lowerCamelCase__         =  {
								    """model""": """SentencePieceUnigram""",
								    """replacement""": replacement,
								    """add_prefix_space""": add_prefix_space,
								}

								super().__init__(_lowerCAmelCase   ,_lowerCAmelCase       )






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = 80_00   ,_lowerCAmelCase = True   ,):
								lowerCamelCase__         =  trainers.UnigramTrainer(
								    vocab_size=_lowerCAmelCase   ,special_tokens=self.special_tokens_list   ,show_progress=_lowerCAmelCase   ,)

								if isinstance(_lowerCAmelCase   ,_lowerCAmelCase       ):
														lowerCamelCase__         =  [files]
								self._tokenizer.train(_lowerCAmelCase   ,trainer=_lowerCAmelCase       )

								self.add_unk_id()






		def 			UpperCamelCase_					(      self   ,_lowerCAmelCase   ,_lowerCAmelCase = 80_00   ,_lowerCAmelCase = True   ,):
								lowerCamelCase__         =  trainers.UnigramTrainer(
								    vocab_size=_lowerCAmelCase   ,special_tokens=self.special_tokens_list   ,show_progress=_lowerCAmelCase   ,)

								self._tokenizer.train_from_iterator(_lowerCAmelCase   ,trainer=_lowerCAmelCase       )

								self.add_unk_id()






		def 			UpperCamelCase_					(      self       ):
								lowerCamelCase__         =  json.loads(self._tokenizer.to_str()       )

								lowerCamelCase__         =  self.special_tokens["""unk"""]["""id"""]

								lowerCamelCase__         =  Tokenizer.from_str(json.dumps(_lowerCAmelCase       )       )

