




from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers import (
		    TFDeiTForImageClassification,
		    TFDeiTForImageClassificationWithTeacher,
		    TFDeiTForMaskedImageModeling,
		    TFDeiTModel,
		)
		from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		from PIL import Image

		from transformers import DeiTImageProcessor

class 						__lowerCAmelCase      :
							def __init__(		self		:Dict						, __magic_name__		:Union[str, Any]						, __magic_name__		:Optional[int]=13						, __magic_name__		:Union[str, Any]=30						, __magic_name__		:Tuple=2						, __magic_name__		:int=3						, __magic_name__		:List[str]=True						, __magic_name__		:Union[str, Any]=True						, __magic_name__		:Any=32						, __magic_name__		:Dict=2						, __magic_name__		:List[Any]=4						, __magic_name__		:Dict=37						, __magic_name__		:Tuple="gelu"						, __magic_name__		:List[str]=0.1						, __magic_name__		:Union[str, Any]=0.1						, __magic_name__		:Optional[int]=10						, __magic_name__		:Optional[int]=0.02						, __magic_name__		:List[str]=3						, __magic_name__		:Any=None						, __magic_name__		:str=2						, ):







								'''simple docstring'''







								a									=				parent
								a									=				batch_size
								a									=				image_size
								a									=				patch_size
								a									=				num_channels
								a									=				is_training
								a									=				use_labels
								a									=				hidden_size
								a									=				num_hidden_layers
								a									=				num_attention_heads
								a									=				intermediate_size
								a									=				hidden_act
								a									=				hidden_dropout_prob
								a									=				attention_probs_dropout_prob
								a									=				type_sequence_label_size
								a									=				initializer_range
								a									=				scope
								a									=				encoder_stride

								# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
								a									=				(image_size // patch_size) ** 2
								a									=				num_patches + 2
							def  lowerCamelCase__				(		self		:Any							):







								'''simple docstring'''







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

								a									=				None
								if self.use_labels:
									a									=				ids_tensor([self.batch_size]						, self.type_sequence_label_size							)

								a									=				self.get_config()

								return config, pixel_values, labels
							def  lowerCamelCase__				(		self		:Dict							):







								'''simple docstring'''







								return DeiTConfig(
								    image_size=self.image_size						, patch_size=self.patch_size						, num_channels=self.num_channels						, hidden_size=self.hidden_size						, num_hidden_layers=self.num_hidden_layers						, num_attention_heads=self.num_attention_heads						, intermediate_size=self.intermediate_size						, hidden_act=self.hidden_act						, hidden_dropout_prob=self.hidden_dropout_prob						, attention_probs_dropout_prob=self.attention_probs_dropout_prob						, is_decoder=__magic_name__						, initializer_range=self.initializer_range						, encoder_stride=self.encoder_stride						, )
							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:Any						, __magic_name__		:Dict						, __magic_name__		:Optional[int]							):







								'''simple docstring'''







								a									=				TFDeiTModel(config=__magic_name__							)
								a									=				model(__magic_name__							)
								self.parent.assertEqual(result.last_hidden_state.shape						, (self.batch_size, self.seq_length, self.hidden_size)							)
							def  lowerCamelCase__				(		self		:Union[str, Any]						, __magic_name__		:Any						, __magic_name__		:int						, __magic_name__		:Optional[int]							):







								'''simple docstring'''







								a									=				TFDeiTForMaskedImageModeling(config=__magic_name__							)
								a									=				model(__magic_name__							)
								self.parent.assertEqual(
								    result.reconstruction.shape						, (self.batch_size, self.num_channels, self.image_size, self.image_size)							)

								# test greyscale images
								a									=				1
								a									=				TFDeiTForMaskedImageModeling(__magic_name__							)

								a									=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
								a									=				model(__magic_name__							)
								self.parent.assertEqual(result.reconstruction.shape						, (self.batch_size, 1, self.image_size, self.image_size)							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:Any						, __magic_name__		:Optional[int]						, __magic_name__		:Optional[int]							):







								'''simple docstring'''







								a									=				self.type_sequence_label_size
								a									=				TFDeiTForImageClassification(__magic_name__							)
								a									=				model(__magic_name__						, labels=__magic_name__							)
								self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size)							)

								# test greyscale images
								a									=				1
								a									=				TFDeiTForImageClassification(__magic_name__							)

								a									=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]							)
								a									=				model(__magic_name__						, labels=__magic_name__							)
								self.parent.assertEqual(result.logits.shape						, (self.batch_size, self.type_sequence_label_size)							)





							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a									=				self.prepare_config_and_inputs()
								a							,					a							,					a									=				config_and_inputs
								a									=				{"""pixel_values""": pixel_values}
								return config, inputs_dict







@require_tf
class 						__lowerCAmelCase      (      __magic_name__ ,			__magic_name__ ,			unittest.TestCase					):
							UpperCamelCase__   	=    (
							    (
							        TFDeiTModel,
							        TFDeiTForImageClassification,
							        TFDeiTForImageClassificationWithTeacher,
							        TFDeiTForMaskedImageModeling,
							    )
							    if is_tf_available()
							    else ()
							)
							UpperCamelCase__   	=    (
							    {
							        '''feature-extraction''': TFDeiTModel,
							        '''image-classification''': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
							    }
							    if is_tf_available()
							    else {}
							)

							UpperCamelCase__   	=    False
							UpperCamelCase__   	=    False
							UpperCamelCase__   	=    False
							UpperCamelCase__   	=    False
							def  lowerCamelCase__				(		self		:int							):







								'''simple docstring'''







								a									=				TFDeiTModelTester(self							)
								a									=				ConfigTester(self						, config_class=__magic_name__						, has_text_modality=__magic_name__						, hidden_size=37							)
							def  lowerCamelCase__				(		self		:Dict							):







								'''simple docstring'''







								self.config_tester.run_common_tests()
							@unittest.skip(reason="""DeiT does not use inputs_embeds"""							)
							def  lowerCamelCase__				(		self		:List[str]							):







								'''simple docstring'''







								pass
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a							,					a									=				self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
									a									=				model_class(__magic_name__							)
									self.assertIsInstance(model.get_input_embeddings()						, (tf.keras.layers.Layer)							)
									a									=				model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(__magic_name__						, tf.keras.layers.Dense							)							)
							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								a							,					a									=				self.model_tester.prepare_config_and_inputs_for_common()

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

									a									=				["""pixel_values"""]
									self.assertListEqual(arg_names[:1]						, __magic_name__							)
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a									=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_model(*__magic_name__							)
							def  lowerCamelCase__				(		self		:Any							):







								'''simple docstring'''







								a									=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_masked_image_modeling(*__magic_name__							)
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								a									=				self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_for_image_classification(*__magic_name__							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:List[str]						, __magic_name__		:Tuple						, __magic_name__		:Tuple=False							):







								'''simple docstring'''







								a									=				super()._prepare_for_class(__magic_name__						, __magic_name__						, return_labels=__magic_name__							)

								if return_labels:
									if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call							).parameters:
										del inputs_dict["labels"]

								return inputs_dict





							@slow
							def  lowerCamelCase__				(		self		:List[Any]							):







								'''simple docstring'''







								for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									a									=				TFDeiTModel.from_pretrained(__magic_name__							)
									self.assertIsNotNone(__magic_name__							)







def 						__A						(      ) ->			List[Any]:
	a									=				Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""       )
	return image



@require_tf
@require_vision
class 						__lowerCAmelCase      (      unittest.TestCase					):
							@cached_property
							def  lowerCamelCase__				(		self		:str							):







								'''simple docstring'''







								return (
								    DeiTImageProcessor.from_pretrained("""facebook/deit-base-distilled-patch16-224"""							)
								    if is_vision_available()
								    else None
								)





							@slow
							def  lowerCamelCase__				(		self		:Optional[int]							):







								'''simple docstring'''







								a									=				TFDeiTForImageClassificationWithTeacher.from_pretrained("""facebook/deit-base-distilled-patch16-224"""							)

								a									=				self.default_image_processor
								a									=				prepare_img()
								a									=				image_processor(images=__magic_name__						, return_tensors="""tf"""							)

								# forward pass
								a									=				model(**__magic_name__							)

								# verify the logits
								a									=				tf.TensorShape((1, 1000)							)
								self.assertEqual(outputs.logits.shape						, __magic_name__							)

								a									=				tf.constant([-1.0266, 0.1912, -1.2861]							)

								self.assertTrue(np.allclose(outputs.logits[0, :3]						, __magic_name__						, 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


__UpperCamelCase   :			Union[str, Any]          = logging.get_logger(__name__)

__UpperCamelCase   :			Optional[Any]          = "▁"

__UpperCamelCase   :			Any          = {"vocab_file": "sentencepiece.bpe.model"}

__UpperCamelCase   :			int          = {
    "vocab_file": {
        "facebook/xglm-564M": "https://huggingface.co/facebook/xglm-564M/resolve/main/sentencepiece.bpe.model",
    }
}

__UpperCamelCase   :			Dict          = {
    "facebook/xglm-564M": 2_048,
}

class 						__lowerCAmelCase      (      __magic_name__					):
							UpperCamelCase__   	=    VOCAB_FILES_NAMES
							UpperCamelCase__   	=    PRETRAINED_VOCAB_FILES_MAP
							UpperCamelCase__   	=    PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
							UpperCamelCase__   	=    ['''input_ids''', '''attention_mask''']
							def __init__(		self		:Optional[Any]						, __magic_name__		:List[str]						, __magic_name__		:Tuple="<s>"						, __magic_name__		:List[Any]="</s>"						, __magic_name__		:List[str]="</s>"						, __magic_name__		:Any="<s>"						, __magic_name__		:Tuple="<unk>"						, __magic_name__		:int="<pad>"						, __magic_name__		:Optional[Dict[str, Any]] = None						, **__magic_name__		:Optional[Any]						, ):







								'''simple docstring'''







								a									=				{} if sp_model_kwargs is None else sp_model_kwargs

								# Compatibility with the original tokenizer
								a									=				7
								a									=				[F'<madeupword{i}>' for i in range(self.num_madeup_words							)]

								a									=				kwargs.get("""additional_special_tokens"""						, []							)
								kwargs["additional_special_tokens"] += [
								    word for word in madeup_words if word not in kwargs["additional_special_tokens"]
								]

								super().__init__(
								    bos_token=__magic_name__						, eos_token=__magic_name__						, unk_token=__magic_name__						, sep_token=__magic_name__						, cls_token=__magic_name__						, pad_token=__magic_name__						, sp_model_kwargs=self.sp_model_kwargs						, **__magic_name__						, )

								a									=				spm.SentencePieceProcessor(**self.sp_model_kwargs							)
								self.sp_model.Load(str(__magic_name__							)							)
								a									=				vocab_file

								# Original fairseq vocab and spm vocab must be "aligned":
								# Vocab    |    0    |    1    |   2    |    3    |  4  |  5  |  6  |   7   |   8   |  9
								# -------- | ------- | ------- | ------ | ------- | --- | --- | --- | ----- | ----- | ----
								# fairseq  | '<s>'   | '<pad>' | '</s>' | '<unk>' | ',' | '.' | '▁' | 's'   | '▁de' | '-'
								# spm      | '<unk>' | '<s>'   | '</s>' | ','     | '.' | '▁' | 's' | '▁de' | '-'   | '▁a'

								# The first "real" token "," has position 4 in the original fairseq vocab and position 3 in the spm vocab
								a									=				1

								# Mimic fairseq token-to-id alignment for the first 4 token
								a									=				{"""<s>""": 0, """<pad>""": 1, """</s>""": 2, """<unk>""": 3}

								a									=				len(self.sp_model							)
								a									=				{F'<madeupword{i}>': sp_size + i + self.fairseq_offset for i in range(self.num_madeup_words							)}
								self.fairseq_tokens_to_ids.update(__magic_name__							)

								a									=				{v: k for k, v in self.fairseq_tokens_to_ids.items()}
							def __getstate__(		self		:List[Any]							):







								'''simple docstring'''







								a									=				self.__dict__.copy()
								a									=				None
								a									=				self.sp_model.serialized_model_proto()
								return state
							def __setstate__(		self		:Any						, __magic_name__		:Any							):







								'''simple docstring'''







								a									=				d

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

								a									=				spm.SentencePieceProcessor(**self.sp_model_kwargs							)
								self.sp_model.LoadFromSerializedProto(self.sp_model_proto							)
							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:List[int]						, __magic_name__		:Optional[List[int]] = None							):







								'''simple docstring'''







								if token_ids_a is None:
									return [self.sep_token_id] + token_ids_a
								a									=				[self.sep_token_id]
								return sep + token_ids_a + sep + sep + token_ids_a
							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:List[int]						, __magic_name__		:Optional[List[int]] = None						, __magic_name__		:bool = False							):







								'''simple docstring'''







								if already_has_special_tokens:
									return super().get_special_tokens_mask(
									    token_ids_a=__magic_name__						, token_ids_a=__magic_name__						, already_has_special_tokens=__magic_name__							)

								if token_ids_a is None:
									return [1] + ([0] * len(__magic_name__							))
								return [1] + ([0] * len(__magic_name__							)) + [1, 1] + ([0] * len(__magic_name__							))
							def  lowerCamelCase__				(		self		:Optional[Any]						, __magic_name__		:List[int]						, __magic_name__		:Optional[List[int]] = None							):







								'''simple docstring'''







								a									=				[self.sep_token_id]

								if token_ids_a is None:
									return len(sep + token_ids_a							) * [0]
								return len(sep + token_ids_a + sep + sep + token_ids_a							) * [0]
							@property
							def  lowerCamelCase__				(		self		:str							):







								'''simple docstring'''







								return len(self.sp_model							) + self.fairseq_offset + self.num_madeup_words
							def  lowerCamelCase__				(		self		:Dict							):







								'''simple docstring'''







								a									=				{self.convert_ids_to_tokens(__magic_name__							): i for i in range(self.vocab_size							)}
								vocab.update(self.added_tokens_encoder							)
								return vocab
							def  lowerCamelCase__				(		self		:Any						, __magic_name__		:str							):







								'''simple docstring'''







								return self.sp_model.encode(__magic_name__						, out_type=__magic_name__							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:Any							):







								'''simple docstring'''







								if token in self.fairseq_tokens_to_ids:
									return self.fairseq_tokens_to_ids[token]
								a									=				self.sp_model.PieceToId(__magic_name__							)

								# Need to return unknown token if the SP model returned 0
								return spm_id + self.fairseq_offset if spm_id else self.unk_token_id
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:Dict							):







								'''simple docstring'''







								if index in self.fairseq_ids_to_tokens:
									return self.fairseq_ids_to_tokens[index]
								return self.sp_model.IdToPiece(index - self.fairseq_offset							)
							def  lowerCamelCase__				(		self		:List[str]						, __magic_name__		:List[str]							):







								'''simple docstring'''







								a									=				"""""".join(__magic_name__							).replace(__magic_name__						, """ """							).strip()
								return out_string





							def  lowerCamelCase__				(		self		:List[Any]						, __magic_name__		:str						, __magic_name__		:Optional[str] = None							):







								'''simple docstring'''







								if not os.path.isdir(__magic_name__							):
									logger.error(F'Vocabulary path ({save_directory}) should be a directory'							)
									return
								a									=				os.path.join(
								    __magic_name__						, (filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]							)

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

								return (out_vocab_file,)

