





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		,      lowerCAmelCase__		,      lowerCAmelCase__=1_3		,      lowerCAmelCase__=3_2		,      lowerCAmelCase__=2		,      lowerCAmelCase__=3		,      lowerCAmelCase__=1_6		,      lowerCAmelCase__=[1, 2, 1]		,      lowerCAmelCase__=[2, 2, 4]		,      lowerCAmelCase__=2		,      lowerCAmelCase__=2.0		,      lowerCAmelCase__=True		,      lowerCAmelCase__=0.0		,      lowerCAmelCase__=0.0		,      lowerCAmelCase__=0.1		,      lowerCAmelCase__="gelu"		,      lowerCAmelCase__=False		,      lowerCAmelCase__=True		,      lowerCAmelCase__=0.02		,      lowerCAmelCase__=1E-5		,      lowerCAmelCase__=True		,      lowerCAmelCase__=None		,      lowerCAmelCase__=True		,      lowerCAmelCase__=1_0		,      lowerCAmelCase__=8		,      )   ->						Union[str, Any]:



        '''simple docstring'''




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







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




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

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

        a__	:					Any										=self.get_config()

        return config, pixel_values, labels







      def    _lowercase  (    self							)   ->						Optional[int]:



        '''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    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						Dict:



        '''simple docstring'''




        a__	:					List[Any]										=SwinvaModel(config=_snake_case							)
        model.to(_snake_case							)
        model.eval()
        a__	:					Optional[int]										=model(_snake_case							)

        a__	:					List[Any]										=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths							) - 1))
        a__	:					List[Any]										=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    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						List[Any]:



        '''simple docstring'''




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

        # test greyscale images
        a__	:					Optional[Any]										=1
        a__	:					Optional[int]										=SwinvaForMaskedImageModeling(_snake_case							)
        model.to(_snake_case							)
        model.eval()

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







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						Any:



        '''simple docstring'''




        a__	:					Tuple										=self.type_sequence_label_size
        a__	:					int										=SwinvaForImageClassification(_snake_case							)
        model.to(_snake_case							)
        model.eval()
        a__	:					Tuple										=model(_snake_case		,      labels=_snake_case							)
        self.parent.assertEqual(result.logits.shape		,      (self.batch_size, self.type_sequence_label_size)							)







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




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







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

      _lowercase				:					int						=			False
      _lowercase				:					List[str]						=			False
      _lowercase				:					List[Any]						=			False
      _lowercase				:					int						=			False







      def    _lowercase  (    self							)   ->						Dict:



        '''simple docstring'''




        a__	:					Optional[int]										=SwinvaModelTester(self							)
        a__	:					int										=ConfigTester(self		,      config_class=_snake_case		,      embed_dim=3_7							)







      def    _lowercase  (    self							)   ->						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    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        a__	:					int										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*_snake_case							)







      @unittest.skip(reason="Got `CUDA error: misaligned address` with PyTorch 2.0.0."							)
      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        pass







      @unittest.skip(reason="Swinv2 does not use inputs_embeds"							)
      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




        pass







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



        '''simple docstring'''




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

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







      def    _lowercase  (    self							)   ->						Optional[int]:



        '''simple docstring'''




        a__	:					Any										=self.model_tester.prepare_config_and_inputs_for_common()

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

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







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



        '''simple docstring'''




        a__	:					Any										=self.model_tester.prepare_config_and_inputs_for_common()
        a__	:					int										=True

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

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

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

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

          if hasattr(self.model_tester		,      "num_hidden_states_types"							):
            a__	:					str										=self.model_tester.num_hidden_states_types
          else:
            # also another +1 for reshaped_hidden_states
            a__	:					Dict										=2
          self.assertEqual(out_len + added_hidden_states		,      len(_snake_case							)							)

          a__	:					Any										=outputs.attentions

          self.assertEqual(len(_snake_case							)		,      _snake_case							)

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







      def    _lowercase  (    self		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__		,      lowerCAmelCase__							)   ->						str:



        '''simple docstring'''




        a__	:					Dict										=model_class(_snake_case							)
        model.to(_snake_case							)
        model.eval()

        with torch.no_grad():
          a__	:					List[Any]										=model(**self._prepare_for_class(_snake_case		,      _snake_case							)							)

        a__	:					Dict										=outputs.hidden_states

        a__	:					int										=getattr(
            self.model_tester		,      "expected_num_hidden_layers"		,      len(self.model_tester.depths							) + 1							)
        self.assertEqual(len(_snake_case							)		,      _snake_case							)

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

        a__	:					Optional[Any]										=(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__	:					Union[str, Any]										=outputs.reshaped_hidden_states
        self.assertEqual(len(_snake_case							)		,      _snake_case							)

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







      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					Any										=self.model_tester.prepare_config_and_inputs_for_common()

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

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

          self.check_hidden_states_output(_snake_case		,      _snake_case		,      _snake_case		,      _snake_case							)







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




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

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

        a__	:					Tuple										=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__	:					int										=True
          self.check_hidden_states_output(_snake_case		,      _snake_case		,      _snake_case		,      (padded_height, padded_width)							)

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







      def    _lowercase  (    self							)   ->						Optional[int]:



        '''simple docstring'''




        a__	:					Any										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_image_modeling(*_snake_case							)







      def    _lowercase  (    self							)   ->						str:



        '''simple docstring'''




        a__	:					Any										=self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*_snake_case							)







      @slow
      def    _lowercase  (    self							)   ->						Union[str, Any]:



        '''simple docstring'''




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







      def    _lowercase  (    self							)   ->						List[str]:



        '''simple docstring'''




        a__	:					Any										=self.model_tester.prepare_config_and_inputs_for_common()

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



        '''simple docstring'''




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







      @slow
      def    _lowercase  (    self							)   ->						List[Any]:



        '''simple docstring'''




        a__	:					Any										=SwinvaForImageClassification.from_pretrained("microsoft/swinv2-tiny-patch4-window8-256"							).to(
            _snake_case							)
        a__	:					int										=self.default_image_processor

        a__	:					Union[str, Any]										=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"							)
        a__	:					Optional[Any]										=image_processor(images=_snake_case		,      return_tensors="pt"							).to(_snake_case							)

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

        # verify the logits
        a__	:					int										=torch.Size((1, 1_0_0_0)							)
        self.assertEqual(outputs.logits.shape		,      _snake_case							)
        a__	:					Optional[int]										=torch.tensor([-0.39_47, -0.43_06, 0.00_26]							).to(_snake_case							)
        self.assertTrue(torch.allclose(outputs.logits[0, :3]		,      _snake_case		,      atol=1E-4							)							)











import os
import unittest

from transformers.models.transfo_xl.tokenization_transfo_xl import VOCAB_FILES_NAMES, TransfoXLTokenizer

from ...test_tokenization_common import TokenizerTesterMixin




class 					snake_case__						(       lowerCAmelCase_				,     unittest.TestCase     ):






     """simple docstring"""


     _SCREAMING_SNAKE_CASE						=	TransfoXLTokenizer
     _SCREAMING_SNAKE_CASE						=	False
     _SCREAMING_SNAKE_CASE						=	False






     def    lowercase_   (       self  :  Optional[int]					)   ->Any:
            super().setUp()

            snake_case__							:	Tuple   	=	[
                '<unk>',
                '[CLS]',
                '[SEP]',
                'want',
                'unwanted',
                'wa',
                'un',
                'running',
                ',',
                'low',
                'l',
            ]
            snake_case__							:	Any   	=	os.path.join(self.tmpdirname, VOCAB_FILES_NAMES['vocab_file']					)
            with open(self.vocab_file, 'w', encoding='utf-8'					) as vocab_writer:
                   vocab_writer.write(''.join([x + '\n' for x in vocab_tokens]					)					)






     def    lowercase_   (       self  :  Union[str, Any], **_snake_case  :  List[Any]					)   ->Dict:
            snake_case__							:	str   	=	True
            return TransfoXLTokenizer.from_pretrained(self.tmpdirname, **_snake_case					)






     def    lowercase_   (       self  :  Optional[Any], _snake_case  :  str					)   ->Dict:
            snake_case__							:	List[Any]   	=	'<unk> UNwanted , running'
            snake_case__							:	List[Any]   	=	'<unk> unwanted, running'
            return input_text, output_text






     def    lowercase_   (       self  :  List[Any]					)   ->Tuple:
            snake_case__							:	Dict   	=	TransfoXLTokenizer(vocab_file=self.vocab_file, lower_case=_snake_case					)

            snake_case__							:	str   	=	tokenizer.tokenize('<unk> UNwanted , running'					)
            self.assertListEqual(_snake_case, ['<unk>', 'unwanted', ',', 'running']					)

            self.assertListEqual(tokenizer.convert_tokens_to_ids(_snake_case					), [0, 4, 8, 7]					)






     def    lowercase_   (       self  :  List[str]					)   ->List[Any]:
            snake_case__							:	str   	=	TransfoXLTokenizer(lower_case=_snake_case					)

            self.assertListEqual(
                tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '					), ['hello', '!', 'how', 'are', 'you', '?']					)






     def    lowercase_   (       self  :  Optional[int]					)   ->Optional[Any]:
            snake_case__							:	Optional[int]   	=	TransfoXLTokenizer(lower_case=_snake_case					)

            self.assertListEqual(
                tokenizer.tokenize(' \tHeLLo ! how  \n Are yoU ?  '					), ['HeLLo', '!', 'how', 'Are', 'yoU', '?']					)






     def    lowercase_   (       self  :  Optional[int]					)   ->Union[str, Any]:
            snake_case__							:	List[Any]   	=	TransfoXLTokenizer(lower_case=_snake_case					)
            snake_case__							:	Dict   	=	'Hello (bracket) and side-scrolled [and] Henry\'s $5,000 with 3.34 m. What\'s up!?'
            snake_case__							:	List[Any]   	=	[
                'Hello',
                '(',
                'bracket',
                ')',
                'and',
                'side',
                '@-@',
                'scrolled',
                '[',
                'and',
                ']',
                'Henry',
                '\'s',
                '$',
                '5',
                '@,@',
                '000',
                'with',
                '3',
                '@.@',
                '34',
                'm',
                '.',
                'What',
                '\'s',
                'up',
                '!',
                '?',
            ]

            self.assertListEqual(tokenizer.tokenize(_snake_case					), _snake_case					)

            self.assertEqual(tokenizer.convert_tokens_to_string(_snake_case					), _snake_case					)






     def    lowercase_   (       self  :  Dict					)   ->Any:
            snake_case__							:	Dict   	=	self.get_tokenizer()
            snake_case__							:	Optional[Any]   	=	len(_snake_case					)

            tokenizer.add_tokens(['new1', 'new2']					)
            tokenizer.move_added_token('new1', 1					)

            # Check that moved token is not copied (duplicate)
            self.assertEqual(len(_snake_case					), original_len + 2					)
            # Check that token is moved to specified id
            self.assertEqual(tokenizer.encode('new1'					), [1]					)
            self.assertEqual(tokenizer.decode([1]					), 'new1'					)






