"""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      (   lowercase						):



				def  _lowercase					(self    :				Optional[Any])   ->     Tuple:
					__snake_case      :							int						=			self.config_class(**self.inputs_dict)
					self.parent.assertTrue(hasattr(_A     ,  'tf_padding'))
					self.parent.assertTrue(hasattr(_A     ,  'depth_multiplier'))







class 		UpperCamelCase      :



				def __init__(self    :				Tuple     ,  _A    :				List[str]     ,  _A    :				Optional[int]=13     ,  _A    :				Dict=3     ,  _A    :				Union[str, Any]=32     ,  _A    :				List[Any]=0.25     ,  _A    :				List[str]=8     ,  _A    :				Any=True     ,  _A    :				List[str]=10_24     ,  _A    :				Optional[Any]=32     ,  _A    :				int="relu6"     ,  _A    :				List[Any]=0.1     ,  _A    :				Optional[Any]=0.02     ,  _A    :				Optional[Any]=True     ,  _A    :				int=True     ,  _A    :				List[str]=10     ,  _A    :				List[Any]=None     ,  )   ->     Dict:
					__snake_case      :							int						=			parent
					__snake_case      :							str						=			batch_size
					__snake_case      :							Dict						=			num_channels
					__snake_case      :							Optional[Any]						=			image_size
					__snake_case      :							Any						=			depth_multiplier
					__snake_case      :							Any						=			min_depth
					__snake_case      :							Dict						=			tf_padding
					__snake_case      :							List[str]						=			int(last_hidden_size * depth_multiplier)
					__snake_case      :							Tuple						=			output_stride
					__snake_case      :							Optional[Any]						=			hidden_act
					__snake_case      :							Any						=			classifier_dropout_prob
					__snake_case      :							Union[str, Any]						=			use_labels
					__snake_case      :							Union[str, Any]						=			is_training
					__snake_case      :							Optional[int]						=			num_labels
					__snake_case      :							Tuple						=			initializer_range
					__snake_case      :							List[Any]						=			scope



				def  _lowercase					(self    :				Tuple)   ->     Dict:
					__snake_case      :							Tuple						=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

					__snake_case      :							Dict						=			self.get_config()

					return config, pixel_values, labels, pixel_labels



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



				def  _lowercase					(self    :				int     ,  _A    :				int     ,  _A    :				List[Any]     ,  _A    :				Optional[int]     ,  _A    :				int)   ->     List[Any]:
					__snake_case      :							Optional[Any]						=			self.num_labels
					__snake_case      :							int						=			MobileNetVaForImageClassification(_A)
					model.to(_A)
					model.eval()
					__snake_case      :							Any						=			model(_A     ,  labels=_A)
					self.parent.assertEqual(result.logits.shape     ,  (self.batch_size, self.num_labels))






				def  _lowercase					(self    :				Union[str, Any])   ->     Optional[int]:
					__snake_case      :							Any						=			self.prepare_config_and_inputs()
					__snake_case		,    __snake_case		,    __snake_case		,    __snake_case      :							Dict						=			config_and_inputs
					__snake_case      :							Any						=			{'pixel_values': pixel_values}
					return config, inputs_dict







@require_torch
class 		UpperCamelCase      (   lowercase				,     lowercase				,     unittest.TestCase						):
				UpperCAmelCase    :				Optional[Any]   =		(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
				UpperCAmelCase    :				str   =		(
				    {"""feature-extraction""": MobileNetVaModel, """image-classification""": MobileNetVaForImageClassification}
				    if is_torch_available()
				    else {}
				)

				UpperCAmelCase    :				Dict   =		False
				UpperCAmelCase    :				str   =		False
				UpperCAmelCase    :				str   =		False
				UpperCAmelCase    :				Union[str, Any]   =		False



				def  _lowercase					(self    :				Tuple)   ->     List[Any]:
					__snake_case      :							Optional[int]						=			MobileNetVaModelTester(self)
					__snake_case      :							str						=			MobileNetVaConfigTester(self     ,  config_class=_A     ,  has_text_modality=_A)



				def  _lowercase					(self    :				List[Any])   ->     int:
					self.config_tester.run_common_tests()



				@unittest.skip(reason='MobileNetV1 does not use inputs_embeds')
				def  _lowercase					(self    :				Optional[Any])   ->     Tuple:
					pass



				@unittest.skip(reason='MobileNetV1 does not support input and output embeddings')
				def  _lowercase					(self    :				List[Any])   ->     Union[str, Any]:
					pass



				@unittest.skip(reason='MobileNetV1 does not output attentions')
				def  _lowercase					(self    :				Tuple)   ->     Union[str, Any]:
					pass



				def  _lowercase					(self    :				Optional[int])   ->     Tuple:
					__snake_case		,    __snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs_for_common()

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

						__snake_case      :							Any						=			['pixel_values']
						self.assertListEqual(arg_names[:1]     ,  _A)



				def  _lowercase					(self    :				int)   ->     Tuple:
					__snake_case      :							Tuple						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_model(*_A)



				def  _lowercase					(self    :				Any)   ->     Union[str, Any]:
					def check_hidden_states_output(_A    :				List[Any]     ,  _A    :				Tuple     ,  _A    :				Dict):
						__snake_case      :							Any						=			model_class(_A)
						model.to(_A)
						model.eval()

						with torch.no_grad():
							__snake_case      :							Dict						=			model(**self._prepare_for_class(_A     ,  _A))

						__snake_case      :							Dict						=			outputs.hidden_states

						__snake_case      :							str						=			26
						self.assertEqual(len(_A)     ,  _A)

					__snake_case		,    __snake_case      :							str						=			self.model_tester.prepare_config_and_inputs_for_common()

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

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

						check_hidden_states_output(_A     ,  _A     ,  _A)



				def  _lowercase					(self    :				str)   ->     List[Any]:
					__snake_case      :							Dict						=			self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_for_image_classification(*_A)






				@slow
				def  _lowercase					(self    :				Union[str, Any])   ->     int:
					for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__snake_case      :							Tuple						=			MobileNetVaModel.from_pretrained(_A)
						self.assertIsNotNone(_A)







def   __UpperCAmelCase    (   )							->      List[str]:

	'''simple docstring'''





	__snake_case      :							Optional[int]						=			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
	return image

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



				@cached_property
				def  _lowercase					(self    :				int)   ->     str:
					return (
					    MobileNetVaImageProcessor.from_pretrained('google/mobilenet_v1_1.0_224') if is_vision_available() else None
					)






				@slow
				def  _lowercase					(self    :				Dict)   ->     Union[str, Any]:
					__snake_case      :							str						=			MobileNetVaForImageClassification.from_pretrained('google/mobilenet_v1_1.0_224').to(_A)

					__snake_case      :							Optional[Any]						=			self.default_image_processor
					__snake_case      :							str						=			prepare_img()
					__snake_case      :							Optional[int]						=			image_processor(images=_A     ,  return_tensors='pt').to(_A)

					# forward pass
					with torch.no_grad():
						__snake_case      :							Union[str, Any]						=			model(**_A)

					# verify the logits
					__snake_case      :							Optional[int]						=			torch.Size((1, 10_01))
					self.assertEqual(outputs.logits.shape     ,  _A)

					__snake_case      :							List[Any]						=			torch.tensor([-4.1_739, -1.1_233, 3.1_205]).to(_A)

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






"""simple docstring"""







import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_funnel import FunnelTokenizer


_a					:					Any=			logging.get_logger(__name__)

_a					:					str=			{"vocab_file": "vocab.txt", "tokenizer_file": "tokenizer.json"}

_a					:					Optional[Any]=			[
    "small",
    "small-base",
    "medium",
    "medium-base",
    "intermediate",
    "intermediate-base",
    "large",
    "large-base",
    "xlarge",
    "xlarge-base",
]

_a					:					List[Any]=			{
    "vocab_file": {
        "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/vocab.txt",
        "funnel-transformer/small-base": "https://huggingface.co/funnel-transformer/small-base/resolve/main/vocab.txt",
        "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/vocab.txt",
        "funnel-transformer/medium-base": (
            "https://huggingface.co/funnel-transformer/medium-base/resolve/main/vocab.txt"
        ),
        "funnel-transformer/intermediate": (
            "https://huggingface.co/funnel-transformer/intermediate/resolve/main/vocab.txt"
        ),
        "funnel-transformer/intermediate-base": (
            "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/vocab.txt"
        ),
        "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/vocab.txt",
        "funnel-transformer/large-base": "https://huggingface.co/funnel-transformer/large-base/resolve/main/vocab.txt",
        "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/vocab.txt",
        "funnel-transformer/xlarge-base": (
            "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/vocab.txt"
        ),
    },
    "tokenizer_file": {
        "funnel-transformer/small": "https://huggingface.co/funnel-transformer/small/resolve/main/tokenizer.json",
        "funnel-transformer/small-base": (
            "https://huggingface.co/funnel-transformer/small-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/medium": "https://huggingface.co/funnel-transformer/medium/resolve/main/tokenizer.json",
        "funnel-transformer/medium-base": (
            "https://huggingface.co/funnel-transformer/medium-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/intermediate": (
            "https://huggingface.co/funnel-transformer/intermediate/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/intermediate-base": (
            "https://huggingface.co/funnel-transformer/intermediate-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/large": "https://huggingface.co/funnel-transformer/large/resolve/main/tokenizer.json",
        "funnel-transformer/large-base": (
            "https://huggingface.co/funnel-transformer/large-base/resolve/main/tokenizer.json"
        ),
        "funnel-transformer/xlarge": "https://huggingface.co/funnel-transformer/xlarge/resolve/main/tokenizer.json",
        "funnel-transformer/xlarge-base": (
            "https://huggingface.co/funnel-transformer/xlarge-base/resolve/main/tokenizer.json"
        ),
    },
}
_a					:					str=			{f'''funnel-transformer/{name}''': 512 for name in _model_names}
_a					:					List[Any]=			{f'''funnel-transformer/{name}''': {"do_lower_case": True} for name in _model_names}

class 		UpperCamelCase      (   lowercase						):
				UpperCAmelCase    :				List[str]   =		VOCAB_FILES_NAMES
				UpperCAmelCase    :				Tuple   =		PRETRAINED_VOCAB_FILES_MAP
				UpperCAmelCase    :				int   =		PRETRAINED_INIT_CONFIGURATION
				UpperCAmelCase    :				Tuple   =		FunnelTokenizer
				UpperCAmelCase    :				Tuple   =		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
				UpperCAmelCase    :				int   =		2



				def __init__(self    :				int     ,  _A    :				Any=None     ,  _A    :				Union[str, Any]=None     ,  _A    :				Union[str, Any]=True     ,  _A    :				List[str]="<unk>"     ,  _A    :				Any="<sep>"     ,  _A    :				Dict="<pad>"     ,  _A    :				Tuple="<cls>"     ,  _A    :				Dict="<mask>"     ,  _A    :				Optional[Any]="<s>"     ,  _A    :				List[Any]="</s>"     ,  _A    :				Optional[int]=True     ,  _A    :				Dict=True     ,  _A    :				Tuple=None     ,  _A    :				int="##"     ,  **_A    :				Any     ,  )   ->     str:
					super().__init__(
					    _A     ,  tokenizer_file=_A     ,  do_lower_case=_A     ,  unk_token=_A     ,  sep_token=_A     ,  pad_token=_A     ,  cls_token=_A     ,  mask_token=_A     ,  bos_token=_A     ,  eos_token=_A     ,  clean_text=_A     ,  tokenize_chinese_chars=_A     ,  strip_accents=_A     ,  wordpieces_prefix=_A     ,  **_A     ,  )

					__snake_case      :							Optional[int]						=			json.loads(self.backend_tokenizer.normalizer.__getstate__())
					if (
					    normalizer_state.get('lowercase'     ,  _A) != do_lower_case
					    or normalizer_state.get('strip_accents'     ,  _A) != strip_accents
					    or normalizer_state.get('handle_chinese_chars'     ,  _A) != tokenize_chinese_chars
					):
						__snake_case      :							List[str]						=			getattr(_A     ,  normalizer_state.pop('type'))
						__snake_case      :							int						=			do_lower_case
						__snake_case      :							Optional[int]						=			strip_accents
						__snake_case      :							str						=			tokenize_chinese_chars
						__snake_case      :							Optional[int]						=			normalizer_class(**_A)

					__snake_case      :							str						=			do_lower_case



				def  _lowercase					(self    :				Optional[Any]     ,  _A    :				Dict     ,  _A    :				Tuple=None)   ->     Any:
					__snake_case      :							List[Any]						=			[self.cls_token_id] + token_ids_a + [self.sep_token_id]

					if token_ids_a:
						output += token_ids_a + [self.sep_token_id]

					return output



				def  _lowercase					(self    :				str     ,  _A    :				List[int]     ,  _A    :				Optional[List[int]] = None)   ->     List[int]:
					__snake_case      :							Union[str, Any]						=			[self.sep_token_id]
					__snake_case      :							Union[str, Any]						=			[self.cls_token_id]
					if token_ids_a is None:
						return len(cls) * [self.cls_token_type_id] + len(token_ids_a + sep) * [0]
					return len(cls) * [self.cls_token_type_id] + len(token_ids_a + sep) * [0] + len(token_ids_a + sep) * [1]






				def  _lowercase					(self    :				Tuple     ,  _A    :				str     ,  _A    :				Optional[str] = None)   ->     Tuple[str]:
					__snake_case      :							int						=			self._tokenizer.model.save(_A     ,  name=_A)
					return tuple(_A)





