

"""simple docstring"""






import collections
import inspect
import unittest

from transformers import SwinvaConfig
from transformers.testing_utils import require_torch, 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 SwinvaForImageClassification, SwinvaForMaskedImageModeling, SwinvaModel
 from transformers.models.swinva.modeling_swinva import SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor
class        _lowercase					:







     def __init__(      self ,						UpperCamelCase_ ,						UpperCamelCase_=13 ,						UpperCamelCase_=32 ,						UpperCamelCase_=2 ,						UpperCamelCase_=3 ,						UpperCamelCase_=16 ,						UpperCamelCase_=[1, 2, 1] ,						UpperCamelCase_=[2, 2, 4] ,						UpperCamelCase_=2 ,						UpperCamelCase_=2.0 ,						UpperCamelCase_=True ,						UpperCamelCase_=0.0 ,						UpperCamelCase_=0.0 ,						UpperCamelCase_=0.1 ,						UpperCamelCase_="gelu" ,						UpperCamelCase_=False ,						UpperCamelCase_=True ,						UpperCamelCase_=0.0_2 ,						UpperCamelCase_=1E-5 ,						UpperCamelCase_=True ,						UpperCamelCase_=None ,						UpperCamelCase_=True ,						UpperCamelCase_=10 ,						UpperCamelCase_=8 ,						):
      __magic_name__     =      parent
      __magic_name__     =      batch_size
      __magic_name__     =      image_size
      __magic_name__     =      patch_size
      __magic_name__     =      num_channels
      __magic_name__     =      embed_dim
      __magic_name__     =      depths
      __magic_name__     =      num_heads
      __magic_name__     =      window_size
      __magic_name__     =      mlp_ratio
      __magic_name__     =      qkv_bias
      __magic_name__     =      hidden_dropout_prob
      __magic_name__     =      attention_probs_dropout_prob
      __magic_name__     =      drop_path_rate
      __magic_name__     =      hidden_act
      __magic_name__     =      use_absolute_embeddings
      __magic_name__     =      patch_norm
      __magic_name__     =      layer_norm_eps
      __magic_name__     =      initializer_range
      __magic_name__     =      is_training
      __magic_name__     =      scope
      __magic_name__     =      use_labels
      __magic_name__     =      type_sequence_label_size
      __magic_name__     =      encoder_stride







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

      __magic_name__     =      None
      if self.use_labels:
       __magic_name__     =      ids_tensor([self.batch_size] ,						self.type_sequence_label_size       )

      __magic_name__     =      self.get_config()

      return config, pixel_values, labels







     def 	lowerCAmelCase__    (      self       ):
      return SwinvaConfig(
          image_size=self.image_size ,						patch_size=self.patch_size ,						num_channels=self.num_channels ,						embed_dim=self.embed_dim ,						depths=self.depths ,						num_heads=self.num_heads ,						window_size=self.window_size ,						mlp_ratio=self.mlp_ratio ,						qkv_bias=self.qkv_bias ,						hidden_dropout_prob=self.hidden_dropout_prob ,						attention_probs_dropout_prob=self.attention_probs_dropout_prob ,						drop_path_rate=self.drop_path_rate ,						hidden_act=self.hidden_act ,						use_absolute_embeddings=self.use_absolute_embeddings ,						path_norm=self.patch_norm ,						layer_norm_eps=self.layer_norm_eps ,						initializer_range=self.initializer_range ,						encoder_stride=self.encoder_stride ,						)







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      SwinvaModel(config=UpperCamelCase_       )
      model.to(UpperCamelCase_       )
      model.eval()
      __magic_name__     =      model(UpperCamelCase_       )

      __magic_name__     =      ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
      __magic_name__     =      int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

      self.parent.assertEqual(result.last_hidden_state.shape ,						(self.batch_size, expected_seq_len, expected_dim)       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      SwinvaForMaskedImageModeling(config=UpperCamelCase_       )
      model.to(UpperCamelCase_       )
      model.eval()
      __magic_name__     =      model(UpperCamelCase_       )
      self.parent.assertEqual(
          result.logits.shape ,						(self.batch_size, self.num_channels, self.image_size, self.image_size)       )

      # test greyscale images
      __magic_name__     =      1
      __magic_name__     =      SwinvaForMaskedImageModeling(UpperCamelCase_       )
      model.to(UpperCamelCase_       )
      model.eval()

      __magic_name__     =      floats_tensor([self.batch_size, 1, self.image_size, self.image_size]       )
      __magic_name__     =      model(UpperCamelCase_       )
      self.parent.assertEqual(result.logits.shape ,						(self.batch_size, 1, self.image_size, self.image_size)       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      self.type_sequence_label_size
      __magic_name__     =      SwinvaForImageClassification(UpperCamelCase_       )
      model.to(UpperCamelCase_       )
      model.eval()
      __magic_name__     =      model(UpperCamelCase_ ,						labels=UpperCamelCase_       )
      self.parent.assertEqual(result.logits.shape ,						(self.batch_size, self.type_sequence_label_size)       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.prepare_config_and_inputs()
      __magic_name__     =      config_and_inputs
      __magic_name__     =      {'pixel_values': pixel_values}
      return config, inputs_dict


@require_torch
class        _lowercase					(     __UpperCAmelCase   , __UpperCAmelCase   , unittest.TestCase     ):
     _lowerCamelCase     =       (
         (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
     )
     _lowerCamelCase     =       (
         {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
         if is_torch_available()
         else {}
     )

     _lowerCamelCase     =       False
     _lowerCamelCase     =       False
     _lowerCamelCase     =       False
     _lowerCamelCase     =       False







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      SwinvaModelTester(self       )
      __magic_name__     =      ConfigTester(self ,						config_class=UpperCamelCase_ ,						embed_dim=37       )







     def 	lowerCAmelCase__    (      self       ):
      self.config_tester.create_and_test_config_to_json_string()
      self.config_tester.create_and_test_config_to_json_file()
      self.config_tester.create_and_test_config_from_and_save_pretrained()
      self.config_tester.create_and_test_config_with_num_labels()
      self.config_tester.check_config_can_be_init_without_params()
      self.config_tester.check_config_arguments_init()







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*UpperCamelCase_       )







     @unittest.skip(reason='''Got `CUDA error: misaligned address` with PyTorch 2.0.0.'''       )
     def 	lowerCAmelCase__    (      self       ):
      pass







     @unittest.skip(reason='''Swinv2 does not use inputs_embeds'''       )
     def 	lowerCAmelCase__    (      self       ):
      pass







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

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







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

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

       __magic_name__     =      ['pixel_values']
       self.assertListEqual(arg_names[:1] ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()
      __magic_name__     =      True

      for model_class in self.all_model_classes:
       __magic_name__     =      True
       __magic_name__     =      False
       __magic_name__     =      True
       __magic_name__     =      model_class(UpperCamelCase_       )
       model.to(UpperCamelCase_       )
       model.eval()
       with torch.no_grad():
        __magic_name__     =      model(**self._prepare_for_class(UpperCamelCase_ ,						UpperCamelCase_       )       )
       __magic_name__     =      outputs.attentions
       __magic_name__     =      len(self.model_tester.depths       )
       self.assertEqual(len(UpperCamelCase_       ) ,						UpperCamelCase_       )

       # check that output_attentions also work using config
       del inputs_dict["output_attentions"]
       __magic_name__     =      True
       __magic_name__     =      config.window_size**2
       __magic_name__     =      model_class(UpperCamelCase_       )
       model.to(UpperCamelCase_       )
       model.eval()
       with torch.no_grad():
        __magic_name__     =      model(**self._prepare_for_class(UpperCamelCase_ ,						UpperCamelCase_       )       )
       __magic_name__     =      outputs.attentions
       self.assertEqual(len(UpperCamelCase_       ) ,						UpperCamelCase_       )

       self.assertListEqual(
           list(attentions[0].shape[-3:]       ) ,						[self.model_tester.num_heads[0], window_size_squared, window_size_squared] ,						)
       __magic_name__     =      len(UpperCamelCase_       )

       # Check attention is always last and order is fine
       __magic_name__     =      True
       __magic_name__     =      True
       __magic_name__     =      model_class(UpperCamelCase_       )
       model.to(UpperCamelCase_       )
       model.eval()
       with torch.no_grad():
        __magic_name__     =      model(**self._prepare_for_class(UpperCamelCase_ ,						UpperCamelCase_       )       )

       if hasattr(self.model_tester ,						'''num_hidden_states_types'''       ):
        __magic_name__     =      self.model_tester.num_hidden_states_types
       else:
        # also another +1 for reshaped_hidden_states
        __magic_name__     =      2
       self.assertEqual(out_len + added_hidden_states ,						len(UpperCamelCase_       )       )

       __magic_name__     =      outputs.attentions

       self.assertEqual(len(UpperCamelCase_       ) ,						UpperCamelCase_       )

       self.assertListEqual(
           list(self_attentions[0].shape[-3:]       ) ,						[self.model_tester.num_heads[0], window_size_squared, window_size_squared] ,						)







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      model_class(UpperCamelCase_       )
      model.to(UpperCamelCase_       )
      model.eval()

      with torch.no_grad():
       __magic_name__     =      model(**self._prepare_for_class(UpperCamelCase_ ,						UpperCamelCase_       )       )

      __magic_name__     =      outputs.hidden_states

      __magic_name__     =      getattr(
          self.model_tester ,						'''expected_num_hidden_layers''' ,						len(self.model_tester.depths       ) + 1       )
      self.assertEqual(len(UpperCamelCase_       ) ,						UpperCamelCase_       )

      # Swinv2 has a different seq_length
      __magic_name__     =      (
          config.patch_size
          if isinstance(config.patch_size ,						collections.abc.Iterable       )
          else (config.patch_size, config.patch_size)
      )

      __magic_name__     =      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

      self.assertListEqual(
          list(hidden_states[0].shape[-2:]       ) ,						[num_patches, self.model_tester.embed_dim] ,						)

      __magic_name__     =      outputs.reshaped_hidden_states
      self.assertEqual(len(UpperCamelCase_       ) ,						UpperCamelCase_       )

      __magic_name__     =      reshaped_hidden_states[0].shape
      __magic_name__     =      (
          reshaped_hidden_states[0].view(UpperCamelCase_ ,						UpperCamelCase_ ,						height * width       ).permute(0 ,						2 ,						1       )
      )
      self.assertListEqual(
          list(reshaped_hidden_states.shape[-2:]       ) ,						[num_patches, self.model_tester.embed_dim] ,						)







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

      __magic_name__     =      (
          self.model_tester.image_size
          if isinstance(self.model_tester.image_size ,						collections.abc.Iterable       )
          else (self.model_tester.image_size, self.model_tester.image_size)
      )

      for model_class in self.all_model_classes:
       __magic_name__     =      True
       self.check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       )

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       __magic_name__     =      True

       self.check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()
      __magic_name__     =      3

      __magic_name__     =      (
          self.model_tester.image_size
          if isinstance(self.model_tester.image_size ,						collections.abc.Iterable       )
          else (self.model_tester.image_size, self.model_tester.image_size)
      )
      __magic_name__     =      (
          config.patch_size
          if isinstance(config.patch_size ,						collections.abc.Iterable       )
          else (config.patch_size, config.patch_size)
      )

      __magic_name__     =      image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
      __magic_name__     =      image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

      for model_class in self.all_model_classes:
       __magic_name__     =      True
       self.check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						(padded_height, padded_width)       )

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       __magic_name__     =      True
       self.check_hidden_states_output(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_ ,						(padded_height, padded_width)       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*UpperCamelCase_       )







     @slow
     def 	lowerCAmelCase__    (      self       ):







      for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       __magic_name__     =      SwinvaModel.from_pretrained(UpperCamelCase_       )
       self.assertIsNotNone(UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      self.model_tester.prepare_config_and_inputs_for_common()

      __magic_name__     =      _config_zero_init(UpperCamelCase_       )
      for model_class in self.all_model_classes:
       __magic_name__     =      model_class(config=UpperCamelCase_       )
       for name, param in model.named_parameters():
        if "embeddings" not in name and "logit_scale" not in name and 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''' ,						)


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







     @cached_property
     def 	lowerCAmelCase__    (      self       ):
      return (
          AutoImageProcessor.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''       )
          if is_vision_available()
          else None
      )







     @slow
     def 	lowerCAmelCase__    (      self       ):
      __magic_name__     =      SwinvaForImageClassification.from_pretrained('''microsoft/swinv2-tiny-patch4-window8-256'''       ).to(
          UpperCamelCase_       )
      __magic_name__     =      self.default_image_processor

      __magic_name__     =      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''       )
      __magic_name__     =      image_processor(images=UpperCamelCase_ ,						return_tensors='''pt'''       ).to(UpperCamelCase_       )

      # forward pass
      with torch.no_grad():
       __magic_name__     =      model(**UpperCamelCase_       )

      # verify the logits
      __magic_name__     =      torch.Size((1, 1000)       )
      self.assertEqual(outputs.logits.shape ,						UpperCamelCase_       )
      __magic_name__     =      torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6]       ).to(UpperCamelCase_       )
      self.assertTrue(torch.allclose(outputs.logits[0, :3] ,						UpperCamelCase_ ,						atol=1E-4       )       )








"""simple docstring"""






import html

from ...feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from ...utils import is_bsa_available, logging, requires_backends


if is_bsa_available():
 import bsa
 from bsa import BeautifulSoup


__lowerCamelCase										=		logging.get_logger(__name__)
class        _lowercase					(     __UpperCAmelCase     ):







     def __init__(      self ,						**UpperCamelCase_       ):
      requires_backends(self ,						['''bs4''']       )
      super().__init__(**UpperCamelCase_       )







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_       ):
      __magic_name__     =      []
      __magic_name__     =      []
      __magic_name__     =      element if element.name else element.parent
      for parent in child.parents:  # type: bs4.element.Tag
       __magic_name__     =      parent.find_all(child.name ,						recursive=UpperCamelCase_       )
       xpath_tags.append(child.name       )
       xpath_subscripts.append(
           0 if 1 == len(UpperCamelCase_       ) else next(i for i, s in enumerate(UpperCamelCase_ ,						1       ) if s is child       )       )
       __magic_name__     =      parent
      xpath_tags.reverse()
      xpath_subscripts.reverse()
      return xpath_tags, xpath_subscripts







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_       ):
      __magic_name__     =      BeautifulSoup(UpperCamelCase_ ,						'''html.parser'''       )

      __magic_name__     =      []
      __magic_name__     =      []
      __magic_name__     =      []

      for element in html_code.descendants:
       if type(UpperCamelCase_       ) == bsa.element.NavigableString:
        if type(element.parent       ) != bsa.element.Tag:
         continue

        __magic_name__     =      html.unescape(UpperCamelCase_       ).strip()
        if not text_in_this_tag:
         continue

        all_doc_strings.append(UpperCamelCase_       )

        __magic_name__   ,						__magic_name__     =      self.xpath_soup(UpperCamelCase_       )
        stringaxtag_seq.append(UpperCamelCase_       )
        stringaxsubs_seq.append(UpperCamelCase_       )

      if len(UpperCamelCase_       ) != len(UpperCamelCase_       ):
       raise ValueError('''Number of doc strings and xtags does not correspond'''       )
      if len(UpperCamelCase_       ) != len(UpperCamelCase_       ):
       raise ValueError('''Number of doc strings and xsubs does not correspond'''       )

      return all_doc_strings, stringaxtag_seq, stringaxsubs_seq







     def 	lowerCAmelCase__    (      self ,						UpperCamelCase_ ,						UpperCamelCase_       ):
      __magic_name__     =      ''''''
      for tagname, subs in zip(UpperCamelCase_ ,						UpperCamelCase_       ):
       xpath += f'''/{tagname}'''
       if subs != 0:
        xpath += f'''[{subs}]'''
      return xpath







     def __call__(      self ,						UpperCamelCase_       ):
      __magic_name__     =      False

      # Check that strings has a valid type
      if isinstance(UpperCamelCase_ ,						UpperCamelCase_       ):
       __magic_name__     =      True
      elif isinstance(UpperCamelCase_ ,						(list, tuple)       ):
       if len(UpperCamelCase_       ) == 0 or isinstance(html_strings[0] ,						UpperCamelCase_       ):
        __magic_name__     =      True

      if not valid_strings:
       raise ValueError(
           '''HTML strings must of type `str`, `List[str]` (batch of examples), '''
           f'''but is of type {type(UpperCamelCase_       )}.'''       )

      __magic_name__     =      bool(isinstance(UpperCamelCase_ ,						(list, tuple)       ) and (isinstance(html_strings[0] ,						UpperCamelCase_       ))       )

      if not is_batched:
       __magic_name__     =      [html_strings]

      # Get nodes + xpaths
      __magic_name__     =      []
      __magic_name__     =      []
      for html_string in html_strings:
       __magic_name__   ,						__magic_name__   ,						__magic_name__     =      self.get_three_from_single(UpperCamelCase_       )
       nodes.append(UpperCamelCase_       )
       __magic_name__     =      []
       for node, tag_list, sub_list in zip(UpperCamelCase_ ,						UpperCamelCase_ ,						UpperCamelCase_       ):
        __magic_name__     =      self.construct_xpath(UpperCamelCase_ ,						UpperCamelCase_       )
        xpath_strings.append(UpperCamelCase_       )
       xpaths.append(UpperCamelCase_       )

      # return as Dict
      __magic_name__     =      {'''nodes''': nodes, '''xpaths''': xpaths}
      __magic_name__     =      BatchFeature(data=UpperCamelCase_ ,						tensor_type=UpperCamelCase_       )

      return encoded_inputs

