



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 				snake_case__:

					"""simple docstring"""






					def __init__(							self :     int				,       SCREAMING_SNAKE_CASE :     List[str]				,       SCREAMING_SNAKE_CASE :     Any=100				,       SCREAMING_SNAKE_CASE :     List[str]=13				,       SCREAMING_SNAKE_CASE :     Any=30				,       SCREAMING_SNAKE_CASE :     Any=2				,       SCREAMING_SNAKE_CASE :     Optional[int]=3				,       SCREAMING_SNAKE_CASE :     Tuple=True				,       SCREAMING_SNAKE_CASE :     Optional[int]=True				,       SCREAMING_SNAKE_CASE :     str=32				,       SCREAMING_SNAKE_CASE :     Dict=4				,       SCREAMING_SNAKE_CASE :     Dict=4				,       SCREAMING_SNAKE_CASE :     List[Any]=37				,       SCREAMING_SNAKE_CASE :     List[Any]="gelu"				,       SCREAMING_SNAKE_CASE :     Dict=0.1				,       SCREAMING_SNAKE_CASE :     Any=0.1				,       SCREAMING_SNAKE_CASE :     Optional[int]=10				,       SCREAMING_SNAKE_CASE :     List[Any]=0.02				,       SCREAMING_SNAKE_CASE :     Union[str, Any]=3				,       SCREAMING_SNAKE_CASE :     str=None				,       SCREAMING_SNAKE_CASE :     Optional[Any]=[0, 1, 2, 3]				,       ):
									lowercase__					:   Any													=					parent
									lowercase__					:   List[str]													=					100
									lowercase__					:   Tuple													=					batch_size
									lowercase__					:   Optional[int]													=					image_size
									lowercase__					:   int													=					patch_size
									lowercase__					:   List[str]													=					num_channels
									lowercase__					:   int													=					is_training
									lowercase__					:   int													=					use_labels
									lowercase__					:   Dict													=					hidden_size
									lowercase__					:   Optional[Any]													=					num_hidden_layers
									lowercase__					:   List[Any]													=					num_attention_heads
									lowercase__					:   Tuple													=					intermediate_size
									lowercase__					:   List[Any]													=					hidden_act
									lowercase__					:   Any													=					hidden_dropout_prob
									lowercase__					:   int													=					attention_probs_dropout_prob
									lowercase__					:   int													=					type_sequence_label_size
									lowercase__					:   Optional[int]													=					initializer_range
									lowercase__					:   List[Any]													=					scope
									lowercase__					:   Optional[Any]													=					out_indices
									lowercase__					:   List[str]													=					num_labels

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







					def 			snake_case					(							self :     List[str]					):
									lowercase__					:   Optional[int]													=					floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]					)

									lowercase__					:   Optional[int]													=					None
									lowercase__					:   Optional[int]													=					None
									if self.use_labels:
													lowercase__					:   Optional[Any]													=					ids_tensor([self.batch_size]				,       self.type_sequence_label_size					)
													lowercase__					:   str													=					ids_tensor([self.batch_size, self.image_size, self.image_size]				,       self.num_labels					)

									lowercase__					:   List[str]													=					self.get_config()

									return config, pixel_values, labels, pixel_labels







					def 			snake_case					(							self :     str					):
									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 			snake_case					(							self :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Optional[int]				,       SCREAMING_SNAKE_CASE :     Dict				,       SCREAMING_SNAKE_CASE :     List[Any]				,       SCREAMING_SNAKE_CASE :     int					):
									lowercase__					:   Any													=					BeitModel(config=SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   Dict													=					model(SCREAMING_SNAKE_CASE					)
									self.parent.assertEqual(result.last_hidden_state.shape				,       (self.batch_size, self.seq_length, self.hidden_size)					)







					def 			snake_case					(							self :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     Dict				,       SCREAMING_SNAKE_CASE :     Any				,       SCREAMING_SNAKE_CASE :     Dict				,       SCREAMING_SNAKE_CASE :     Optional[int]					):
									lowercase__					:   Optional[int]													=					BeitForMaskedImageModeling(config=SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   int													=					model(SCREAMING_SNAKE_CASE					)
									self.parent.assertEqual(result.logits.shape				,       (self.batch_size, self.seq_length - 1, self.vocab_size)					)







					def 			snake_case					(							self :     Any				,       SCREAMING_SNAKE_CASE :     Dict				,       SCREAMING_SNAKE_CASE :     List[str]				,       SCREAMING_SNAKE_CASE :     List[Any]				,       SCREAMING_SNAKE_CASE :     Dict					):
									lowercase__					:   Dict													=					self.type_sequence_label_size
									lowercase__					:   Optional[int]													=					BeitForImageClassification(SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   Union[str, Any]													=					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
									lowercase__					:   str													=					1
									lowercase__					:   Union[str, Any]													=					BeitForImageClassification(SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()

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







					def 			snake_case					(							self :     Dict				,       SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     Dict				,       SCREAMING_SNAKE_CASE :     Optional[Any]				,       SCREAMING_SNAKE_CASE :     str					):
									lowercase__					:   Union[str, Any]													=					self.num_labels
									lowercase__					:   List[str]													=					BeitForSemanticSegmentation(SCREAMING_SNAKE_CASE					)
									model.to(SCREAMING_SNAKE_CASE					)
									model.eval()
									lowercase__					:   Optional[int]													=					model(SCREAMING_SNAKE_CASE					)
									self.parent.assertEqual(
									    result.logits.shape				,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)					)
									lowercase__					:   List[str]													=					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 			snake_case					(							self :     Any					):
									lowercase__					:   Dict													=					self.prepare_config_and_inputs()
									lowercase__							,							lowercase__							,							lowercase__							,							lowercase__					:   List[Any]													=					config_and_inputs
									lowercase__					:   Dict													=					{"pixel_values": pixel_values}
									return config, inputs_dict






@require_torch
class 				snake_case__(_UpperCamelCase    ,      _UpperCamelCase    ,      unittest.TestCase      ):

					"""simple docstring"""






					lowercase_								=		(
					    (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
					    if is_torch_available()
					    else ()
					)
					lowercase_								=		(
					    {
					        """feature-extraction""": BeitModel,
					        """image-classification""": BeitForImageClassification,
					        """image-segmentation""": BeitForSemanticSegmentation,
					    }
					    if is_torch_available()
					    else {}
					)

					lowercase_								=		False
					lowercase_								=		False
					lowercase_								=		False







					def 			snake_case					(							self :     List[Any]					):
									lowercase__					:   List[Any]													=					BeitModelTester(self					)
									lowercase__					:   Tuple													=					ConfigTester(self				,       config_class=SCREAMING_SNAKE_CASE				,       has_text_modality=SCREAMING_SNAKE_CASE				,       hidden_size=37					)







					def 			snake_case					(							self :     Optional[Any]					):
									self.config_tester.run_common_tests()







					@unittest.skip(reason="BEiT does not use inputs_embeds"					)
					def 			snake_case					(							self :     int					):
									pass







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







					def 			snake_case					(							self :     str					):
									lowercase__							,							lowercase__					:   List[str]													=					self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
													lowercase__					:   Optional[Any]													=					model_class(SCREAMING_SNAKE_CASE					)
													self.assertIsInstance(model.get_input_embeddings()				,       (nn.Module)					)
													lowercase__					:   Dict													=					model.get_output_embeddings()
													self.assertTrue(x is None or isinstance(SCREAMING_SNAKE_CASE				,       nn.Linear					)					)







					def 			snake_case					(							self :     int					):
									lowercase__							,							lowercase__					:   List[str]													=					self.model_tester.prepare_config_and_inputs_for_common()

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

													lowercase__					:   Any													=					["pixel_values"]
													self.assertListEqual(arg_names[:1]				,       SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     List[Any]					):
									lowercase__					:   List[Any]													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     str					):
									lowercase__					:   str													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     List[str]					):
									lowercase__					:   Tuple													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_classification(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     int					):
									lowercase__					:   Union[str, Any]													=					self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_semantic_segmentation(*SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     Tuple					):
									if not self.model_tester.is_training:
													return

									lowercase__							,							lowercase__					:   Dict													=					self.model_tester.prepare_config_and_inputs_for_common()
									lowercase__					:   Any													=					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

													lowercase__					:   Any													=					model_class(SCREAMING_SNAKE_CASE					)
													model.to(SCREAMING_SNAKE_CASE					)
													model.train()
													lowercase__					:   Tuple													=					self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       return_labels=SCREAMING_SNAKE_CASE					)
													lowercase__					:   Union[str, Any]													=					model(**SCREAMING_SNAKE_CASE					).loss
													loss.backward()







					def 			snake_case					(							self :     Tuple					):
									lowercase__							,							lowercase__					:   Tuple													=					self.model_tester.prepare_config_and_inputs_for_common()
									if not self.model_tester.is_training:
													return

									lowercase__					:   Optional[Any]													=					False
									lowercase__					:   Dict													=					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

													lowercase__					:   int													=					model_class(SCREAMING_SNAKE_CASE					)
													model.gradient_checkpointing_enable()
													model.to(SCREAMING_SNAKE_CASE					)
													model.train()
													lowercase__					:   List[str]													=					self._prepare_for_class(SCREAMING_SNAKE_CASE				,       SCREAMING_SNAKE_CASE				,       return_labels=SCREAMING_SNAKE_CASE					)
													lowercase__					:   List[str]													=					model(**SCREAMING_SNAKE_CASE					).loss
													loss.backward()







					def 			snake_case					(							self :     List[str]					):
									lowercase__							,							lowercase__					:   int													=					self.model_tester.prepare_config_and_inputs_for_common()

									lowercase__					:   str													=					_config_zero_init(SCREAMING_SNAKE_CASE					)
									for model_class in self.all_model_classes:
													lowercase__					:   List[str]													=					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 			snake_case					(							self :     Optional[Any]					):
									for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													lowercase__					:   Any													=					BeitModel.from_pretrained(SCREAMING_SNAKE_CASE					)
													self.assertIsNotNone(SCREAMING_SNAKE_CASE					)






def  __lowerCamelCase (       ):

				"""simple docstring"""


				lowercase__					:   Union[str, Any]													=					Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
				return image

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

					"""simple docstring"""






					@cached_property
					def 			snake_case					(							self :     List[Any]					):
									return BeitImageProcessor.from_pretrained("microsoft/beit-base-patch16-224"					) if is_vision_available() else None







					@slow
					def 			snake_case					(							self :     Any					):
									lowercase__					:   Dict													=					BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k"					).to(SCREAMING_SNAKE_CASE					)

									lowercase__					:   Tuple													=					self.default_image_processor
									lowercase__					:   int													=					prepare_img()
									lowercase__					:   List[Any]													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					).pixel_values.to(SCREAMING_SNAKE_CASE					)

									# prepare bool_masked_pos
									lowercase__					:   Union[str, Any]													=					torch.ones((1, 196)				,       dtype=torch.bool					).to(SCREAMING_SNAKE_CASE					)

									# forward pass
									with torch.no_grad():
													lowercase__					:   Dict													=					model(pixel_values=SCREAMING_SNAKE_CASE				,       bool_masked_pos=SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[int]													=					outputs.logits

									# verify the logits
									lowercase__					:   Optional[int]													=					torch.Size((1, 196, 8_192)					)
									self.assertEqual(logits.shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   Dict													=					torch.tensor(
									    [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -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 			snake_case					(							self :     Optional[int]					):
									lowercase__					:   Union[str, Any]													=					BeitForImageClassification.from_pretrained("microsoft/beit-base-patch16-224"					).to(SCREAMING_SNAKE_CASE					)

									lowercase__					:   Tuple													=					self.default_image_processor
									lowercase__					:   Optional[int]													=					prepare_img()
									lowercase__					:   Any													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					).to(SCREAMING_SNAKE_CASE					)

									# forward pass
									with torch.no_grad():
													lowercase__					:   Optional[Any]													=					model(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Union[str, Any]													=					outputs.logits

									# verify the logits
									lowercase__					:   Optional[Any]													=					torch.Size((1, 1_000)					)
									self.assertEqual(logits.shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   Tuple													=					torch.tensor([-1.2_385, -1.0_987, -1.0_108]					).to(SCREAMING_SNAKE_CASE					)

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

									lowercase__					:   int													=					281
									self.assertEqual(logits.argmax(-1					).item()				,       SCREAMING_SNAKE_CASE					)







					@slow
					def 			snake_case					(							self :     int					):
									lowercase__					:   Dict													=					BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k"					).to(
									    SCREAMING_SNAKE_CASE					)

									lowercase__					:   Dict													=					self.default_image_processor
									lowercase__					:   Any													=					prepare_img()
									lowercase__					:   str													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					).to(SCREAMING_SNAKE_CASE					)

									# forward pass
									with torch.no_grad():
													lowercase__					:   Any													=					model(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   str													=					outputs.logits

									# verify the logits
									lowercase__					:   Optional[int]													=					torch.Size((1, 21_841)					)
									self.assertEqual(logits.shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   Dict													=					torch.tensor([1.6_881, -0.2_787, 0.5_901]					).to(SCREAMING_SNAKE_CASE					)

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

									lowercase__					:   int													=					2_396
									self.assertEqual(logits.argmax(-1					).item()				,       SCREAMING_SNAKE_CASE					)







					@slow
					def 			snake_case					(							self :     Dict					):
									lowercase__					:   List[Any]													=					BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640"					)
									lowercase__					:   List[str]													=					model.to(SCREAMING_SNAKE_CASE					)

									lowercase__					:   Optional[int]													=					BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE				,       size=640				,       do_center_crop=SCREAMING_SNAKE_CASE					)

									lowercase__					:   str													=					load_dataset("hf-internal-testing/fixtures_ade20k"				,       split="test"					)
									lowercase__					:   Tuple													=					Image.open(ds[0]["file"]					)
									lowercase__					:   List[str]													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					).to(SCREAMING_SNAKE_CASE					)

									# forward pass
									with torch.no_grad():
													lowercase__					:   str													=					model(**SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[Any]													=					outputs.logits

									# verify the logits
									lowercase__					:   str													=					torch.Size((1, 150, 160, 160)					)
									self.assertEqual(logits.shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   int													=					version.parse(PIL.__version__					) < version.parse("9.0.0"					)

									if is_pillow_less_than_a:
													lowercase__					:   Optional[int]													=					torch.tensor(
													    [
													        [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]],
													        [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]],
													        [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]],
													    ]				,       device=SCREAMING_SNAKE_CASE				,       )
									else:
													lowercase__					:   Optional[int]													=					torch.tensor(
													    [
													        [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]],
													        [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]],
													        [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]],
													    ]				,       device=SCREAMING_SNAKE_CASE				,       )

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







					@slow
					def 			snake_case					(							self :     Any					):
									lowercase__					:   Any													=					BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640"					)
									lowercase__					:   Optional[int]													=					model.to(SCREAMING_SNAKE_CASE					)

									lowercase__					:   int													=					BeitImageProcessor(do_resize=SCREAMING_SNAKE_CASE				,       size=640				,       do_center_crop=SCREAMING_SNAKE_CASE					)

									lowercase__					:   Tuple													=					load_dataset("hf-internal-testing/fixtures_ade20k"				,       split="test"					)
									lowercase__					:   Any													=					Image.open(ds[0]["file"]					)
									lowercase__					:   List[str]													=					image_processor(images=SCREAMING_SNAKE_CASE				,       return_tensors="pt"					).to(SCREAMING_SNAKE_CASE					)

									# forward pass
									with torch.no_grad():
													lowercase__					:   int													=					model(**SCREAMING_SNAKE_CASE					)

									lowercase__					:   Tuple													=					outputs.logits.detach().cpu()

									lowercase__					:   Optional[int]													=					image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE				,       target_sizes=[(500, 300)]					)
									lowercase__					:   List[str]													=					torch.Size((500, 300)					)
									self.assertEqual(segmentation[0].shape				,       SCREAMING_SNAKE_CASE					)

									lowercase__					:   List[str]													=					image_processor.post_process_semantic_segmentation(outputs=SCREAMING_SNAKE_CASE					)
									lowercase__					:   Optional[Any]													=					torch.Size((160, 160)					)
									self.assertEqual(segmentation[0].shape				,       SCREAMING_SNAKE_CASE					)









import os
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 logging


lowerCAmelCase__					  =  logging.get_logger(__name__)

lowerCAmelCase__					  =  '''▁'''

lowerCAmelCase__					  =  {'''vocab_file''': '''sentencepiece.bpe.model''', '''monolingual_vocab_file''': '''dict.txt'''}

lowerCAmelCase__					  =  {
    '''vocab_file''': {
        '''vinai/bartpho-syllable''': '''https://huggingface.co/vinai/bartpho-syllable/resolve/main/sentencepiece.bpe.model''',
    },
    '''monolingual_vocab_file''': {
        '''vinai/bartpho-syllable''': '''https://huggingface.co/vinai/bartpho-syllable/resolve/main/dict.txt''',
    },
}

lowerCAmelCase__					  =  {'''vinai/bartpho-syllable''': 1_0_2_4}

class 				snake_case__(_UpperCamelCase      ):

					"""simple docstring"""






					lowercase_								=		VOCAB_FILES_NAMES
					lowercase_								=		PRETRAINED_VOCAB_FILES_MAP
					lowercase_								=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
					lowercase_								=		["""input_ids""", """attention_mask"""]







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

									lowercase__					:   int													=					{} if sp_model_kwargs is None else sp_model_kwargs

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

									lowercase__					:   Dict													=					vocab_file
									lowercase__					:   Union[str, Any]													=					monolingual_vocab_file
									lowercase__					:   Union[str, Any]													=					spm.SentencePieceProcessor(**self.sp_model_kwargs					)
									self.sp_model.Load(str(SCREAMING_SNAKE_CASE					)					)

									# Load the reduced vocab

									# Keep order of special tokens for backward compatibility
									lowercase__					:   Any													=					{}
									lowercase__					:   int													=					0
									for token in [bos_token, pad_token, eos_token, unk_token, sep_token, cls_token]:
													if str(SCREAMING_SNAKE_CASE					) not in self.fairseq_tokens_to_ids:
																	lowercase__					:   Dict													=					cnt
																	cnt += 1
									with open(SCREAMING_SNAKE_CASE				,       "r"				,       encoding="utf-8"					) as f:
													for line in f.readlines():
																	lowercase__					:   int													=					line.strip().split()[0]
																	lowercase__					:   List[str]													=					len(self.fairseq_tokens_to_ids					)
									if str(SCREAMING_SNAKE_CASE					) not in self.fairseq_tokens_to_ids:
													lowercase__					:   Optional[Any]													=					len(self.fairseq_tokens_to_ids					)

									lowercase__					:   List[str]													=					{v: k for k, v in self.fairseq_tokens_to_ids.items()}







					def __getstate__(							self :     int					):
									lowercase__					:   Dict													=					self.__dict__.copy()
									lowercase__					:   Union[str, Any]													=					None
									lowercase__					:   Dict													=					self.sp_model.serialized_model_proto()
									return state







					def __setstate__(							self :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     Optional[Any]					):
									lowercase__					:   Union[str, Any]													=					d

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

									lowercase__					:   List[str]													=					spm.SentencePieceProcessor(**self.sp_model_kwargs					)
									self.sp_model.LoadFromSerializedProto(self.sp_model_proto					)







					def 			snake_case					(							self :     Optional[int]				,       SCREAMING_SNAKE_CASE :     List[int]				,       SCREAMING_SNAKE_CASE :     Optional[List[int]] = None					):

									if token_ids_a is None:
													return [self.cls_token_id] + token_ids_a + [self.sep_token_id]
									lowercase__					:   List[Any]													=					[self.cls_token_id]
									lowercase__					:   str													=					[self.sep_token_id]
									return cls + token_ids_a + sep + sep + token_ids_a + sep







					def 			snake_case					(							self :     Dict				,       SCREAMING_SNAKE_CASE :     List[int]				,       SCREAMING_SNAKE_CASE :     Optional[List[int]] = None				,       SCREAMING_SNAKE_CASE :     bool = False					):

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

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







					def 			snake_case					(							self :     str				,       SCREAMING_SNAKE_CASE :     List[int]				,       SCREAMING_SNAKE_CASE :     Optional[List[int]] = None					):
									lowercase__					:   Tuple													=					[self.sep_token_id]
									lowercase__					:   str													=					[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 + sep + token_ids_a + sep					) * [0]







					@property
					def 			snake_case					(							self :     Optional[int]					):
									return len(self.fairseq_ids_to_tokens					)







					def 			snake_case					(							self :     List[Any]					):
									lowercase__					:   Any													=					{self.convert_ids_to_tokens(SCREAMING_SNAKE_CASE					): i for i in range(self.vocab_size					)}
									vocab.update(self.added_tokens_encoder					)
									return vocab







					def 			snake_case					(							self :     Union[str, Any]				,       SCREAMING_SNAKE_CASE :     str					):
									return self.sp_model.encode(SCREAMING_SNAKE_CASE				,       out_type=SCREAMING_SNAKE_CASE					)







					def 			snake_case					(							self :     List[str]				,       SCREAMING_SNAKE_CASE :     int					):
									if token in self.fairseq_tokens_to_ids:
													return self.fairseq_tokens_to_ids[token]
									else:
													return self.unk_token_id







					def 			snake_case					(							self :     List[str]				,       SCREAMING_SNAKE_CASE :     Any					):
									return self.fairseq_ids_to_tokens[index]







					def 			snake_case					(							self :     List[Any]				,       SCREAMING_SNAKE_CASE :     Union[str, Any]					):
									lowercase__					:   str													=					"".join(SCREAMING_SNAKE_CASE					).replace(SCREAMING_SNAKE_CASE				,       " "					).strip()
									return out_string







					def 			snake_case					(							self :     Optional[int]				,       SCREAMING_SNAKE_CASE :     str				,       SCREAMING_SNAKE_CASE :     Optional[str] = None					):
									if not os.path.isdir(SCREAMING_SNAKE_CASE					):
													logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""					)
													return
									lowercase__					:   str													=					os.path.join(
									    SCREAMING_SNAKE_CASE				,       (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["vocab_file"]					)
									lowercase__					:   List[str]													=					os.path.join(
									    SCREAMING_SNAKE_CASE				,       (filename_prefix + "-" if filename_prefix else "") + VOCAB_FILES_NAMES["monolingual_vocab_file"]				,       )

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

									if os.path.abspath(self.monolingual_vocab_file					) != os.path.abspath(
									    SCREAMING_SNAKE_CASE					) and os.path.isfile(self.monolingual_vocab_file					):
													copyfile(self.monolingual_vocab_file				,       SCREAMING_SNAKE_CASE					)
									elif not os.path.isfile(self.monolingual_vocab_file					):
													with open(SCREAMING_SNAKE_CASE				,       "w"				,       encoding="utf-8"					) as fp:
																	for token in self.fairseq_tokens_to_ids:
																					if token not in self.all_special_tokens:
																									fp.write(f"""{str(SCREAMING_SNAKE_CASE					)} \n"""					)

									return out_vocab_file, out_monolingual_vocab_file




