


'''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 						_lowerCamelCase					:




 '''simple docstring'''


 def __init__(      self      ,      __lowercase      ,      __lowercase=13      ,      __lowercase=32      ,      __lowercase=2      ,      __lowercase=3      ,      __lowercase=16      ,      __lowercase=[1, 2, 1]      ,      __lowercase=[2, 2, 4]      ,      __lowercase=2      ,      __lowercase=2.0      ,      __lowercase=True      ,      __lowercase=0.0      ,      __lowercase=0.0      ,      __lowercase=0.1      ,      __lowercase="gelu"      ,      __lowercase=False      ,      __lowercase=True      ,      __lowercase=0.0_2      ,      __lowercase=1E-5      ,      __lowercase=True      ,      __lowercase=None      ,      __lowercase=True      ,      __lowercase=10      ,      __lowercase=8      ,      ):

  """simple docstring"""







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


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

  __A  :		Union[str, Any]       =			None
  if self.use_labels:
   __A  :		str       =			ids_tensor([self.batch_size]      ,      self.type_sequence_label_size						)

  __A  :		Tuple       =			self.get_config()

  return config, pixel_values, labels


 def  snake_case__   (      self						):

  """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      ,      __lowercase      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  __A  :		Optional[Any]       =			SwinvaModel(config=__lowercase						)
  model.to(__lowercase						)
  model.eval()
  __A  :		List[Any]       =			model(__lowercase						)

  __A  :		Dict       =			((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths						) - 1))
  __A  :		List[str]       =			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      ,      __lowercase      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  __A  :		Tuple       =			SwinvaForMaskedImageModeling(config=__lowercase						)
  model.to(__lowercase						)
  model.eval()
  __A  :		List[Any]       =			model(__lowercase						)
  self.parent.assertEqual(
      result.logits.shape      ,      (self.batch_size, self.num_channels, self.image_size, self.image_size)						)

  # test greyscale images
  __A  :		int       =			1
  __A  :		Dict       =			SwinvaForMaskedImageModeling(__lowercase						)
  model.to(__lowercase						)
  model.eval()

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


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  __A  :		Dict       =			self.type_sequence_label_size
  __A  :		Optional[int]       =			SwinvaForImageClassification(__lowercase						)
  model.to(__lowercase						)
  model.eval()
  __A  :		Any       =			model(__lowercase      ,      labels=__lowercase						)
  self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.type_sequence_label_size)						)







 def  snake_case__   (      self						):

  """simple docstring"""







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


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




 '''simple docstring'''
 __lowercase				:		List[str]    							=						(
     (SwinvaModel, SwinvaForImageClassification, SwinvaForMaskedImageModeling) if is_torch_available() else ()
 )
 __lowercase				:		str    							=						(
     {'''feature-extraction''': SwinvaModel, '''image-classification''': SwinvaForImageClassification}
     if is_torch_available()
     else {}
 )

 __lowercase				:		Optional[int]    							=						False
 __lowercase				:		Any    							=						False
 __lowercase				:		Optional[int]    							=						False
 __lowercase				:		Dict    							=						False


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			SwinvaModelTester(self						)
  __A  :		Any       =			ConfigTester(self      ,      config_class=__lowercase      ,      embed_dim=37						)


 def  snake_case__   (      self						):

  """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						):

  """simple docstring"""







  __A  :		List[Any]       =			self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_model(*__lowercase						)


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

  """simple docstring"""







  pass


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

  """simple docstring"""







  pass


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		Tuple       =			self.model_tester.prepare_config_and_inputs_for_common()

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


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		Dict       =			self.model_tester.prepare_config_and_inputs_for_common()

  for model_class in self.all_model_classes:
   __A  :		Union[str, Any]       =			model_class(__lowercase						)
   __A  :		List[str]       =			inspect.signature(model.forward						)
   # signature.parameters is an OrderedDict => so arg_names order is deterministic
   __A  :		Union[str, Any]       =			[*signature.parameters.keys()]

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


 def  snake_case__   (      self						):

  """simple docstring"""







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

  for model_class in self.all_model_classes:
   __A  :		Any       =			True
   __A  :		Optional[Any]       =			False
   __A  :		Optional[int]       =			True
   __A  :		Dict       =			model_class(__lowercase						)
   model.to(__lowercase						)
   model.eval()
   with torch.no_grad():
    __A  :		Tuple       =			model(**self._prepare_for_class(__lowercase      ,      __lowercase						)						)
   __A  :		Any       =			outputs.attentions
   __A  :		Optional[int]       =			len(self.model_tester.depths						)
   self.assertEqual(len(__lowercase						)      ,      __lowercase						)

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

   self.assertListEqual(
       list(attentions[0].shape[-3:]						)      ,      [self.model_tester.num_heads[0], window_size_squared, window_size_squared]      ,      )
   __A  :		List[Any]       =			len(__lowercase						)

   # Check attention is always last and order is fine
   __A  :		Optional[Any]       =			True
   __A  :		str       =			True
   __A  :		List[Any]       =			model_class(__lowercase						)
   model.to(__lowercase						)
   model.eval()
   with torch.no_grad():
    __A  :		List[Any]       =			model(**self._prepare_for_class(__lowercase      ,      __lowercase						)						)

   if hasattr(self.model_tester      ,      'num_hidden_states_types'						):
    __A  :		Optional[Any]       =			self.model_tester.num_hidden_states_types
   else:
    # also another +1 for reshaped_hidden_states
    __A  :		Union[str, Any]       =			2
   self.assertEqual(out_len + added_hidden_states      ,      len(__lowercase						)						)

   __A  :		Tuple       =			outputs.attentions

   self.assertEqual(len(__lowercase						)      ,      __lowercase						)

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


 def  snake_case__   (      self      ,      __lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase						):

  """simple docstring"""







  __A  :		List[str]       =			model_class(__lowercase						)
  model.to(__lowercase						)
  model.eval()

  with torch.no_grad():
   __A  :		Dict       =			model(**self._prepare_for_class(__lowercase      ,      __lowercase						)						)

  __A  :		Optional[int]       =			outputs.hidden_states

  __A  :		Dict       =			getattr(
      self.model_tester      ,      'expected_num_hidden_layers'      ,      len(self.model_tester.depths						) + 1						)
  self.assertEqual(len(__lowercase						)      ,      __lowercase						)

  # Swinv2 has a different seq_length
  __A  :		List[str]       =			(
      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  :		int       =			outputs.reshaped_hidden_states
  self.assertEqual(len(__lowercase						)      ,      __lowercase						)

  __A     ,__A     ,__A     ,__A  :		Tuple       =			reshaped_hidden_states[0].shape
  __A  :		Optional[int]       =			(
      reshaped_hidden_states[0].view(__lowercase      ,      __lowercase      ,      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						):

  """simple docstring"""







  __A     ,__A  :		List[Any]       =			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  :		Tuple       =			True
   self.check_hidden_states_output(__lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase						)

   # check that output_hidden_states also work using config
   del inputs_dict["output_hidden_states"]
   __A  :		Union[str, Any]       =			True

   self.check_hidden_states_output(__lowercase      ,      __lowercase      ,      __lowercase      ,      __lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		Any       =			self.model_tester.prepare_config_and_inputs_for_common()
  __A  :		str       =			3

  __A  :		Optional[int]       =			(
      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  :		Dict       =			(
      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  :		Optional[Any]       =			image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

  for model_class in self.all_model_classes:
   __A  :		List[Any]       =			True
   self.check_hidden_states_output(__lowercase      ,      __lowercase      ,      __lowercase      ,      (padded_height, padded_width)						)

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


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Dict       =			self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_masked_image_modeling(*__lowercase						)


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Optional[int]       =			self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_for_image_classification(*__lowercase						)


 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  for model_name in SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   __A  :		Union[str, Any]       =			SwinvaModel.from_pretrained(__lowercase						)
   self.assertIsNotNone(__lowercase						)







 def  snake_case__   (      self						):

  """simple docstring"""







  __A     ,__A  :		str       =			self.model_tester.prepare_config_and_inputs_for_common()

  __A  :		List[Any]       =			_config_zero_init(__lowercase						)
  for model_class in self.all_model_classes:
   __A  :		List[Any]       =			model_class(config=__lowercase						)
   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				):




 '''simple docstring'''


 @cached_property
 def  snake_case__   (      self						):

  """simple docstring"""







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







 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		str       =			SwinvaForImageClassification.from_pretrained('microsoft/swinv2-tiny-patch4-window8-256'						).to(
      __lowercase						)
  __A  :		Any       =			self.default_image_processor

  __A  :		List[Any]       =			Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'						)
  __A  :		int       =			image_processor(images=__lowercase      ,      return_tensors='pt'						).to(__lowercase						)

  # forward pass
  with torch.no_grad():
   __A  :		int       =			model(**__lowercase						)

  # verify the logits
  __A  :		Optional[int]       =			torch.Size((1, 1_000)						)
  self.assertEqual(outputs.logits.shape      ,      __lowercase						)
  __A  :		Union[str, Any]       =			torch.tensor([-0.3_9_4_7, -0.4_3_0_6, 0.0_0_2_6]						).to(__lowercase						)
  self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,      __lowercase      ,      atol=1E-4						)						)










'''simple docstring'''
import json
import os
import unittest

from transformers.models.xlm.tokenization_xlm import VOCAB_FILES_NAMES, XLMTokenizer
from transformers.testing_utils import slow

from ...test_tokenization_common import TokenizerTesterMixin

class 						_lowerCamelCase					(    snake_case_    ,		unittest.TestCase				):




 '''simple docstring'''
 __lowercase				:		Dict    							=						XLMTokenizer
 __lowercase				:		Any    							=						False


 def  snake_case__   (      self						):

  """simple docstring"""







  super().setUp()

  # Adapted from Sennrich et al. 2015 and https://github.com/rsennrich/subword-nmt
  __A  :		Optional[int]       =			[
      'l',
      'o',
      'w',
      'e',
      'r',
      's',
      't',
      'i',
      'd',
      'n',
      'w</w>',
      'r</w>',
      't</w>',
      'lo',
      'low',
      'er</w>',
      'low</w>',
      'lowest</w>',
      'newer</w>',
      'wider</w>',
      '<unk>',
  ]
  __A  :		int       =			dict(zip(__lowercase      ,      range(len(__lowercase						)						)						)						)
  __A  :		str       =			['l o 123', 'lo w 1456', 'e r</w> 1789', '']

  __A  :		Dict       =			os.path.join(self.tmpdirname      ,      VOCAB_FILES_NAMES['vocab_file']						)
  __A  :		List[Any]       =			os.path.join(self.tmpdirname      ,      VOCAB_FILES_NAMES['merges_file']						)
  with open(self.vocab_file      ,      'w'						) as fp:
   fp.write(json.dumps(__lowercase						)						)
  with open(self.merges_file      ,      'w'						) as fp:
   fp.write('\n'.join(__lowercase						)						)


 def  snake_case__   (      self      ,      __lowercase						):

  """simple docstring"""







  __A  :		Any       =			'lower newer'
  __A  :		int       =			'lower newer'
  return input_text, output_text


 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		Any       =			XLMTokenizer(self.vocab_file      ,      self.merges_file						)

  __A  :		Optional[Any]       =			'lower'
  __A  :		Union[str, Any]       =			['low', 'er</w>']
  __A  :		Any       =			tokenizer.tokenize(__lowercase						)
  self.assertListEqual(__lowercase      ,      __lowercase						)

  __A  :		Optional[int]       =			tokens + ['<unk>']
  __A  :		Any       =			[14, 15, 20]
  self.assertListEqual(tokenizer.convert_tokens_to_ids(__lowercase						)      ,      __lowercase						)







 @slow
 def  snake_case__   (      self						):

  """simple docstring"""







  __A  :		List[Any]       =			XLMTokenizer.from_pretrained('xlm-mlm-en-2048'						)

  __A  :		Tuple       =			tokenizer.encode('sequence builders'      ,      add_special_tokens=__lowercase						)
  __A  :		Union[str, Any]       =			tokenizer.encode('multi-sequence build'      ,      add_special_tokens=__lowercase						)

  __A  :		int       =			tokenizer.build_inputs_with_special_tokens(__lowercase						)
  __A  :		Tuple       =			tokenizer.build_inputs_with_special_tokens(__lowercase      ,      __lowercase						)

  assert encoded_sentence == [0] + text + [1]
  assert encoded_pair == [0] + text + [1] + text_a + [1]






