import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch
    from torch import nn

    from transformers import SwiftFormerForImageClassification, SwiftFormerModel
    from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import ViTImageProcessor
class     a_			:






 '''simple docstring'''






 def __init__(      self :  Optional[Any]      ,   lowercase__ :  Tuple      ,   lowercase__ :  Optional[Any]=13      ,   lowercase__ :  List[Any]=3      ,   lowercase__ :  Optional[Any]=True      ,   lowercase__ :  str=True      ,   lowercase__ :  Optional[int]=0.1      ,   lowercase__ :  Optional[int]=0.1      ,   lowercase__ :  Optional[Any]=224      ,   lowercase__ :  Tuple=1_000      ,   lowercase__ :  Optional[int]=[3, 3, 6, 4]      ,   lowercase__ :  List[Any]=[48, 56, 112, 220]      ,   ):







        '''simple docstring'''



        lowerCAmelCase__					     = parent
        lowerCAmelCase__					     = batch_size
        lowerCAmelCase__					     = num_channels
        lowerCAmelCase__					     = is_training
        lowerCAmelCase__					     = use_labels
        lowerCAmelCase__					     = hidden_dropout_prob
        lowerCAmelCase__					     = attention_probs_dropout_prob
        lowerCAmelCase__					     = num_labels
        lowerCAmelCase__					     = image_size
        lowerCAmelCase__					     = layer_depths
        lowerCAmelCase__					     = embed_dims




 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



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

        lowerCAmelCase__					     = None
        if self.use_labels:
               lowerCAmelCase__					     = ids_tensor([self.batch_size]      ,   self.num_labels)

        lowerCAmelCase__					     = self.get_config()

        return config, pixel_values, labels




 def 		__snake_case						(      self :  List[Any]):







        '''simple docstring'''



        return SwiftFormerConfig(
            depths=self.layer_depths      ,   embed_dims=self.embed_dims      ,   mlp_ratio=4      ,   downsamples=[True, True, True, True]      ,   hidden_act='gelu'      ,   num_labels=self.num_labels      ,   down_patch_size=3      ,   down_stride=2      ,   down_pad=1      ,   drop_rate=0.0      ,   drop_path_rate=0.0      ,   use_layer_scale=lowercase__      ,   layer_scale_init_value=1e-5      ,   )




 def 		__snake_case						(      self :  str      ,   lowercase__ :  Tuple      ,   lowercase__ :  str      ,   lowercase__ :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = SwiftFormerModel(config=lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__)
        self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.embed_dims[-1], 7, 7))




 def 		__snake_case						(      self :  Dict      ,   lowercase__ :  Optional[int]      ,   lowercase__ :  Dict      ,   lowercase__ :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__					     = self.num_labels
        lowerCAmelCase__					     = SwiftFormerForImageClassification(lowercase__)
        model.to(lowercase__)
        model.eval()
        lowerCAmelCase__					     = model(lowercase__      ,   labels=lowercase__)
        self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_labels))

        lowerCAmelCase__					     = SwiftFormerForImageClassification(lowercase__)
        model.to(lowercase__)
        model.eval()

        lowerCAmelCase__					     = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])
        lowerCAmelCase__					     = model(lowercase__)
        self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_labels))




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



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






@require_torch
class     a_			(						SCREAMING_SNAKE_CASE	,					SCREAMING_SNAKE_CASE	,					unittest.TestCase						):






 '''simple docstring'''






 UpperCAmelCase_     			= (SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
 UpperCAmelCase_     			= (
     {'feature-extraction': SwiftFormerModel, 'image-classification': SwiftFormerForImageClassification}
     if is_torch_available()
     else {}
 )

 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False
 UpperCAmelCase_     			= False




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__					     = SwiftFormerModelTester(self)
        lowerCAmelCase__					     = ConfigTester(
            self      ,   config_class=lowercase__      ,   has_text_modality=lowercase__      ,   hidden_size=37      ,   num_attention_heads=12      ,   num_hidden_layers=12      ,   )




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        self.config_tester.run_common_tests()




 @unittest.skip(reason='SwiftFormer does not use inputs_embeds')
 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        pass




 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = model_class(lowercase__)
               lowerCAmelCase__					     = model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowercase__      ,   nn.Linear))




 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

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

               lowerCAmelCase__					     = ['pixel_values']
               self.assertListEqual(arg_names[:1]      ,   lowercase__)




 def 		__snake_case						(      self :  List[str]):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowercase__)




 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowercase__)




 @slow
 def 		__snake_case						(      self :  Tuple):







        '''simple docstring'''



        for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowerCAmelCase__					     = SwiftFormerModel.from_pretrained(lowercase__)
               self.assertIsNotNone(lowercase__)




 @unittest.skip(reason='SwiftFormer does not output attentions')
 def 		__snake_case						(      self :  Union[str, Any]):







        '''simple docstring'''



        pass




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        def check_hidden_states_output(lowercase__ :  Dict      ,   lowercase__ :  List[str]      ,   lowercase__ :  str):
               lowerCAmelCase__					     = model_class(lowercase__)
               model.to(lowercase__)
               model.eval()

               with torch.no_grad():
                      lowerCAmelCase__					     = model(**self._prepare_for_class(lowercase__      ,   lowercase__))

               lowerCAmelCase__					     = outputs.hidden_states

               lowerCAmelCase__					     = 8
               self.assertEqual(len(lowercase__)      ,   lowercase__)  # TODO

               # SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
               # with the width and height being successively divided by 2, after every 2 blocks
               for i in range(len(lowercase__)):
                      self.assertEqual(
                          hidden_states[i].shape      ,   torch.Size(
                              [
                                  self.model_tester.batch_size,
                                  self.model_tester.embed_dims[i // 2],
                                  (self.model_tester.image_size // 4) // 2 ** (i // 2),
                                  (self.model_tester.image_size // 4) // 2 ** (i // 2),
                              ])      ,   )

        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = True
               check_hidden_states_output(lowercase__      ,   lowercase__      ,   lowercase__)

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

               check_hidden_states_output(lowercase__      ,   lowercase__      ,   lowercase__)




 def 		__snake_case						(      self :  Dict):







        '''simple docstring'''



        def _config_zero_init(lowercase__ :  Dict):
               lowerCAmelCase__					     = copy.deepcopy(lowercase__)
               for key in configs_no_init.__dict__.keys():
                      if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
                             setattr(lowercase__      ,   lowercase__      ,   1e-10)
                      if isinstance(getattr(lowercase__      ,   lowercase__      ,   lowercase__)      ,   lowercase__):
                             lowerCAmelCase__					     = _config_zero_init(getattr(lowercase__      ,   lowercase__))
                             setattr(lowercase__      ,   lowercase__      ,   lowercase__)
               return configs_no_init

        lowerCAmelCase__     , lowerCAmelCase__					     = self.model_tester.prepare_config_and_inputs_for_common()

        lowerCAmelCase__					     = _config_zero_init(lowercase__)
        for model_class in self.all_model_classes:
               lowerCAmelCase__					     = model_class(config=lowercase__)
               for name, param in model.named_parameters():
                      if param.requires_grad:
                             self.assertIn(
                                 ((param.data.mean() * 1e9) / 1e9).round().item()      ,   [0.0, 1.0]      ,   msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""      ,   )




 @unittest.skip('Will be fixed soon by reducing the size of the model used for common tests.')
 def 		__snake_case						(      self :  int):







        '''simple docstring'''



        pass






def 				__lowerCamelCase		(						):
       lowerCAmelCase__					     = Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
       return image



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






 '''simple docstring'''






 @cached_property
 def 		__snake_case						(      self :  Any):







        '''simple docstring'''



        return ViTImageProcessor.from_pretrained('MBZUAI/swiftformer-xs') if is_vision_available() else None




 @slow
 def 		__snake_case						(      self :  Optional[Any]):







        '''simple docstring'''



        lowerCAmelCase__					     = SwiftFormerForImageClassification.from_pretrained('MBZUAI/swiftformer-xs').to(lowercase__)

        lowerCAmelCase__					     = self.default_image_processor
        lowerCAmelCase__					     = prepare_img()
        lowerCAmelCase__					     = image_processor(images=lowercase__      ,   return_tensors='pt').to(lowercase__)

        # forward pass
        with torch.no_grad():
               lowerCAmelCase__					     = model(**lowercase__)

        # verify the logits
        lowerCAmelCase__					     = torch.Size((1, 1_000))
        self.assertEqual(outputs.logits.shape      ,   lowercase__)

        lowerCAmelCase__					     = torch.tensor([[-2.1703e00, 2.1107e00, -2.0811e00]]).to(lowercase__)
        self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,   lowercase__      ,   atol=1e-4))

lowerCAmelCase__     						=			'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       # Make sure the supplied data is a bytes-like object
       if not isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ):
              lowerCAmelCase__					     = F"""a bytes-like object is required, not '{data.__class__.__name__}'"""
              raise TypeError(lowerCAmelCase__     )

       lowerCAmelCase__					     = ''.join(bin(lowerCAmelCase__     )[2:].zfill(8     ) for byte in data     )

       lowerCAmelCase__					     = len(lowerCAmelCase__     ) % 6 != 0

       if padding_needed:
              # The padding that will be added later
              lowerCAmelCase__					     = b'=' * ((6 - len(lowerCAmelCase__     ) % 6) // 2)

              # Append binary_stream with arbitrary binary digits (0's by default) to make its
              # length a multiple of 6.
              binary_stream += "0" * (6 - len(lowerCAmelCase__     ) % 6)
       else:
              lowerCAmelCase__					     = b''

       # Encode every 6 binary digits to their corresponding Base64 character
       return (
           "".join(
               B64_CHARSET[int(binary_stream[index : index + 6]     ,  2     )]
               for index in range(0     ,  len(lowerCAmelCase__     )     ,  6     )     ).encode()
           + padding
       )






def 				__lowerCamelCase		(						lowerCAmelCase__     ):
       # Make sure encoded_data is either a string or a bytes-like object
       if not isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ) and not isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ):
              lowerCAmelCase__					     = (
                  'argument should be a bytes-like object or ASCII string, '
                  F"""not '{encoded_data.__class__.__name__}'"""
              )
              raise TypeError(lowerCAmelCase__     )

       # In case encoded_data is a bytes-like object, make sure it contains only
       # ASCII characters so we convert it to a string object
       if isinstance(lowerCAmelCase__     ,  lowerCAmelCase__     ):
              try:
                     lowerCAmelCase__					     = encoded_data.decode('utf-8'     )
              except UnicodeDecodeError:
                     raise ValueError('base64 encoded data should only contain ASCII characters'     )

       lowerCAmelCase__					     = encoded_data.count('='     )

       # Check if the encoded string contains non base64 characters
       if padding:
              assert all(
                  char in B64_CHARSET for char in encoded_data[:-padding]     ), "Invalid base64 character(s) found."
       else:
              assert all(
                  char in B64_CHARSET for char in encoded_data     ), "Invalid base64 character(s) found."

       # Check the padding
       assert len(lowerCAmelCase__     ) % 4 == 0 and padding < 3, "Incorrect padding"

       if padding:
              # Remove padding if there is one
              lowerCAmelCase__					     = encoded_data[:-padding]

              lowerCAmelCase__					     = ''.join(
                  bin(B64_CHARSET.index(lowerCAmelCase__     )     )[2:].zfill(6     ) for char in encoded_data     )[: -padding * 2]
       else:
              lowerCAmelCase__					     = ''.join(
                  bin(B64_CHARSET.index(lowerCAmelCase__     )     )[2:].zfill(6     ) for char in encoded_data     )

       lowerCAmelCase__					     = [
           int(binary_stream[index : index + 8]     ,  2     )
           for index in range(0     ,  len(lowerCAmelCase__     )     ,  8     )
       ]

       return bytes(lowerCAmelCase__     )


if __name__ == "__main__":
    import doctest

    doctest.testmod()
