
import inspect
import unittest

from transformers import DataaVecVisionConfig
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,
       DataaVecVisionForImageClassification,
       DataaVecVisionForSemanticSegmentation,
       DataaVecVisionModel,
   )
   from transformers.models.dataavec.modeling_dataavec_vision import DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import BeitImageProcessor







class 	lowerCamelCase						:





     def __init__(self :       List[Any]							,       _A :       int							,       _A :       Tuple=1_0_0							,       _A :       str=1_3							,       _A :       Union[str, Any]=3_0							,       _A :       int=2							,       _A :       int=3							,       _A :       Optional[int]=True							,       _A :       Optional[Any]=True							,       _A :       List[str]=3_2							,       _A :       Tuple=4							,       _A :       Optional[Any]=4							,       _A :       Optional[int]=3_7							,       _A :       Dict="gelu"							,       _A :       Any=0.1							,       _A :       List[Any]=0.1							,       _A :       int=1_0							,       _A :       List[str]=0.02							,       _A :       Tuple=3							,       _A :       int=None							,       _A :       List[Any]=[0, 1, 2, 3]							,       )		->		int:
         snake_case						=   parent
         snake_case						=   1_0_0
         snake_case						=   batch_size
         snake_case						=   image_size
         snake_case						=   patch_size
         snake_case						=   num_channels
         snake_case						=   is_training
         snake_case						=   use_labels
         snake_case						=   hidden_size
         snake_case						=   num_hidden_layers
         snake_case						=   num_attention_heads
         snake_case						=   intermediate_size
         snake_case						=   hidden_act
         snake_case						=   hidden_dropout_prob
         snake_case						=   attention_probs_dropout_prob
         snake_case						=   type_sequence_label_size
         snake_case						=   initializer_range
         snake_case						=   scope
         snake_case						=   out_indices
         snake_case						=   num_labels

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





     def 						UpperCAmelCase(self :       Any  )		->		Any:
         snake_case						=   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

         snake_case						=   None
         snake_case						=   None
         if self.use_labels:
             snake_case						=   ids_tensor([self.batch_size]							,       self.type_sequence_label_size  )
             snake_case						=   ids_tensor([self.batch_size, self.image_size, self.image_size]							,       self.num_labels  )

         snake_case						=   self.get_config()

         return config, pixel_values, labels, pixel_labels





     def 						UpperCAmelCase(self :       Dict  )		->		Optional[Any]:
         return DataaVecVisionConfig(
             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 						UpperCAmelCase(self :       List[str]							,       _A :       Optional[Any]							,       _A :       List[str]							,       _A :       Optional[Any]							,       _A :       List[str]  )		->		Tuple:
         snake_case						=   DataaVecVisionModel(config=_A  )
         model.to(_A  )
         model.eval()
         snake_case						=   model(_A  )
         # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
         snake_case						=   (self.image_size // self.patch_size) ** 2
         self.parent.assertEqual(result.last_hidden_state.shape							,       (self.batch_size, num_patches + 1, self.hidden_size)  )





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





     def 						UpperCAmelCase(self :       Union[str, Any]							,       _A :       str							,       _A :       int							,       _A :       Optional[int]							,       _A :       str  )		->		Tuple:
         snake_case						=   self.num_labels
         snake_case						=   DataaVecVisionForSemanticSegmentation(_A  )
         model.to(_A  )
         model.eval()
         snake_case						=   model(_A  )
         self.parent.assertEqual(
             result.logits.shape							,       (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)  )
         snake_case						=   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 						UpperCAmelCase(self :       str  )		->		Union[str, Any]:
         snake_case						=   self.prepare_config_and_inputs()
         snake_case	,				snake_case	,				snake_case	,				snake_case						=   config_and_inputs
         snake_case						=   {"pixel_values": pixel_values}
         return config, inputs_dict







@require_torch
class 	lowerCamelCase						(    A_				,					A_				,					unittest.TestCase    ):
     UpperCAmelCase__							:				str 	=						(
         (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     UpperCAmelCase__							:				Union[str, Any] 	=						(
         {
             "feature-extraction": DataaVecVisionModel,
             "image-classification": DataaVecVisionForImageClassification,
             "image-segmentation": DataaVecVisionForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     UpperCAmelCase__							:				Union[str, Any] 	=						False
     UpperCAmelCase__							:				int 	=						False
     UpperCAmelCase__							:				int 	=						False





     def 						UpperCAmelCase(self :       Optional[int]  )		->		Optional[int]:
         snake_case						=   DataaVecVisionModelTester(self  )
         snake_case						=   ConfigTester(
             self							,       config_class=_A							,       has_text_modality=_A							,       hidden_size=3_7  )





     def 						UpperCAmelCase(self :       List[str]  )		->		List[Any]:
         self.config_tester.run_common_tests()





     def 						UpperCAmelCase(self :       Tuple  )		->		Optional[int]:
         pass





     @require_torch_multi_gpu
     @unittest.skip(
         reason="Data2VecVision has some layers using `add_module` which doesn't work well with `nn.DataParallel`"  )
     def 						UpperCAmelCase(self :       int  )		->		List[Any]:
         pass





     def 						UpperCAmelCase(self :       Tuple  )		->		Optional[int]:
         snake_case	,				snake_case						=   self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
             snake_case						=   model_class(_A  )
             self.assertIsInstance(model.get_input_embeddings()							,       (nn.Module)  )
             snake_case						=   model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_A							,       nn.Linear  )  )





     def 						UpperCAmelCase(self :       int  )		->		List[str]:
         snake_case	,				snake_case						=   self.model_tester.prepare_config_and_inputs_for_common()

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

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





     def 						UpperCAmelCase(self :       str  )		->		Optional[int]:
         snake_case						=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_A  )





     def 						UpperCAmelCase(self :       List[str]  )		->		int:
         snake_case						=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_segmentation(*_A  )





     def 						UpperCAmelCase(self :       str  )		->		Dict:







         if not self.model_tester.is_training:
             return

         snake_case	,				snake_case						=   self.model_tester.prepare_config_and_inputs_for_common()
         snake_case						=   True

         for model_class in self.all_model_classes:
             if model_class in [*get_values(_A  )]:
                 continue

             snake_case						=   model_class(_A  )
             model.to(_A  )
             model.train()
             snake_case						=   self._prepare_for_class(_A							,       _A							,       return_labels=_A  )
             snake_case						=   model(**_A  ).loss
             loss.backward()





     def 						UpperCAmelCase(self :       str  )		->		Optional[Any]:
         snake_case	,				snake_case						=   self.model_tester.prepare_config_and_inputs_for_common()
         if not self.model_tester.is_training:
             return

         snake_case						=   False
         snake_case						=   True

         for model_class in self.all_model_classes:
             if model_class in [*get_values(_A  )] or not model_class.supports_gradient_checkpointing:
                 continue
             # TODO: remove the following 3 lines once we have a MODEL_FOR_SEMANTIC_SEGMENTATION_MAPPING
             # this can then be incorporated into _prepare_for_class in test_modeling_common.py
             elif model_class.__name__ == "Data2VecVisionForSemanticSegmentation":
                 snake_case	,				snake_case	,				snake_case	,				snake_case						=   inputs_dict["pixel_values"].shape
                 snake_case						=   torch.zeros(
                     [self.model_tester.batch_size, height, width]							,       device=_A  ).long()
             snake_case						=   model_class(_A  )
             model.gradient_checkpointing_enable()
             model.to(_A  )
             model.train()
             snake_case						=   self._prepare_for_class(_A							,       _A							,       return_labels=_A  )
             snake_case						=   model(**_A  ).loss
             loss.backward()





     def 						UpperCAmelCase(self :       List[str]  )		->		List[str]:
         snake_case	,				snake_case						=   self.model_tester.prepare_config_and_inputs_for_common()

         snake_case						=   _config_zero_init(_A  )
         for model_class in self.all_model_classes:
             snake_case						=   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'							,       )





     def 						UpperCAmelCase(self :       Any							,       _A :       List[str]							,       _A :       int							,       _A :       List[Any]							,       _A :       Dict=2E-4							,       _A :       str="outputs"							,       _A :       Tuple=None  )		->		List[str]:
         super().check_pt_tf_outputs(_A							,       _A							,       _A							,       _A							,       _A							,       _A  )





     def 						UpperCAmelCase(self :       Any  )		->		List[Any]:
         snake_case						=   self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_A  )





     @slow
     def 						UpperCAmelCase(self :       List[Any]  )		->		Union[str, Any]:







         for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             snake_case						=   DataaVecVisionModel.from_pretrained(_A  )
             self.assertIsNotNone(_A  )



def 		lowercase_     (  )				->				Dict:
    """simple docstring"""

    snake_case						=   Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"						)
    return image







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





     @cached_property
     def 						UpperCAmelCase(self :       Dict  )		->		Dict:
         return (
             BeitImageProcessor.from_pretrained("facebook/data2vec-vision-base-ft1k"  ) if is_vision_available() else None
         )





     @slow
     def 						UpperCAmelCase(self :       Any  )		->		Dict:
         snake_case						=   DataaVecVisionForImageClassification.from_pretrained("facebook/data2vec-vision-base-ft1k"  ).to(
             _A  )

         snake_case						=   self.default_image_processor
         snake_case						=   prepare_img()
         snake_case						=   image_processor(images=_A							,       return_tensors="pt"  ).to(_A  )

         # forward pass
         with torch.no_grad():
             snake_case						=   model(**_A  )
         snake_case						=   outputs.logits

         # verify the logits
         snake_case						=   torch.Size((1, 1_0_0_0)  )
         self.assertEqual(logits.shape							,       _A  )

         snake_case						=   torch.tensor([0.32_77, -0.13_95, 0.09_11]  ).to(_A  )

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

         snake_case						=   [model.config.labelaid[i] for i in ["remote control, remote", "tabby, tabby cat"]]
         self.assertEqual(logits[0].topk(2  ).indices.cpu().tolist()							,       _A  )









'''simple docstring'''


from itertools import combinations_with_replacement







def 						__magic_name__(			_A = 100       ):
						'''simple docstring'''





						UpperCamelCase__       =			[*list(range(1    ,   21       )       ), 25]
						UpperCamelCase__       =			[2 * x for x in range(1    ,   21       )] + [50]
						UpperCamelCase__       =			[3 * x for x in range(1    ,   21       )]
						UpperCamelCase__       =			singles + doubles + triples + [0]

						UpperCamelCase__       =			0
						UpperCamelCase__       =			42
						UpperCamelCase__       =			42
						UpperCamelCase__       =			42
						UpperCamelCase__       =			42

						for double in doubles:
												for throwa, throwa in combinations_with_replacement(_A    ,   2       ):
																		UpperCamelCase__       =			double + throwa + throwa
																		if checkout_total < limit:
																								num_checkouts += 1

						return num_checkouts


if __name__ == "__main__":
					print(f"""{solution() = }""")
