



"""simple docstring"""






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




	'''simple docstring'''





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

				"""simple docstring"""

				__lowerCAmelCase   :						Any				= parent
				__lowerCAmelCase   :						Dict				= 100
				__lowerCAmelCase   :						Tuple				= batch_size
				__lowerCAmelCase   :						Tuple				= image_size
				__lowerCAmelCase   :						Dict				= patch_size
				__lowerCAmelCase   :						Dict				= num_channels
				__lowerCAmelCase   :						Tuple				= is_training
				__lowerCAmelCase   :						List[str]				= use_labels
				__lowerCAmelCase   :						Optional[int]				= hidden_size
				__lowerCAmelCase   :						List[str]				= num_hidden_layers
				__lowerCAmelCase   :						int				= num_attention_heads
				__lowerCAmelCase   :						Optional[Any]				= intermediate_size
				__lowerCAmelCase   :						Any				= hidden_act
				__lowerCAmelCase   :						Tuple				= hidden_dropout_prob
				__lowerCAmelCase   :						Any				= attention_probs_dropout_prob
				__lowerCAmelCase   :						Tuple				= type_sequence_label_size
				__lowerCAmelCase   :						Dict				= initializer_range
				__lowerCAmelCase   :						Optional[Any]				= scope
				__lowerCAmelCase   :						Optional[int]				= out_indices
				__lowerCAmelCase   :						Optional[int]				= num_labels

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

	def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> Union[str, Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

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

				__lowerCAmelCase   :						Optional[Any]				= self.get_config()

				return config, pixel_values, labels, pixel_labels

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str])       -> Tuple:

				"""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 							_SCREAMING_SNAKE_CASE     (  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	Dict ,    _SCREAMING_SNAKE_CASE:	str ,    _SCREAMING_SNAKE_CASE:	List[Any] ,    _SCREAMING_SNAKE_CASE:	Dict)       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase   :						Dict				= BeitModel(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase   :						Optional[Any]				= model(_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size))

	def 							_SCREAMING_SNAKE_CASE     (  self:	str ,    _SCREAMING_SNAKE_CASE:	Optional[Any] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	Optional[Any])       -> Union[str, Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						List[str]				= BeitForMaskedImageModeling(config=_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase   :						Optional[int]				= model(_SCREAMING_SNAKE_CASE)
				self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length - 1, self.vocab_size))

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	Union[str, Any] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	List[str])       -> int:

				"""simple docstring"""

				__lowerCAmelCase   :						Dict				= self.type_sequence_label_size
				__lowerCAmelCase   :						Optional[Any]				= BeitForImageClassification(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase   :						Optional[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
				__lowerCAmelCase   :						Tuple				= 1
				__lowerCAmelCase   :						Optional[Any]				= BeitForImageClassification(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()

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

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str] ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	Tuple ,    _SCREAMING_SNAKE_CASE:	Optional[int] ,    _SCREAMING_SNAKE_CASE:	Optional[int])       -> Dict:

				"""simple docstring"""

				__lowerCAmelCase   :						Dict				= self.num_labels
				__lowerCAmelCase   :						Optional[int]				= BeitForSemanticSegmentation(_SCREAMING_SNAKE_CASE)
				model.to(_SCREAMING_SNAKE_CASE)
				model.eval()
				__lowerCAmelCase   :						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))
				__lowerCAmelCase   :						Any				= 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 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> List[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Any				= self.prepare_config_and_inputs()
				__lowerCAmelCase		,			__lowerCAmelCase		,			__lowerCAmelCase		,			__lowerCAmelCase   :						Union[str, Any]				= config_and_inputs
				__lowerCAmelCase   :						Optional[int]				= {"pixel_values": pixel_values}
				return config, inputs_dict






@require_torch
class     A__     (						__SCREAMING_SNAKE_CASE						,	__SCREAMING_SNAKE_CASE						,	unittest.TestCase							):




	'''simple docstring'''





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

	SCREAMING_SNAKE_CASE					   =			False
	SCREAMING_SNAKE_CASE					   =			False
	SCREAMING_SNAKE_CASE					   =			False

	def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= BeitModelTester(self)
				__lowerCAmelCase   :						List[str]				= ConfigTester(self ,    config_class=_SCREAMING_SNAKE_CASE ,    has_text_modality=_SCREAMING_SNAKE_CASE ,    hidden_size=37)

	def 							_SCREAMING_SNAKE_CASE     (  self:	str)       -> Dict:

				"""simple docstring"""

				self.config_tester.run_common_tests()

	@unittest.skip(reason="BEiT does not use inputs_embeds")
	def 							_SCREAMING_SNAKE_CASE     (  self:	Union[str, Any])       -> List[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 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> Optional[int]:

				"""simple docstring"""

				pass

	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> Dict:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						Dict				= self.model_tester.prepare_config_and_inputs_for_common()

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

	def 							_SCREAMING_SNAKE_CASE     (  self:	Union[str, Any])       -> List[Any]:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						int				= self.model_tester.prepare_config_and_inputs_for_common()

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

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

	def 							_SCREAMING_SNAKE_CASE     (  self:	Union[str, Any])       -> Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[int]				= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> List[str]:

				"""simple docstring"""

				__lowerCAmelCase   :						Union[str, Any]				= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_masked_lm(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any])       -> List[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Union[str, Any])       -> str:

				"""simple docstring"""

				__lowerCAmelCase   :						int				= self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_semantic_segmentation(*_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any])       -> List[str]:

				"""simple docstring"""

				if not self.model_tester.is_training:
							return

				__lowerCAmelCase		,			__lowerCAmelCase   :						Optional[int]				= self.model_tester.prepare_config_and_inputs_for_common()
				__lowerCAmelCase   :						List[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

							__lowerCAmelCase   :						List[Any]				= model_class(_SCREAMING_SNAKE_CASE)
							model.to(_SCREAMING_SNAKE_CASE)
							model.train()
							__lowerCAmelCase   :						Union[str, Any]				= self._prepare_for_class(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE ,    return_labels=_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase   :						Optional[Any]				= model(**_SCREAMING_SNAKE_CASE).loss
							loss.backward()

	def 							_SCREAMING_SNAKE_CASE     (  self:	List[str])       -> List[Any]:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						str				= self.model_tester.prepare_config_and_inputs_for_common()
				if not self.model_tester.is_training:
							return

				__lowerCAmelCase   :						List[Any]				= False
				__lowerCAmelCase   :						List[str]				= 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

							__lowerCAmelCase   :						Tuple				= model_class(_SCREAMING_SNAKE_CASE)
							model.gradient_checkpointing_enable()
							model.to(_SCREAMING_SNAKE_CASE)
							model.train()
							__lowerCAmelCase   :						Union[str, Any]				= self._prepare_for_class(_SCREAMING_SNAKE_CASE ,    _SCREAMING_SNAKE_CASE ,    return_labels=_SCREAMING_SNAKE_CASE)
							__lowerCAmelCase   :						Optional[int]				= model(**_SCREAMING_SNAKE_CASE).loss
							loss.backward()

	def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> Optional[int]:

				"""simple docstring"""

				__lowerCAmelCase		,			__lowerCAmelCase   :						List[Any]				= self.model_tester.prepare_config_and_inputs_for_common()

				__lowerCAmelCase   :						Optional[Any]				= _config_zero_init(_SCREAMING_SNAKE_CASE)
				for model_class in self.all_model_classes:
							__lowerCAmelCase   :						Dict				= 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 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> int:

				"""simple docstring"""

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






def     _lowercase       (						)		->     Optional[Any]:
			__lowerCAmelCase   :						Union[str, Any]				= Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"			)
			return image





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




	'''simple docstring'''





	@cached_property
	def 							_SCREAMING_SNAKE_CASE     (  self:	List[Any])       -> List[Any]:

				"""simple docstring"""

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

	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	Tuple)       -> Union[str, Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[Any]				= BeitForMaskedImageModeling.from_pretrained("microsoft/beit-base-patch16-224-pt22k").to(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						Optional[int]				= self.default_image_processor
				__lowerCAmelCase   :						Union[str, Any]				= prepare_img()
				__lowerCAmelCase   :						int				= image_processor(images=_SCREAMING_SNAKE_CASE ,    return_tensors="pt").pixel_values.to(_SCREAMING_SNAKE_CASE)

				# prepare bool_masked_pos
				__lowerCAmelCase   :						Optional[int]				= torch.ones((1, 196) ,    dtype=torch.bool).to(_SCREAMING_SNAKE_CASE)

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

				# verify the logits
				__lowerCAmelCase   :						Union[str, Any]				= torch.Size((1, 196, 8192))
				self.assertEqual(logits.shape ,    _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						Optional[int]				= torch.tensor(
				    [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]).to(_SCREAMING_SNAKE_CASE)

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

	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	int)       -> Any:

				"""simple docstring"""

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

				__lowerCAmelCase   :						str				= self.default_image_processor
				__lowerCAmelCase   :						List[str]				= prepare_img()
				__lowerCAmelCase   :						Dict				= image_processor(images=_SCREAMING_SNAKE_CASE ,    return_tensors="pt").to(_SCREAMING_SNAKE_CASE)

				# forward pass
				with torch.no_grad():
							__lowerCAmelCase   :						Union[str, Any]				= model(**_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						Tuple				= outputs.logits

				# verify the logits
				__lowerCAmelCase   :						List[Any]				= torch.Size((1, 1000))
				self.assertEqual(logits.shape ,    _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						Optional[int]				= torch.tensor([-1.2385, -1.0987, -1.0108]).to(_SCREAMING_SNAKE_CASE)

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

				__lowerCAmelCase   :						int				= 281
				self.assertEqual(logits.argmax(-1).item() ,    _SCREAMING_SNAKE_CASE)

	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	Any)       -> Optional[Any]:

				"""simple docstring"""

				__lowerCAmelCase   :						Optional[int]				= BeitForImageClassification.from_pretrained("microsoft/beit-large-patch16-224-pt22k-ft22k").to(
				    _SCREAMING_SNAKE_CASE)

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

				# forward pass
				with torch.no_grad():
							__lowerCAmelCase   :						List[str]				= model(**_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						Optional[int]				= outputs.logits

				# verify the logits
				__lowerCAmelCase   :						Dict				= torch.Size((1, 2_1841))
				self.assertEqual(logits.shape ,    _SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						List[Any]				= torch.tensor([1.6881, -0.2787, 0.5901]).to(_SCREAMING_SNAKE_CASE)

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

				__lowerCAmelCase   :						Tuple				= 2396
				self.assertEqual(logits.argmax(-1).item() ,    _SCREAMING_SNAKE_CASE)

	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	str)       -> Tuple:

				"""simple docstring"""

				__lowerCAmelCase   :						List[str]				= BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640")
				__lowerCAmelCase   :						List[str]				= model.to(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						Union[str, Any]				= BeitImageProcessor(do_resize=_SCREAMING_SNAKE_CASE ,    size=640 ,    do_center_crop=_SCREAMING_SNAKE_CASE)

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

				# forward pass
				with torch.no_grad():
							__lowerCAmelCase   :						Union[str, Any]				= model(**_SCREAMING_SNAKE_CASE)
				__lowerCAmelCase   :						Tuple				= outputs.logits

				# verify the logits
				__lowerCAmelCase   :						Union[str, Any]				= torch.Size((1, 150, 160, 160))
				self.assertEqual(logits.shape ,    _SCREAMING_SNAKE_CASE)

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

				if is_pillow_less_than_a:
							__lowerCAmelCase   :						str				= torch.tensor(
							    [
							        [[-4.9225, -2.3954, -3.0522], [-2.8822, -1.0046, -1.7561], [-2.9549, -1.3228, -2.1347]],
							        [[-5.8168, -3.4129, -4.0778], [-3.8651, -2.2214, -3.0277], [-3.8356, -2.4643, -3.3535]],
							        [[-0.0078, 3.9952, 4.0754], [2.9856, 4.6944, 5.0035], [3.2413, 4.7813, 4.9969]],
							    ] ,    device=_SCREAMING_SNAKE_CASE ,    )
				else:
							__lowerCAmelCase   :						List[Any]				= torch.tensor(
							    [
							        [[-4.8960, -2.3688, -3.0355], [-2.8478, -0.9836, -1.7418], [-2.9449, -1.3332, -2.1456]],
							        [[-5.8081, -3.4124, -4.1006], [-3.8561, -2.2081, -3.0323], [-3.8365, -2.4601, -3.3669]],
							        [[-0.0309, 3.9868, 4.0540], [2.9640, 4.6877, 4.9976], [3.2081, 4.7690, 4.9942]],
							    ] ,    device=_SCREAMING_SNAKE_CASE ,    )

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







	@slow
	def 							_SCREAMING_SNAKE_CASE     (  self:	Optional[Any])       -> Dict:

				"""simple docstring"""

				__lowerCAmelCase   :						str				= BeitForSemanticSegmentation.from_pretrained("microsoft/beit-base-finetuned-ade-640-640")
				__lowerCAmelCase   :						int				= model.to(_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						List[str]				= BeitImageProcessor(do_resize=_SCREAMING_SNAKE_CASE ,    size=640 ,    do_center_crop=_SCREAMING_SNAKE_CASE)

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

				# forward pass
				with torch.no_grad():
							__lowerCAmelCase   :						List[Any]				= model(**_SCREAMING_SNAKE_CASE)

				__lowerCAmelCase   :						Any				= outputs.logits.detach().cpu()

				__lowerCAmelCase   :						Optional[Any]				= image_processor.post_process_semantic_segmentation(outputs=_SCREAMING_SNAKE_CASE ,    target_sizes=[(500, 300)])
				__lowerCAmelCase   :						int				= torch.Size((500, 300))
				self.assertEqual(segmentation[0].shape ,    _SCREAMING_SNAKE_CASE)

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




"""simple docstring"""






import string
from math import logaa


def     _lowercase       (						__snake_case      ,__snake_case			)		->     int:
			__lowerCAmelCase   :						int				= document.translate(
			    str.maketrans(""      ,""      ,string.punctuation			)			).replace("\n"      ,""			)
			__lowerCAmelCase   :						Dict				= document_without_punctuation.split(" "			)  # word tokenization
			return len([word for word in tokenize_document if word.lower() == term.lower()]			)


def     _lowercase       (						__snake_case      ,__snake_case			)		->     tuple[int, int]:
			__lowerCAmelCase   :						Optional[Any]				= corpus.lower().translate(
			    str.maketrans(""      ,""      ,string.punctuation			)			)  # strip all punctuation and replace it with ''
			__lowerCAmelCase   :						List[str]				= corpus_without_punctuation.split("\n"			)
			__lowerCAmelCase   :						str				= term.lower()
			return (len([doc for doc in docs if term in doc]			), len(__snake_case			))


def     _lowercase       (						__snake_case      ,__snake_case      ,__snake_case=False			)		->     float:
			if smoothing:
						if n == 0:
									raise ValueError("log10(0) is undefined."			)
						return round(1 + logaa(n / (1 + df)			)      ,3			)

			if df == 0:
						raise ZeroDivisionError("df must be > 0"			)
			elif n == 0:
						raise ValueError("log10(0) is undefined."			)
			return round(logaa(n / df			)      ,3			)





def     _lowercase       (						__snake_case      ,__snake_case			)		->     float:
			return round(tf * idf      ,3			)