


'''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      UpperCamelCase					:


   '''simple docstring'''







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

        # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        lowercase_       :Tuple     =       (image_size // patch_size) ** 2
        lowercase_       :Optional[int]     =       num_patches + 1
   def 		UpperCamelCase  (     self		):
        lowercase_       :str     =       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

        lowercase_       :Optional[Any]     =       None
        lowercase_       :Dict     =       None
        if self.use_labels:
             lowercase_       :Dict     =       ids_tensor([self.batch_size]   ,							self.type_sequence_label_size		)
             lowercase_       :Dict     =       ids_tensor([self.batch_size, self.image_size, self.image_size]   ,							self.num_labels		)

        lowercase_       :int     =       self.get_config()

        return config, pixel_values, labels, pixel_labels
   def 		UpperCamelCase  (     self		):
        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=UpperCamelCase_   ,							initializer_range=self.initializer_range   ,							out_indices=self.out_indices   ,							)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Any     =       BeitModel(config=UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()
        lowercase_       :Union[str, Any]     =       model(UpperCamelCase_		)
        self.parent.assertEqual(result.last_hidden_state.shape   ,							(self.batch_size, self.seq_length, self.hidden_size)		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Dict     =       BeitForMaskedImageModeling(config=UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()
        lowercase_       :List[str]     =       model(UpperCamelCase_		)
        self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.seq_length - 1, self.vocab_size)		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Union[str, Any]     =       self.type_sequence_label_size
        lowercase_       :Union[str, Any]     =       BeitForImageClassification(UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()
        lowercase_       :Optional[int]     =       model(UpperCamelCase_   ,							labels=UpperCamelCase_		)
        self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.type_sequence_label_size)		)

        # test greyscale images
        lowercase_       :Any     =       1
        lowercase_       :Optional[int]     =       BeitForImageClassification(UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()

        lowercase_       :Optional[int]     =       floats_tensor([self.batch_size, 1, self.image_size, self.image_size]		)
        lowercase_       :Any     =       model(UpperCamelCase_   ,							labels=UpperCamelCase_		)
        self.parent.assertEqual(result.logits.shape   ,							(self.batch_size, self.type_sequence_label_size)		)
   def 		UpperCamelCase  (     self   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_   ,							UpperCamelCase_		):
        lowercase_       :Dict     =       self.num_labels
        lowercase_       :str     =       BeitForSemanticSegmentation(UpperCamelCase_		)
        model.to(UpperCamelCase_		)
        model.eval()
        lowercase_       :int     =       model(UpperCamelCase_		)
        self.parent.assertEqual(
            result.logits.shape   ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
        lowercase_       :Optional[int]     =       model(UpperCamelCase_   ,							labels=UpperCamelCase_		)
        self.parent.assertEqual(
            result.logits.shape   ,							(self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)




   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[Any]     =       self.prepare_config_and_inputs()
        lowercase_       :Any     =       config_and_inputs
        lowercase_       :List[Any]     =       {'''pixel_values''': pixel_values}
        return config, inputs_dict






@require_torch
class      UpperCamelCase					(		lowercase__						,      lowercase__						,      unittest.TestCase     ):


   '''simple docstring'''







   lowercase					:	List[str]									=(
       (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   lowercase					:	Union[str, Any]									=(
       {
           """feature-extraction""": BeitModel,
           """image-classification""": BeitForImageClassification,
           """image-segmentation""": BeitForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )

   lowercase					:	Tuple									=False
   lowercase					:	Any									=False
   lowercase					:	Optional[Any]									=False
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[str]     =       BeitModelTester(self		)
        lowercase_       :str     =       ConfigTester(self   ,							config_class=UpperCamelCase_   ,							has_text_modality=UpperCamelCase_   ,							hidden_size=37		)
   def 		UpperCamelCase  (     self		):
        self.config_tester.run_common_tests()
   @unittest.skip(reason='''BEiT does not use inputs_embeds'''		)
   def 		UpperCamelCase  (     self		):
        pass
   @require_torch_multi_gpu
   @unittest.skip(reason='''BEiT has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''		)
   def 		UpperCamelCase  (     self		):
        pass
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[Any]     =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             lowercase_       :Tuple     =       model_class(UpperCamelCase_		)
             self.assertIsInstance(model.get_input_embeddings()   ,							(nn.Module)		)
             lowercase_       :List[str]     =       model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(UpperCamelCase_   ,							nn.Linear		)		)
   def 		UpperCamelCase  (     self		):
        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(UpperCamelCase_		)
             lowercase_       :Optional[Any]     =       inspect.signature(model.forward		)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             lowercase_       :Union[str, Any]     =       [*signature.parameters.keys()]

             lowercase_       :Union[str, Any]     =       ['''pixel_values''']
             self.assertListEqual(arg_names[:1]   ,							UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :str     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[int]     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[Any]     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        lowercase_       :int     =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_semantic_segmentation(*UpperCamelCase_		)
   def 		UpperCamelCase  (     self		):
        if not self.model_tester.is_training:
             return

        lowercase_       :str     =       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(UpperCamelCase_		), BeitForMaskedImageModeling]:
                  continue

             lowercase_       :List[str]     =       model_class(UpperCamelCase_		)
             model.to(UpperCamelCase_		)
             model.train()
             lowercase_       :int     =       self._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_   ,							return_labels=UpperCamelCase_		)
             lowercase_       :str     =       model(**UpperCamelCase_		).loss
             loss.backward()
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[int]     =       self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
             return

        lowercase_       :Optional[int]     =       False
        lowercase_       :Tuple     =       True

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

             lowercase_       :Optional[int]     =       model_class(UpperCamelCase_		)
             model.gradient_checkpointing_enable()
             model.to(UpperCamelCase_		)
             model.train()
             lowercase_       :str     =       self._prepare_for_class(UpperCamelCase_   ,							UpperCamelCase_   ,							return_labels=UpperCamelCase_		)
             lowercase_       :int     =       model(**UpperCamelCase_		).loss
             loss.backward()
   def 		UpperCamelCase  (     self		):
        lowercase_       :Dict     =       self.model_tester.prepare_config_and_inputs_for_common()

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




   @slow
   def 		UpperCamelCase  (     self		):
        for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             lowercase_       :List[str]     =       BeitModel.from_pretrained(UpperCamelCase_		)
             self.assertIsNotNone(UpperCamelCase_		)




def     UpperCamelCase    (      )					->							int:
     '''simple docstring'''





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






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


   '''simple docstring'''







   @cached_property
   def 		UpperCamelCase  (     self		):
        return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''		) if is_vision_available() else None
   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[Any]     =       BeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''		).to(UpperCamelCase_		)

        lowercase_       :Dict     =       self.default_image_processor
        lowercase_       :Union[str, Any]     =       prepare_img()
        lowercase_       :Optional[Any]     =       image_processor(images=UpperCamelCase_   ,							return_tensors='''pt'''		).pixel_values.to(UpperCamelCase_		)

        # prepare bool_masked_pos
        lowercase_       :int     =       torch.ones((1, 196)   ,							dtype=torch.bool		).to(UpperCamelCase_		)

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

        # verify the logits
        lowercase_       :int     =       torch.Size((1, 196, 8192)		)
        self.assertEqual(logits.shape   ,							UpperCamelCase_		)

        lowercase_       :str     =       torch.tensor(
            [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]		).to(UpperCamelCase_		)

        self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3]   ,							UpperCamelCase_   ,							atol=1E-2		)		)
   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :str     =       BeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''		).to(UpperCamelCase_		)

        lowercase_       :Optional[int]     =       self.default_image_processor
        lowercase_       :List[Any]     =       prepare_img()
        lowercase_       :Optional[int]     =       image_processor(images=UpperCamelCase_   ,							return_tensors='''pt'''		).to(UpperCamelCase_		)

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

        # verify the logits
        lowercase_       :List[str]     =       torch.Size((1, 1000)		)
        self.assertEqual(logits.shape   ,							UpperCamelCase_		)

        lowercase_       :Union[str, Any]     =       torch.tensor([-1.2385, -1.0987, -1.0108]		).to(UpperCamelCase_		)

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

        lowercase_       :Dict     =       281
        self.assertEqual(logits.argmax(-1		).item()   ,							UpperCamelCase_		)
   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[Any]     =       BeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''		).to(
            UpperCamelCase_		)

        lowercase_       :str     =       self.default_image_processor
        lowercase_       :List[str]     =       prepare_img()
        lowercase_       :Tuple     =       image_processor(images=UpperCamelCase_   ,							return_tensors='''pt'''		).to(UpperCamelCase_		)

        # forward pass
        with torch.no_grad():
             lowercase_       :Tuple     =       model(**UpperCamelCase_		)
        lowercase_       :int     =       outputs.logits

        # verify the logits
        lowercase_       :str     =       torch.Size((1, 2_1841)		)
        self.assertEqual(logits.shape   ,							UpperCamelCase_		)

        lowercase_       :Union[str, Any]     =       torch.tensor([1.6881, -0.2787, 0.5901]		).to(UpperCamelCase_		)

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

        lowercase_       :Optional[int]     =       2396
        self.assertEqual(logits.argmax(-1		).item()   ,							UpperCamelCase_		)
   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :int     =       BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
        lowercase_       :Union[str, Any]     =       model.to(UpperCamelCase_		)

        lowercase_       :Tuple     =       BeitImageProcessor(do_resize=UpperCamelCase_   ,							size=640   ,							do_center_crop=UpperCamelCase_		)

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

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

        # verify the logits
        lowercase_       :Optional[Any]     =       torch.Size((1, 150, 160, 160)		)
        self.assertEqual(logits.shape   ,							UpperCamelCase_		)

        lowercase_       :Optional[int]     =       version.parse(PIL.__version__		) < version.parse('''9.0.0'''		)

        if is_pillow_less_than_a:
             lowercase_       :List[Any]     =       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=UpperCamelCase_   ,							)
        else:
             lowercase_       :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=UpperCamelCase_   ,							)

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




   @slow
   def 		UpperCamelCase  (     self		):
        lowercase_       :List[Any]     =       BeitForSemanticSegmentation.from_pretrained('''microsoft/beit-base-finetuned-ade-640-640'''		)
        lowercase_       :Optional[int]     =       model.to(UpperCamelCase_		)

        lowercase_       :Dict     =       BeitImageProcessor(do_resize=UpperCamelCase_   ,							size=640   ,							do_center_crop=UpperCamelCase_		)

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

        # forward pass
        with torch.no_grad():
             lowercase_       :Optional[int]     =       model(**UpperCamelCase_		)

        lowercase_       :int     =       outputs.logits.detach().cpu()

        lowercase_       :str     =       image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase_   ,							target_sizes=[(500, 300)]		)
        lowercase_       :Any     =       torch.Size((500, 300)		)
        self.assertEqual(segmentation[0].shape   ,							UpperCamelCase_		)

        lowercase_       :List[Any]     =       image_processor.post_process_semantic_segmentation(outputs=UpperCamelCase_		)
        lowercase_       :Tuple     =       torch.Size((160, 160)		)
        self.assertEqual(segmentation[0].shape   ,							UpperCamelCase_		)





import math

SCREAMING_SNAKE_CASE       :		List[str]         =				10
SCREAMING_SNAKE_CASE       :		Dict         =				7
SCREAMING_SNAKE_CASE       :		int         =				BALLS_PER_COLOUR * NUM_COLOURS




def     UpperCamelCase    (      _a = 2_0		)					->							str:
     '''simple docstring'''





     lowercase_       :List[str]     =       math.comb(_a       ,		_a		)
     lowercase_       :Tuple     =       math.comb(NUM_BALLS - BALLS_PER_COLOUR       ,		_a		)

     lowercase_       :Dict     =       NUM_COLOURS * (1 - missing_colour / total)

     return f"{result:.9f}"


if __name__ == "__main__":
   print(solution(20))

