


import collections
import inspect
import unittest

from transformers import SwinConfig
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_backbone_common import BackboneTesterMixin
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 SwinBackbone, SwinForImageClassification, SwinForMaskedImageModeling, SwinModel
   from transformers.models.swin.modeling_swin import SWIN_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import AutoImageProcessor

class  UpperCamelCase__ :






      def __init__(     self					:   Optional[int],		__lowerCamelCase					:   Any,		__lowerCamelCase					:   int=13,		__lowerCamelCase					:   Optional[int]=32,		__lowerCamelCase					:   List[Any]=2,		__lowerCamelCase					:   Optional[int]=3,		__lowerCamelCase					:   Optional[int]=16,		__lowerCamelCase					:   Dict=[1, 2, 1],		__lowerCamelCase					:   Tuple=[2, 2, 4],		__lowerCamelCase					:   Union[str, Any]=2,		__lowerCamelCase					:   Union[str, Any]=2.0,		__lowerCamelCase					:   Optional[Any]=True,		__lowerCamelCase					:   Optional[int]=0.0,		__lowerCamelCase					:   Dict=0.0,		__lowerCamelCase					:   Any=0.1,		__lowerCamelCase					:   List[Any]="gelu",		__lowerCamelCase					:   List[str]=False,		__lowerCamelCase					:   Union[str, Any]=True,		__lowerCamelCase					:   List[Any]=0.02,		__lowerCamelCase					:   Tuple=1e-5,		__lowerCamelCase					:   List[Any]=True,		__lowerCamelCase					:   Union[str, Any]=None,		__lowerCamelCase					:   Optional[Any]=True,		__lowerCamelCase					:   List[Any]=10,		__lowerCamelCase					:   Tuple=8,		__lowerCamelCase					:   str=["stage1", "stage2"],		__lowerCamelCase					:   List[str]=[1, 2],		)  -> Tuple:
           UpperCamelCase__							:  List[str]								=     parent
           UpperCamelCase__							:  Optional[Any]								=     batch_size
           UpperCamelCase__							:  Optional[Any]								=     image_size
           UpperCamelCase__							:  Optional[Any]								=     patch_size
           UpperCamelCase__							:  Tuple								=     num_channels
           UpperCamelCase__							:  List[str]								=     embed_dim
           UpperCamelCase__							:  int								=     depths
           UpperCamelCase__							:  str								=     num_heads
           UpperCamelCase__							:  List[str]								=     window_size
           UpperCamelCase__							:  List[str]								=     mlp_ratio
           UpperCamelCase__							:  Optional[Any]								=     qkv_bias
           UpperCamelCase__							:  Tuple								=     hidden_dropout_prob
           UpperCamelCase__							:  Optional[int]								=     attention_probs_dropout_prob
           UpperCamelCase__							:  Any								=     drop_path_rate
           UpperCamelCase__							:  Dict								=     hidden_act
           UpperCamelCase__							:  Optional[int]								=     use_absolute_embeddings
           UpperCamelCase__							:  str								=     patch_norm
           UpperCamelCase__							:  str								=     layer_norm_eps
           UpperCamelCase__							:  List[Any]								=     initializer_range
           UpperCamelCase__							:  Dict								=     is_training
           UpperCamelCase__							:  Any								=     scope
           UpperCamelCase__							:  str								=     use_labels
           UpperCamelCase__							:  Any								=     type_sequence_label_size
           UpperCamelCase__							:  Any								=     encoder_stride
           UpperCamelCase__							:  List[Any]								=     out_features
           UpperCamelCase__							:  List[Any]								=     out_indices






      def 	__lowercase(     self					:   Optional[Any]   )  -> Any:
           UpperCamelCase__							:  Tuple								=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]   )

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

           UpperCamelCase__							:  int								=     self.get_config()

           return config, pixel_values, labels






      def 	__lowercase(     self					:   List[str]   )  -> List[Any]:
           return SwinConfig(
               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,		out_features=self.out_features,		out_indices=self.out_indices,		)






      def 	__lowercase(     self					:   List[Any],		__lowerCamelCase					:   Optional[int],		__lowerCamelCase					:   Any,		__lowerCamelCase					:   Tuple   )  -> Optional[Any]:
           UpperCamelCase__							:  Tuple								=     SwinModel(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Optional[int]								=     model(__lowerCamelCase   )

           UpperCamelCase__							:  List[str]								=     ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths   ) - 1))
           UpperCamelCase__							:  Dict								=     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					:   Optional[Any],		__lowerCamelCase					:   int,		__lowerCamelCase					:   Union[str, Any],		__lowerCamelCase					:   Any   )  -> str:
           UpperCamelCase__							:  str								=     SwinBackbone(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Optional[int]								=     model(__lowerCamelCase   )

           # verify hidden states
           self.parent.assertEqual(len(result.feature_maps   ),		len(config.out_features   )   )
           self.parent.assertListEqual(list(result.feature_maps[0].shape   ),		[self.batch_size, model.channels[0], 16, 16]   )

           # verify channels
           self.parent.assertEqual(len(model.channels   ),		len(config.out_features   )   )

           # verify backbone works with out_features=None
           UpperCamelCase__							:  Union[str, Any]								=     None
           UpperCamelCase__							:  Optional[int]								=     SwinBackbone(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  Tuple								=     model(__lowerCamelCase   )

           # verify feature maps
           self.parent.assertEqual(len(result.feature_maps   ),		1   )
           self.parent.assertListEqual(list(result.feature_maps[0].shape   ),		[self.batch_size, model.channels[-1], 4, 4]   )

           # verify channels
           self.parent.assertEqual(len(model.channels   ),		1   )






      def 	__lowercase(     self					:   List[Any],		__lowerCamelCase					:   Any,		__lowerCamelCase					:   int,		__lowerCamelCase					:   Dict   )  -> int:
           UpperCamelCase__							:  List[Any]								=     SwinForMaskedImageModeling(config=__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  List[str]								=     model(__lowerCamelCase   )
           self.parent.assertEqual(
               result.logits.shape,		(self.batch_size, self.num_channels, self.image_size, self.image_size)   )

           # test greyscale images
           UpperCamelCase__							:  Optional[Any]								=     1
           UpperCamelCase__							:  Optional[int]								=     SwinForMaskedImageModeling(__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()

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






      def 	__lowercase(     self					:   Tuple,		__lowerCamelCase					:   List[Any],		__lowerCamelCase					:   int,		__lowerCamelCase					:   Dict   )  -> Tuple:
           UpperCamelCase__							:  Any								=     self.type_sequence_label_size
           UpperCamelCase__							:  int								=     SwinForImageClassification(__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()
           UpperCamelCase__							:  int								=     model(__lowerCamelCase,		labels=__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.type_sequence_label_size)   )

           # test greyscale images
           UpperCamelCase__							:  Tuple								=     1
           UpperCamelCase__							:  str								=     SwinForImageClassification(__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()

           UpperCamelCase__							:  List[str]								=     floats_tensor([self.batch_size, 1, self.image_size, self.image_size]   )
           UpperCamelCase__							:  Dict								=     model(__lowerCamelCase   )
           self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.type_sequence_label_size)   )






      def 	__lowercase(     self					:   Optional[int]   )  -> str:
           UpperCamelCase__							:  Dict								=     self.prepare_config_and_inputs()
           (
               (
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,(
               UpperCamelCase__
           )	,
           )							:  List[str]								=     config_and_inputs
           UpperCamelCase__							:  Optional[int]								=     {'''pixel_values''': pixel_values}
           return config, inputs_dict

@require_torch
class  UpperCamelCase__ ( __lowerCamelCase							,						__lowerCamelCase							,						unittest.TestCase			):
      a__   :     Tuple								=						(
          (
              SwinModel,
              SwinBackbone,
              SwinForImageClassification,
              SwinForMaskedImageModeling,
          )
          if is_torch_available()
          else ()
      )
      a__   :     Union[str, Any]								=						(
          {'feature-extraction': SwinModel, 'image-classification': SwinForImageClassification}
          if is_torch_available()
          else {}
      )
      a__   :     List[Any]								=						True

      a__   :     List[Any]								=						False
      a__   :     Optional[Any]								=						False
      a__   :     Dict								=						False






      def 	__lowercase(     self					:   List[str]   )  -> str:
           UpperCamelCase__							:  Dict								=     SwinModelTester(self   )
           UpperCamelCase__							:  List[Any]								=     ConfigTester(self,		config_class=__lowerCamelCase,		embed_dim=37   )






      def 	__lowercase(     self					:   List[str]   )  -> Dict:
           self.create_and_test_config_common_properties()
           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					:   Dict   )  -> List[Any]:
           return






      def 	__lowercase(     self					:   int   )  -> Any:
           UpperCamelCase__							:  Tuple								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*__lowerCamelCase   )






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






      def 	__lowercase(     self					:   Optional[Any]   )  -> List[Any]:
           super().test_training_gradient_checkpointing()






      def 	__lowercase(     self					:   Optional[int]   )  -> str:
           UpperCamelCase__							:  str								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[int]   )  -> Optional[Any]:
           UpperCamelCase__							:  str								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_masked_image_modeling(*__lowerCamelCase   )






      def 	__lowercase(     self					:   str   )  -> Tuple:
           UpperCamelCase__							:  Optional[Any]								=     self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase   )






      @unittest.skip(reason='''Swin does not use inputs_embeds'''   )
      def 	__lowercase(     self					:   Optional[Any]   )  -> List[Any]:
           pass






      @unittest.skip(reason='''Swin Transformer does not use feedforward chunking'''   )
      def 	__lowercase(     self					:   Any   )  -> Union[str, Any]:
           pass






      def 	__lowercase(     self					:   Optional[Any]   )  -> Union[str, Any]:
           UpperCamelCase__	,UpperCamelCase__							:  List[str]								=     self.model_tester.prepare_config_and_inputs_for_common()

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






      def 	__lowercase(     self					:   Any   )  -> Union[str, Any]:
           UpperCamelCase__	,UpperCamelCase__							:  Dict								=     self.model_tester.prepare_config_and_inputs_for_common()

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

                UpperCamelCase__							:  Union[str, Any]								=     ['''pixel_values''']
                self.assertListEqual(arg_names[:1],		__lowerCamelCase   )






      def 	__lowercase(     self					:   str   )  -> Dict:
           UpperCamelCase__	,UpperCamelCase__							:  str								=     self.model_tester.prepare_config_and_inputs_for_common()
           UpperCamelCase__							:  Any								=     True

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

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

                self.assertListEqual(
                    list(attentions[0].shape[-3:]   ),		[self.model_tester.num_heads[0], window_size_squared, window_size_squared],		)
                UpperCamelCase__							:  List[str]								=     len(__lowerCamelCase   )

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

                # also another +1 for reshaped_hidden_states
                UpperCamelCase__							:  str								=     1 if model_class.__name__ == '''SwinBackbone''' else 2
                self.assertEqual(out_len + added_hidden_states,		len(__lowerCamelCase   )   )

                UpperCamelCase__							:  str								=     outputs.attentions

                self.assertEqual(len(__lowerCamelCase   ),		__lowerCamelCase   )

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






      def 	__lowercase(     self					:   Dict,		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   Optional[Any],		__lowerCamelCase					:   List[str],		__lowerCamelCase					:   List[Any]   )  -> str:
           UpperCamelCase__							:  List[Any]								=     model_class(__lowerCamelCase   )
           model.to(__lowerCamelCase   )
           model.eval()

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

           UpperCamelCase__							:  Dict								=     outputs.hidden_states

           UpperCamelCase__							:  List[str]								=     getattr(
               self.model_tester,		'''expected_num_hidden_layers''',		len(self.model_tester.depths   ) + 1   )
           self.assertEqual(len(__lowerCamelCase   ),		__lowerCamelCase   )

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

           UpperCamelCase__							:  List[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],		)

           if not model_class.__name__ == "SwinBackbone":
                UpperCamelCase__							:  Union[str, Any]								=     outputs.reshaped_hidden_states
                self.assertEqual(len(__lowerCamelCase   ),		__lowerCamelCase   )

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






      def 	__lowercase(     self					:   str   )  -> Tuple:
           UpperCamelCase__	,UpperCamelCase__							:  Union[str, Any]								=     self.model_tester.prepare_config_and_inputs_for_common()

           UpperCamelCase__							:  Optional[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:
                UpperCamelCase__							:  Tuple								=     True
                self.check_hidden_states_output(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )

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

                self.check_hidden_states_output(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase   )






      def 	__lowercase(     self					:   Optional[Any]   )  -> List[Any]:
           UpperCamelCase__	,UpperCamelCase__							:  str								=     self.model_tester.prepare_config_and_inputs_for_common()
           UpperCamelCase__							:  str								=     3

           UpperCamelCase__							:  Tuple								=     (
               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)
           )
           UpperCamelCase__							:  int								=     (
               config.patch_size
               if isinstance(config.patch_size,		collections.abc.Iterable   )
               else (config.patch_size, config.patch_size)
           )

           UpperCamelCase__							:  Union[str, Any]								=     image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
           UpperCamelCase__							:  Dict								=     image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

           for model_class in self.all_model_classes:
                UpperCamelCase__							:  Any								=     True
                self.check_hidden_states_output(__lowerCamelCase,		__lowerCamelCase,		__lowerCamelCase,		(padded_height, padded_width)   )

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






      @slow
      def 	__lowercase(     self					:   Union[str, Any]   )  -> Dict:
           for model_name in SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                UpperCamelCase__							:  List[Any]								=     SwinModel.from_pretrained(__lowerCamelCase   )
                self.assertIsNotNone(__lowerCamelCase   )






      def 	__lowercase(     self					:   int   )  -> int:
           UpperCamelCase__	,UpperCamelCase__							:  Any								=     self.model_tester.prepare_config_and_inputs_for_common()

           UpperCamelCase__							:  List[str]								=     _config_zero_init(__lowerCamelCase   )
           for model_class in self.all_model_classes:
                UpperCamelCase__							:  List[Any]								=     model_class(config=__lowerCamelCase   )
                for name, param in model.named_parameters():
                     if "embeddings" 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  UpperCamelCase__ ( unittest.TestCase			):






      @cached_property
      def 	__lowercase(     self					:   Optional[Any]   )  -> Optional[Any]:
           return (
               AutoImageProcessor.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''   )
               if is_vision_available()
               else None
           )






      @slow
      def 	__lowercase(     self					:   str   )  -> List[Any]:
           UpperCamelCase__							:  List[Any]								=     SwinForImageClassification.from_pretrained('''microsoft/swin-tiny-patch4-window7-224'''   ).to(__lowerCamelCase   )
           UpperCamelCase__							:  Optional[Any]								=     self.default_image_processor

           UpperCamelCase__							:  int								=     Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
           UpperCamelCase__							:  Any								=     image_processor(images=__lowerCamelCase,		return_tensors='''pt'''   ).to(__lowerCamelCase   )

           # forward pass
           with torch.no_grad():
                UpperCamelCase__							:  Union[str, Any]								=     model(**__lowerCamelCase   )

           # verify the logits
           UpperCamelCase__							:  Any								=     torch.Size((1, 10_00)   )
           self.assertEqual(outputs.logits.shape,		__lowerCamelCase   )
           UpperCamelCase__							:  Dict								=     torch.tensor([-0.0948, -0.6454, -0.0921]   ).to(__lowerCamelCase   )
           self.assertTrue(torch.allclose(outputs.logits[0, :3],		__lowerCamelCase,		atol=1e-4   )   )

@require_torch
class  UpperCamelCase__ ( unittest.TestCase							,						__lowerCamelCase			):
      a__   :     Union[str, Any]								=						(SwinBackbone,) if is_torch_available() else ()
      a__   :     Optional[Any]								=						SwinConfig






      def 	__lowercase(     self					:   Optional[int]   )  -> Tuple:
           UpperCamelCase__							:  Tuple								=     SwinModelTester(self   )








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_flax_available,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


_SCREAMING_SNAKE_CASE :					Tuple			=	{
    """configuration_marian""": ["""MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MarianConfig""", """MarianOnnxConfig"""],
}

try:
   if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _SCREAMING_SNAKE_CASE :					Any			=	["""MarianTokenizer"""]

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _SCREAMING_SNAKE_CASE :					List[Any]			=	[
       """MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """MarianForCausalLM""",
       """MarianModel""",
       """MarianMTModel""",
       """MarianPreTrainedModel""",
   ]

try:
   if not is_tf_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _SCREAMING_SNAKE_CASE :					Dict			=	["""TFMarianModel""", """TFMarianMTModel""", """TFMarianPreTrainedModel"""]

try:
   if not is_flax_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   _SCREAMING_SNAKE_CASE :					Optional[Any]			=	["""FlaxMarianModel""", """FlaxMarianMTModel""", """FlaxMarianPreTrainedModel"""]

if TYPE_CHECKING:
   from .configuration_marian import MARIAN_PRETRAINED_CONFIG_ARCHIVE_MAP, MarianConfig, MarianOnnxConfig

   try:
      if not is_sentencepiece_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .tokenization_marian import MarianTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_marian import (
          MARIAN_PRETRAINED_MODEL_ARCHIVE_LIST,
          MarianForCausalLM,
          MarianModel,
          MarianMTModel,
          MarianPreTrainedModel,
      )

   try:
      if not is_tf_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_tf_marian import TFMarianModel, TFMarianMTModel, TFMarianPreTrainedModel

   try:
      if not is_flax_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_flax_marian import FlaxMarianModel, FlaxMarianMTModel, FlaxMarianPreTrainedModel

else:
   import sys

   _SCREAMING_SNAKE_CASE :					List[Any]			=	_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)




