




'''simple docstring'''

import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, 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 EfficientNetForImageClassification, EfficientNetModel
						from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
						from PIL import Image

						from transformers import AutoImageProcessor




class 		SCREAMING_SNAKE_CASE_				:



			"""simple docstring"""






			def __init__(       self							, snake_case_							, snake_case_=13							, snake_case_=32							, snake_case_=3							, snake_case_=[3, 3, 5]							, snake_case_=[32, 16, 24]							, snake_case_=[16, 24, 40]							, snake_case_=[1, 1, 2]							, snake_case_=[1, 1, 2]							, snake_case_=[1, 6, 6]							, snake_case_=True							, snake_case_=True							, snake_case_=37							, snake_case_="gelu"							, snake_case_=10							, ):
									snake_case   :					Dict							=						parent
									snake_case   :					Optional[Any]							=						batch_size
									snake_case   :					Any							=						image_size
									snake_case   :					Optional[int]							=						num_channels
									snake_case   :					Optional[Any]							=						kernel_sizes
									snake_case   :					Optional[Any]							=						in_channels
									snake_case   :					List[str]							=						out_channels
									snake_case   :					Any							=						strides
									snake_case   :					List[str]							=						num_block_repeats
									snake_case   :					Optional[Any]							=						expand_ratios
									snake_case   :					str							=						is_training
									snake_case   :					List[Any]							=						hidden_act
									snake_case   :					Optional[Any]							=						num_labels
									snake_case   :					str							=						use_labels



			def      lowerCamelCase   (       self				):
									snake_case   :					Tuple							=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

									snake_case   :					Dict							=						None
									if self.use_labels:
															snake_case   :					List[Any]							=						ids_tensor([self.batch_size]							, self.num_labels				)

									snake_case   :					Any							=						self.get_config()
									return config, pixel_values, labels



			def      lowerCamelCase   (       self				):
									return EfficientNetConfig(
									    num_channels=self.num_channels							, kernel_sizes=self.kernel_sizes							, in_channels=self.in_channels							, out_channels=self.out_channels							, strides=self.strides							, num_block_repeats=self.num_block_repeats							, expand_ratios=self.expand_ratios							, hidden_act=self.hidden_act							, num_labels=self.num_labels							, )



			def      lowerCamelCase   (       self							, snake_case_							, snake_case_							, snake_case_				):
									snake_case   :					Union[str, Any]							=						EfficientNetModel(config=snake_case_				)
									model.to(snake_case_				)
									model.eval()
									snake_case   :					Dict							=						model(snake_case_				)
									# expected last hidden states: B, C, H // 4, W // 4
									self.parent.assertEqual(
									    result.last_hidden_state.shape							, (self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)							, )



			def      lowerCamelCase   (       self							, snake_case_							, snake_case_							, snake_case_				):
									snake_case   :					List[str]							=						EfficientNetForImageClassification(snake_case_				)
									model.to(snake_case_				)
									model.eval()
									snake_case   :					List[Any]							=						model(snake_case_							, labels=snake_case_				)
									self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)				)






			def      lowerCamelCase   (       self				):
									snake_case   :					str							=						self.prepare_config_and_inputs()
									snake_case   :					Tuple							=						config_and_inputs
									snake_case   :					Dict							=						{"pixel_values": pixel_values}
									return config, inputs_dict







@require_torch
class 		SCREAMING_SNAKE_CASE_				(_UpperCAmelCase       ,							_UpperCAmelCase       ,							unittest.TestCase			):



			"""simple docstring"""






			__magic_name__   :							Any             =					(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
			__magic_name__   :							int             =					(
			    {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
			    if is_torch_available()
			    else {}
			)

			__magic_name__   :							int             =					False
			__magic_name__   :							Any             =					False
			__magic_name__   :							Optional[int]             =					False
			__magic_name__   :							str             =					False
			__magic_name__   :							Optional[int]             =					False



			def      lowerCamelCase   (       self				):
									snake_case   :					Optional[Any]							=						EfficientNetModelTester(self				)
									snake_case   :					List[Any]							=						ConfigTester(
									    self							, config_class=snake_case_							, has_text_modality=snake_case_							, hidden_size=37				)



			def      lowerCamelCase   (       self				):
									self.create_and_test_config_common_properties()
									self.config_tester.create_and_test_config_to_json_string()
									self.config_tester.create_and_test_config_to_json_file()
									self.config_tester.create_and_test_config_from_and_save_pretrained()
									self.config_tester.create_and_test_config_with_num_labels()
									self.config_tester.check_config_can_be_init_without_params()
									self.config_tester.check_config_arguments_init()



			def      lowerCamelCase   (       self				):
									return



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



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



			@unittest.skip(reason="EfficientNet does not use feedforward chunking"				)
			def      lowerCamelCase   (       self				):
									pass



			def      lowerCamelCase   (       self				):
									snake_case   :					Union[str, Any]							=						self.model_tester.prepare_config_and_inputs_for_common()

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

															snake_case   :					Optional[Any]							=						["pixel_values"]
															self.assertListEqual(arg_names[:1]							, snake_case_				)



			def      lowerCamelCase   (       self				):
									snake_case   :					Any							=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*snake_case_				)



			def      lowerCamelCase   (       self				):
									def check_hidden_states_output(snake_case_							, snake_case_							, snake_case_				):
															snake_case   :					Optional[Any]							=						model_class(snake_case_				)
															model.to(snake_case_				)
															model.eval()

															with torch.no_grad():
																					snake_case   :					Optional[int]							=						model(**self._prepare_for_class(snake_case_							, snake_case_				)				)

															snake_case   :					List[str]							=						outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
															snake_case   :					Optional[Any]							=						sum(config.num_block_repeats				) * 4
															self.assertEqual(len(snake_case_				)							, snake_case_				)

															# EfficientNet's feature maps are of shape (batch_size, num_channels, height, width)
															self.assertListEqual(
															    list(hidden_states[0].shape[-2:]				)							, [self.model_tester.image_size // 2, self.model_tester.image_size // 2]							, )

									snake_case   :					Dict							=						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															snake_case   :					Tuple							=						True
															check_hidden_states_output(snake_case_							, snake_case_							, snake_case_				)

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															snake_case   :					List[Any]							=						True

															check_hidden_states_output(snake_case_							, snake_case_							, snake_case_				)



			def      lowerCamelCase   (       self				):
									snake_case   :					Dict							=						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*snake_case_				)



			@unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."				)
			def      lowerCamelCase   (       self				):
									pass



			@slow
			def      lowerCamelCase   (       self				):
									for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															snake_case   :					Union[str, Any]							=						EfficientNetModel.from_pretrained(snake_case_				)
															self.assertIsNotNone(snake_case_				)






			@is_pipeline_test
			@require_vision
			@slow
			def      lowerCamelCase   (       self				):
									super().test_pipeline_image_classification()







def 					UpperCAmelCase__      (							)						->		Dict:



						"""simple docstring"""

						snake_case   :					int							=						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
						return image




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



			"""simple docstring"""






			@cached_property
			def      lowerCamelCase   (       self				):
									return AutoImageProcessor.from_pretrained("google/efficientnet-b7"				) if is_vision_available() else None






			@slow
			def      lowerCamelCase   (       self				):
									snake_case   :					Tuple							=						EfficientNetForImageClassification.from_pretrained("google/efficientnet-b7"				).to(snake_case_				)

									snake_case   :					str							=						self.default_image_processor
									snake_case   :					Any							=						prepare_img()
									snake_case   :					Any							=						image_processor(images=snake_case_							, return_tensors="pt"				).to(snake_case_				)

									# forward pass
									with torch.no_grad():
															snake_case   :					List[str]							=						model(**snake_case_				)

									# verify the logits
									snake_case   :					Any							=						torch.Size((1, 1_000)				)
									self.assertEqual(outputs.logits.shape							, snake_case_				)

									snake_case   :					str							=						torch.tensor([0.0_001, 0.0_002, 0.0_002]				).to(snake_case_				)
									self.assertTrue(torch.allclose(outputs.logits[0, :3]							, snake_case_							, atol=1e-4				)				)






'''simple docstring'''

import json
from typing import TYPE_CHECKING, List, Optional, Tuple

from tokenizers import pre_tokenizers

from ...tokenization_utils_base import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import logging
from .tokenization_deberta import DebertaTokenizer


if TYPE_CHECKING:
						from transformers.pipelines.conversational import Conversation


A_        =   logging.get_logger(__name__)

A_        =   {"vocab_file": "vocab.json", "merges_file": "merges.txt", "tokenizer_file": "tokenizer.json"}

A_        =   {
    "vocab_file": {
        "microsoft/deberta-base": "https://huggingface.co/microsoft/deberta-base/resolve/main/vocab.json",
        "microsoft/deberta-large": "https://huggingface.co/microsoft/deberta-large/resolve/main/vocab.json",
        "microsoft/deberta-xlarge": "https://huggingface.co/microsoft/deberta-xlarge/resolve/main/vocab.json",
        "microsoft/deberta-base-mnli": "https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/vocab.json",
        "microsoft/deberta-large-mnli": "https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/vocab.json",
        "microsoft/deberta-xlarge-mnli": (
            "https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/vocab.json"
        ),
    },
    "merges_file": {
        "microsoft/deberta-base": "https://huggingface.co/microsoft/deberta-base/resolve/main/merges.txt",
        "microsoft/deberta-large": "https://huggingface.co/microsoft/deberta-large/resolve/main/merges.txt",
        "microsoft/deberta-xlarge": "https://huggingface.co/microsoft/deberta-xlarge/resolve/main/merges.txt",
        "microsoft/deberta-base-mnli": "https://huggingface.co/microsoft/deberta-base-mnli/resolve/main/merges.txt",
        "microsoft/deberta-large-mnli": "https://huggingface.co/microsoft/deberta-large-mnli/resolve/main/merges.txt",
        "microsoft/deberta-xlarge-mnli": (
            "https://huggingface.co/microsoft/deberta-xlarge-mnli/resolve/main/merges.txt"
        ),
    },
}

A_        =   {
    "microsoft/deberta-base": 512,
    "microsoft/deberta-large": 512,
    "microsoft/deberta-xlarge": 512,
    "microsoft/deberta-base-mnli": 512,
    "microsoft/deberta-large-mnli": 512,
    "microsoft/deberta-xlarge-mnli": 512,
}

A_        =   {
    "microsoft/deberta-base": {"do_lower_case": False},
    "microsoft/deberta-large": {"do_lower_case": False},
}




class 		SCREAMING_SNAKE_CASE_				(_UpperCAmelCase			):



			"""simple docstring"""






			__magic_name__   :							Optional[Any]             =					VOCAB_FILES_NAMES
			__magic_name__   :							int             =					PRETRAINED_VOCAB_FILES_MAP
			__magic_name__   :							Dict             =					PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
			__magic_name__   :							List[str]             =					["""input_ids""", """attention_mask""", """token_type_ids"""]
			__magic_name__   :							Optional[int]             =					DebertaTokenizer



			def __init__(       self							, snake_case_=None							, snake_case_=None							, snake_case_=None							, snake_case_="replace"							, snake_case_="[CLS]"							, snake_case_="[SEP]"							, snake_case_="[SEP]"							, snake_case_="[CLS]"							, snake_case_="[UNK]"							, snake_case_="[PAD]"							, snake_case_="[MASK]"							, snake_case_=False							, **snake_case_							, ):
									super().__init__(
									    snake_case_							, snake_case_							, tokenizer_file=snake_case_							, errors=snake_case_							, bos_token=snake_case_							, eos_token=snake_case_							, unk_token=snake_case_							, sep_token=snake_case_							, cls_token=snake_case_							, pad_token=snake_case_							, mask_token=snake_case_							, add_prefix_space=snake_case_							, **snake_case_							, )
									snake_case   :					List[str]							=						kwargs.pop("add_bos_token"							, snake_case_				)

									snake_case   :					int							=						json.loads(self.backend_tokenizer.pre_tokenizer.__getstate__()				)
									if pre_tok_state.get("add_prefix_space"							, snake_case_				) != add_prefix_space:
															snake_case   :					Dict							=						getattr(snake_case_							, pre_tok_state.pop("type"				)				)
															snake_case   :					int							=						add_prefix_space
															snake_case   :					Optional[int]							=						pre_tok_class(**snake_case_				)

									snake_case   :					Any							=						add_prefix_space



			@property
			def      lowerCamelCase   (       self				):
									if self._mask_token is None:
															if self.verbose:
																					logger.error("Using mask_token, but it is not set yet."				)
															return None
									return str(self._mask_token				)



			@mask_token.setter
			def      lowerCamelCase   (       self							, snake_case_				):
									snake_case   :					Optional[Any]							=						AddedToken(snake_case_							, lstrip=snake_case_							, rstrip=snake_case_				) if isinstance(snake_case_							, snake_case_				) else value
									snake_case   :					Optional[int]							=						value



			def      lowerCamelCase   (       self							, snake_case_							, snake_case_ = None				):
									if token_ids_a is None:
															return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
									snake_case   :					Optional[int]							=						[self.cls_token_id]
									snake_case   :					Optional[int]							=						[self.sep_token_id]
									return cls + token_ids_a + sep + token_ids_a + sep



			def      lowerCamelCase   (       self							, snake_case_							, snake_case_ = None				):
									snake_case   :					Dict							=						[self.sep_token_id]
									snake_case   :					Dict							=						[self.cls_token_id]

									if token_ids_a is None:
															return len(cls + token_ids_a + sep				) * [0]
									return len(cls + token_ids_a + sep				) * [0] + len(token_ids_a + sep				) * [1]



			def      lowerCamelCase   (       self							, *snake_case_							, **snake_case_				):
									snake_case   :					str							=						kwargs.get("is_split_into_words"							, snake_case_				)
									assert self.add_prefix_space or not is_split_into_words, (
									    F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
									    "to use it with pretokenized inputs."
									)

									return super()._batch_encode_plus(*snake_case_							, **snake_case_				)



			def      lowerCamelCase   (       self							, *snake_case_							, **snake_case_				):
									snake_case   :					List[Any]							=						kwargs.get("is_split_into_words"							, snake_case_				)

									assert self.add_prefix_space or not is_split_into_words, (
									    F"""You need to instantiate {self.__class__.__name__} with add_prefix_space=True """
									    "to use it with pretokenized inputs."
									)

									return super()._encode_plus(*snake_case_							, **snake_case_				)



			def      lowerCamelCase   (       self							, snake_case_							, snake_case_ = None				):
									snake_case   :					List[str]							=						self._tokenizer.model.save(snake_case_							, name=snake_case_				)
									return tuple(snake_case_				)






			def      lowerCamelCase   (       self							, snake_case_				):
									snake_case   :					int							=						[]
									for is_user, text in conversation.iter_texts():
															input_ids.extend(self.encode(snake_case_							, add_special_tokens=snake_case_				) + [self.eos_token_id]				)

									if len(snake_case_				) > self.model_max_length:
															snake_case   :					Union[str, Any]							=						input_ids[-self.model_max_length :]
									return input_ids





