import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_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, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
		import torch
		from torch import nn

		from transformers import (
		    MODEL_MAPPING,
		    BeitForImageClassification,
		    BeitForMaskedImageModeling,
		    BeitForSemanticSegmentation,
		    BeitModel,
		)
		from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
		import PIL
		from PIL import Image

		from transformers import BeitImageProcessor



class 				__UpperCAmelCase		:







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







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  parent
									_SCREAMING_SNAKE_CASE     				=  100
									_SCREAMING_SNAKE_CASE     				=  batch_size
									_SCREAMING_SNAKE_CASE     				=  image_size
									_SCREAMING_SNAKE_CASE     				=  patch_size
									_SCREAMING_SNAKE_CASE     				=  num_channels
									_SCREAMING_SNAKE_CASE     				=  is_training
									_SCREAMING_SNAKE_CASE     				=  use_labels
									_SCREAMING_SNAKE_CASE     				=  hidden_size
									_SCREAMING_SNAKE_CASE     				=  num_hidden_layers
									_SCREAMING_SNAKE_CASE     				=  num_attention_heads
									_SCREAMING_SNAKE_CASE     				=  intermediate_size
									_SCREAMING_SNAKE_CASE     				=  hidden_act
									_SCREAMING_SNAKE_CASE     				=  hidden_dropout_prob
									_SCREAMING_SNAKE_CASE     				=  attention_probs_dropout_prob
									_SCREAMING_SNAKE_CASE     				=  type_sequence_label_size
									_SCREAMING_SNAKE_CASE     				=  initializer_range
									_SCREAMING_SNAKE_CASE     				=  scope
									_SCREAMING_SNAKE_CASE     				=  out_indices
									_SCREAMING_SNAKE_CASE     				=  num_labels

									# in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
									_SCREAMING_SNAKE_CASE     				=  (image_size // patch_size) ** 2
									_SCREAMING_SNAKE_CASE     				=  num_patches + 1







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




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

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

									_SCREAMING_SNAKE_CASE     				=  self.get_config()

									return config, pixel_values, labels, pixel_labels







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									return BeitConfig(
									    vocab_size=self.vocab_size				,     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=SCREAMING_SNAKE_CASE_				,     initializer_range=self.initializer_range				,     out_indices=self.out_indices				,     )







			def 							UpperCamelCase				(     self:  List[str]				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  Any				,     UpperCAmelCase_:  List[str]				,     UpperCAmelCase_:  int					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitModel(config=SCREAMING_SNAKE_CASE_					)
									model.to(SCREAMING_SNAKE_CASE_					)
									model.eval()
									_SCREAMING_SNAKE_CASE     				=  model(SCREAMING_SNAKE_CASE_					)
									self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)					)







			def 							UpperCamelCase				(     self:  Dict				,     UpperCAmelCase_:  Optional[Any]				,     UpperCAmelCase_:  Any				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  int					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitForMaskedImageModeling(config=SCREAMING_SNAKE_CASE_					)
									model.to(SCREAMING_SNAKE_CASE_					)
									model.eval()
									_SCREAMING_SNAKE_CASE     				=  model(SCREAMING_SNAKE_CASE_					)
									self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length - 1, self.vocab_size)					)







			def 							UpperCamelCase				(     self:  List[Any]				,     UpperCAmelCase_:  Tuple				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  Optional[int]				,     UpperCAmelCase_:  int					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.type_sequence_label_size
									_SCREAMING_SNAKE_CASE     				=  BeitForImageClassification(SCREAMING_SNAKE_CASE_					)
									model.to(SCREAMING_SNAKE_CASE_					)
									model.eval()
									_SCREAMING_SNAKE_CASE     				=  model(SCREAMING_SNAKE_CASE_				,     labels=SCREAMING_SNAKE_CASE_					)
									self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.type_sequence_label_size)					)

									# test greyscale images
									_SCREAMING_SNAKE_CASE     				=  1
									_SCREAMING_SNAKE_CASE     				=  BeitForImageClassification(SCREAMING_SNAKE_CASE_					)
									model.to(SCREAMING_SNAKE_CASE_					)
									model.eval()

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







			def 							UpperCamelCase				(     self:  int				,     UpperCAmelCase_:  Any				,     UpperCAmelCase_:  Dict				,     UpperCAmelCase_:  Dict				,     UpperCAmelCase_:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.num_labels
									_SCREAMING_SNAKE_CASE     				=  BeitForSemanticSegmentation(SCREAMING_SNAKE_CASE_					)
									model.to(SCREAMING_SNAKE_CASE_					)
									model.eval()
									_SCREAMING_SNAKE_CASE     				=  model(SCREAMING_SNAKE_CASE_					)
									self.parent.assertEqual(
									    result.logits.shape				,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)
									_SCREAMING_SNAKE_CASE     				=  model(SCREAMING_SNAKE_CASE_				,     labels=SCREAMING_SNAKE_CASE_					)
									self.parent.assertEqual(
									    result.logits.shape				,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)







			def 							UpperCamelCase				(     self:  List[Any]					):







									'''simple docstring'''




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



@require_torch
class 				__UpperCAmelCase		(_lowerCAmelCase	,_lowerCAmelCase	,unittest.TestCase   ):
			__snake_case    :				Dict									=							(
			    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
			    if is_torch_available()
			    else ()
			)
			__snake_case    :				List[Any]									=							(
			    {
			        "feature-extraction": BeitModel,
			        "image-classification": BeitForImageClassification,
			        "image-segmentation": BeitForSemanticSegmentation,
			    }
			    if is_torch_available()
			    else {}
			)

			__snake_case    :				int									=							False
			__snake_case    :				int									=							False
			__snake_case    :				List[str]									=							False







			def 							UpperCamelCase				(     self:  Optional[int]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitModelTester(self					)
									_SCREAMING_SNAKE_CASE     				=  ConfigTester(self				,     config_class=SCREAMING_SNAKE_CASE_				,     has_text_modality=SCREAMING_SNAKE_CASE_				,     hidden_size=37					)







			def 							UpperCamelCase				(     self:  Dict					):







									'''simple docstring'''




									self.config_tester.run_common_tests()







			@unittest.skip(reason="""BEiT does not use inputs_embeds"""					)
			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									pass







			@require_torch_multi_gpu
			@unittest.skip(reason="""BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""					)
			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									pass







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs_for_common()

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







			def 							UpperCamelCase				(     self:  int					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs_for_common()

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

															_SCREAMING_SNAKE_CASE     				=  ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]				,     SCREAMING_SNAKE_CASE_					)







			def 							UpperCamelCase				(     self:  Optional[int]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE_					)







			def 							UpperCamelCase				(     self:  int					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE_					)







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE_					)







			def 							UpperCamelCase				(     self:  Dict					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE_					)







			def 							UpperCamelCase				(     self:  Any					):







									'''simple docstring'''




									if not self.model_tester.is_training:
															return

									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs_for_common()
									_SCREAMING_SNAKE_CASE     				=  True

									for model_class in self.all_model_classes:
															# we don't test BeitForMaskedImageModeling
															if model_class in [*get_values(SCREAMING_SNAKE_CASE_					), BeitForMaskedImageModeling]:
																					continue

															_SCREAMING_SNAKE_CASE     				=  model_class(SCREAMING_SNAKE_CASE_					)
															model.to(SCREAMING_SNAKE_CASE_					)
															model.train()
															_SCREAMING_SNAKE_CASE     				=  self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     return_labels=SCREAMING_SNAKE_CASE_					)
															_SCREAMING_SNAKE_CASE     				=  model(**SCREAMING_SNAKE_CASE_					).loss
															loss.backward()







			def 							UpperCamelCase				(     self:  Tuple					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
															return

									_SCREAMING_SNAKE_CASE     				=  False
									_SCREAMING_SNAKE_CASE     				=  True

									for model_class in self.all_model_classes:
															# we don't test BeitForMaskedImageModeling
															if (
															    model_class in [*get_values(SCREAMING_SNAKE_CASE_					), BeitForMaskedImageModeling]
															    or not model_class.supports_gradient_checkpointing
															):
																					continue

															_SCREAMING_SNAKE_CASE     				=  model_class(SCREAMING_SNAKE_CASE_					)
															model.gradient_checkpointing_enable()
															model.to(SCREAMING_SNAKE_CASE_					)
															model.train()
															_SCREAMING_SNAKE_CASE     				=  self._prepare_for_class(SCREAMING_SNAKE_CASE_				,     SCREAMING_SNAKE_CASE_				,     return_labels=SCREAMING_SNAKE_CASE_					)
															_SCREAMING_SNAKE_CASE     				=  model(**SCREAMING_SNAKE_CASE_					).loss
															loss.backward()







			def 							UpperCamelCase				(     self:  Optional[int]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.model_tester.prepare_config_and_inputs_for_common()

									_SCREAMING_SNAKE_CASE     				=  _config_zero_init(SCREAMING_SNAKE_CASE_					)
									for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE     				=  model_class(config=SCREAMING_SNAKE_CASE_					)
															for name, param in model.named_parameters():
																					# we skip lambda parameters as these require special initial values
																					# determined by config.layer_scale_init_value
																					if "lambda" in name:
																											continue
																					if param.requires_grad:
																											self.assertIn(
																											    ((param.data.mean() * 1E9).round() / 1E9).item()				,     [0.0, 1.0]				,     msg=F'Parameter {name} of model {model_class} seems not properly initialized'				,     )







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







									'''simple docstring'''




									for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															_SCREAMING_SNAKE_CASE     				=  BeitModel.from_pretrained(SCREAMING_SNAKE_CASE_					)
															self.assertIsNotNone(SCREAMING_SNAKE_CASE_					)

def 	__lowerCamelCase      (		)							->  List[Any]:






						"""simple docstring"""





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




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







			@cached_property
			def 							UpperCamelCase				(     self:  Optional[int]					):







									'''simple docstring'''




									return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""					) if is_vision_available() else None







			@slow
			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""					).to(SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  self.default_image_processor
									_SCREAMING_SNAKE_CASE     				=  prepare_img()
									_SCREAMING_SNAKE_CASE     				=  image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors="""pt"""					).pixel_values.to(SCREAMING_SNAKE_CASE_					)

									# prepare bool_masked_pos
									_SCREAMING_SNAKE_CASE     				=  torch.ones((1, 196)				,     dtype=torch.bool					).to(SCREAMING_SNAKE_CASE_					)

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE     				=  model(pixel_values=SCREAMING_SNAKE_CASE_				,     bool_masked_pos=SCREAMING_SNAKE_CASE_					)
									_SCREAMING_SNAKE_CASE     				=  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE     				=  torch.Size((1, 196, 8_192)					)
									self.assertEqual(logits.shape				,     SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  torch.tensor(
									    [[-3.24_37, 0.50_72, -13.9_174], [-3.24_56, 0.49_48, -13.9_401], [-3.20_33, 0.51_21, -13.8_550]]					).to(SCREAMING_SNAKE_CASE_					)

									self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]				,     SCREAMING_SNAKE_CASE_				,     atol=1E-2					)					)







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







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""					).to(SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  self.default_image_processor
									_SCREAMING_SNAKE_CASE     				=  prepare_img()
									_SCREAMING_SNAKE_CASE     				=  image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors="""pt"""					).to(SCREAMING_SNAKE_CASE_					)

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE     				=  model(**SCREAMING_SNAKE_CASE_					)
									_SCREAMING_SNAKE_CASE     				=  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE     				=  torch.Size((1, 1_000)					)
									self.assertEqual(logits.shape				,     SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  torch.tensor([-1.23_85, -1.09_87, -1.01_08]					).to(SCREAMING_SNAKE_CASE_					)

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

									_SCREAMING_SNAKE_CASE     				=  281
									self.assertEqual(logits.argmax(-1					).item()				,     SCREAMING_SNAKE_CASE_					)







			@slow
			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""					).to(
									    SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  self.default_image_processor
									_SCREAMING_SNAKE_CASE     				=  prepare_img()
									_SCREAMING_SNAKE_CASE     				=  image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors="""pt"""					).to(SCREAMING_SNAKE_CASE_					)

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE     				=  model(**SCREAMING_SNAKE_CASE_					)
									_SCREAMING_SNAKE_CASE     				=  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE     				=  torch.Size((1, 21_841)					)
									self.assertEqual(logits.shape				,     SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  torch.tensor([1.68_81, -0.27_87, 0.59_01]					).to(SCREAMING_SNAKE_CASE_					)

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

									_SCREAMING_SNAKE_CASE     				=  2_396
									self.assertEqual(logits.argmax(-1					).item()				,     SCREAMING_SNAKE_CASE_					)







			@slow
			def 							UpperCamelCase				(     self:  str					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""					)
									_SCREAMING_SNAKE_CASE     				=  model.to(SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE_				,     size=640				,     do_center_crop=SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  load_dataset("""hf-internal-testing/fixtures_ade20k"""				,     split="""test"""					)
									_SCREAMING_SNAKE_CASE     				=  Image.open(ds[0]["""file"""]					)
									_SCREAMING_SNAKE_CASE     				=  image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors="""pt"""					).to(SCREAMING_SNAKE_CASE_					)

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE     				=  model(**SCREAMING_SNAKE_CASE_					)
									_SCREAMING_SNAKE_CASE     				=  outputs.logits

									# verify the logits
									_SCREAMING_SNAKE_CASE     				=  torch.Size((1, 150, 160, 160)					)
									self.assertEqual(logits.shape				,     SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  version.parse(PIL.__version__					) < version.parse("""9.0.0"""					)

									if is_pillow_less_than_a:
															_SCREAMING_SNAKE_CASE     				=  torch.tensor(
															    [
															        [[-4.92_25, -2.39_54, -3.05_22], [-2.88_22, -1.00_46, -1.75_61], [-2.95_49, -1.32_28, -2.13_47]],
															        [[-5.81_68, -3.41_29, -4.07_78], [-3.86_51, -2.22_14, -3.02_77], [-3.83_56, -2.46_43, -3.35_35]],
															        [[-0.00_78, 3.99_52, 4.07_54], [2.98_56, 4.69_44, 5.00_35], [3.24_13, 4.78_13, 4.99_69]],
															    ]				,     device=SCREAMING_SNAKE_CASE_				,     )
									else:
															_SCREAMING_SNAKE_CASE     				=  torch.tensor(
															    [
															        [[-4.89_60, -2.36_88, -3.03_55], [-2.84_78, -0.98_36, -1.74_18], [-2.94_49, -1.33_32, -2.14_56]],
															        [[-5.80_81, -3.41_24, -4.10_06], [-3.85_61, -2.20_81, -3.03_23], [-3.83_65, -2.46_01, -3.36_69]],
															        [[-0.03_09, 3.98_68, 4.05_40], [2.96_40, 4.68_77, 4.99_76], [3.20_81, 4.76_90, 4.99_42]],
															    ]				,     device=SCREAMING_SNAKE_CASE_				,     )

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







			@slow
			def 							UpperCamelCase				(     self:  List[str]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""					)
									_SCREAMING_SNAKE_CASE     				=  model.to(SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE_				,     size=640				,     do_center_crop=SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  load_dataset("""hf-internal-testing/fixtures_ade20k"""				,     split="""test"""					)
									_SCREAMING_SNAKE_CASE     				=  Image.open(ds[0]["""file"""]					)
									_SCREAMING_SNAKE_CASE     				=  image_processor(images=SCREAMING_SNAKE_CASE_				,     return_tensors="""pt"""					).to(SCREAMING_SNAKE_CASE_					)

									# forward pass
									with torch.no_grad():
															_SCREAMING_SNAKE_CASE     				=  model(**SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  outputs.logits.detach().cpu()

									_SCREAMING_SNAKE_CASE     				=  image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE_				,     target_sizes=[(500, 300)]					)
									_SCREAMING_SNAKE_CASE     				=  torch.Size((500, 300)					)
									self.assertEqual(segmentation[0].shape				,     SCREAMING_SNAKE_CASE_					)

									_SCREAMING_SNAKE_CASE     				=  image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE_					)
									_SCREAMING_SNAKE_CASE     				=  torch.Size((160, 160)					)
									self.assertEqual(segmentation[0].shape				,     SCREAMING_SNAKE_CASE_					)









import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin



class 				__UpperCAmelCase		(_UpperCAmelCase	,unittest.TestCase   ):
			__snake_case    :				int									=							TransfoXLTokenizer
			__snake_case    :				Tuple									=							False
			__snake_case    :				List[Any]									=							False







			def 							UpperCamelCase				(     self:  int					):







									'''simple docstring'''




									super().setUp()

									_SCREAMING_SNAKE_CASE     				=  [
									    """<unk>""",
									    """[CLS]""",
									    """[SEP]""",
									    """want""",
									    """unwanted""",
									    """wa""",
									    """un""",
									    """running""",
									    """,""",
									    """low""",
									    """l""",
									]
									_SCREAMING_SNAKE_CASE     				=  os.path.join(self.tmpdirname				,     VOCAB_FILES_NAMES["""vocab_file"""]					)
									with open(self.vocab_file				,     """w"""				,     encoding="""utf-8"""					) as vocab_writer:
															vocab_writer.write("""""".join([x + """\n""" for x in vocab_tokens]					)					)







			def 							UpperCamelCase				(     self:  Any				,     **UpperCAmelCase_:  Optional[Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  True
									return TransfoXLTokenizer.from_pretrained(self.tmpdirname				,     **UpperCAmelCase_					)







			def 							UpperCamelCase				(     self:  Optional[int]				,     UpperCAmelCase_:  List[str]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  """<unk> UNwanted , running"""
									_SCREAMING_SNAKE_CASE     				=  """<unk> unwanted, running"""
									return input_text, output_text







			def 							UpperCamelCase				(     self:  Union[str, Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  TransfoXLTokenizer(vocab_file=self.vocab_file				,     lower_case=UpperCAmelCase_					)

									_SCREAMING_SNAKE_CASE     				=  tokenizer.tokenize("""<unk> UNwanted , running"""					)
									self.assertListEqual(UpperCAmelCase_				,     ["""<unk>""", """unwanted""", """,""", """running"""]					)

									self.assertListEqual(tokenizer.convert_tokens_to_ids(UpperCAmelCase_					)				,     [0, 4, 8, 7]					)







			def 							UpperCamelCase				(     self:  int					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  TransfoXLTokenizer(lower_case=UpperCAmelCase_					)

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """					)				,     ["""hello""", """!""", """how""", """are""", """you""", """?"""]					)







			def 							UpperCamelCase				(     self:  Optional[Any]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  TransfoXLTokenizer(lower_case=UpperCAmelCase_					)

									self.assertListEqual(
									    tokenizer.tokenize(""" \tHeLLo ! how  \n Are yoU ?  """					)				,     ["""HeLLo""", """!""", """how""", """Are""", """yoU""", """?"""]					)







			def 							UpperCamelCase				(     self:  List[str]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  TransfoXLTokenizer(lower_case=UpperCAmelCase_					)
									_SCREAMING_SNAKE_CASE     				=  """Hello (bracket) and side-scrolled [and] Henry's $5,000 with 3.34 m. What's up!?"""
									_SCREAMING_SNAKE_CASE     				=  [
									    """Hello""",
									    """(""",
									    """bracket""",
									    """)""",
									    """and""",
									    """side""",
									    """@-@""",
									    """scrolled""",
									    """[""",
									    """and""",
									    """]""",
									    """Henry""",
									    """'s""",
									    """$""",
									    """5""",
									    """@,@""",
									    """000""",
									    """with""",
									    """3""",
									    """@.@""",
									    """34""",
									    """m""",
									    """.""",
									    """What""",
									    """'s""",
									    """up""",
									    """!""",
									    """?""",
									]

									self.assertListEqual(tokenizer.tokenize(UpperCAmelCase_					)				,     UpperCAmelCase_					)

									self.assertEqual(tokenizer.convert_tokens_to_string(UpperCAmelCase_					)				,     UpperCAmelCase_					)







			def 							UpperCamelCase				(     self:  List[str]					):







									'''simple docstring'''




									_SCREAMING_SNAKE_CASE     				=  self.get_tokenizer()
									_SCREAMING_SNAKE_CASE     				=  len(UpperCAmelCase_					)

									tokenizer.add_tokens(["""new1""", """new2"""]					)
									tokenizer.move_added_token("""new1"""				,     1					)

									# Check that moved token is not copied (duplicate)
									self.assertEqual(len(UpperCAmelCase_					)				,     original_len + 2					)
									# Check that token is moved to specified id
									self.assertEqual(tokenizer.encode("""new1"""					)				,     [1]					)
									self.assertEqual(tokenizer.decode([1]					)				,     """new1"""					)

