code: 
  
  
  
  
  
  
  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     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     int							,     SCREAMING_SNAKE_CASE      :     str							,     SCREAMING_SNAKE_CASE      :     Dict=1_00							,     SCREAMING_SNAKE_CASE      :     List[str]=13							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=30							,     SCREAMING_SNAKE_CASE      :     List[str]=2							,     SCREAMING_SNAKE_CASE      :     str=3							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=True							,     SCREAMING_SNAKE_CASE      :     List[Any]=True							,     SCREAMING_SNAKE_CASE      :     Optional[int]=32							,     SCREAMING_SNAKE_CASE      :     List[str]=4							,     SCREAMING_SNAKE_CASE      :     int=4							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=37							,     SCREAMING_SNAKE_CASE      :     Optional[int]="gelu"							,     SCREAMING_SNAKE_CASE      :     List[Any]=0.1							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=0.1							,     SCREAMING_SNAKE_CASE      :     Dict=10							,     SCREAMING_SNAKE_CASE      :     Any=0.0_2							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]=3							,     SCREAMING_SNAKE_CASE      :     Optional[int]=None							,     SCREAMING_SNAKE_CASE      :     Optional[Any]=[0, 1, 2, 3]							,     ):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Tuple        =						parent
        UpperCamelCase__    :     Optional[int]        =						1_00
        UpperCamelCase__    :     Dict        =						batch_size
        UpperCamelCase__    :     str        =						image_size
        UpperCamelCase__    :     Optional[Any]        =						patch_size
        UpperCamelCase__    :     Tuple        =						num_channels
        UpperCamelCase__    :     Optional[Any]        =						is_training
        UpperCamelCase__    :     Tuple        =						use_labels
        UpperCamelCase__    :     int        =						hidden_size
        UpperCamelCase__    :     Union[str, Any]        =						num_hidden_layers
        UpperCamelCase__    :     Optional[int]        =						num_attention_heads
        UpperCamelCase__    :     List[Any]        =						intermediate_size
        UpperCamelCase__    :     List[Any]        =						hidden_act
        UpperCamelCase__    :     str        =						hidden_dropout_prob
        UpperCamelCase__    :     Union[str, Any]        =						attention_probs_dropout_prob
        UpperCamelCase__    :     Any        =						type_sequence_label_size
        UpperCamelCase__    :     List[Any]        =						initializer_range
        UpperCamelCase__    :     str        =						scope
        UpperCamelCase__    :     List[Any]        =						out_indices
        UpperCamelCase__    :     int        =						num_labels
  
        # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
        UpperCamelCase__    :     int        =						(image_size // patch_size) ** 2
        UpperCamelCase__    :     Tuple        =						num_patches + 1
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     str        =						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)
  
        UpperCamelCase__    :     Tuple        =						None
        UpperCamelCase__    :     int        =						None
        if self.use_labels:
             UpperCamelCase__    :     Dict        =						ids_tensor([self.batch_size]							,     self.type_sequence_label_size		)
             UpperCamelCase__    :     Tuple        =						ids_tensor([self.batch_size, self.image_size, self.image_size]							,     self.num_labels		)
  
        UpperCamelCase__    :     List[Any]        =						self.get_config()
  
        return config, pixel_values, labels, pixel_labels
  
  
  
  
  
  
  
   def       __lowercase      (	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=_lowerCamelCase							,     initializer_range=self.initializer_range							,     out_indices=self.out_indices							,     )
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     List[str]							,     SCREAMING_SNAKE_CASE      :     Tuple							,     SCREAMING_SNAKE_CASE      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     List[Any]        =						DataaVecVisionModel(config=_lowerCamelCase		)
        model.to(_lowerCamelCase		)
        model.eval()
        UpperCamelCase__    :     List[str]        =						model(_lowerCamelCase		)
        # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
        UpperCamelCase__    :     int        =						(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       __lowercase      (	self      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     List[Any]							,     SCREAMING_SNAKE_CASE      :     Any							,     SCREAMING_SNAKE_CASE      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						self.type_sequence_label_size
        UpperCamelCase__    :     Optional[int]        =						DataaVecVisionForImageClassification(_lowerCamelCase		)
        model.to(_lowerCamelCase		)
        model.eval()
        UpperCamelCase__    :     Tuple        =						model(_lowerCamelCase							,     labels=_lowerCamelCase		)
        self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.type_sequence_label_size)		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     str							,     SCREAMING_SNAKE_CASE      :     Optional[int]							,     SCREAMING_SNAKE_CASE      :     Dict							,     SCREAMING_SNAKE_CASE      :     Tuple							,     SCREAMING_SNAKE_CASE      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						self.num_labels
        UpperCamelCase__    :     Union[str, Any]        =						DataaVecVisionForSemanticSegmentation(_lowerCamelCase		)
        model.to(_lowerCamelCase		)
        model.eval()
        UpperCamelCase__    :     int        =						model(_lowerCamelCase		)
        self.parent.assertEqual(
            result.logits.shape							,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
        UpperCamelCase__    :     Any        =						model(_lowerCamelCase							,     labels=_lowerCamelCase		)
        self.parent.assertEqual(
            result.logits.shape							,     (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     int        =						self.prepare_config_and_inputs()
        UpperCamelCase__    :     Union[str, Any]        =						config_and_inputs
        UpperCamelCase__    :     Tuple        =						{"""pixel_values""": pixel_values}
        return config, inputs_dict
  
  
  
  
  
  
  
  @require_torch
  class     __a						(					lowerCAmelCase__ ,     lowerCAmelCase__ ,     unittest.TestCase ):
   _lowerCAmelCase : Union[str, Any] 				=				(
       (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
       if is_torch_available()
       else ()
   )
   _lowerCAmelCase : int 				=				(
       {
           '''feature-extraction''': DataaVecVisionModel,
           '''image-classification''': DataaVecVisionForImageClassification,
           '''image-segmentation''': DataaVecVisionForSemanticSegmentation,
       }
       if is_torch_available()
       else {}
   )
  
   _lowerCAmelCase : Tuple 				=				False
   _lowerCAmelCase : List[Any] 				=				False
   _lowerCAmelCase : Tuple 				=				False
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						DataaVecVisionModelTester(self		)
        UpperCamelCase__    :     Union[str, Any]        =						ConfigTester(
            self							,     config_class=_lowerCamelCase							,     has_text_modality=_lowerCamelCase							,     hidden_size=37		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        self.config_tester.run_common_tests()
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Union[str, Any]		):
  
  
        '''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       __lowercase      (	self      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        pass
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[Any]        =						self.model_tester.prepare_config_and_inputs_for_common()
  
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     Optional[Any]        =						model_class(_lowerCamelCase		)
             self.assertIsInstance(model.get_input_embeddings()							,     (nn.Module)		)
             UpperCamelCase__    :     Union[str, Any]        =						model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(_lowerCamelCase							,     nn.Linear		)		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						self.model_tester.prepare_config_and_inputs_for_common()
  
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     Tuple        =						model_class(_lowerCamelCase		)
             UpperCamelCase__    :     Optional[Any]        =						inspect.signature(model.forward		)
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             UpperCamelCase__    :     int        =						[*signature.parameters.keys()]
  
             UpperCamelCase__    :     Union[str, Any]        =						["""pixel_values"""]
             self.assertListEqual(arg_names[:1]							,     _lowerCamelCase		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Dict		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     List[Any]        =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_lowerCamelCase		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Union[str, Any]        =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*_lowerCamelCase		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if not self.model_tester.is_training:
             return
  
        UpperCamelCase__    :     List[Any]        =						self.model_tester.prepare_config_and_inputs_for_common()
        UpperCamelCase__    :     Any        =						True
  
        for model_class in self.all_model_classes:
             if model_class in [*get_values(_lowerCamelCase		)]:
                  continue
  
             UpperCamelCase__    :     List[Any]        =						model_class(_lowerCamelCase		)
             model.to(_lowerCamelCase		)
             model.train()
             UpperCamelCase__    :     Dict        =						self._prepare_for_class(_lowerCamelCase							,     _lowerCamelCase							,     return_labels=_lowerCamelCase		)
             UpperCamelCase__    :     List[Any]        =						model(**_lowerCamelCase		).loss
             loss.backward()
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     int        =						self.model_tester.prepare_config_and_inputs_for_common()
        if not self.model_tester.is_training:
             return
  
        UpperCamelCase__    :     Any        =						False
        UpperCamelCase__    :     Any        =						True
  
        for model_class in self.all_model_classes:
             if model_class in [*get_values(_lowerCamelCase		)] 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":
                  UpperCamelCase__    :     str        =						inputs_dict["""pixel_values"""].shape
                  UpperCamelCase__    :     List[Any]        =						torch.zeros(
                      [self.model_tester.batch_size, height, width]							,     device=_lowerCamelCase		).long()
             UpperCamelCase__    :     Optional[Any]        =						model_class(_lowerCamelCase		)
             model.gradient_checkpointing_enable()
             model.to(_lowerCamelCase		)
             model.train()
             UpperCamelCase__    :     str        =						self._prepare_for_class(_lowerCamelCase							,     _lowerCamelCase							,     return_labels=_lowerCamelCase		)
             UpperCamelCase__    :     Optional[int]        =						model(**_lowerCamelCase		).loss
             loss.backward()
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Dict        =						self.model_tester.prepare_config_and_inputs_for_common()
  
        UpperCamelCase__    :     Optional[int]        =						_config_zero_init(_lowerCamelCase		)
        for model_class in self.all_model_classes:
             UpperCamelCase__    :     List[str]        =						model_class(config=_lowerCamelCase		)
             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       __lowercase      (	self      :     Tuple							,     SCREAMING_SNAKE_CASE      :     Dict							,     SCREAMING_SNAKE_CASE      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     Optional[Any]							,     SCREAMING_SNAKE_CASE      :     Any=2e-4							,     SCREAMING_SNAKE_CASE      :     Any="outputs"							,     SCREAMING_SNAKE_CASE      :     int=None		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        super().check_pt_tf_outputs(_lowerCamelCase							,     _lowerCamelCase							,     _lowerCamelCase							,     _lowerCamelCase							,     _lowerCamelCase							,     _lowerCamelCase		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[Any]        =						self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_lowerCamelCase		)
  
  
  
  
  
  
  
   @slow
   def       __lowercase      (	self      :     int		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCamelCase__    :     str        =						DataaVecVisionModel.from_pretrained(_lowerCamelCase		)
             self.assertIsNotNone(_lowerCamelCase		)
  
  
  
  
  
  
  
  def 	SCREAMING_SNAKE_CASE							(      )				->			Union[str, Any]:
       UpperCamelCase__    :     Union[str, Any]        =						Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
       return image
  
  
  
  
  
  @require_torch
  @require_vision
  class     __a						(					unittest.TestCase ):
  
  
  
  
  
  
  
   @cached_property
   def       __lowercase      (	self      :     Union[str, Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        return (
            BeitImageProcessor.from_pretrained("facebook/data2vec-vision-base-ft1k"		) if is_vision_available() else None
        )
  
  
  
  
  
  
  
   @slow
   def       __lowercase      (	self      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     int        =						DataaVecVisionForImageClassification.from_pretrained("facebook/data2vec-vision-base-ft1k"		).to(
            _lowerCamelCase		)
  
        UpperCamelCase__    :     int        =						self.default_image_processor
        UpperCamelCase__    :     Tuple        =						prepare_img()
        UpperCamelCase__    :     Union[str, Any]        =						image_processor(images=_lowerCamelCase							,     return_tensors="pt"		).to(_lowerCamelCase		)
  
        # forward pass
        with torch.no_grad():
             UpperCamelCase__    :     str        =						model(**_lowerCamelCase		)
        UpperCamelCase__    :     Union[str, Any]        =						outputs.logits
  
        # verify the logits
        UpperCamelCase__    :     Optional[int]        =						torch.Size((1, 10_00)		)
        self.assertEqual(logits.shape							,     _lowerCamelCase		)
  
        UpperCamelCase__    :     List[str]        =						torch.tensor([0.3_2_7_7, -0.1_3_9_5, 0.0_9_1_1]		).to(_lowerCamelCase		)
  
        self.assertTrue(torch.allclose(logits[0, :3]							,     _lowerCamelCase							,     atol=1e-4		)		)
  
        UpperCamelCase__    :     Dict        =						[model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
        self.assertEqual(logits[0].topk(2		).indices.cpu().tolist()							,     _lowerCamelCase		)
style_context: 
  
  
  
  
  
  
  from __future__ import annotations
  
  from collections.abc import Iterator
  from typing import Any
  
  
  
  
  
  class     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     str							,     SCREAMING_SNAKE_CASE      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Any        =						data
        UpperCamelCase__    :     Node | None        =						None
  
  
  
  
  
  
  
   def __str__(	self      :     Union[str, Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        return F'{self.data}'
  
  
  
  
  
  
  
  class     __a						:
  
  
  
  
  
  
  
   def __init__(	self      :     Optional[int]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Node | None        =						None
        UpperCamelCase__    :     Node | None        =						None
  
  
  
  
  
  
  
   def __iter__(	self      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     int        =						self.front
        while node:
             yield node.data
             UpperCamelCase__    :     Any        =						node.next
  
  
  
  
  
  
  
   def __len__(	self      :     Tuple		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        return len(tuple(iter(self		)		)		)
  
  
  
  
  
  
  
   def __str__(	self      :     Union[str, Any]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        return " <- ".join(str(SCREAMING_SNAKE_CASE		) for item in self		)
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     List[str]		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        return len(self		) == 0
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Union[str, Any]							,     SCREAMING_SNAKE_CASE      :     Any		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     int        =						Node(SCREAMING_SNAKE_CASE		)
        if self.is_empty():
             UpperCamelCase__    :     int        =						node
        else:
             assert isinstance(self.rear							,     SCREAMING_SNAKE_CASE		)
             UpperCamelCase__    :     Dict        =						node
             UpperCamelCase__    :     str        =						node
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     Dict		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        if self.is_empty():
             raise IndexError("dequeue from empty queue"		)
        assert isinstance(self.front							,     SCREAMING_SNAKE_CASE		)
        UpperCamelCase__    :     Optional[int]        =						self.front
        UpperCamelCase__    :     List[str]        =						self.front.next
        if self.front is None:
             UpperCamelCase__    :     List[Any]        =						None
        return node.data
  
  
  
  
  
  
  
   def       __lowercase      (	self      :     str		):
  
  
        '''simple docstring'''
  
  
  
  
  
  
        UpperCamelCase__    :     Optional[int]        =						None
  
  
  if __name__ == "__main__":
   from doctest import testmod
  
   testmod()