





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





   def __init__(			self       ,      snake_case       ,      snake_case=13       ,      snake_case=32       ,      snake_case=2       ,      snake_case=3       ,      snake_case=16       ,      snake_case=[1, 2, 1]       ,      snake_case=[2, 2, 4]       ,      snake_case=2       ,      snake_case=2.0       ,      snake_case=True       ,      snake_case=0.0       ,      snake_case=0.0       ,      snake_case=0.1       ,      snake_case="gelu"       ,      snake_case=False       ,      snake_case=True       ,      snake_case=0.02       ,      snake_case=1E-5       ,      snake_case=True       ,      snake_case=None       ,      snake_case=True       ,      snake_case=10       ,      snake_case=8       ,      )					->	List[Any]:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	parent
      a__						:    int   		=	batch_size
      a__						:    Union[str, Any]   		=	image_size
      a__						:    str   		=	patch_size
      a__						:    Dict   		=	num_channels
      a__						:    List[str]   		=	embed_dim
      a__						:    str   		=	depths
      a__						:    Any   		=	num_heads
      a__						:    Dict   		=	window_size
      a__						:    Dict   		=	mlp_ratio
      a__						:    str   		=	qkv_bias
      a__						:    Any   		=	hidden_dropout_prob
      a__						:    List[str]   		=	attention_probs_dropout_prob
      a__						:    Union[str, Any]   		=	drop_path_rate
      a__						:    Optional[int]   		=	hidden_act
      a__						:    int   		=	use_absolute_embeddings
      a__						:    str   		=	patch_norm
      a__						:    List[str]   		=	layer_norm_eps
      a__						:    Optional[Any]   		=	initializer_range
      a__						:    List[Any]   		=	is_training
      a__						:    List[Any]   		=	scope
      a__						:    List[str]   		=	use_labels
      a__						:    Tuple   		=	type_sequence_label_size
      a__						:    List[Any]   		=	encoder_stride





   def 						_snake_case			(			self   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    int   		=	floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

      a__						:    List[str]   		=	self.get_config()

      return config, pixel_values, labels





   def 						_snake_case			(			self   )					->	Union[str, Any]:
      """simple docstring"""





      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 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case   )					->	List[str]:
      """simple docstring"""





      a__						:    Any   		=	SwinvaModel(config=_A   )
      model.to(_A   )
      model.eval()
      a__						:    Union[str, Any]   		=	model(_A   )

      a__						:    List[str]   		=	((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths   ) - 1))
      a__						:    Tuple   		=	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 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Union[str, Any]:
      """simple docstring"""





      a__						:    Optional[Any]   		=	SwinvaForMaskedImageModeling(config=_A   )
      model.to(_A   )
      model.eval()
      a__						:    List[str]   		=	model(_A   )
      self.parent.assertEqual(
          result.logits.shape       ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)   )

      # test greyscale images
      a__						:    List[str]   		=	1
      a__						:    Optional[int]   		=	SwinvaForMaskedImageModeling(_A   )
      model.to(_A   )
      model.eval()

      a__						:    Any   		=	floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
      a__						:    str   		=	model(_A   )
      self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, 1, self.image_size, self.image_size)   )





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case   )					->	Optional[Any]:
      """simple docstring"""





      a__						:    Optional[int]   		=	self.type_sequence_label_size
      a__						:    str   		=	SwinvaForImageClassification(_A   )
      model.to(_A   )
      model.eval()
      a__						:    List[Any]   		=	model(_A       ,      labels=_A   )
      self.parent.assertEqual(result.logits.shape       ,      (self.batch_size, self.type_sequence_label_size)   )





   def 						_snake_case			(			self   )					->	str:
      """simple docstring"""





      a__						:    Optional[int]   		=	self.prepare_config_and_inputs()
      a__						:    Any   		=	config_and_inputs
      a__						:    str   		=	{'''pixel_values''': pixel_values}
      return config, inputs_dict







@require_torch
class 	__lowerCAmelCase      (       _UpperCamelCase		,_UpperCamelCase		,unittest.TestCase	):
   _UpperCamelCase			:    Optional[int]  		=					(
       (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
   )
   _UpperCamelCase			:    List[str]  		=					(
       {"""feature-extraction""": SwinvaModel, """image-classification""": SwinvaForImageClassification}
       if is_torch_available()
       else {}
   )

   _UpperCamelCase			:    Union[str, Any]  		=					False
   _UpperCamelCase			:    Dict  		=					False
   _UpperCamelCase			:    Tuple  		=					False
   _UpperCamelCase			:    int  		=					False





   def 						_snake_case			(			self   )					->	Union[str, Any]:
      """simple docstring"""





      a__						:    List[Any]   		=	SwinvaModelTester(self   )
      a__						:    Tuple   		=	ConfigTester(self       ,      config_class=_A       ,      embed_dim=37   )





   def 						_snake_case			(			self   )					->	Optional[int]:
      """simple docstring"""





      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 						_snake_case			(			self   )					->	int:
      """simple docstring"""





      a__						:    Dict   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*_A   )





   @unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."   )
   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      pass





   @unittest.skip(reason="Swinv2 does not use inputs_embeds"   )
   def 						_snake_case			(			self   )					->	str:
      """simple docstring"""





      pass





   def 						_snake_case			(			self   )					->	Optional[int]:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	self.model_tester.prepare_config_and_inputs_for_common()

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





   def 						_snake_case			(			self   )					->	List[Any]:
      """simple docstring"""





      a__						:    Optional[int]   		=	self.model_tester.prepare_config_and_inputs_for_common()

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

         a__						:    Optional[Any]   		=	['''pixel_values''']
         self.assertListEqual(arg_names[:1]       ,      _A   )





   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      a__						:    Any   		=	self.model_tester.prepare_config_and_inputs_for_common()
      a__						:    List[Any]   		=	True

      for model_class in self.all_model_classes:
         a__						:    List[Any]   		=	True
         a__						:    Any   		=	False
         a__						:    List[str]   		=	True
         a__						:    List[Any]   		=	model_class(_A   )
         model.to(_A   )
         model.eval()
         with torch.no_grad():
            a__						:    List[Any]   		=	model(**self._prepare_for_class(_A       ,      _A   )   )
         a__						:    int   		=	outputs.attentions
         a__						:    Any   		=	len(self.model_tester.depths   )
         self.assertEqual(len(_A   )       ,      _A   )

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

         self.assertListEqual(
             list(attentions[0].shape[-3:]   )       ,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]       ,      )
         a__						:    Union[str, Any]   		=	len(_A   )

         # Check attention is always last and order is fine
         a__						:    str   		=	True
         a__						:    Dict   		=	True
         a__						:    Union[str, Any]   		=	model_class(_A   )
         model.to(_A   )
         model.eval()
         with torch.no_grad():
            a__						:    str   		=	model(**self._prepare_for_class(_A       ,      _A   )   )

         if hasattr(self.model_tester       ,      "num_hidden_states_types"   ):
            a__						:    List[Any]   		=	self.model_tester.num_hidden_states_types
         else:
            # also another +1 for reshaped_hidden_states
            a__						:    Optional[int]   		=	2
         self.assertEqual(out_len + added_hidden_states       ,      len(_A   )   )

         a__						:    Dict   		=	outputs.attentions

         self.assertEqual(len(_A   )       ,      _A   )

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





   def 						_snake_case			(			self       ,      snake_case       ,      snake_case       ,      snake_case       ,      snake_case   )					->	List[Any]:
      """simple docstring"""





      a__						:    Any   		=	model_class(_A   )
      model.to(_A   )
      model.eval()

      with torch.no_grad():
         a__						:    Optional[int]   		=	model(**self._prepare_for_class(_A       ,      _A   )   )

      a__						:    List[str]   		=	outputs.hidden_states

      a__						:    List[Any]   		=	getattr(
          self.model_tester       ,      "expected_num_hidden_layers"       ,      len(self.model_tester.depths   ) + 1   )
      self.assertEqual(len(_A   )       ,      _A   )

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

      a__						:    List[str]   		=	(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]       ,      )

      a__						:    Any   		=	outputs.reshaped_hidden_states
      self.assertEqual(len(_A   )       ,      _A   )

      a__						:    Optional[Any]   		=	reshaped_hidden_states[0].shape
      a__						:    Tuple   		=	(
          reshaped_hidden_states[0].view(_A       ,      _A       ,      height * width   ).permute(0       ,      2       ,      1   )
      )
      self.assertListEqual(
          list(reshaped_hidden_states.shape[-2:]   )       ,      [num_patches, self.model_tester.embed_dim]       ,      )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    Tuple   		=	self.model_tester.prepare_config_and_inputs_for_common()

      a__						:    List[str]   		=	(
          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:
         a__						:    Optional[int]   		=	True
         self.check_hidden_states_output(_A       ,      _A       ,      _A       ,      _A   )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         a__						:    Optional[Any]   		=	True

         self.check_hidden_states_output(_A       ,      _A       ,      _A       ,      _A   )





   def 						_snake_case			(			self   )					->	int:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	self.model_tester.prepare_config_and_inputs_for_common()
      a__						:    Optional[Any]   		=	3

      a__						:    Dict   		=	(
          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)
      )
      a__						:    Union[str, Any]   		=	(
          config.patch_size
          if isinstance(config.patch_size       ,      collections.abc.Iterable   )
          else (config.patch_size, config.patch_size)
      )

      a__						:    Dict   		=	image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
      a__						:    int   		=	image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

      for model_class in self.all_model_classes:
         a__						:    Optional[Any]   		=	True
         self.check_hidden_states_output(_A       ,      _A       ,      _A       ,      (padded_height, padded_width)   )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         a__						:    str   		=	True
         self.check_hidden_states_output(_A       ,      _A       ,      _A       ,      (padded_height, padded_width)   )





   def 						_snake_case			(			self   )					->	Dict:
      """simple docstring"""





      a__						:    Dict   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_masked_image_modeling(*_A   )





   def 						_snake_case			(			self   )					->	int:
      """simple docstring"""





      a__						:    Union[str, Any]   		=	self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*_A   )





   @slow
   def 						_snake_case			(			self   )					->	List[str]:
      """simple docstring"""





      for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         a__						:    Tuple   		=	SwinvaModel.from_pretrained(_A   )
         self.assertIsNotNone(_A   )





   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      a__						:    List[Any]   		=	self.model_tester.prepare_config_and_inputs_for_common()

      a__						:    Optional[Any]   		=	_config_zero_init(_A   )
      for model_class in self.all_model_classes:
         a__						:    Optional[int]   		=	model_class(config=_A   )
         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 	__lowerCAmelCase      (       unittest.TestCase	):





   @cached_property
   def 						_snake_case			(			self   )					->	Any:
      """simple docstring"""





      return (
          AutoImageProcessor.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"   )
          if is_vision_available()
          else None
      )





   @slow
   def 						_snake_case			(			self   )					->	Tuple:
      """simple docstring"""





      a__						:    Dict   		=	SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"   ).to(
          _A   )
      a__						:    Optional[int]   		=	self.default_image_processor

      a__						:    str   		=	Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"   )
      a__						:    Tuple   		=	image_processor(images=_A       ,      return_tensors="pt"   ).to(_A   )

      # forward pass
      with torch.no_grad():
         a__						:    List[Any]   		=	model(**_A   )

      # verify the logits
      a__						:    Any   		=	torch.Size((1, 1_000)   )
      self.assertEqual(outputs.logits.shape       ,      _A   )
      a__						:    Tuple   		=	torch.tensor([-0.3_947, -0.4_306, 0.0_026]   ).to(_A   )
      self.assertTrue(torch.allclose(outputs.logits[0, :3]       ,      _A       ,      atol=1E-4   )   )









'''simple docstring'''





import warnings
from typing import List, Optional, Union

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding, PaddingStrategy, PreTokenizedInput, TextInput, TruncationStrategy
from ...utils import TensorType


class  snake_case__      (					UpperCamelCase):
					a_        =					["image_processor", "tokenizer"]
					a_        =					"LayoutLMv2ImageProcessor"
					a_        =					("LayoutXLMTokenizer", "LayoutXLMTokenizerFast")




					def __init__(  self  :		Optional[int]				, _A  :		str=None				, _A  :		Optional[Any]=None				, **_A  :		Any      )    ->							Tuple:
								if "feature_extractor" in kwargs:
											warnings.warn(
											    '''The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`'''
											    ''' instead.'''				, _A				, )
											UpperCAmelCase_      :       int          =       kwargs.pop('''feature_extractor'''      )

								UpperCAmelCase_      :       str          =       image_processor if image_processor is not None else feature_extractor
								if image_processor is None:
											raise ValueError('''You need to specify an `image_processor`.'''      )
								if tokenizer is None:
											raise ValueError('''You need to specify a `tokenizer`.'''      )

								super().__init__(_A				, _A      )




					def __call__(  self  :		str				, _A  :		Optional[int]				, _A  :		Union[TextInput, PreTokenizedInput, List[TextInput], List[PreTokenizedInput]] = None				, _A  :		Optional[Union[PreTokenizedInput, List[PreTokenizedInput]]] = None				, _A  :		Union[List[List[int]], List[List[List[int]]]] = None				, _A  :		Optional[Union[List[int], List[List[int]]]] = None				, _A  :		bool = True				, _A  :		Union[bool, str, PaddingStrategy] = False				, _A  :		Union[bool, str, TruncationStrategy] = None				, _A  :		Optional[int] = None				, _A  :		int = 0				, _A  :		Optional[int] = None				, _A  :		Optional[bool] = None				, _A  :		Optional[bool] = None				, _A  :		bool = False				, _A  :		bool = False				, _A  :		bool = False				, _A  :		bool = False				, _A  :		bool = True				, _A  :		Optional[Union[str, TensorType]] = None				, **_A  :		Dict				, )    ->							BatchEncoding:
								# verify input
								if self.image_processor.apply_ocr and (boxes is not None):
											raise ValueError(
											    '''You cannot provide bounding boxes '''
											    '''if you initialized the image processor with apply_ocr set to True.'''      )

								if self.image_processor.apply_ocr and (word_labels is not None):
											raise ValueError(
											    '''You cannot provide word labels if you initialized the image processor with apply_ocr set to True.'''      )

								if return_overflowing_tokens is True and return_offsets_mapping is False:
											raise ValueError('''You cannot return overflowing tokens without returning the offsets mapping.'''      )

								# first, apply the image processor
								UpperCAmelCase_      :       int          =       self.image_processor(images=_A				, return_tensors=_A      )

								# second, apply the tokenizer
								if text is not None and self.image_processor.apply_ocr and text_pair is None:
											if isinstance(_A				, _A      ):
														UpperCAmelCase_      :       int          =       [text]  # add batch dimension (as the image processor always adds a batch dimension)
											UpperCAmelCase_      :       int          =       features['''words''']

								UpperCAmelCase_      :       str          =       self.tokenizer(
								    text=text if text is not None else features['''words''']				, text_pair=text_pair if text_pair is not None else None				, boxes=boxes if boxes is not None else features['''boxes''']				, word_labels=_A				, add_special_tokens=_A				, padding=_A				, truncation=_A				, max_length=_A				, stride=_A				, pad_to_multiple_of=_A				, return_token_type_ids=_A				, return_attention_mask=_A				, return_overflowing_tokens=_A				, return_special_tokens_mask=_A				, return_offsets_mapping=_A				, return_length=_A				, verbose=_A				, return_tensors=_A				, **_A				, )

								# add pixel values
								UpperCAmelCase_      :       int          =       features.pop('''pixel_values'''      )
								if return_overflowing_tokens is True:
											UpperCAmelCase_      :       List[Any]          =       self.get_overflowing_images(_A				, encoded_inputs['''overflow_to_sample_mapping''']      )
								UpperCAmelCase_      :       Optional[int]          =       images

								return encoded_inputs




					def 		A      (  self  :		Union[str, Any]				, _A  :		int				, _A  :		Tuple      )    ->							Dict:
								# in case there's an overflow, ensure each `input_ids` sample is mapped to its corresponding image
								UpperCAmelCase_      :       Tuple          =       []
								for sample_idx in overflow_to_sample_mapping:
											images_with_overflow.append(images[sample_idx]      )

								if len(_A      ) != len(_A      ):
											raise ValueError(
											    '''Expected length of images to be the same as the length of `overflow_to_sample_mapping`, but got'''
											    F" {len(_A      )} and {len(_A      )}"      )

								return images_with_overflow




					def 		A      (  self  :		Optional[Any]				, *_A  :		Union[str, Any]				, **_A  :		Union[str, Any]      )    ->							Tuple:
								return self.tokenizer.batch_decode(*_A				, **_A      )




					def 		A      (  self  :		Any				, *_A  :		Optional[Any]				, **_A  :		Tuple      )    ->							Tuple:
								return self.tokenizer.decode(*_A				, **_A      )




					@property
					def 		A      (  self  :		Union[str, Any]      )    ->							List[Any]:
								return ["input_ids", "bbox", "attention_mask", "image"]




					@property
					def 		A      (  self  :		Tuple      )    ->							Optional[int]:
								warnings.warn(
								    '''`feature_extractor_class` is deprecated and will be removed in v5. Use `image_processor_class` instead.'''				, _A				, )
								return self.image_processor_class





					@property
					def 		A      (  self  :		Tuple      )    ->							str:
								warnings.warn(
								    '''`feature_extractor` is deprecated and will be removed in v5. Use `image_processor` instead.'''				, _A				, )
								return self.image_processor


