



"""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        __snake_case       :




  def __init__( self     :						Tuple						,					__lowerCAmelCase     :						Union[str, Any]						,					__lowerCAmelCase     :						Optional[Any]=1_0_0						,					__lowerCAmelCase     :						Any=1_3						,					__lowerCAmelCase     :						Optional[int]=3_0						,					__lowerCAmelCase     :						Any=2						,					__lowerCAmelCase     :						Union[str, Any]=3						,					__lowerCAmelCase     :						Optional[Any]=True						,					__lowerCAmelCase     :						List[Any]=True						,					__lowerCAmelCase     :						str=3_2						,					__lowerCAmelCase     :						List[Any]=4						,					__lowerCAmelCase     :						Any=4						,					__lowerCAmelCase     :						str=3_7						,					__lowerCAmelCase     :						str="gelu"						,					__lowerCAmelCase     :						Dict=0.1						,					__lowerCAmelCase     :						Dict=0.1						,					__lowerCAmelCase     :						Optional[Any]=1_0						,					__lowerCAmelCase     :						str=0.02						,					__lowerCAmelCase     :						str=3						,					__lowerCAmelCase     :						Union[str, Any]=None						,					__lowerCAmelCase     :						Union[str, Any]=[0, 1, 2, 3]						,					):






         """simple docstring"""






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

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




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[int]		):






         """simple docstring"""






         _lowerCamelCase						:				Dict    			= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

         _lowerCamelCase						:				Tuple    			= None
         _lowerCamelCase						:				Any    			= None
         if self.use_labels:
                _lowerCamelCase						:				List[Any]    			= ids_tensor([self.batch_size]						,					self.type_sequence_label_size		)
                _lowerCamelCase						:				List[Any]    			= ids_tensor([self.batch_size, self.image_size, self.image_size]						,					self.num_labels		)

         _lowerCamelCase						:				Tuple    			= self.get_config()

         return config, pixel_values, labels, pixel_labels




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[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=__lowerCAmelCase						,					initializer_range=self.initializer_range						,					out_indices=self.out_indices						,					)




  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Dict						,					__lowerCAmelCase     :						Dict		):






         """simple docstring"""






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




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						List[Any]						,					__lowerCAmelCase     :						int		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= BeitForMaskedImageModeling(config=__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				Tuple    			= model(__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.seq_length - 1, self.vocab_size)		)




  def 						SCREAMING_SNAKE_CASE				( self     :						List[str]						,					__lowerCAmelCase     :						Tuple						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				Dict    			= self.type_sequence_label_size
         _lowerCamelCase						:				int    			= BeitForImageClassification(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				Union[str, Any]    			= model(__lowerCAmelCase						,					labels=__lowerCAmelCase		)
         self.parent.assertEqual(result.logits.shape						,					(self.batch_size, self.type_sequence_label_size)		)

         # test greyscale images
         _lowerCamelCase						:				Dict    			= 1
         _lowerCamelCase						:				Union[str, Any]    			= BeitForImageClassification(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()

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




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple						,					__lowerCAmelCase     :						int						,					__lowerCAmelCase     :						Optional[int]						,					__lowerCAmelCase     :						str						,					__lowerCAmelCase     :						Any		):






         """simple docstring"""






         _lowerCamelCase						:				Tuple    			= self.num_labels
         _lowerCamelCase						:				List[Any]    			= BeitForSemanticSegmentation(__lowerCAmelCase		)
         model.to(__lowerCAmelCase		)
         model.eval()
         _lowerCamelCase						:				Tuple    			= model(__lowerCAmelCase		)
         self.parent.assertEqual(
             result.logits.shape						,					(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
         _lowerCamelCase						:				List[Any]    			= model(__lowerCAmelCase						,					labels=__lowerCAmelCase		)
         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     :						List[str]		):






         """simple docstring"""






         _lowerCamelCase						:				Tuple    			= self.prepare_config_and_inputs()
         _lowerCamelCase			,    _lowerCamelCase			,    _lowerCamelCase			,    _lowerCamelCase						:				Any    			= config_and_inputs
         _lowerCamelCase						:				Any    			= {'''pixel_values''': pixel_values}
         return config, inputs_dict






@require_torch
class        __snake_case       (	_lowercase  ,     _lowercase  ,     unittest.TestCase):
  snake_case__				:   str  			=						(
      (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__				:   str  			=						False
  snake_case__				:   str  			=						False
  snake_case__				:   Optional[Any]  			=						False




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				int    			= BeitModelTester(self		)
         _lowerCamelCase						:				str    			= ConfigTester(self						,					config_class=__lowerCAmelCase						,					has_text_modality=__lowerCAmelCase						,					hidden_size=3_7		)




  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         self.config_tester.run_common_tests()




  @unittest.skip(reason='''BEiT does not use inputs_embeds'''		)
  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """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     :						Tuple		):






         """simple docstring"""






         pass




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase			,    _lowerCamelCase						:				Optional[Any]    			= self.model_tester.prepare_config_and_inputs_for_common()

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




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         _lowerCamelCase			,    _lowerCamelCase						:				Optional[int]    			= self.model_tester.prepare_config_and_inputs_for_common()

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

                _lowerCamelCase						:				Dict    			= ['''pixel_values''']
                self.assertListEqual(arg_names[:1]						,					__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Optional[Any]		):






         """simple docstring"""






         _lowerCamelCase						:				Tuple    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				Any    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:				Optional[int]    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         _lowerCamelCase						:				Dict    			= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_semantic_segmentation(*__lowerCAmelCase		)




  def 						SCREAMING_SNAKE_CASE				( self     :						Tuple		):






         """simple docstring"""






         if not self.model_tester.is_training:
                return

         _lowerCamelCase			,    _lowerCamelCase						:				Tuple    			= self.model_tester.prepare_config_and_inputs_for_common()
         _lowerCamelCase						:				int    			= True

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

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




  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






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

         _lowerCamelCase						:				str    			= False
         _lowerCamelCase						:				int    			= True

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

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




  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase			,    _lowerCamelCase						:				Optional[Any]    			= self.model_tester.prepare_config_and_inputs_for_common()

         _lowerCamelCase						:				Optional[Any]    			= _config_zero_init(__lowerCAmelCase		)
         for model_class in self.all_model_classes:
                _lowerCamelCase						:				str    			= model_class(config=__lowerCAmelCase		)
                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     :						Union[str, Any]		):






         """simple docstring"""






         for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowerCamelCase						:				Optional[Any]    			= BeitModel.from_pretrained(__lowerCAmelCase		)
                self.assertIsNotNone(__lowerCAmelCase		)






def        snake_case_  (      ):


       '''simple docstring'''





       _lowerCamelCase						:				Optional[int]    			= Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
       return image







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




  @cached_property
  def 						SCREAMING_SNAKE_CASE				( self     :						Union[str, Any]		):






         """simple docstring"""






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




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






         """simple docstring"""






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

         _lowerCamelCase						:				str    			= self.default_image_processor
         _lowerCamelCase						:				List[str]    			= prepare_img()
         _lowerCamelCase						:				List[Any]    			= image_processor(images=__lowerCAmelCase						,					return_tensors='''pt'''		).pixel_values.to(__lowerCAmelCase		)

         # prepare bool_masked_pos
         _lowerCamelCase						:				List[Any]    			= torch.ones((1, 1_9_6)						,					dtype=torch.bool		).to(__lowerCAmelCase		)

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

         # verify the logits
         _lowerCamelCase						:				Optional[int]    			= torch.Size((1, 1_9_6, 8_1_9_2)		)
         self.assertEqual(logits.shape						,					__lowerCAmelCase		)

         _lowerCamelCase						:				List[str]    			= torch.tensor(
             [[-3.24_37, 0.50_72, -13.91_74], [-3.24_56, 0.49_48, -13.94_01], [-3.20_33, 0.51_21, -13.85_50]]		).to(__lowerCAmelCase		)

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




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						str		):






         """simple docstring"""






         _lowerCamelCase						:				Optional[int]    			= BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''		).to(__lowerCAmelCase		)

         _lowerCamelCase						:				List[str]    			= self.default_image_processor
         _lowerCamelCase						:				str    			= prepare_img()
         _lowerCamelCase						:				str    			= image_processor(images=__lowerCAmelCase						,					return_tensors='''pt'''		).to(__lowerCAmelCase		)

         # forward pass
         with torch.no_grad():
                _lowerCamelCase						:				Optional[Any]    			= model(**__lowerCAmelCase		)
         _lowerCamelCase						:				Optional[Any]    			= outputs.logits

         # verify the logits
         _lowerCamelCase						:				Tuple    			= torch.Size((1, 1_0_0_0)		)
         self.assertEqual(logits.shape						,					__lowerCAmelCase		)

         _lowerCamelCase						:				List[Any]    			= torch.tensor([-1.23_85, -1.09_87, -1.01_08]		).to(__lowerCAmelCase		)

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

         _lowerCamelCase						:				List[str]    			= 2_8_1
         self.assertEqual(logits.argmax(-1		).item()						,					__lowerCAmelCase		)




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						int		):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''		).to(
             __lowerCAmelCase		)

         _lowerCamelCase						:				int    			= self.default_image_processor
         _lowerCamelCase						:				Dict    			= prepare_img()
         _lowerCamelCase						:				Dict    			= image_processor(images=__lowerCAmelCase						,					return_tensors='''pt'''		).to(__lowerCAmelCase		)

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

         # verify the logits
         _lowerCamelCase						:				Optional[Any]    			= torch.Size((1, 2_1_8_4_1)		)
         self.assertEqual(logits.shape						,					__lowerCAmelCase		)

         _lowerCamelCase						:				Dict    			= torch.tensor([1.68_81, -0.27_87, 0.59_01]		).to(__lowerCAmelCase		)

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

         _lowerCamelCase						:				Optional[Any]    			= 2_3_9_6
         self.assertEqual(logits.argmax(-1		).item()						,					__lowerCAmelCase		)




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






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
         _lowerCamelCase						:				Union[str, Any]    			= model.to(__lowerCAmelCase		)

         _lowerCamelCase						:				Optional[int]    			= BeitImageProcessor(do_resize=__lowerCAmelCase						,					size=6_4_0						,					do_center_crop=__lowerCAmelCase		)

         _lowerCamelCase						:				Optional[int]    			= load_dataset('''hf-internal-testing/fixtures_ade20k'''						,					split='''test'''		)
         _lowerCamelCase						:				Dict    			= Image.open(ds[0]['''file''']		)
         _lowerCamelCase						:				Tuple    			= image_processor(images=__lowerCAmelCase						,					return_tensors='''pt'''		).to(__lowerCAmelCase		)

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

         # verify the logits
         _lowerCamelCase						:				str    			= torch.Size((1, 1_5_0, 1_6_0, 1_6_0)		)
         self.assertEqual(logits.shape						,					__lowerCAmelCase		)

         _lowerCamelCase						:				int    			= version.parse(PIL.__version__		) < version.parse('''9.0.0'''		)

         if is_pillow_less_than_a:
                _lowerCamelCase						:				str    			= 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=__lowerCAmelCase						,					)
         else:
                _lowerCamelCase						:				Optional[int]    			= 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=__lowerCAmelCase						,					)

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




  @slow
  def 						SCREAMING_SNAKE_CASE				( self     :						Dict		):






         """simple docstring"""






         _lowerCamelCase						:				List[Any]    			= BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
         _lowerCamelCase						:				Any    			= model.to(__lowerCAmelCase		)

         _lowerCamelCase						:				Optional[int]    			= BeitImageProcessor(do_resize=__lowerCAmelCase						,					size=6_4_0						,					do_center_crop=__lowerCAmelCase		)

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

         # forward pass
         with torch.no_grad():
                _lowerCamelCase						:				Dict    			= model(**__lowerCAmelCase		)

         _lowerCamelCase						:				Dict    			= outputs.logits.detach().cpu()

         _lowerCamelCase						:				Optional[int]    			= image_processor.post_process_semantic_segmentation(outputs=__lowerCAmelCase						,					target_sizes=[(5_0_0, 3_0_0)]		)
         _lowerCamelCase						:				Tuple    			= torch.Size((5_0_0, 3_0_0)		)
         self.assertEqual(segmentation[0].shape						,					__lowerCAmelCase		)

         _lowerCamelCase						:				List[Any]    			= image_processor.post_process_semantic_segmentation(outputs=__lowerCAmelCase		)
         _lowerCamelCase						:				Tuple    			= torch.Size((1_6_0, 1_6_0)		)
         self.assertEqual(segmentation[0].shape						,					__lowerCAmelCase		)







"""simple docstring"""
A:   Union[str, Any]								=		{
    0: "0",
    1: "1",
    2: "2",
    3: "3",
    4: "4",
    5: "5",
    6: "6",
    7: "7",
    8: "8",
    9: "9",
    1_0: "a",
    1_1: "b",
    1_2: "c",
    1_3: "d",
    1_4: "e",
    1_5: "f",
}

def       _snake_case						(						UpperCamelCase						:		float						):
 assert type(UpperCamelCase						) in (int, float) and decimal == int(UpperCamelCase						)
 UpperCAmelCase	:					str	=	int(UpperCamelCase						)
 UpperCAmelCase	:					Optional[int]	=	""""""
 UpperCAmelCase	:					List[str]	=	False
 if decimal < 0:
  UpperCAmelCase	:					Any	=	True
  decimal *= -1
 while decimal > 0:
  UpperCAmelCase	,		UpperCAmelCase	:					Dict	=	divmod(UpperCamelCase			,		16						)
  UpperCAmelCase	:					Union[str, Any]	=	values[remainder] + hexadecimal
 UpperCAmelCase	:					int	=	"""0x""" + hexadecimal
 if negative:
  UpperCAmelCase	:					Optional[int]	=	"""-""" + hexadecimal
 return hexadecimal


if __name__ == "__main__":
      import doctest

      doctest.testmod()
