




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  :

      """simple docstring"""

      def __init__(       self   :	Optional[int]	,    snake_case   :	str	,    snake_case   :	str=100	,    snake_case   :	Union[str, Any]=13	,    snake_case   :	int=30	,    snake_case   :	Dict=2	,    snake_case   :	Union[str, Any]=3	,    snake_case   :	Tuple=True	,    snake_case   :	List[Any]=True	,    snake_case   :	int=32	,    snake_case   :	Tuple=4	,    snake_case   :	List[Any]=4	,    snake_case   :	int=37	,    snake_case   :	List[str]="gelu"	,    snake_case   :	List[Any]=0.1	,    snake_case   :	Any=0.1	,    snake_case   :	Dict=10	,    snake_case   :	Dict=0.02	,    snake_case   :	Any=3	,    snake_case   :	Optional[int]=None	,    snake_case   :	int=[0, 1, 2, 3]	,    ):
          __UpperCamelCase											=  parent
          __UpperCamelCase											=  100
          __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
          __UpperCamelCase											=  scope
          __UpperCamelCase											=  out_indices
          __UpperCamelCase											=  num_labels

          # 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  snake_case		(       self   :	int			):
          __UpperCamelCase											=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

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

          __UpperCamelCase											=  self.get_config()

          return config, pixel_values, labels, pixel_labels







      def  snake_case		(       self   :	str			):
          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=snake_case	,    initializer_range=self.initializer_range	,    out_indices=self.out_indices	,    )







      def  snake_case		(       self   :	Dict	,    snake_case   :	int	,    snake_case   :	List[Any]	,    snake_case   :	Any	,    snake_case   :	Any			):
          __UpperCamelCase											=  DataaVecVisionModel(config=snake_case			)
          model.to(snake_case			)
          model.eval()
          __UpperCamelCase											=  model(snake_case			)
          # expected sequence length = num_patches + 1 (we add 1 for the [CLS] token)
          __UpperCamelCase											=  (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  snake_case		(       self   :	Union[str, Any]	,    snake_case   :	str	,    snake_case   :	Tuple	,    snake_case   :	List[Any]	,    snake_case   :	Optional[int]			):
          __UpperCamelCase											=  self.type_sequence_label_size
          __UpperCamelCase											=  DataaVecVisionForImageClassification(snake_case			)
          model.to(snake_case			)
          model.eval()
          __UpperCamelCase											=  model(snake_case	,    labels=snake_case			)
          self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.type_sequence_label_size)			)







      def  snake_case		(       self   :	Optional[Any]	,    snake_case   :	int	,    snake_case   :	Any	,    snake_case   :	List[Any]	,    snake_case   :	List[str]			):
          __UpperCamelCase											=  self.num_labels
          __UpperCamelCase											=  DataaVecVisionForSemanticSegmentation(snake_case			)
          model.to(snake_case			)
          model.eval()
          __UpperCamelCase											=  model(snake_case			)
          self.parent.assertEqual(
              result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)			)
          __UpperCamelCase											=  model(snake_case	,    labels=snake_case			)
          self.parent.assertEqual(
              result.logits.shape	,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)			)







      def  snake_case		(       self   :	Union[str, Any]			):
          __UpperCamelCase											=  self.prepare_config_and_inputs()
          __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase											=  config_and_inputs
          __UpperCamelCase											=  {'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_torch
class 	_lowerCamelCase  (     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     unittest.TestCase				):

      """simple docstring"""

      lowerCAmelCase__     :	str      =   (
          (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
          if is_torch_available()
          else ()
      )
      lowerCAmelCase__     :	Tuple      =   (
          {
              "feature-extraction": DataaVecVisionModel,
              "image-classification": DataaVecVisionForImageClassification,
              "image-segmentation": DataaVecVisionForSemanticSegmentation,
          }
          if is_torch_available()
          else {}
      )

      lowerCAmelCase__     :	List[Any]      =   False
      lowerCAmelCase__     :	Optional[Any]      =   False
      lowerCAmelCase__     :	Union[str, Any]      =   False







      def  snake_case		(       self   :	List[str]			):
          __UpperCamelCase											=  DataaVecVisionModelTester(self			)
          __UpperCamelCase											=  ConfigTester(
              self	,    config_class=snake_case	,    has_text_modality=snake_case	,    hidden_size=37			)







      def  snake_case		(       self   :	str			):
          self.config_tester.run_common_tests()







      def  snake_case		(       self   :	List[Any]			):
          # Data2VecVision does not use inputs_embeds
          pass







      @require_torch_multi_gpu
      @unittest.skip(
          reason='''Data2VecVision has some layers using `add_module` which doesn\'t work well with `nn.DataParallel`'''			)
      def  snake_case		(       self   :	Optional[int]			):
          pass







      def  snake_case		(       self   :	Tuple			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

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







      def  snake_case		(       self   :	Any			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

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

              __UpperCamelCase											=  ['''pixel_values''']
              self.assertListEqual(arg_names[:1]	,    snake_case			)







      def  snake_case		(       self   :	Any			):
          __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case			)







      def  snake_case		(       self   :	Optional[Any]			):
          __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_segmentation(*snake_case			)







      def  snake_case		(       self   :	Any			):
          if not self.model_tester.is_training:
              return

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

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

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







      def  snake_case		(       self   :	Tuple			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()
          if not self.model_tester.is_training:
              return

          __UpperCamelCase											=  False
          __UpperCamelCase											=  True

          for model_class in self.all_model_classes:
              if model_class in [*get_values(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":
                  __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase						, __UpperCamelCase											=  inputs_dict['''pixel_values'''].shape
                  __UpperCamelCase											=  torch.zeros(
                      [self.model_tester.batch_size, height, width]	,    device=snake_case			).long()
              __UpperCamelCase											=  model_class(snake_case			)
              model.gradient_checkpointing_enable()
              model.to(snake_case			)
              model.train()
              __UpperCamelCase											=  self._prepare_for_class(snake_case	,    snake_case	,    return_labels=snake_case			)
              __UpperCamelCase											=  model(**snake_case			).loss
              loss.backward()







      def  snake_case		(       self   :	List[Any]			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

          __UpperCamelCase											=  _config_zero_init(snake_case			)
          for model_class in self.all_model_classes:
              __UpperCamelCase											=  model_class(config=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  snake_case		(       self   :	Any	,    snake_case   :	int	,    snake_case   :	Optional[Any]	,    snake_case   :	List[str]	,    snake_case   :	Dict=2E-4	,    snake_case   :	Tuple="outputs"	,    snake_case   :	List[Any]=None			):
          # We override with a slightly higher tol value, as semseg models tend to diverge a bit more
          super().check_pt_tf_outputs(snake_case	,    snake_case	,    snake_case	,    snake_case	,    snake_case	,    snake_case			)







      def  snake_case		(       self   :	Tuple			):
          __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case			)







      @slow
      def  snake_case		(       self   :	Dict			):
          for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              __UpperCamelCase											=  DataaVecVisionModel.from_pretrained(snake_case			)
              self.assertIsNotNone(snake_case			)







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

    """simple docstring"""


    __UpperCamelCase											=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
    return image


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

      """simple docstring"""

      @cached_property
      def  snake_case		(       self   :	Any			):
          return (
              BeitImageProcessor.from_pretrained('''facebook/data2vec-vision-base-ft1k'''			) if is_vision_available() else None
          )







      @slow
      def  snake_case		(       self   :	int			):
          __UpperCamelCase											=  DataaVecVisionForImageClassification.from_pretrained('''facebook/data2vec-vision-base-ft1k'''			).to(
              snake_case			)

          __UpperCamelCase											=  self.default_image_processor
          __UpperCamelCase											=  prepare_img()
          __UpperCamelCase											=  image_processor(images=snake_case	,    return_tensors='''pt'''			).to(snake_case			)

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

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

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

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

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












from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


a_										=       {
    "configuration_marian": ["MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP", "MarianConfig", "MarianOnnxConfig"],
}

try:
 if not is_sentencepiece_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a_										=       ["MarianTokenizer"]

try:
 if not is_torch_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a_										=       [
     "MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST",
     "MarianForCausalLM",
     "MarianModel",
     "MarianMTModel",
     "MarianPreTrainedModel",
 ]

try:
 if not is_tf_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a_										=       ["TFMarianModel", "TFMarianMTModel", "TFMarianPreTrainedModel"]

try:
 if not is_flax_available():
  raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
 pass
else:
 a_										=       ["FlaxMarianModel", "FlaxMarianMTModel", "FlaxMarianPreTrainedModel"]

if TYPE_CHECKING:
 from .configuration_marian import MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP, MarianConfig, MarianOnnxConfig

 try:
  if not is_sentencepiece_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .tokenization_marian import MarianTokenizer

 try:
  if not is_torch_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_marian import (
      MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST,
      MarianForCausalLM,
      MarianModel,
      MarianMTModel,
      MarianPreTrainedModel,
  )

 try:
  if not is_tf_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_tf_marian import TFMarianModel, TFMarianMTModel, TFMarianPreTrainedModel

 try:
  if not is_flax_available():
   raise OptionalDependencyNotAvailable()
 except OptionalDependencyNotAvailable:
  pass
 else:
  from .modeling_flax_marian import FlaxMarianModel, FlaxMarianMTModel, FlaxMarianPreTrainedModel

else:
 import sys

 a_										=       _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)






