





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   lowercase__     :







  def __init__(						self     ,    _lowercase     ,    _lowercase=100     ,    _lowercase=13     ,    _lowercase=30     ,    _lowercase=2     ,    _lowercase=3     ,    _lowercase=True     ,    _lowercase=True     ,    _lowercase=32     ,    _lowercase=4     ,    _lowercase=4     ,    _lowercase=37     ,    _lowercase="gelu"     ,    _lowercase=0.1     ,    _lowercase=0.1     ,    _lowercase=10     ,    _lowercase=0.02     ,    _lowercase=3     ,    _lowercase=None     ,    _lowercase=[0, 1, 2, 3]     ,    ):
       lowerCAmelCase_       :							Tuple			 =		parent
       lowerCAmelCase_       :							List[str]			 =		100
       lowerCAmelCase_       :							Optional[int]			 =		batch_size
       lowerCAmelCase_       :							Optional[Any]			 =		image_size
       lowerCAmelCase_       :							List[str]			 =		patch_size
       lowerCAmelCase_       :							List[str]			 =		num_channels
       lowerCAmelCase_       :							str			 =		is_training
       lowerCAmelCase_       :							Any			 =		use_labels
       lowerCAmelCase_       :							Union[str, Any]			 =		hidden_size
       lowerCAmelCase_       :							Optional[Any]			 =		num_hidden_layers
       lowerCAmelCase_       :							Optional[Any]			 =		num_attention_heads
       lowerCAmelCase_       :							Dict			 =		intermediate_size
       lowerCAmelCase_       :							str			 =		hidden_act
       lowerCAmelCase_       :							str			 =		hidden_dropout_prob
       lowerCAmelCase_       :							List[str]			 =		attention_probs_dropout_prob
       lowerCAmelCase_       :							List[str]			 =		type_sequence_label_size
       lowerCAmelCase_       :							Optional[int]			 =		initializer_range
       lowerCAmelCase_       :							Tuple			 =		scope
       lowerCAmelCase_       :							Any			 =		out_indices
       lowerCAmelCase_       :							List[Any]			 =		num_labels

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							str			 =		floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

       lowerCAmelCase_       :							int			 =		None
       lowerCAmelCase_       :							Optional[Any]			 =		None
       if self.use_labels:
            lowerCAmelCase_       :							List[Any]			 =		ids_tensor([self.batch_size]     ,    self.type_sequence_label_size    )
            lowerCAmelCase_       :							Optional[Any]			 =		ids_tensor([self.batch_size, self.image_size, self.image_size]     ,    self.num_labels    )

       lowerCAmelCase_       :							Dict			 =		self.get_config()

       return config, pixel_values, labels, pixel_labels







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







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







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.num_labels
       lowerCAmelCase_       :							str			 =		DataaVecVisionForSemanticSegmentation(_lowercase    )
       model.to(_lowercase    )
       model.eval()
       lowerCAmelCase_       :							Dict			 =		model(_lowercase    )
       self.parent.assertEqual(
           result.logits.shape     ,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)    )
       lowerCAmelCase_       :							Dict			 =		model(_lowercase     ,    labels=_lowercase    )
       self.parent.assertEqual(
           result.logits.shape     ,    (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Any			 =		self.prepare_config_and_inputs()
       lowerCAmelCase_ ,	lowerCAmelCase_ ,	lowerCAmelCase_ ,	lowerCAmelCase_       :							Dict			 =		config_and_inputs
       lowerCAmelCase_       :							List[str]			 =		{"""pixel_values""": pixel_values}
       return config, inputs_dict





@require_torch
class   lowercase__     (				__A  ,       __A  ,       unittest.TestCase	):
  __UpperCamelCase						  =	(
      (DataaVecVisionModel, DataaVecVisionForImageClassification, DataaVecVisionForSemanticSegmentation)
      if is_torch_available()
      else ()
  )
  __UpperCamelCase						  =	(
      {
          """feature-extraction""": DataaVecVisionModel,
          """image-classification""": DataaVecVisionForImageClassification,
          """image-segmentation""": DataaVecVisionForSemanticSegmentation,
      }
      if is_torch_available()
      else {}
  )

  __UpperCamelCase						  =	False
  __UpperCamelCase						  =	False
  __UpperCamelCase						  =	False







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Tuple			 =		DataaVecVisionModelTester(self    )
       lowerCAmelCase_       :							Tuple			 =		ConfigTester(
           self     ,    config_class=_lowercase     ,    has_text_modality=_lowercase     ,    hidden_size=37    )







  def 						UpperCAmelCase__		(						self    ):
       self.config_tester.run_common_tests()







  def 						UpperCAmelCase__		(						self    ):
       # 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 						UpperCAmelCase__		(						self    ):
       pass







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_ ,	lowerCAmelCase_       :							int			 =		self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            lowerCAmelCase_       :							Union[str, Any]			 =		model_class(_lowercase    )
            self.assertIsInstance(model.get_input_embeddings()     ,    (nn.Module)    )
            lowerCAmelCase_       :							Optional[Any]			 =		model.get_output_embeddings()
            self.assertTrue(x is None or isinstance(_lowercase     ,    nn.Linear    )    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_ ,	lowerCAmelCase_       :							str			 =		self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
            lowerCAmelCase_       :							List[Any]			 =		model_class(_lowercase    )
            lowerCAmelCase_       :							Tuple			 =		inspect.signature(model.forward    )
            # signature.parameters is an OrderedDict => so arg_names order is deterministic
            lowerCAmelCase_       :							Optional[Any]			 =		[*signature.parameters.keys()]

            lowerCAmelCase_       :							Dict			 =		["""pixel_values"""]
            self.assertListEqual(arg_names[:1]     ,    _lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_segmentation(*_lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       if not self.model_tester.is_training:
            return

       lowerCAmelCase_ ,	lowerCAmelCase_       :							Union[str, Any]			 =		self.model_tester.prepare_config_and_inputs_for_common()
       lowerCAmelCase_       :							Dict			 =		True

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

            lowerCAmelCase_       :							Union[str, Any]			 =		model_class(_lowercase    )
            model.to(_lowercase    )
            model.train()
            lowerCAmelCase_       :							List[str]			 =		self._prepare_for_class(_lowercase     ,    _lowercase     ,    return_labels=_lowercase    )
            lowerCAmelCase_       :							str			 =		model(**_lowercase    ).loss
            loss.backward()







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_ ,	lowerCAmelCase_       :							int			 =		self.model_tester.prepare_config_and_inputs_for_common()
       if not self.model_tester.is_training:
            return

       lowerCAmelCase_       :							List[Any]			 =		False
       lowerCAmelCase_       :							Tuple			 =		True

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_ ,	lowerCAmelCase_       :							Dict			 =		self.model_tester.prepare_config_and_inputs_for_common()

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







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							Optional[int]			 =		self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*_lowercase    )







  @slow
  def 						UpperCAmelCase__		(						self    ):
       for model_name in DATA2VEC_VISION_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            lowerCAmelCase_       :							Optional[Any]			 =		DataaVecVisionModel.from_pretrained(_lowercase    )
            self.assertIsNotNone(_lowercase    )






def 				_lowerCAmelCase			(	)						->      Union[str, Any]:
     lowerCAmelCase_       :							str			 =		Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""      )
     return image







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







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







  @slow
  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							int			 =		DataaVecVisionForImageClassification.from_pretrained("""facebook/data2vec-vision-base-ft1k"""    ).to(
           _lowercase    )

       lowerCAmelCase_       :							Any			 =		self.default_image_processor
       lowerCAmelCase_       :							List[Any]			 =		prepare_img()
       lowerCAmelCase_       :							Dict			 =		image_processor(images=_lowercase     ,    return_tensors="""pt"""    ).to(_lowercase    )

       # forward pass
       with torch.no_grad():
            lowerCAmelCase_       :							Dict			 =		model(**_lowercase    )
       lowerCAmelCase_       :							int			 =		outputs.logits

       # verify the logits
       lowerCAmelCase_       :							Union[str, Any]			 =		torch.Size((1, 1_000)    )
       self.assertEqual(logits.shape     ,    _lowercase    )

       lowerCAmelCase_       :							Union[str, Any]			 =		torch.tensor([0.3277, -0.1395, 0.0911]    ).to(_lowercase    )

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

       lowerCAmelCase_       :							Optional[int]			 =		[model.config.labelaid[i] for i in ["""remote control, remote""", """tabby, tabby cat"""]]
       self.assertEqual(logits[0].topk(2    ).indices.cpu().tolist()     ,    _lowercase    )







import math





class   lowercase__     :







  def __init__(						self     ,    _lowercase    ):
       lowerCAmelCase_       :							Optional[Any]			 =		len(_lowercase    )
       lowerCAmelCase_       :							Union[str, Any]			 =		[0] * (
           4 * self.N
       )  # approximate the overall size of segment tree with array N
       self.build(1     ,    0     ,    self.N - 1    )







  def 						UpperCAmelCase__		(						self     ,    _lowercase    ):
       return idx * 2







  def 						UpperCAmelCase__		(						self     ,    _lowercase    ):
       return idx * 2 + 1







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase     ,    _lowercase    ):  # noqa: E741
       if l == r:
            lowerCAmelCase_       :							Dict			 =		A[l]
       else:
            lowerCAmelCase_       :							List[str]			 =		(l + r) // 2
            self.build(self.left(_lowercase    )     ,    _lowercase     ,    _lowercase    )
            self.build(self.right(_lowercase    )     ,    mid + 1     ,    _lowercase    )
            lowerCAmelCase_       :							Dict			 =		max(self.st[self.left(_lowercase    )]     ,    self.st[self.right(_lowercase    )]    )







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase     ,    _lowercase    ):
       return self.update_recursive(1     ,    0     ,    self.N - 1     ,    a - 1     ,    b - 1     ,    _lowercase    )







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase    ):  # noqa: E741
       if r < a or l > b:
            return True
       if l == r:
            lowerCAmelCase_       :							List[str]			 =		val
            return True
       lowerCAmelCase_       :							List[str]			 =		(l + r) // 2
       self.update_recursive(self.left(_lowercase    )     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase    )
       self.update_recursive(self.right(_lowercase    )     ,    mid + 1     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase    )
       lowerCAmelCase_       :							List[Any]			 =		max(self.st[self.left(_lowercase    )]     ,    self.st[self.right(_lowercase    )]    )
       return True







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase    ):
       return self.query_recursive(1     ,    0     ,    self.N - 1     ,    a - 1     ,    b - 1    )







  def 						UpperCAmelCase__		(						self     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase    ):  # noqa: E741
       if r < a or l > b:
            return -math.inf
       if l >= a and r <= b:
            return self.st[idx]
       lowerCAmelCase_       :							int			 =		(l + r) // 2
       lowerCAmelCase_       :							List[Any]			 =		self.query_recursive(self.left(_lowercase    )     ,    _lowercase     ,    _lowercase     ,    _lowercase     ,    _lowercase    )
       lowerCAmelCase_       :							List[str]			 =		self.query_recursive(self.right(_lowercase    )     ,    mid + 1     ,    _lowercase     ,    _lowercase     ,    _lowercase    )
       return max(_lowercase     ,    _lowercase    )







  def 						UpperCAmelCase__		(						self    ):
       lowerCAmelCase_       :							List[Any]			 =		[]
       for i in range(1     ,    N + 1    ):
            show_list += [self.query(_lowercase     ,    _lowercase    )]
       print(_lowercase    )


if __name__ == "__main__":
      UpperCAmelCase_       : Any							=    [1, 2, -4, 7, 3, -5, 6, 11, -20, 9, 14, 15, 5, 2, -8]
      UpperCAmelCase_       : Optional[Any]							=    15
      UpperCAmelCase_       : List[Any]							=    SegmentTree(A)
      print(segt.query(4, 6))
      print(segt.query(7, 11))
      print(segt.query(7, 12))
      segt.update(1, 3, 1_11)
      print(segt.query(1, 15))
      segt.update(7, 8, 2_35)
      segt.show_data()
