






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

       '''simple docstring'''



       def __init__(				self      ,    a__      ,    a__=13      ,    a__=3      ,    a__=True      ,    a__=True      ,    a__=0.1      ,    a__=0.1      ,    a__=2_24      ,    a__=10_00      ,    a__=[3, 3, 6, 4]      ,    a__=[48, 56, 1_12, 2_20]      ,    )			->   str:






            '''simple docstring'''





            __snake_case      :Optional[int] 							=    parent
            __snake_case      :List[str] 							=    batch_size
            __snake_case      :Dict 							=    num_channels
            __snake_case      :Optional[int] 							=    is_training
            __snake_case      :Union[str, Any] 							=    use_labels
            __snake_case      :List[str] 							=    hidden_dropout_prob
            __snake_case      :Union[str, Any] 							=    attention_probs_dropout_prob
            __snake_case      :Tuple 							=    num_labels
            __snake_case      :Optional[Any] 							=    image_size
            __snake_case      :List[str] 							=    layer_depths
            __snake_case      :Optional[Any] 							=    embed_dims



       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





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

            __snake_case      :List[str] 							=    None
            if self.use_labels:
                 __snake_case      :List[str] 							=    ids_tensor([self.batch_size]      ,    self.num_labels     )

            __snake_case      :str 							=    self.get_config()

            return config, pixel_values, labels



       def        __lowercase    (				self     )			->   Union[str, 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=a__      ,    layer_scale_init_value=1e-5      ,    )



       def        __lowercase    (				self      ,    a__      ,    a__      ,    a__     )			->   Tuple:






            '''simple docstring'''





            __snake_case      :str 							=    SwiftFormerModel(config=a__     )
            model.to(a__     )
            model.eval()
            __snake_case      :Optional[Any] 							=    model(a__     )
            self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, self.embed_dims[-1], 7, 7)     )



       def        __lowercase    (				self      ,    a__      ,    a__      ,    a__     )			->   Tuple:






            '''simple docstring'''





            __snake_case      :Tuple 							=    self.num_labels
            __snake_case      :str 							=    SwiftFormerForImageClassification(a__     )
            model.to(a__     )
            model.eval()
            __snake_case      :Optional[int] 							=    model(a__      ,    labels=a__     )
            self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels)     )

            __snake_case      :Optional[Any] 							=    SwiftFormerForImageClassification(a__     )
            model.to(a__     )
            model.eval()

            __snake_case      :Optional[int] 							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]     )
            __snake_case      :int 							=    model(a__     )
            self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.num_labels)     )



       def        __lowercase    (				self     )			->   Tuple:






            '''simple docstring'''





            ((__snake_case)							,			(__snake_case)							,			(__snake_case))      :List[str] 							=    self.prepare_config_and_inputs()
            __snake_case      :Optional[Any] 							=    {"""pixel_values""": pixel_values}
            return config, inputs_dict




@require_torch
class       snake_case__ (				lowercase_						,     lowercase_						,     unittest.TestCase):

       '''simple docstring'''
       lowerCamelCase				:					Optional[int]								=			(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
       lowerCamelCase				:					Dict								=			(
           {"feature-extraction": SwiftFormerModel, "image-classification": SwiftFormerForImageClassification}
           if is_torch_available()
           else {}
       )

       lowerCamelCase				:					int								=			False
       lowerCamelCase				:					Optional[int]								=			False
       lowerCamelCase				:					List[str]								=			False
       lowerCamelCase				:					int								=			False
       lowerCamelCase				:					Any								=			False



       def        __lowercase    (				self     )			->   Tuple:






            '''simple docstring'''





            __snake_case      :List[Any] 							=    SwiftFormerModelTester(self     )
            __snake_case      :Dict 							=    ConfigTester(
                self      ,    config_class=a__      ,    has_text_modality=a__      ,    hidden_size=37      ,    num_attention_heads=12      ,    num_hidden_layers=12      ,    )



       def        __lowercase    (				self     )			->   Any:






            '''simple docstring'''





            self.config_tester.run_common_tests()



       @unittest.skip(reason="""SwiftFormer does not use inputs_embeds"""     )
       def        __lowercase    (				self     )			->   Tuple:






            '''simple docstring'''





            pass



       def        __lowercase    (				self     )			->   str:






            '''simple docstring'''





            __snake_case							,			__snake_case      :int 							=    self.model_tester.prepare_config_and_inputs_for_common()

            for model_class in self.all_model_classes:
                 __snake_case      :Dict 							=    model_class(a__     )
                 __snake_case      :Optional[int] 							=    model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(a__      ,    nn.Linear     )     )



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






            '''simple docstring'''





            __snake_case							,			__snake_case      :List[str] 							=    self.model_tester.prepare_config_and_inputs_for_common()

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

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



       def        __lowercase    (				self     )			->   Dict:






            '''simple docstring'''





            __snake_case      :str 							=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*a__     )



       def        __lowercase    (				self     )			->   Any:






            '''simple docstring'''





            __snake_case      :Optional[int] 							=    self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_image_classification(*a__     )



       @slow
       def        __lowercase    (				self     )			->   int:






            '''simple docstring'''





            for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __snake_case      :List[Any] 							=    SwiftFormerModel.from_pretrained(a__     )
                 self.assertIsNotNone(a__     )



       @unittest.skip(reason="""SwiftFormer does not output attentions"""     )
       def        __lowercase    (				self     )			->   Optional[int]:






            '''simple docstring'''





            pass



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






            '''simple docstring'''





            def check_hidden_states_output(a__      ,    a__      ,    a__     ):
                 __snake_case      :Optional[Any] 							=    model_class(a__     )
                 model.to(a__     )
                 model.eval()

                 with torch.no_grad():
                      __snake_case      :Union[str, Any] 							=    model(**self._prepare_for_class(a__      ,    a__     )     )

                 __snake_case      :List[Any] 							=    outputs.hidden_states

                 __snake_case      :List[str] 							=    8
                 self.assertEqual(len(a__     )      ,    a__     )  # 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(a__     )     ):
                      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),
                              ]     )      ,    )

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

            for model_class in self.all_model_classes:
                 __snake_case      :str 							=    True
                 check_hidden_states_output(a__      ,    a__      ,    a__     )

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

                 check_hidden_states_output(a__      ,    a__      ,    a__     )



       def        __lowercase    (				self     )			->   Optional[Any]:






            '''simple docstring'''





            def _config_zero_init(a__     ):
                 __snake_case      :Any 							=    copy.deepcopy(a__     )
                 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(a__      ,    a__      ,    1e-10     )
                      if isinstance(getattr(a__      ,    a__      ,    a__     )      ,    a__     ):
                           __snake_case      :List[Any] 							=    _config_zero_init(getattr(a__      ,    a__     )     )
                           setattr(a__      ,    a__      ,    a__     )
                 return configs_no_init

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

            __snake_case      :List[str] 							=    _config_zero_init(a__     )
            for model_class in self.all_model_classes:
                 __snake_case      :Union[str, Any] 							=    model_class(config=a__     )
                 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        __lowercase    (				self     )			->   Optional[int]:






            '''simple docstring'''





            pass






def 	UpperCamelCase	(							):




     '''simple docstring'''






     __snake_case      :Tuple 							=    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""	)
     return image




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

       '''simple docstring'''



       @cached_property
       def        __lowercase    (				self     )			->   List[Any]:






            '''simple docstring'''





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



       @slow
       def        __lowercase    (				self     )			->   Any:






            '''simple docstring'''





            __snake_case      :List[str] 							=    SwiftFormerForImageClassification.from_pretrained("""MBZUAI/swiftformer-xs"""     ).to(a__     )

            __snake_case      :List[Any] 							=    self.default_image_processor
            __snake_case      :List[Any] 							=    prepare_img()
            __snake_case      :Union[str, Any] 							=    image_processor(images=a__      ,    return_tensors="""pt"""     ).to(a__     )

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

            # verify the logits
            __snake_case      :str 							=    torch.Size((1, 10_00)     )
            self.assertEqual(outputs.logits.shape      ,    a__     )

            __snake_case      :Dict 							=    torch.tensor([[-2.17_03e00, 2.11_07e00, -2.08_11e00]]     ).to(a__     )
            self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,    a__      ,    atol=1e-4     )     )










# Lint as: python3
import sys
from collections.abc import Mapping
from typing import TYPE_CHECKING

import numpy as np
import pyarrow as pa

from .. import config
from ..utils.py_utils import map_nested
from .formatting import TensorFormatter


if TYPE_CHECKING:
     import torch




class       snake_case__ (				TensorFormatter[Mapping, "torch.Tensor", Mapping]):

       '''simple docstring'''



       def __init__(				self      ,    a__=None      ,    **a__     )			->   Tuple:






            '''simple docstring'''





            super().__init__(features=a__     )
            __snake_case      :int 							=    torch_tensor_kwargs
            import torch  # noqa import torch at initialization



       def        __lowercase    (				self      ,    a__     )			->   List[str]:






            '''simple docstring'''





            import torch

            if isinstance(a__      ,    a__     ) and column:
                 if all(
                     isinstance(a__      ,    torch.Tensor     ) and x.shape == column[0].shape and x.dtype == column[0].dtype
                     for x in column     ):
                      return torch.stack(a__     )
            return column



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






            '''simple docstring'''





            import torch

            if isinstance(a__      ,    (str, bytes, type(a__     ))     ):
                 return value
            elif isinstance(a__      ,    (np.character, np.ndarray)     ) and np.issubdtype(value.dtype      ,    np.character     ):
                 return value.tolist()

            __snake_case      :Optional[int] 							=    {}

            if isinstance(a__      ,    (np.number, np.ndarray)     ) and np.issubdtype(value.dtype      ,    np.integer     ):
                 __snake_case      :List[Any] 							=    {"""dtype""": torch.intaa}
            elif isinstance(a__      ,    (np.number, np.ndarray)     ) and np.issubdtype(value.dtype      ,    np.floating     ):
                 __snake_case      :Tuple 							=    {"""dtype""": torch.floataa}
            elif config.PIL_AVAILABLE and "PIL" in sys.modules:
                 import PIL.Image

                 if isinstance(a__      ,    PIL.Image.Image     ):
                      __snake_case      :Union[str, Any] 							=    np.asarray(a__     )
            return torch.tensor(a__      ,    **{**default_dtype, **self.torch_tensor_kwargs}     )



       def        __lowercase    (				self      ,    a__     )			->   str:






            '''simple docstring'''





            import torch

            # support for torch, tf, jax etc.
            if hasattr(a__      ,    """__array__"""     ) and not isinstance(a__      ,    torch.Tensor     ):
                 __snake_case      :Optional[Any] 							=    data_struct.__array__()
            # support for nested types like struct of list of struct
            if isinstance(a__      ,    np.ndarray     ):
                 if data_struct.dtype == object:  # torch tensors cannot be instantied from an array of objects
                      return self._consolidate([self.recursive_tensorize(a__     ) for substruct in data_struct]     )
            elif isinstance(a__      ,    (list, tuple)     ):
                 return self._consolidate([self.recursive_tensorize(a__     ) for substruct in data_struct]     )
            return self._tensorize(a__     )



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






            '''simple docstring'''





            return map_nested(self._recursive_tensorize      ,    a__      ,    map_list=a__     )



       def        __lowercase    (				self      ,    a__     )			->   Mapping:






            '''simple docstring'''





            __snake_case      :Tuple 							=    self.numpy_arrow_extractor().extract_row(a__     )
            __snake_case      :Any 							=    self.python_features_decoder.decode_row(a__     )
            return self.recursive_tensorize(a__     )



       def        __lowercase    (				self      ,    a__     )			->   "torch.Tensor":






            '''simple docstring'''





            __snake_case      :List[str] 							=    self.numpy_arrow_extractor().extract_column(a__     )
            __snake_case      :List[Any] 							=    self.python_features_decoder.decode_column(a__      ,    pa_table.column_names[0]     )
            __snake_case      :Optional[Any] 							=    self.recursive_tensorize(a__     )
            __snake_case      :Any 							=    self._consolidate(a__     )
            return column



       def        __lowercase    (				self      ,    a__     )			->   Mapping:






            '''simple docstring'''





            __snake_case      :Optional[int] 							=    self.numpy_arrow_extractor().extract_batch(a__     )
            __snake_case      :Tuple 							=    self.python_features_decoder.decode_batch(a__     )
            __snake_case      :Tuple 							=    self.recursive_tensorize(a__     )
            for column_name in batch:
                 __snake_case      :str 							=    self._consolidate(batch[column_name]     )
            return batch


