




'''simple docstring'''





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							:






     def __init__(					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE=13   ,_SCREAMING_SNAKE_CASE=3   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=0.1   ,_SCREAMING_SNAKE_CASE=0.1   ,_SCREAMING_SNAKE_CASE=224   ,_SCREAMING_SNAKE_CASE=1_000   ,_SCREAMING_SNAKE_CASE=[3, 3, 6, 4]   ,_SCREAMING_SNAKE_CASE=[48, 56, 112, 220]   ,)				->     int:
          _snake_case				= parent
          _snake_case				= batch_size
          _snake_case				= num_channels
          _snake_case				= is_training
          _snake_case				= use_labels
          _snake_case				= hidden_dropout_prob
          _snake_case				= attention_probs_dropout_prob
          _snake_case				= num_labels
          _snake_case				= image_size
          _snake_case				= layer_depths
          _snake_case				= embed_dims






     def 							_lowercase      (					self       )				->     List[str]:
          _snake_case				= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

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

          _snake_case				= self.get_config()

          return config, pixel_values, labels






     def 							_lowercase      (					self       )				->     Optional[Any]:
          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=_SCREAMING_SNAKE_CASE   ,layer_scale_init_value=1e-5   ,)






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )				->     List[str]:
          _snake_case				= SwiftFormerModel(config=_SCREAMING_SNAKE_CASE       )
          model.to(_SCREAMING_SNAKE_CASE       )
          model.eval()
          _snake_case				= model(_SCREAMING_SNAKE_CASE       )
          self.parent.assertEqual(result.last_hidden_state.shape   ,(self.batch_size, self.embed_dims[-1], 7, 7)       )






     def 							_lowercase      (					self   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )				->     int:
          _snake_case				= self.num_labels
          _snake_case				= SwiftFormerForImageClassification(_SCREAMING_SNAKE_CASE       )
          model.to(_SCREAMING_SNAKE_CASE       )
          model.eval()
          _snake_case				= model(_SCREAMING_SNAKE_CASE   ,labels=_SCREAMING_SNAKE_CASE       )
          self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)       )

          _snake_case				= SwiftFormerForImageClassification(_SCREAMING_SNAKE_CASE       )
          model.to(_SCREAMING_SNAKE_CASE       )
          model.eval()

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






     def 							_lowercase      (					self       )				->     Any:
          ((_snake_case)				,   (_snake_case)				,   (_snake_case))				= self.prepare_config_and_inputs()
          _snake_case				= {"pixel_values": pixel_values}
          return config, inputs_dict







@require_torch
class   _a							(		__lowerCAmelCase   ,   __lowerCAmelCase   ,   unittest.TestCase				):
     SCREAMING_SNAKE_CASE_				:       Any       							=		(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
     SCREAMING_SNAKE_CASE_				:       str       							=		(
         {"""feature-extraction""": SwiftFormerModel, """image-classification""": SwiftFormerForImageClassification}
         if is_torch_available()
         else {}
     )

     SCREAMING_SNAKE_CASE_				:       int       							=		False
     SCREAMING_SNAKE_CASE_				:       str       							=		False
     SCREAMING_SNAKE_CASE_				:       Optional[int]       							=		False
     SCREAMING_SNAKE_CASE_				:       List[str]       							=		False
     SCREAMING_SNAKE_CASE_				:       int       							=		False






     def 							_lowercase      (					self       )				->     Dict:
          _snake_case				= SwiftFormerModelTester(self       )
          _snake_case				= ConfigTester(
              self   ,config_class=_SCREAMING_SNAKE_CASE   ,has_text_modality=_SCREAMING_SNAKE_CASE   ,hidden_size=37   ,num_attention_heads=12   ,num_hidden_layers=12   ,)






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






     @unittest.skip(reason="SwiftFormer does not use inputs_embeds"       )
     def 							_lowercase      (					self       )				->     Optional[int]:
          pass






     def 							_lowercase      (					self       )				->     str:
          _snake_case				,   _snake_case				= self.model_tester.prepare_config_and_inputs_for_common()

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






     def 							_lowercase      (					self       )				->     Optional[Any]:
          _snake_case				,   _snake_case				= self.model_tester.prepare_config_and_inputs_for_common()

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

               _snake_case				= ["pixel_values"]
               self.assertListEqual(arg_names[:1]   ,_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self       )				->     Optional[Any]:
          _snake_case				= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self       )				->     List[Any]:
          _snake_case				= self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*_SCREAMING_SNAKE_CASE       )






     @slow
     def 							_lowercase      (					self       )				->     int:
          for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               _snake_case				= SwiftFormerModel.from_pretrained(_SCREAMING_SNAKE_CASE       )
               self.assertIsNotNone(_SCREAMING_SNAKE_CASE       )






     @unittest.skip(reason="SwiftFormer does not output attentions"       )
     def 							_lowercase      (					self       )				->     Any:
          pass






     def 							_lowercase      (					self       )				->     Dict:
          def check_hidden_states_output(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       ):
               _snake_case				= model_class(_SCREAMING_SNAKE_CASE       )
               model.to(_SCREAMING_SNAKE_CASE       )
               model.eval()

               with torch.no_grad():
                    _snake_case				= model(**self._prepare_for_class(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )       )

               _snake_case				= outputs.hidden_states

               _snake_case				= 8
               self.assertEqual(len(_SCREAMING_SNAKE_CASE       )   ,_SCREAMING_SNAKE_CASE       )  # 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(_SCREAMING_SNAKE_CASE       )       ):
                    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				= self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
               _snake_case				= True
               check_hidden_states_output(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )

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

               check_hidden_states_output(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )






     def 							_lowercase      (					self       )				->     str:
          def _config_zero_init(_SCREAMING_SNAKE_CASE       ):
               _snake_case				= copy.deepcopy(_SCREAMING_SNAKE_CASE       )
               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(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,1e-10       )
                    if isinstance(getattr(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )   ,_SCREAMING_SNAKE_CASE       ):
                         _snake_case				= _config_zero_init(getattr(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )       )
                         setattr(_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE   ,_SCREAMING_SNAKE_CASE       )
               return configs_no_init

          _snake_case				,   _snake_case				= self.model_tester.prepare_config_and_inputs_for_common()

          _snake_case				= _config_zero_init(_SCREAMING_SNAKE_CASE       )
          for model_class in self.all_model_classes:
               _snake_case				= model_class(config=_SCREAMING_SNAKE_CASE       )
               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       )				->     Dict:
          pass







def       __a      (  )		->					List[str]:

     """simple docstring"""




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





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






     @cached_property
     def 							_lowercase      (					self       )				->     Optional[int]:
          return ViTImageProcessor.from_pretrained("MBZUAI/swiftformer-xs"       ) if is_vision_available() else None






     @slow
     def 							_lowercase      (					self       )				->     Any:
          _snake_case				= SwiftFormerForImageClassification.from_pretrained("MBZUAI/swiftformer-xs"       ).to(_SCREAMING_SNAKE_CASE       )

          _snake_case				= self.default_image_processor
          _snake_case				= prepare_img()
          _snake_case				= image_processor(images=_SCREAMING_SNAKE_CASE   ,return_tensors="pt"       ).to(_SCREAMING_SNAKE_CASE       )

          # forward pass
          with torch.no_grad():
               _snake_case				= model(**_SCREAMING_SNAKE_CASE       )

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

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








'''simple docstring'''





from ...configuration_utils import PretrainedConfig
from ...utils import logging


UpperCamelCase_		:     int          =		logging.get_logger(__name__)

UpperCamelCase_		:     Any          =		{
    '''tiiuae/falcon-40b''': '''https://huggingface.co/tiiuae/falcon-40b/resolve/main/config.json''',
    '''tiiuae/falcon-7b''': '''https://huggingface.co/tiiuae/falcon-7b/resolve/main/config.json''',
}

class   _a							(		__lowerCAmelCase				):
     SCREAMING_SNAKE_CASE_				:       List[str]       							=		"""falcon"""
     SCREAMING_SNAKE_CASE_				:       Optional[int]       							=		["""past_key_values"""]






     def __init__(					self   ,_SCREAMING_SNAKE_CASE=65_024   ,_SCREAMING_SNAKE_CASE=4_544   ,_SCREAMING_SNAKE_CASE=32   ,_SCREAMING_SNAKE_CASE=71   ,_SCREAMING_SNAKE_CASE=1e-5   ,_SCREAMING_SNAKE_CASE=0.0_2   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=0.0   ,_SCREAMING_SNAKE_CASE=0.0   ,_SCREAMING_SNAKE_CASE=None   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=True   ,_SCREAMING_SNAKE_CASE=False   ,_SCREAMING_SNAKE_CASE=11   ,_SCREAMING_SNAKE_CASE=11   ,**_SCREAMING_SNAKE_CASE   ,)				->     Dict:
          _snake_case				= vocab_size
          # Backward compatibility with n_embed kwarg
          _snake_case				= kwargs.pop("n_embed"   ,_SCREAMING_SNAKE_CASE       )
          _snake_case				= hidden_size if n_embed is None else n_embed
          _snake_case				= num_hidden_layers
          _snake_case				= num_attention_heads
          _snake_case				= layer_norm_epsilon
          _snake_case				= initializer_range
          _snake_case				= use_cache
          _snake_case				= hidden_dropout
          _snake_case				= attention_dropout

          _snake_case				= bos_token_id
          _snake_case				= eos_token_id
          _snake_case				= num_attention_heads if num_kv_heads is None else num_kv_heads
          _snake_case				= alibi
          _snake_case				= new_decoder_architecture
          _snake_case				= multi_query  # Ignored when new_decoder_architecture is True
          _snake_case				= parallel_attn
          _snake_case				= bias

          super().__init__(bos_token_id=_SCREAMING_SNAKE_CASE   ,eos_token_id=_SCREAMING_SNAKE_CASE   ,**_SCREAMING_SNAKE_CASE       )






     @property
     def 							_lowercase      (					self       )				->     Union[str, Any]:
          return self.hidden_size // self.num_attention_heads






     @property
     def 							_lowercase      (					self       )				->     Optional[int]:
          return not self.alibi


