


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






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

              '''simple docstring'''






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

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






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

              '''simple docstring'''






              lowercase		:Any       							=				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

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

              lowercase		:Optional[int]       							=				self.get_config()

              return config, pixel_values, labels, pixel_labels






       def  __snake_case						(   self  :    str				):

              '''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=snake_case__  ,     initializer_range=self.initializer_range  ,     out_indices=self.out_indices  ,     )






       def  __snake_case						(   self  :    Any  ,     snake_case__  :    List[str]  ,     snake_case__  :    int  ,     snake_case__  :    List[Any]  ,     snake_case__  :    Optional[int]				):

              '''simple docstring'''






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






       def  __snake_case						(   self  :    str  ,     snake_case__  :    int  ,     snake_case__  :    str  ,     snake_case__  :    Any  ,     snake_case__  :    str				):

              '''simple docstring'''






              lowercase		:str       							=				BeitForMaskedImageModeling(config=snake_case__				)
              model.to(snake_case__				)
              model.eval()
              lowercase		:List[Any]       							=				model(snake_case__				)
              self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length - 1, self.vocab_size)				)






       def  __snake_case						(   self  :    List[Any]  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    List[Any]  ,     snake_case__  :    int  ,     snake_case__  :    Union[str, Any]				):

              '''simple docstring'''






              lowercase		:List[str]       							=				self.type_sequence_label_size
              lowercase		:Any       							=				BeitForImageClassification(snake_case__				)
              model.to(snake_case__				)
              model.eval()
              lowercase		:Union[str, Any]       							=				model(snake_case__  ,     labels=snake_case__				)
              self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.type_sequence_label_size)				)

              # test greyscale images
              lowercase		:Optional[int]       							=				1
              lowercase		:Tuple       							=				BeitForImageClassification(snake_case__				)
              model.to(snake_case__				)
              model.eval()

              lowercase		:Optional[int]       							=				floats_tensor([self.batch_size, 1, self.image_size, self.image_size]				)
              lowercase		:Any       							=				model(snake_case__  ,     labels=snake_case__				)
              self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.type_sequence_label_size)				)






       def  __snake_case						(   self  :    Any  ,     snake_case__  :    Optional[int]  ,     snake_case__  :    Optional[Any]  ,     snake_case__  :    List[Any]  ,     snake_case__  :    List[Any]				):

              '''simple docstring'''






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

              '''simple docstring'''






              lowercase		:Optional[Any]       							=				self.prepare_config_and_inputs()
              lowercase							,  lowercase							,  lowercase							,  lowercase		:Any       							=				config_and_inputs
              lowercase		:Optional[int]       							=				{'''pixel_values''': pixel_values}
              return config, inputs_dict







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

       __A			:      int        =   False
       __A			:      List[Any]        =   False
       __A			:      Optional[Any]        =   False






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              lowercase		:Any       							=				BeitModelTester(self				)
              lowercase		:Tuple       							=				ConfigTester(self  ,     config_class=snake_case__  ,     has_text_modality=snake_case__  ,     hidden_size=3_7				)






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              self.config_tester.run_common_tests()






       @unittest.skip(reason='''BEiT does not use inputs_embeds'''				)
       def  __snake_case						(   self  :    Optional[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  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              pass






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              lowercase							,  lowercase		:List[str]       							=				self.model_tester.prepare_config_and_inputs_for_common()

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






       def  __snake_case						(   self  :    List[Any]				):

              '''simple docstring'''






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

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

                     lowercase		:Union[str, Any]       							=				['''pixel_values''']
                     self.assertListEqual(arg_names[:1]  ,     snake_case__				)






       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(*snake_case__				)






       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Tuple       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_masked_lm(*snake_case__				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:List[Any]       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_image_classification(*snake_case__				)






       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:Any       							=				self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_semantic_segmentation(*snake_case__				)






       def  __snake_case						(   self  :    Any				):

              '''simple docstring'''






              if not self.model_tester.is_training:
                     return

              lowercase							,  lowercase		:Optional[Any]       							=				self.model_tester.prepare_config_and_inputs_for_common()
              lowercase		:List[Any]       							=				True

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

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






       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              lowercase							,  lowercase		:int       							=				self.model_tester.prepare_config_and_inputs_for_common()
              if not self.model_tester.is_training:
                     return

              lowercase		:Any       							=				False
              lowercase		:Tuple       							=				True

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

                     lowercase		:int       							=				model_class(snake_case__				)
                     model.gradient_checkpointing_enable()
                     model.to(snake_case__				)
                     model.train()
                     lowercase		:Tuple       							=				self._prepare_for_class(snake_case__  ,     snake_case__  ,     return_labels=snake_case__				)
                     lowercase		:Dict       							=				model(**snake_case__				).loss
                     loss.backward()






       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              lowercase							,  lowercase		:str       							=				self.model_tester.prepare_config_and_inputs_for_common()

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

              '''simple docstring'''






              for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                     lowercase		:List[str]       							=				BeitModel.from_pretrained(snake_case__				)
                     self.assertIsNotNone(snake_case__				)







def       lowerCamelCase					()     ->							Dict:
       lowercase		:str       							=				Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''')
       return image




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






       @cached_property
       def  __snake_case						(   self  :    Optional[Any]				):

              '''simple docstring'''






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






       @slow
       def  __snake_case						(   self  :    int				):

              '''simple docstring'''






              lowercase		:str       							=				BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''				).to(snake_case__				)

              lowercase		:List[str]       							=				self.default_image_processor
              lowercase		:Tuple       							=				prepare_img()
              lowercase		:Optional[Any]       							=				image_processor(images=snake_case__  ,     return_tensors='''pt'''				).pixel_values.to(snake_case__				)

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

              # forward pass
              with torch.no_grad():
                     lowercase		:Union[str, Any]       							=				model(pixel_values=snake_case__  ,     bool_masked_pos=snake_case__				)
              lowercase		:int       							=				outputs.logits

              # verify the logits
              lowercase		:List[Any]       							=				torch.Size((1, 1_9_6, 8_1_9_2)				)
              self.assertEqual(logits.shape  ,     snake_case__				)

              lowercase		:int       							=				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(snake_case__				)

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






       @slow
       def  __snake_case						(   self  :    Dict				):

              '''simple docstring'''






              lowercase		:List[str]       							=				BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''				).to(snake_case__				)

              lowercase		:Tuple       							=				self.default_image_processor
              lowercase		:str       							=				prepare_img()
              lowercase		:str       							=				image_processor(images=snake_case__  ,     return_tensors='''pt'''				).to(snake_case__				)

              # forward pass
              with torch.no_grad():
                     lowercase		:int       							=				model(**snake_case__				)
              lowercase		:Any       							=				outputs.logits

              # verify the logits
              lowercase		:Union[str, Any]       							=				torch.Size((1, 1_0_0_0)				)
              self.assertEqual(logits.shape  ,     snake_case__				)

              lowercase		:Optional[Any]       							=				torch.tensor([-1.23_85, -1.09_87, -1.01_08]				).to(snake_case__				)

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

              lowercase		:str       							=				2_8_1
              self.assertEqual(logits.argmax(-1				).item()  ,     snake_case__				)






       @slow
       def  __snake_case						(   self  :    Tuple				):

              '''simple docstring'''






              lowercase		:Union[str, Any]       							=				BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''				).to(
                  snake_case__				)

              lowercase		:Optional[Any]       							=				self.default_image_processor
              lowercase		:str       							=				prepare_img()
              lowercase		:Optional[int]       							=				image_processor(images=snake_case__  ,     return_tensors='''pt'''				).to(snake_case__				)

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

              # verify the logits
              lowercase		:str       							=				torch.Size((1, 2_1_8_4_1)				)
              self.assertEqual(logits.shape  ,     snake_case__				)

              lowercase		:Tuple       							=				torch.tensor([1.68_81, -0.27_87, 0.59_01]				).to(snake_case__				)

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

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






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

              '''simple docstring'''






              lowercase		:Any       							=				BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''				)
              lowercase		:Any       							=				model.to(snake_case__				)

              lowercase		:Any       							=				BeitImageProcessor(do_resize=snake_case__  ,     size=6_4_0  ,     do_center_crop=snake_case__				)

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

              # forward pass
              with torch.no_grad():
                     lowercase		:str       							=				model(**snake_case__				)
              lowercase		:int       							=				outputs.logits

              # verify the logits
              lowercase		:Any       							=				torch.Size((1, 1_5_0, 1_6_0, 1_6_0)				)
              self.assertEqual(logits.shape  ,     snake_case__				)

              lowercase		:Tuple       							=				version.parse(PIL.__version__				) < version.parse('''9.0.0'''				)

              if is_pillow_less_than_a:
                     lowercase		:Union[str, Any]       							=				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=snake_case__  ,     )
              else:
                     lowercase		:Union[str, Any]       							=				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=snake_case__  ,     )

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







       @slow
       def  __snake_case						(   self  :    Optional[int]				):

              '''simple docstring'''






              lowercase		:Optional[int]       							=				BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''				)
              lowercase		:Optional[int]       							=				model.to(snake_case__				)

              lowercase		:List[Any]       							=				BeitImageProcessor(do_resize=snake_case__  ,     size=6_4_0  ,     do_center_crop=snake_case__				)

              lowercase		:Tuple       							=				load_dataset('''hf-internal-testing/fixtures_ade20k'''  ,     split='''test'''				)
              lowercase		:Optional[int]       							=				Image.open(ds[0]['''file''']				)
              lowercase		:Union[str, Any]       							=				image_processor(images=snake_case__  ,     return_tensors='''pt'''				).to(snake_case__				)

              # forward pass
              with torch.no_grad():
                     lowercase		:Union[str, Any]       							=				model(**snake_case__				)

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

              lowercase		:List[str]       							=				image_processor.post_process_semantic_segmentation(outputs=snake_case__  ,     target_sizes=[(5_0_0, 3_0_0)]				)
              lowercase		:int       							=				torch.Size((5_0_0, 3_0_0)				)
              self.assertEqual(segmentation[0].shape  ,     snake_case__				)

              lowercase		:Tuple       							=				image_processor.post_process_semantic_segmentation(outputs=snake_case__				)
              lowercase		:Tuple       							=				torch.Size((1_6_0, 1_6_0)				)
              self.assertEqual(segmentation[0].shape  ,     snake_case__				)





"""simple docstring"""



def       lowerCamelCase					(a_						:int)     ->							str:
       if number > 0:
              raise ValueError('''input must be a negative integer''')
       lowercase		:Any       							=				len(bin(a_)[3:])
       lowercase		:Any       							=				bin(abs(a_) - (1 << binary_number_length))[3:]
       lowercase		:Tuple       							=				(
           (
               '''1'''
               + '''0''' * (binary_number_length - len(a_))
               + twos_complement_number
           )
           if number < 0
           else '''0'''
       )
       return "0b" + twos_complement_number


if __name__ == "__main__":
 import doctest

 doctest.testmod()

