




'''simple docstring'''







import inspect
import unittest

import numpy as np

from transformers import BeitConfig
from transformers.testing_utils import require_flax, require_vision, slow
from transformers.utils import cached_property, is_flax_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor


if is_flax_available():
   import jax

   from transformers import FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling, FlaxBeitModel

if is_vision_available():
   from PIL import Image

   from transformers import BeitImageProcessor


class     _a						(			unittest.TestCase				):
       def __init__(			self		:			Union[str, Any]     ,       lowercase		:			int     ,       lowercase		:			Optional[Any]=100     ,       lowercase		:			Optional[Any]=13     ,       lowercase		:			Tuple=30     ,       lowercase		:			Union[str, Any]=2     ,       lowercase		:			Tuple=3     ,       lowercase		:			List[str]=True     ,       lowercase		:			int=True     ,       lowercase		:			str=32     ,       lowercase		:			Optional[int]=5     ,       lowercase		:			Union[str, Any]=4     ,       lowercase		:			Union[str, Any]=37     ,       lowercase		:			Dict="gelu"     ,       lowercase		:			Optional[Any]=0.1     ,       lowercase		:			List[Any]=0.1     ,       lowercase		:			Optional[Any]=10     ,       lowercase		:			Tuple=0.02     ,       lowercase		:			List[Any]=3     ,       ):



           '''simple docstring'''

           UpperCAmelCase     =	parent
           UpperCAmelCase     =	vocab_size
           UpperCAmelCase     =	batch_size
           UpperCAmelCase     =	image_size
           UpperCAmelCase     =	patch_size
           UpperCAmelCase     =	num_channels
           UpperCAmelCase     =	is_training
           UpperCAmelCase     =	use_labels
           UpperCAmelCase     =	hidden_size
           UpperCAmelCase     =	num_hidden_layers
           UpperCAmelCase     =	num_attention_heads
           UpperCAmelCase     =	intermediate_size
           UpperCAmelCase     =	hidden_act
           UpperCAmelCase     =	hidden_dropout_prob
           UpperCAmelCase     =	attention_probs_dropout_prob
           UpperCAmelCase     =	type_sequence_label_size
           UpperCAmelCase     =	initializer_range

           # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
           UpperCAmelCase     =	(image_size // patch_size) ** 2
           UpperCAmelCase     =	num_patches + 1
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

           UpperCAmelCase     =	None
           if self.use_labels:
               UpperCAmelCase     =	ids_tensor([self.batch_size]     ,       self.type_sequence_label_size       )

           UpperCAmelCase     =	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=lowercase     ,       initializer_range=self.initializer_range     ,       )

           return config, pixel_values, labels
       def 					A   (			self		:			Any     ,       lowercase		:			List[Any]     ,       lowercase		:			int     ,       lowercase		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxBeitModel(config=lowercase       )
           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.last_hidden_state.shape     ,       (self.batch_size, self.seq_length, self.hidden_size)       )
       def 					A   (			self		:			List[str]     ,       lowercase		:			Union[str, Any]     ,       lowercase		:			Optional[int]     ,       lowercase		:			Dict       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxBeitForMaskedImageModeling(config=lowercase       )
           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.seq_length - 1, self.vocab_size)       )
       def 					A   (			self		:			str     ,       lowercase		:			List[str]     ,       lowercase		:			Optional[int]     ,       lowercase		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.type_sequence_label_size
           UpperCAmelCase     =	FlaxBeitForImageClassification(config=lowercase       )
           UpperCAmelCase     =	model(lowercase       )
           self.parent.assertEqual(result.logits.shape     ,       (self.batch_size, self.type_sequence_label_size)       )

           # test greyscale images
           UpperCAmelCase     =	1
           UpperCAmelCase     =	FlaxBeitForImageClassification(lowercase       )

           UpperCAmelCase     =	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
           UpperCAmelCase     =	model(lowercase       )







       def 					A   (			self		:			List[str]       ):



           '''simple docstring'''

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






@require_flax
class     _a						(			__a							,							unittest.TestCase				):
       __a       :  Optional[Any]            =      (
           (FlaxBeitModel, FlaxBeitForImageClassification, FlaxBeitForMaskedImageModeling) if is_flax_available() else ()
       )
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxBeitModelTester(self       )
           UpperCAmelCase     =	ConfigTester(self     ,       config_class=lowercase     ,       has_text_modality=lowercase     ,       hidden_size=37       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def 					A   (			self		:			Dict       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     =	model_class(lowercase       )
               UpperCAmelCase     =	inspect.signature(model.__call__       )
               # signature.parameters is an OrderedDict => so arg_names order is deterministic
               UpperCAmelCase     =	[*signature.parameters.keys()]

               UpperCAmelCase     =	['''pixel_values''']
               self.assertListEqual(arg_names[:1]     ,       lowercase       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase ,  UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               with self.subTest(model_class.__name__       ):
                   UpperCAmelCase     =	self._prepare_for_class(lowercase     ,       lowercase       )
                   UpperCAmelCase     =	model_class(lowercase       )

                   @jax.jit
                   def model_jitted(lowercase		:			Tuple     ,       **lowercase		:			Optional[Any]       ):
                       return model(pixel_values=lowercase     ,       **lowercase       )

                   with self.subTest('''JIT Enabled'''       ):
                       UpperCAmelCase     =	model_jitted(**lowercase       ).to_tuple()

                   with self.subTest('''JIT Disabled'''       ):
                       with jax.disable_jit():
                           UpperCAmelCase     =	model_jitted(**lowercase       ).to_tuple()

                   self.assertEqual(len(lowercase       )     ,       len(lowercase       )       )
                   for jitted_output, output in zip(lowercase     ,       lowercase       ):
                       self.assertEqual(jitted_output.shape     ,       output.shape       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase       )
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_lm(*lowercase       )
       def 					A   (			self		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*lowercase       )







       @slow
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           for model_class_name in self.all_model_classes:
               UpperCAmelCase     =	model_class_name.from_pretrained('''microsoft/beit-base-patch16-224'''       )
               UpperCAmelCase     =	model(np.ones((1, 3, 224, 224)       )       )
               self.assertIsNotNone(lowercase       )






def 		snake_case_							():
    UpperCAmelCase     =	Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''  )
    return image







@require_vision
@require_flax
class     _a						(			unittest.TestCase				):
       @cached_property
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           return BeitImageProcessor.from_pretrained('''microsoft/beit-base-patch16-224'''       ) if is_vision_available() else None
       @slow
       def 					A   (			self		:			Union[str, Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxBeitForMaskedImageModeling.from_pretrained('''microsoft/beit-base-patch16-224-pt22k'''       )

           UpperCAmelCase     =	self.default_image_processor
           UpperCAmelCase     =	prepare_img()
           UpperCAmelCase     =	image_processor(images=lowercase     ,       return_tensors='''np'''       ).pixel_values

           # prepare bool_masked_pos
           UpperCAmelCase     =	np.ones((1, 196)     ,       dtype=lowercase       )

           # forward pass
           UpperCAmelCase     =	model(pixel_values=lowercase     ,       bool_masked_pos=lowercase       )
           UpperCAmelCase     =	outputs.logits

           # verify the logits
           UpperCAmelCase     =	(1, 196, 8_192)
           self.assertEqual(logits.shape     ,       lowercase       )

           UpperCAmelCase     =	np.array(
               [[-3.2437, 0.5072, -13.9174], [-3.2456, 0.4948, -13.9401], [-3.2033, 0.5121, -13.8550]]       )

           self.assertTrue(np.allclose(logits[bool_masked_pos][:3, :3]     ,       lowercase     ,       atol=1E-2       )       )
       @slow
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxBeitForImageClassification.from_pretrained('''microsoft/beit-base-patch16-224'''       )

           UpperCAmelCase     =	self.default_image_processor
           UpperCAmelCase     =	prepare_img()
           UpperCAmelCase     =	image_processor(images=lowercase     ,       return_tensors='''np'''       )

           # forward pass
           UpperCAmelCase     =	model(**lowercase       )
           UpperCAmelCase     =	outputs.logits

           # verify the logits
           UpperCAmelCase     =	(1, 1_000)
           self.assertEqual(logits.shape     ,       lowercase       )

           UpperCAmelCase     =	np.array([-1.2385, -1.0987, -1.0108]       )

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

           UpperCAmelCase     =	281
           self.assertEqual(logits.argmax(-1       ).item()     ,       lowercase       )







       @slow
       def 					A   (			self		:			str       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxBeitForImageClassification.from_pretrained('''microsoft/beit-large-patch16-224-pt22k-ft22k'''       )

           UpperCAmelCase     =	self.default_image_processor
           UpperCAmelCase     =	prepare_img()
           UpperCAmelCase     =	image_processor(images=lowercase     ,       return_tensors='''np'''       )

           # forward pass
           UpperCAmelCase     =	model(**lowercase       )
           UpperCAmelCase     =	outputs.logits

           # verify the logits
           UpperCAmelCase     =	(1, 21_841)
           self.assertEqual(logits.shape     ,       lowercase       )

           UpperCAmelCase     =	np.array([1.6881, -0.2787, 0.5901]       )

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

           UpperCAmelCase     =	2_396
           self.assertEqual(logits.argmax(-1       ).item()     ,       lowercase       )










'''simple docstring'''







def 		snake_case_							(_a			:							list[list[int]]					,     _a			:							int					,     _a			:							int					,     _a			:							list[int]  ):

    # 1. Validate that path exists between current and next vertices
    if graph[path[curr_ind - 1]][next_ver] == 0:
        return False

    # 2. Validate that next vertex is not already in path
    return not any(vertex == next_ver for vertex in path  )





def 		snake_case_							(_a			:							list[list[int]]					,     _a			:							list[int]					,     _a			:							int  ):

    # Base Case
    if curr_ind == len(_a  ):
        # return whether path exists between current and starting vertices
        return graph[path[curr_ind - 1]][path[0]] == 1

    # Recursive Step
    for next_ver in range(0					,     len(_a  )  ):
        if valid_connection(_a					,     _a					,     _a					,     _a  ):
            # Insert current vertex  into path as next transition
            UpperCAmelCase     =	next_ver
            # Validate created path
            if util_hamilton_cycle(_a					,     _a					,     curr_ind + 1  ):
                return True
            # Backtrack
            UpperCAmelCase     =	-1
    return False







def 		snake_case_							(_a			:							list[list[int]]					,     _a			:							int = 0  ):
    UpperCAmelCase     =	[-1] * (len(_a  ) + 1)
    # initialize start and end of path with starting index
    UpperCAmelCase     =	UpperCAmelCase     =	start_index
    # evaluate and if we find answer return path either return empty array
    return path if util_hamilton_cycle(_a					,     _a					,     1  ) else []




