




'''simple docstring'''



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			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE=100			,     __SCREAMING_SNAKE_CASE=13			,     __SCREAMING_SNAKE_CASE=30			,     __SCREAMING_SNAKE_CASE=2			,     __SCREAMING_SNAKE_CASE=3			,     __SCREAMING_SNAKE_CASE=True			,     __SCREAMING_SNAKE_CASE=True			,     __SCREAMING_SNAKE_CASE=32			,     __SCREAMING_SNAKE_CASE=4			,     __SCREAMING_SNAKE_CASE=4			,     __SCREAMING_SNAKE_CASE=37			,     __SCREAMING_SNAKE_CASE="gelu"			,     __SCREAMING_SNAKE_CASE=0.1			,     __SCREAMING_SNAKE_CASE=0.1			,     __SCREAMING_SNAKE_CASE=10			,     __SCREAMING_SNAKE_CASE=0.02			,     __SCREAMING_SNAKE_CASE=3			,     __SCREAMING_SNAKE_CASE=None			,     __SCREAMING_SNAKE_CASE=[0, 1, 2, 3]			,     )					->     Dict:



            '''simple docstring'''




            __snake_case			    =     parent
            __snake_case			    =     100
            __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 					lowerCAmelCase					(      self     )					->     Union[str, Any]:



            '''simple docstring'''




            __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 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




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


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     Any:



            '''simple docstring'''




            __snake_case			    =     DataaVecVisionModel(config=__SCREAMING_SNAKE_CASE     )
            model.to(__SCREAMING_SNAKE_CASE     )
            model.eval()
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )
            # 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 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     List[Any]:



            '''simple docstring'''




            __snake_case			    =     self.type_sequence_label_size
            __snake_case			    =     DataaVecVisionForImageClassification(__SCREAMING_SNAKE_CASE     )
            model.to(__SCREAMING_SNAKE_CASE     )
            model.eval()
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE			,     labels=__SCREAMING_SNAKE_CASE     )
            self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.type_sequence_label_size)     )


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     int:



            '''simple docstring'''




            __snake_case			    =     self.num_labels
            __snake_case			    =     DataaVecVisionForSemanticSegmentation(__SCREAMING_SNAKE_CASE     )
            model.to(__SCREAMING_SNAKE_CASE     )
            model.eval()
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )
            self.parent.assertEqual(
                result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE			,     labels=__SCREAMING_SNAKE_CASE     )
            self.parent.assertEqual(
                result.logits.shape			,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)     )




       def 					lowerCAmelCase					(      self     )					->     Optional[Any]:



            '''simple docstring'''




            __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      (       __lowerCAmelCase ,      __lowerCAmelCase ,      unittest.TestCase):
       __lowercase     :					int       				=	(
           (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
           if is_torch_available()
           else ()
       )
       __lowercase     :					Tuple       				=	(
           {
               '''feature-extraction''': DataaVecVisionModel,
               '''image-classification''': DataaVecVisionForImageClassification,
               '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
           }
           if is_torch_available()
           else {}
       )

       __lowercase     :					str       				=	False
       __lowercase     :					Optional[int]       				=	False
       __lowercase     :					str       				=	False


       def 					lowerCAmelCase					(      self     )					->     Optional[Any]:



            '''simple docstring'''




            __snake_case			    =     DataaVecVisionModelTester(self     )
            __snake_case			    =     ConfigTester(
                self			,     config_class=__SCREAMING_SNAKE_CASE			,     has_text_modality=__SCREAMING_SNAKE_CASE			,     hidden_size=37     )


       def 					lowerCAmelCase					(      self     )					->     Optional[Any]:



            '''simple docstring'''




            self.config_tester.run_common_tests()


       def 					lowerCAmelCase					(      self     )					->     str:



            '''simple docstring'''




            pass


       @require_torch_multi_gpu
       @unittest.skip(
           reason='''Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''     )
       def 					lowerCAmelCase					(      self     )					->     str:



            '''simple docstring'''




            pass


       def 					lowerCAmelCase					(      self     )					->     Union[str, Any]:



            '''simple docstring'''




            __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(__SCREAMING_SNAKE_CASE     )
                 self.assertIsInstance(model.get_input_embeddings()			,     (nn.Module)     )
                 __snake_case			    =     model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(__SCREAMING_SNAKE_CASE			,     nn.Linear     )     )


       def 					lowerCAmelCase					(      self     )					->     List[Any]:



            '''simple docstring'''




            __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(__SCREAMING_SNAKE_CASE     )
                 __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]			,     __SCREAMING_SNAKE_CASE     )


       def 					lowerCAmelCase					(      self     )					->     int:



            '''simple docstring'''




            __snake_case			    =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__SCREAMING_SNAKE_CASE     )


       def 					lowerCAmelCase					(      self     )					->     Tuple:



            '''simple docstring'''




            __snake_case			    =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_segmentation(*__SCREAMING_SNAKE_CASE     )


       def 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




            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(__SCREAMING_SNAKE_CASE     )]:
                      continue

                 __snake_case			    =     model_class(__SCREAMING_SNAKE_CASE     )
                 model.to(__SCREAMING_SNAKE_CASE     )
                 model.train()
                 __snake_case			    =     self._prepare_for_class(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     return_labels=__SCREAMING_SNAKE_CASE     )
                 __snake_case			    =     model(**__SCREAMING_SNAKE_CASE     ).loss
                 loss.backward()


       def 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




            __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(__SCREAMING_SNAKE_CASE     )] 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=__SCREAMING_SNAKE_CASE     ).long()
                 __snake_case			    =     model_class(__SCREAMING_SNAKE_CASE     )
                 model.gradient_checkpointing_enable()
                 model.to(__SCREAMING_SNAKE_CASE     )
                 model.train()
                 __snake_case			    =     self._prepare_for_class(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     return_labels=__SCREAMING_SNAKE_CASE     )
                 __snake_case			    =     model(**__SCREAMING_SNAKE_CASE     ).loss
                 loss.backward()


       def 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




            __snake_case    ,			__snake_case			    =     self.model_tester.prepare_config_and_inputs_for_common()

            __snake_case			    =     _config_zero_init(__SCREAMING_SNAKE_CASE     )
            for model_class in self.all_model_classes:
                 __snake_case			    =     model_class(config=__SCREAMING_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'''			,     )


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE=2E-4			,     __SCREAMING_SNAKE_CASE="outputs"			,     __SCREAMING_SNAKE_CASE=None     )					->     Any:



            '''simple docstring'''




            super().check_pt_tf_outputs(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )


       def 					lowerCAmelCase					(      self     )					->     Dict:



            '''simple docstring'''




            __snake_case			    =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*__SCREAMING_SNAKE_CASE     )




       @slow
       def 					lowerCAmelCase					(      self     )					->     Optional[Any]:



            '''simple docstring'''




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







def 	_UpperCamelCase       ()->      Optional[Any]:




     '''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 					lowerCAmelCase					(      self     )					->     List[str]:



            '''simple docstring'''




            return (
                BeitImageProcessor.from_pretrained('''facebook/data2vec-vision-base-ft1k'''     ) if is_vision_available() else None
            )




       @slow
       def 					lowerCAmelCase					(      self     )					->     List[str]:



            '''simple docstring'''




            __snake_case			    =     DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''     ).to(
                __SCREAMING_SNAKE_CASE     )

            __snake_case			    =     self.default_image_processor
            __snake_case			    =     prepare_img()
            __snake_case			    =     image_processor(images=__SCREAMING_SNAKE_CASE			,     return_tensors='''pt'''     ).to(__SCREAMING_SNAKE_CASE     )

            # forward pass
            with torch.no_grad():
                 __snake_case			    =     model(**__SCREAMING_SNAKE_CASE     )
            __snake_case			    =     outputs.logits

            # verify the logits
            __snake_case			    =     torch.Size((1, 1000)     )
            self.assertEqual(logits.shape			,     __SCREAMING_SNAKE_CASE     )

            __snake_case			    =     torch.tensor([0.3_277, -0.1_395, 0.0_911]     ).to(__SCREAMING_SNAKE_CASE     )

            self.assertTrue(torch.allclose(logits[0, :3]			,     __SCREAMING_SNAKE_CASE			,     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()			,     __SCREAMING_SNAKE_CASE     )












'''simple docstring'''



from __future__ import annotations







def 	_UpperCamelCase       (_lowerCamelCase							:   int      ,		_lowerCamelCase							:   list[int]      ,		_lowerCamelCase							:   list[int]      ,		_lowerCamelCase							:   int		)->      int:




     '''simple docstring'''





     if counter == 0 or capacity == 0:
          return 0

     # If weight of the nth item is more than Knapsack of capacity,
     #   then this item cannot be included in the optimal solution,
     # else return the maximum of two cases:
     #   (1) nth item included
     #   (2) not included
     if weights[counter - 1] > capacity:
          return knapsack(_lowerCamelCase      ,		_lowerCamelCase      ,		_lowerCamelCase      ,		counter - 1		)
     else:
          __snake_case			    =     capacity - weights[counter - 1]
          __snake_case			    =     values[counter - 1] + knapsack(
              _lowerCamelCase      ,		_lowerCamelCase      ,		_lowerCamelCase      ,		counter - 1		)
          __snake_case			    =     knapsack(_lowerCamelCase      ,		_lowerCamelCase      ,		_lowerCamelCase      ,		counter - 1		)
          return max(_lowerCamelCase      ,		_lowerCamelCase		)


if __name__ == "__main__":
     import doctest

     doctest.testmod()






