



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

import sentencepiece as spm

from ...tokenization_utils import AddedToken, PreTrainedTokenizer
from ...utils import SPIECE_UNDERLINE, logging


__SCREAMING_SNAKE_CASE           =  logging.get_logger(__name__)

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

__SCREAMING_SNAKE_CASE           =  {
    """vocab_file""": {
        """xlnet-base-cased""": """https://huggingface.co/xlnet-base-cased/resolve/main/spiece.model""",
        """xlnet-large-cased""": """https://huggingface.co/xlnet-large-cased/resolve/main/spiece.model""",
    }
}

__SCREAMING_SNAKE_CASE           =  {
    """xlnet-base-cased""": None,
    """xlnet-large-cased""": None,
}

# Segments (not really needed)
__SCREAMING_SNAKE_CASE           =  0
__SCREAMING_SNAKE_CASE           =  1
__SCREAMING_SNAKE_CASE           =  2
__SCREAMING_SNAKE_CASE           =  3
__SCREAMING_SNAKE_CASE           =  4

class 	lowerCamelCase_    ( _A						):






						'''simple docstring'''
						a__   =  VOCAB_FILES_NAMES
						a__   =  PRETRAINED_VOCAB_FILES_MAP
						a__   =  PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						a__   =  "left"







						def __init__(	self      :			List[str]	,	__lowerCamelCase      :			Any	,	__lowerCamelCase      :			str=False	,	__lowerCamelCase      :			Dict=True	,	__lowerCamelCase      :			List[str]=False	,	__lowerCamelCase      :			List[str]="<s>"	,	__lowerCamelCase      :			int="</s>"	,	__lowerCamelCase      :			str="<unk>"	,	__lowerCamelCase      :			int="<sep>"	,	__lowerCamelCase      :			Optional[Any]="<pad>"	,	__lowerCamelCase      :			Tuple="<cls>"	,	__lowerCamelCase      :			Optional[Any]="<mask>"	,	__lowerCamelCase      :			int=["<eop>", "<eod>"]	,	__lowerCamelCase      :			Optional[Dict[str, Any]] = None	,	**__lowerCamelCase      :			Optional[int]	,	)      ->				None:
									# Mask token behave like a normal word, i.e. include the space before it
									A							:					str              =						AddedToken(__lowerCamelCase	,	lstrip=__lowerCamelCase	,	rstrip=__lowerCamelCase ) if isinstance(__lowerCamelCase	,	__lowerCamelCase ) else mask_token

									A							:					Optional[int]              =						{} if sp_model_kwargs is None else sp_model_kwargs

									super().__init__(
									    do_lower_case=__lowerCamelCase	,	remove_space=__lowerCamelCase	,	keep_accents=__lowerCamelCase	,	bos_token=__lowerCamelCase	,	eos_token=__lowerCamelCase	,	unk_token=__lowerCamelCase	,	sep_token=__lowerCamelCase	,	pad_token=__lowerCamelCase	,	cls_token=__lowerCamelCase	,	mask_token=__lowerCamelCase	,	additional_special_tokens=__lowerCamelCase	,	sp_model_kwargs=self.sp_model_kwargs	,	**__lowerCamelCase	,	)

									A							:					int              =						3

									A							:					Union[str, Any]              =						do_lower_case
									A							:					List[Any]              =						remove_space
									A							:					List[Any]              =						keep_accents
									A							:					str              =						vocab_file

									A							:					Optional[Any]              =						spm.SentencePieceProcessor(**self.sp_model_kwargs )
									self.sp_model.Load(__lowerCamelCase )







						@property
						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				int:
									return len(self.sp_model )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Dict:
									A							:					str              =						{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 )      ->				Optional[Any]:
									A							:					Optional[int]              =						self.__dict__.copy()
									A							:					Dict              =						None
									return state







						def __setstate__(	self      :			Any	,	__lowerCamelCase      :			Any )      ->				Dict:
									A							:					Union[str, Any]              =						d

									# for backward compatibility
									if not hasattr(self	,	"sp_model_kwargs" ):
												A							:					Optional[int]              =						{}

									A							:					Optional[int]              =						spm.SentencePieceProcessor(**self.sp_model_kwargs )
									self.sp_model.Load(self.vocab_file )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			int )      ->				Any:
									if self.remove_space:
												A							:					Union[str, Any]              =						" ".join(inputs.strip().split() )
									else:
												A							:					str              =						inputs
									A							:					Dict              =						outputs.replace("``"	,	"\"" ).replace("''"	,	"\"" )

									if not self.keep_accents:
												A							:					Optional[int]              =						unicodedata.normalize("NFKD"	,	__lowerCamelCase )
												A							:					Tuple              =						"".join([c for c in outputs if not unicodedata.combining(__lowerCamelCase )] )
									if self.do_lower_case:
												A							:					Tuple              =						outputs.lower()

									return outputs







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int]	,	__lowerCamelCase      :			str )      ->				List[str]:
									A							:					Dict              =						self.preprocess_text(__lowerCamelCase )
									A							:					Tuple              =						self.sp_model.encode(__lowerCamelCase	,	out_type=__lowerCamelCase )
									A							:					Any              =						[]
									for piece in pieces:
												if len(__lowerCamelCase ) > 1 and piece[-1] == str("," ) and piece[-2].isdigit():
															A							:					Optional[Any]              =						self.sp_model.EncodeAsPieces(piece[:-1].replace(__lowerCamelCase	,	"" ) )
															if piece[0] != SPIECE_UNDERLINE and cur_pieces[0][0] == SPIECE_UNDERLINE:
																		if len(cur_pieces[0] ) == 1:
																					A							:					Optional[Any]              =						cur_pieces[1:]
																		else:
																					A							:					List[str]              =						cur_pieces[0][1:]
															cur_pieces.append(piece[-1] )
															new_pieces.extend(__lowerCamelCase )
												else:
															new_pieces.append(__lowerCamelCase )

									return new_pieces







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any	,	__lowerCamelCase      :			Tuple )      ->				Dict:
									return self.sp_model.PieceToId(__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int	,	__lowerCamelCase      :			Optional[int] )      ->				List[str]:
									return self.sp_model.IdToPiece(__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any]	,	__lowerCamelCase      :			Tuple )      ->				Tuple:
									A							:					Any              =						"".join(__lowerCamelCase ).replace(__lowerCamelCase	,	" " ).strip()
									return out_string







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			List[int]	,	__lowerCamelCase      :			bool = False	,	__lowerCamelCase      :			bool = None	,	__lowerCamelCase      :			bool = True	,	**__lowerCamelCase      :			Union[str, Any]	,	)      ->				str:
									A							:					Any              =						kwargs.pop("use_source_tokenizer"	,	__lowerCamelCase )

									A							:					Any              =						self.convert_ids_to_tokens(__lowerCamelCase	,	skip_special_tokens=__lowerCamelCase )

									# To avoid mixing byte-level and unicode for byte-level BPT
									# we need to build string separately for added tokens and byte-level tokens
									# cf. https://github.com/huggingface/transformers/issues/1133
									A							:					Dict              =						[]
									A							:					Dict              =						[]
									for token in filtered_tokens:
												if skip_special_tokens and token in self.all_special_ids:
															continue
												if token in self.added_tokens_encoder:
															if current_sub_text:
																		sub_texts.append(self.convert_tokens_to_string(__lowerCamelCase ) )
																		A							:					Optional[Any]              =						[]
															sub_texts.append(__lowerCamelCase )
												else:
															current_sub_text.append(__lowerCamelCase )
									if current_sub_text:
												sub_texts.append(self.convert_tokens_to_string(__lowerCamelCase ) )

									# Mimic the behavior of the Rust tokenizer:
									# By default, there are no spaces between special tokens
									A							:					Any              =						"".join(__lowerCamelCase )

									A							:					Optional[int]              =						(
									    clean_up_tokenization_spaces
									    if clean_up_tokenization_spaces is not None
									    else self.clean_up_tokenization_spaces
									)
									if clean_up_tokenization_spaces:
												A							:					Dict              =						self.clean_up_tokenization(__lowerCamelCase )
												return clean_text
									else:
												return text







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any	,	__lowerCamelCase      :			List[int]	,	__lowerCamelCase      :			Optional[List[int]] = None )      ->				List[int]:
									A							:					Dict              =						[self.sep_token_id]
									A							:					Dict              =						[self.cls_token_id]
									if token_ids_a is None:
												return token_ids_a + sep + cls
									return token_ids_a + sep + token_ids_a + sep + cls







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str	,	__lowerCamelCase      :			List[int]	,	__lowerCamelCase      :			Optional[List[int]] = None	,	__lowerCamelCase      :			bool = False )      ->				List[int]:

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

									if token_ids_a is not None:
												return ([0] * len(__lowerCamelCase )) + [1] + ([0] * len(__lowerCamelCase )) + [1, 1]
									return ([0] * len(__lowerCamelCase )) + [1, 1]







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any]	,	__lowerCamelCase      :			List[int]	,	__lowerCamelCase      :			Optional[List[int]] = None )      ->				List[int]:
									A							:					Optional[Any]              =						[self.sep_token_id]
									A							:					Dict              =						[2]

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







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any]	,	__lowerCamelCase      :			str	,	__lowerCamelCase      :			Optional[str] = None )      ->				Tuple[str]:
									if not os.path.isdir(__lowerCamelCase ):
												logger.error(F"""Vocabulary path ({save_directory}) should be a directory""" )
												return
									A							:					str              =						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:
															A							:					Tuple              =						self.sp_model.serialized_model_proto()
															fi.write(__lowerCamelCase )

									return (out_vocab_file,)




import inspect
import unittest
import warnings

from transformers import DeiTConfig
from transformers.models.auto import get_values
from transformers.testing_utils import (
    require_accelerate,
    require_torch,
    require_torch_gpu,
    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 torch import nn

							from transformers import (
							    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
							    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
							    MODEL_MAPPING,
							    DeiTForImageClassification,
							    DeiTForImageClassificationWithTeacher,
							    DeiTForMaskedImageModeling,
							    DeiTModel,
							)
							from transformers.models.deit.modeling_deit import DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
							from PIL import Image

							from transformers import DeiTImageProcessor

class 	lowerCamelCase_    :






						'''simple docstring'''







						def __init__(	self      :			Tuple	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			Tuple=13	,	__lowerCamelCase      :			List[str]=30	,	__lowerCamelCase      :			Tuple=2	,	__lowerCamelCase      :			Any=3	,	__lowerCamelCase      :			Dict=True	,	__lowerCamelCase      :			Optional[int]=True	,	__lowerCamelCase      :			int=32	,	__lowerCamelCase      :			List[str]=5	,	__lowerCamelCase      :			Any=4	,	__lowerCamelCase      :			Optional[int]=37	,	__lowerCamelCase      :			List[Any]="gelu"	,	__lowerCamelCase      :			int=0.1	,	__lowerCamelCase      :			Union[str, Any]=0.1	,	__lowerCamelCase      :			Optional[Any]=10	,	__lowerCamelCase      :			Dict=0.02	,	__lowerCamelCase      :			Tuple=3	,	__lowerCamelCase      :			Union[str, Any]=None	,	__lowerCamelCase      :			List[Any]=2	,	)      ->				str:
									A							:					List[Any]              =						parent
									A							:					Optional[int]              =						batch_size
									A							:					Any              =						image_size
									A							:					Optional[Any]              =						patch_size
									A							:					Optional[Any]              =						num_channels
									A							:					Tuple              =						is_training
									A							:					Optional[Any]              =						use_labels
									A							:					Union[str, Any]              =						hidden_size
									A							:					Tuple              =						num_hidden_layers
									A							:					Union[str, Any]              =						num_attention_heads
									A							:					Union[str, Any]              =						intermediate_size
									A							:					Any              =						hidden_act
									A							:					Tuple              =						hidden_dropout_prob
									A							:					Dict              =						attention_probs_dropout_prob
									A							:					Any              =						type_sequence_label_size
									A							:					Tuple              =						initializer_range
									A							:					List[Any]              =						scope
									A							:					Optional[int]              =						encoder_stride

									# in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
									A							:					List[str]              =						(image_size // patch_size) ** 2
									A							:					List[str]              =						num_patches + 2







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				int:
									A							:					Union[str, Any]              =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size] )

									A							:					List[Any]              =						None
									if self.use_labels:
												A							:					List[str]              =						ids_tensor([self.batch_size]	,	self.type_sequence_label_size )

									A							:					Dict              =						self.get_config()

									return config, pixel_values, labels







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Dict:
									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=__lowerCamelCase	,	initializer_range=self.initializer_range	,	encoder_stride=self.encoder_stride	,	)







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			List[str] )      ->				int:
									A							:					Optional[int]              =						DeiTModel(config=__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					List[Any]              =						model(__lowerCamelCase )
									self.parent.assertEqual(result.last_hidden_state.shape	,	(self.batch_size, self.seq_length, self.hidden_size) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any]	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			Dict	,	__lowerCamelCase      :			Any )      ->				Any:
									A							:					List[Any]              =						DeiTForMaskedImageModeling(config=__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					Optional[int]              =						model(__lowerCamelCase )
									self.parent.assertEqual(
									    result.reconstruction.shape	,	(self.batch_size, self.num_channels, self.image_size, self.image_size) )

									# test greyscale images
									A							:					List[str]              =						1
									A							:					Optional[int]              =						DeiTForMaskedImageModeling(__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()

									A							:					Dict              =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
									A							:					Optional[int]              =						model(__lowerCamelCase )
									self.parent.assertEqual(result.reconstruction.shape	,	(self.batch_size, 1, self.image_size, self.image_size) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any	,	__lowerCamelCase      :			str	,	__lowerCamelCase      :			List[str]	,	__lowerCamelCase      :			int )      ->				Dict:
									A							:					str              =						self.type_sequence_label_size
									A							:					List[str]              =						DeiTForImageClassification(__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()
									A							:					Tuple              =						model(__lowerCamelCase	,	labels=__lowerCamelCase )
									self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.type_sequence_label_size) )

									# test greyscale images
									A							:					Any              =						1
									A							:					str              =						DeiTForImageClassification(__lowerCamelCase )
									model.to(__lowerCamelCase )
									model.eval()

									A							:					Tuple              =						floats_tensor([self.batch_size, 1, self.image_size, self.image_size] )
									A							:					Tuple              =						model(__lowerCamelCase	,	labels=__lowerCamelCase )
									self.parent.assertEqual(result.logits.shape	,	(self.batch_size, self.type_sequence_label_size) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Optional[int]:
									A							:					Dict              =						self.prepare_config_and_inputs()
									(
									    (
									    A
									)       , (
									    A
									)       , (
									    A
									)       , 
									)							:					Tuple              =						config_and_inputs
									A							:					int              =						{"pixel_values": pixel_values}
									return config, inputs_dict







@require_torch
class 	lowerCamelCase_    ( _A						,_A						,unittest.TestCase						):






						'''simple docstring'''
						a__   =  (
						    (
						        DeiTModel,
						        DeiTForImageClassification,
						        DeiTForImageClassificationWithTeacher,
						        DeiTForMaskedImageModeling,
						    )
						    if is_torch_available()
						    else ()
						)
						a__   =  (
						    {
						        "feature-extraction": DeiTModel,
						        "image-classification": (DeiTForImageClassification, DeiTForImageClassificationWithTeacher),
						    }
						    if is_torch_available()
						    else {}
						)

						a__   =  False
						a__   =  False
						a__   =  False







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Optional[int]:
									A							:					str              =						DeiTModelTester(self )
									A							:					Optional[int]              =						ConfigTester(self	,	config_class=__lowerCamelCase	,	has_text_modality=__lowerCamelCase	,	hidden_size=37 )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Dict )      ->				Dict:
									self.config_tester.run_common_tests()







						@unittest.skip(reason="DeiT does not use inputs_embeds" )
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				int:
									pass







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Union[str, Any]:
									A       , A							:					Dict              =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												A							:					Dict              =						model_class(__lowerCamelCase )
												self.assertIsInstance(model.get_input_embeddings()	,	(nn.Module) )
												A							:					Any              =						model.get_output_embeddings()
												self.assertTrue(x is None or isinstance(__lowerCamelCase	,	nn.Linear ) )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				List[str]:
									A       , A							:					Optional[Any]              =						self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												A							:					Union[str, Any]              =						model_class(__lowerCamelCase )
												A							:					Optional[int]              =						inspect.signature(model.forward )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												A							:					Any              =						[*signature.parameters.keys()]

												A							:					Any              =						["pixel_values"]
												self.assertListEqual(arg_names[:1]	,	__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Any:
									A							:					Any              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[Any] )      ->				Dict:
									A							:					List[Any]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			int )      ->				Optional[Any]:
									A							:					List[Any]              =						self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase )







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple	,	__lowerCamelCase      :			int	,	__lowerCamelCase      :			Optional[Any]	,	__lowerCamelCase      :			List[Any]=False )      ->				str:
									A							:					Union[str, Any]              =						super()._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )

									if return_labels:
												if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
															del inputs_dict["labels"]

									return inputs_dict







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Optional[int] )      ->				Optional[int]:
									if not self.model_tester.is_training:
												return

									A       , A							:					Optional[int]              =						self.model_tester.prepare_config_and_inputs_for_common()
									A							:					Dict              =						True

									for model_class in self.all_model_classes:
												# DeiTForImageClassificationWithTeacher supports inference-only
												if (
												    model_class in get_values(__lowerCamelCase )
												    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
												):
															continue
												A							:					Union[str, Any]              =						model_class(__lowerCamelCase )
												model.to(__lowerCamelCase )
												model.train()
												A							:					Union[str, Any]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												A							:					Dict              =						model(**__lowerCamelCase ).loss
												loss.backward()







						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Dict:
									A       , A							:					Optional[Any]              =						self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
												return

									A							:					Tuple              =						False
									A							:					Any              =						True

									for model_class in self.all_model_classes:
												if model_class in get_values(__lowerCamelCase ) or not model_class.supports_gradient_checkpointing:
															continue
												# DeiTForImageClassificationWithTeacher supports inference-only
												if model_class.__name__ == "DeiTForImageClassificationWithTeacher":
															continue
												A							:					List[str]              =						model_class(__lowerCamelCase )
												model.gradient_checkpointing_enable()
												model.to(__lowerCamelCase )
												model.train()
												A							:					Dict              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )
												A							:					Tuple              =						model(**__lowerCamelCase ).loss
												loss.backward()







						def 			SCREAMING_SNAKE_CASE__		(	self      :			str )      ->				Any:
									A       , A							:					Optional[Any]              =						self.model_tester.prepare_config_and_inputs_for_common()

									A							:					int              =						[
									    {"title": "multi_label_classification", "num_labels": 2, "dtype": torch.float},
									    {"title": "single_label_classification", "num_labels": 1, "dtype": torch.long},
									    {"title": "regression", "num_labels": 1, "dtype": torch.float},
									]

									for model_class in self.all_model_classes:
												if (
												    model_class
												    not in [
												        *get_values(__lowerCamelCase ),
												        *get_values(__lowerCamelCase ),
												    ]
												    or model_class.__name__ == "DeiTForImageClassificationWithTeacher"
												):
															continue

												for problem_type in problem_types:
															with self.subTest(msg=F"""Testing {model_class} with {problem_type["title"]}""" ):
																		A							:					Tuple              =						problem_type["title"]
																		A							:					Optional[Any]              =						problem_type["num_labels"]

																		A							:					List[str]              =						model_class(__lowerCamelCase )
																		model.to(__lowerCamelCase )
																		model.train()

																		A							:					List[Any]              =						self._prepare_for_class(__lowerCamelCase	,	__lowerCamelCase	,	return_labels=__lowerCamelCase )

																		if problem_type["num_labels"] > 1:
																					A							:					List[str]              =						inputs["labels"].unsqueeze(1 ).repeat(1	,	problem_type["num_labels"] )

																		A							:					int              =						inputs["labels"].to(problem_type["dtype"] )

																		# This tests that we do not trigger the warning form PyTorch "Using a target size that is different
																		# to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
																		# they have the same size." which is a symptom something in wrong for the regression problem.
																		# See https://github.com/huggingface/transformers/issues/11780
																		with warnings.catch_warnings(record=__lowerCamelCase ) as warning_list:
																					A							:					Optional[Any]              =						model(**__lowerCamelCase ).loss
																		for w in warning_list:
																					if "Using a target size that is different to the input size" in str(w.message ):
																								raise ValueError(
																								    F"""Something is going wrong in the regression problem: intercepted {w.message}""" )

																		loss.backward()







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			List[Any] )      ->				str:
									for model_name in DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												A							:					Optional[Any]              =						DeiTModel.from_pretrained(__lowerCamelCase )
												self.assertIsNotNone(__lowerCamelCase )







def 	UpperCAmelCase   (						):
			A							:					Optional[int]              =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
			return image






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






						'''simple docstring'''







						@cached_property
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Tuple )      ->				List[Any]:
									return (
									    DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224" )
									    if is_vision_available()
									    else None
									)







						@slow
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Any )      ->				Optional[int]:
									A							:					Optional[int]              =						DeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224" ).to(
									    __lowerCamelCase )

									A							:					List[Any]              =						self.default_image_processor
									A							:					List[Any]              =						prepare_img()
									A							:					Optional[Any]              =						image_processor(images=__lowerCamelCase	,	return_tensors="pt" ).to(__lowerCamelCase )

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

									# verify the logits
									A							:					str              =						torch.Size((1, 10_00) )
									self.assertEqual(outputs.logits.shape	,	__lowerCamelCase )

									A							:					List[str]              =						torch.tensor([-1.0266, 0.1912, -1.2861] ).to(__lowerCamelCase )

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







						@slow
						@require_accelerate
						@require_torch_gpu
						def 			SCREAMING_SNAKE_CASE__		(	self      :			Union[str, Any] )      ->				Optional[Any]:
									A							:					str              =						DeiTModel.from_pretrained(
									    "facebook/deit-base-distilled-patch16-224"	,	torch_dtype=torch.floataa	,	device_map="auto" )
									A							:					Dict              =						self.default_image_processor

									A							:					Optional[int]              =						prepare_img()
									A							:					Union[str, Any]              =						image_processor(images=__lowerCamelCase	,	return_tensors="pt" )
									A							:					Union[str, Any]              =						inputs.pixel_values.to(__lowerCamelCase )

									# forward pass to make sure inference works in fp16
									with torch.no_grad():
												A							:					List[str]              =						model(__lowerCamelCase )