




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

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

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

     def       a__			(						self       )		->							str:
           _A							:  Optional[int]    =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

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

           return config, pixel_values, labels, pixel_labels

     def       a__			(						self       )		->							int:
           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=_a	,  initializer_range=self.initializer_range	,  out_indices=self.out_indices	,  )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a       )		->							str:
           _A							:  List[str]    =		BeitModel(config=_a       )
           model.to(_a       )
           model.eval()
           _A							:  Optional[Any]    =		model(_a       )
           self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)       )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a       )		->							List[Any]:
           _A							:  int    =		BeitForMaskedImageModeling(config=_a       )
           model.to(_a       )
           model.eval()
           _A							:  Any    =		model(_a       )
           self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.seq_length - 1, self.vocab_size)       )

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a       )		->							str:
           _A							:  Optional[int]    =		self.type_sequence_label_size
           _A							:  Dict    =		BeitForImageClassification(_a       )
           model.to(_a       )
           model.eval()
           _A							:  List[Any]    =		model(_a	,  labels=_a       )
           self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)       )

           # test greyscale images
           _A							:  Union[str, Any]    =		1
           _A							:  Tuple    =		BeitForImageClassification(_a       )
           model.to(_a       )
           model.eval()

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

     def       a__			(						self	,  _a	,  _a	,  _a	,  _a       )		->							Tuple:
           _A							:  Union[str, Any]    =		self.num_labels
           _A							:  List[Any]    =		BeitForSemanticSegmentation(_a       )
           model.to(_a       )
           model.eval()
           _A							:  List[Any]    =		model(_a       )
           self.parent.assertEqual(
               result.logits.shape	,  (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)       )
           _A							:  List[str]    =		model(_a	,  labels=_a       )
           self.parent.assertEqual(
               result.logits.shape	,  (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)       )



     def       a__			(						self       )		->							List[Any]:
           _A							:  Union[str, Any]    =		self.prepare_config_and_inputs()
           _A    ,     _A    ,     _A    ,     _A							:  Tuple    =		config_and_inputs
           _A							:  Union[str, Any]    =		{"""pixel_values""": pixel_values}
           return config, inputs_dict







@require_torch
class 		lowercase							(     UpperCamelCase__,UpperCamelCase__,unittest.TestCase	):
     _a											=		(
         (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     _a											=		(
         {
             "feature-extraction": BeitModel,
             "image-classification": BeitForImageClassification,
             "image-segmentation": BeitForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     _a											=		False
     _a											=		False
     _a											=		False

     def       a__			(						self       )		->							Dict:
           _A							:  str    =		BeitModelTester(self       )
           _A							:  int    =		ConfigTester(self	,  config_class=_a	,  has_text_modality=_a	,  hidden_size=37       )

     def       a__			(						self       )		->							Dict:
           self.config_tester.run_common_tests()

     @unittest.skip(reason="""BEiT does not use inputs_embeds"""       )
     def       a__			(						self       )		->							Any:
           pass

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

     def       a__			(						self       )		->							Dict:
           _A    ,     _A							:  str    =		self.model_tester.prepare_config_and_inputs_for_common()

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

     def       a__			(						self       )		->							Any:
           _A    ,     _A							:  Optional[Any]    =		self.model_tester.prepare_config_and_inputs_for_common()

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

                 _A							:  Dict    =		["""pixel_values"""]
                 self.assertListEqual(arg_names[:1]	,  _a       )

     def       a__			(						self       )		->							Optional[int]:
           _A							:  Optional[Any]    =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*_a       )

     def       a__			(						self       )		->							Tuple:
           _A							:  Union[str, Any]    =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*_a       )

     def       a__			(						self       )		->							List[Any]:
           _A							:  Optional[Any]    =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*_a       )

     def       a__			(						self       )		->							Dict:
           _A							:  int    =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_semantic_segmentation(*_a       )

     def       a__			(						self       )		->							Tuple:
           if not self.model_tester.is_training:
                 return

           _A    ,     _A							:  Any    =		self.model_tester.prepare_config_and_inputs_for_common()
           _A							:  Optional[int]    =		True

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

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

     def       a__			(						self       )		->							Dict:
           _A    ,     _A							:  Dict    =		self.model_tester.prepare_config_and_inputs_for_common()
           if not self.model_tester.is_training:
                 return

           _A							:  int    =		False
           _A							:  Tuple    =		True

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

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

     def       a__			(						self       )		->							List[str]:
           _A    ,     _A							:  Dict    =		self.model_tester.prepare_config_and_inputs_for_common()

           _A							:  Optional[int]    =		_config_zero_init(_a       )
           for model_class in self.all_model_classes:
                 _A							:  Tuple    =		model_class(config=_a       )
                 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       a__			(						self       )		->							Union[str, Any]:
           for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 _A							:  Union[str, Any]    =		BeitModel.from_pretrained(_a       )
                 self.assertIsNotNone(_a       )







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







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

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

     @slow
     def       a__			(						self       )		->							str:
           _A							:  int    =		BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""       ).to(_a       )

           _A							:  Union[str, Any]    =		self.default_image_processor
           _A							:  str    =		prepare_img()
           _A							:  List[Any]    =		image_processor(images=_a	,  return_tensors="""pt"""       ).pixel_values.to(_a       )

           # prepare bool_masked_pos
           _A							:  str    =		torch.ones((1, 196)	,  dtype=torch.bool       ).to(_a       )

           # forward pass
           with torch.no_grad():
                 _A							:  List[str]    =		model(pixel_values=_a	,  bool_masked_pos=_a       )
           _A							:  List[Any]    =		outputs.logits

           # verify the logits
           _A							:  Optional[int]    =		torch.Size((1, 196, 8192)       )
           self.assertEqual(logits.shape	,  _a       )

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

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

     @slow
     def       a__			(						self       )		->							List[str]:
           _A							:  Any    =		BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""       ).to(_a       )

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

           # forward pass
           with torch.no_grad():
                 _A							:  Optional[int]    =		model(**_a       )
           _A							:  List[Any]    =		outputs.logits

           # verify the logits
           _A							:  int    =		torch.Size((1, 1000)       )
           self.assertEqual(logits.shape	,  _a       )

           _A							:  List[str]    =		torch.tensor([-1.2385, -1.0987, -1.0108]       ).to(_a       )

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

           _A							:  Union[str, Any]    =		281
           self.assertEqual(logits.argmax(-1       ).item()	,  _a       )

     @slow
     def       a__			(						self       )		->							Dict:
           _A							:  Optional[int]    =		BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""       ).to(
               _a       )

           _A							:  Optional[Any]    =		self.default_image_processor
           _A							:  List[Any]    =		prepare_img()
           _A							:  Optional[int]    =		image_processor(images=_a	,  return_tensors="""pt"""       ).to(_a       )

           # forward pass
           with torch.no_grad():
                 _A							:  List[Any]    =		model(**_a       )
           _A							:  Optional[int]    =		outputs.logits

           # verify the logits
           _A							:  int    =		torch.Size((1, 2_1841)       )
           self.assertEqual(logits.shape	,  _a       )

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

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

           _A							:  Optional[Any]    =		2396
           self.assertEqual(logits.argmax(-1       ).item()	,  _a       )

     @slow
     def       a__			(						self       )		->							str:
           _A							:  Optional[Any]    =		BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""       )
           _A							:  int    =		model.to(_a       )

           _A							:  Any    =		BeitImageProcessor(do_resize=_a	,  size=640	,  do_center_crop=_a       )

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

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

           # verify the logits
           _A							:  List[Any]    =		torch.Size((1, 150, 160, 160)       )
           self.assertEqual(logits.shape	,  _a       )

           _A							:  Dict    =		version.parse(PIL.__version__       ) < version.parse("""9.0.0"""       )

           if is_pillow_less_than_a:
                 _A							:  Optional[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=_a	,  )
           else:
                 _A							:  Optional[int]    =		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=_a	,  )

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



     @slow
     def       a__			(						self       )		->							int:
           _A							:  Union[str, Any]    =		BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""       )
           _A							:  Union[str, Any]    =		model.to(_a       )

           _A							:  Dict    =		BeitImageProcessor(do_resize=_a	,  size=640	,  do_center_crop=_a       )

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

           # forward pass
           with torch.no_grad():
                 _A							:  Any    =		model(**_a       )

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

           _A							:  Dict    =		image_processor.post_process_semantic_segmentation(outputs=_a	,  target_sizes=[(500, 300)]       )
           _A							:  List[Any]    =		torch.Size((500, 300)       )
           self.assertEqual(segmentation[0].shape	,  _a       )

           _A							:  Optional[int]    =		image_processor.post_process_semantic_segmentation(outputs=_a       )
           _A							:  List[Any]    =		torch.Size((160, 160)       )
           self.assertEqual(segmentation[0].shape	,  _a       )







import operator







def        lowerCAmelCase_			(  snake_case_,snake_case_ = False,snake_case_ = None  ):
      _A							:  str    =		operator.lt if reverse else operator.gt
      _A							:  Optional[Any]    =		solution or []

      if not arr:
            return solution

      _A							:  Dict    =		[arr.pop(0  )]
      for i, item in enumerate(snake_case_  ):
            if _operator(snake_case_,sublist[-1]  ):
                  sublist.append(snake_case_  )
                  arr.pop(snake_case_  )

    #  merging sublist into solution list
      if not solution:
            solution.extend(snake_case_  )
      else:
            while sublist:
                  _A							:  Union[str, Any]    =		sublist.pop(0  )
                  for i, xx in enumerate(snake_case_  ):
                        if not _operator(snake_case_,snake_case_  ):
                              solution.insert(snake_case_,snake_case_  )
                              break
                  else:
                        solution.append(snake_case_  )

      strand_sort(snake_case_,snake_case_,snake_case_  )
      return solution


if __name__ == "__main__":
  assert strand_sort([4, 3, 5, 1, 2]) == [1, 2, 3, 4, 5]
  assert strand_sort([4, 3, 5, 1, 2], reverse=True) == [5, 4, 3, 2, 1]

