





'''simple docstring'''







import collections
import inspect
import unittest

from transformers import NatConfig
from transformers.testing_utils import require_natten, 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 NatBackbone, NatForImageClassification, NatModel
    from transformers.models.nat.modeling_nat import NAT_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor



class 							__SCREAMING_SNAKE_CASE			:
      '''simple docstring'''



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

      def   _lowerCamelCase		(							self    : Any  )		->  Any:
           _lowercase							:      int  						=							floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

           _lowercase							:      int  						=							None
           if self.use_labels:
                _lowercase							:      Any  						=							ids_tensor([self.batch_size]   ,self.num_labels  )

           _lowercase							:      Any  						=							self.get_config()

           return config, pixel_values, labels

      def   _lowerCamelCase		(							self    : Dict  )		->  List[str]:
           return NatConfig(
               num_labels=self.num_labels   ,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   ,kernel_size=self.kernel_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   ,patch_norm=self.patch_norm   ,layer_norm_eps=self.layer_norm_eps   ,initializer_range=self.initializer_range   ,out_features=self.out_features   ,out_indices=self.out_indices   ,)

      def   _lowerCamelCase		(							self    : Optional[int]   ,UpperCamelCase    : Optional[Any]   ,UpperCamelCase    : int   ,UpperCamelCase    : str  )		->  Optional[int]:
           _lowercase							:      Tuple  						=							NatModel(config=UpperCamelCase  )
           model.to(UpperCamelCase  )
           model.eval()
           _lowercase							:      List[Any]  						=							model(UpperCamelCase  )

           _lowercase							:      Any  						=							(config.image_size // config.patch_size) // (2 ** (len(config.depths  ) - 1))
           _lowercase							:      Tuple  						=							int(config.embed_dim * 2 ** (len(config.depths  ) - 1)  )

           self.parent.assertEqual(
               result.last_hidden_state.shape   ,(self.batch_size, expected_height, expected_width, expected_dim)  )

      def   _lowerCamelCase		(							self    : int   ,UpperCamelCase    : Optional[int]   ,UpperCamelCase    : List[Any]   ,UpperCamelCase    : Optional[int]  )		->  int:
           _lowercase							:      List[str]  						=							NatForImageClassification(UpperCamelCase  )
           model.to(UpperCamelCase  )
           model.eval()
           _lowercase							:      Optional[int]  						=							model(UpperCamelCase   ,labels=UpperCamelCase  )
           self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)  )

           # test greyscale images
           _lowercase							:      int  						=							1
           _lowercase							:      Tuple  						=							NatForImageClassification(UpperCamelCase  )
           model.to(UpperCamelCase  )
           model.eval()

           _lowercase							:      Optional[int]  						=							floats_tensor([self.batch_size, 1, self.image_size, self.image_size]  )
           _lowercase							:      int  						=							model(UpperCamelCase  )
           self.parent.assertEqual(result.logits.shape   ,(self.batch_size, self.num_labels)  )

      def   _lowerCamelCase		(							self    : str   ,UpperCamelCase    : Optional[Any]   ,UpperCamelCase    : Any   ,UpperCamelCase    : Optional[int]  )		->  List[Any]:
           _lowercase							:      int  						=							NatBackbone(config=UpperCamelCase  )
           model.to(UpperCamelCase  )
           model.eval()
           _lowercase							:      List[Any]  						=							model(UpperCamelCase  )

           # 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
           _lowercase							:      Optional[Any]  						=							None
           _lowercase							:      str  						=							NatBackbone(config=UpperCamelCase  )
           model.to(UpperCamelCase  )
           model.eval()
           _lowercase							:      Tuple  						=							model(UpperCamelCase  )

           # 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   _lowerCamelCase		(							self    : int  )		->  int:
           _lowercase							:      int  						=							self.prepare_config_and_inputs()
           _lowercase						, _lowercase						, _lowercase							:      Tuple  						=							config_and_inputs
           _lowercase							:      Optional[Any]  						=							{'pixel_values': pixel_values}
           return config, inputs_dict



@require_natten
@require_torch
class 							__SCREAMING_SNAKE_CASE			(       lowerCAmelCase__							,   lowerCAmelCase__							,   unittest.TestCase      ):
      '''simple docstring'''



      lowerCAmelCase__       :      Optional[Any] 		=		(
          (
              NatModel,
              NatForImageClassification,
              NatBackbone,
          )
          if is_torch_available()
          else ()
      )
      lowerCAmelCase__       :      int 		=		(
          {"feature-extraction": NatModel, "image-classification": NatForImageClassification}
          if is_torch_available()
          else {}
      )
      lowerCAmelCase__       :      Dict 		=		False

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

      def   _lowerCamelCase		(							self    : Optional[int]  )		->  Union[str, Any]:
           _lowercase							:      Any  						=							NatModelTester(self  )
           _lowercase							:      List[Any]  						=							ConfigTester(self   ,config_class=UpperCamelCase   ,embed_dim=37  )

      def   _lowerCamelCase		(							self    : List[Any]  )		->  Tuple:
           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   _lowerCamelCase		(							self    : Optional[int]  )		->  int:
           return

      def   _lowerCamelCase		(							self    : Optional[int]  )		->  Union[str, Any]:
           _lowercase							:      Tuple  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase  )

      def   _lowerCamelCase		(							self    : List[str]  )		->  Optional[Any]:
           _lowercase							:      Optional[Any]  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCamelCase  )

      def   _lowerCamelCase		(							self    : Any  )		->  int:
           _lowercase							:      List[Any]  						=							self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_backbone(*UpperCamelCase  )

      @unittest.skip(reason='Nat does not use inputs_embeds'  )
      def   _lowerCamelCase		(							self    : Tuple  )		->  List[str]:
           pass

      @unittest.skip(reason='Nat does not use feedforward chunking'  )
      def   _lowerCamelCase		(							self    : str  )		->  int:
           pass

      def   _lowerCamelCase		(							self    : Any  )		->  Any:
           _lowercase						, _lowercase							:      Dict  						=							self.model_tester.prepare_config_and_inputs_for_common()

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

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

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

                _lowercase							:      Tuple  						=							['pixel_values']
                self.assertListEqual(arg_names[:1]   ,UpperCamelCase  )

      def   _lowerCamelCase		(							self    : int  )		->  Optional[int]:
           self.skipTest('Nat\'s attention operation is handled entirely by NATTEN.'  )

      def   _lowerCamelCase		(							self    : Dict   ,UpperCamelCase    : Dict   ,UpperCamelCase    : Optional[int]   ,UpperCamelCase    : Tuple   ,UpperCamelCase    : Union[str, Any]  )		->  str:
           _lowercase							:      Optional[Any]  						=							model_class(UpperCamelCase  )
           model.to(UpperCamelCase  )
           model.eval()

           with torch.no_grad():
                _lowercase							:      str  						=							model(**self._prepare_for_class(UpperCamelCase   ,UpperCamelCase  )  )

           _lowercase							:      Optional[Any]  						=							outputs.hidden_states

           _lowercase							:      Any  						=							getattr(
               self.model_tester   ,'expected_num_hidden_layers'   ,len(self.model_tester.depths  ) + 1  )
           self.assertEqual(len(UpperCamelCase  )   ,UpperCamelCase  )

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

           _lowercase							:      str  						=							image_size[0] // patch_size[0]
           _lowercase							:      int  						=							image_size[1] // patch_size[1]

           self.assertListEqual(
               list(hidden_states[0].shape[-3:]  )   ,[height, width, self.model_tester.embed_dim]   ,)

           if model_class.__name__ != "NatBackbone":
                _lowercase							:      List[Any]  						=							outputs.reshaped_hidden_states
                self.assertEqual(len(UpperCamelCase  )   ,UpperCamelCase  )

                _lowercase						, _lowercase						, _lowercase						, _lowercase							:      int  						=							reshaped_hidden_states[0].shape
                _lowercase							:      Optional[Any]  						=							(
                    reshaped_hidden_states[0].view(UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase  ).permute(0   ,2   ,3   ,1  )
                )
                self.assertListEqual(
                    list(reshaped_hidden_states.shape[-3:]  )   ,[height, width, self.model_tester.embed_dim]   ,)

      def   _lowerCamelCase		(							self    : Any  )		->  List[Any]:
           _lowercase						, _lowercase							:      Tuple  						=							self.model_tester.prepare_config_and_inputs_for_common()

           _lowercase							:      int  						=							(
               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:
                _lowercase							:      Any  						=							True
                self.check_hidden_states_output(UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase  )

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

                self.check_hidden_states_output(UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase   ,UpperCamelCase  )

      @slow
      def   _lowerCamelCase		(							self    : Any  )		->  str:
           for model_name in NAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                _lowercase							:      Optional[int]  						=							NatModel.from_pretrained(UpperCamelCase  )
                self.assertIsNotNone(UpperCamelCase  )



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

           _lowercase							:      Dict  						=							_config_zero_init(UpperCamelCase  )
           for model_class in self.all_model_classes:
                _lowercase							:      List[str]  						=							model_class(config=UpperCamelCase  )
                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_natten
@require_vision
@require_torch
class 							__SCREAMING_SNAKE_CASE			(       unittest.TestCase      ):
      '''simple docstring'''



      @cached_property
      def   _lowerCamelCase		(							self    : Tuple  )		->  int:
           return AutoImageProcessor.from_pretrained('shi-labs/nat-mini-in1k-224'  ) if is_vision_available() else None



      @slow
      def   _lowerCamelCase		(							self    : Optional[Any]  )		->  Tuple:
           _lowercase							:      List[str]  						=							NatForImageClassification.from_pretrained('shi-labs/nat-mini-in1k-224'  ).to(UpperCamelCase  )
           _lowercase							:      Dict  						=							self.default_image_processor

           _lowercase							:      Optional[Any]  						=							Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
           _lowercase							:      Any  						=							image_processor(images=UpperCamelCase   ,return_tensors='pt'  ).to(UpperCamelCase  )

           # forward pass
           with torch.no_grad():
                _lowercase							:      Tuple  						=							model(**UpperCamelCase  )

           # verify the logits
           _lowercase							:      Optional[int]  						=							torch.Size((1, 1000)  )
           self.assertEqual(outputs.logits.shape   ,UpperCamelCase  )
           _lowercase							:      List[Any]  						=							torch.tensor([0.3_8_0_5, -0.8_6_7_6, -0.3_9_1_2]  ).to(UpperCamelCase  )
           self.assertTrue(torch.allclose(outputs.logits[0, :3]   ,UpperCamelCase   ,atol=1e-4  )  )



@require_torch
@require_natten
class 							__SCREAMING_SNAKE_CASE			(       unittest.TestCase							,   lowerCAmelCase__      ):
      '''simple docstring'''



      lowerCAmelCase__       :      Union[str, Any] 		=		(NatBackbone,) if is_torch_available() else ()
      lowerCAmelCase__       :      Union[str, Any] 		=		NatConfig

      def   _lowerCamelCase		(							self    : Tuple  )		->  Dict:
           _lowercase							:      Optional[Any]  						=							NatModelTester(self  )






'''simple docstring'''







from __future__ import annotations

import math
import random

def 		SCREAMING_SNAKE_CASE      (					lowerCAmelCase__     :	int       ,  lowerCAmelCase__     :	int       ,  lowerCAmelCase__     :	int)			->						list[int]:

     '''simple docstring'''




     _lowercase							:      Optional[Any]  						=							d * e - 1
     _lowercase							:      Optional[Any]  						=							0
     _lowercase							:      Dict  						=							0
     while p == 0:
          _lowercase							:      Optional[Any]  						=							random.randint(2       ,  n - 1)
          _lowercase							:      int  						=							k
          while True:
               if t % 2 == 0:
                    _lowercase							:      int  						=							t // 2
                    _lowercase							:      Any  						=							(g**t) % n
                    _lowercase							:      Tuple  						=							math.gcd(x - 1       ,  lowerCAmelCase__)
                    if x > 1 and y > 1:
                         _lowercase							:      Union[str, Any]  						=							y
                         _lowercase							:      Dict  						=							n // y
                         break  # find the correct factors
               else:
                    break  # t is not divisible by 2, break and choose another g
     return sorted([p, q])


if __name__ == "__main__":
    import doctest

    doctest.testmod()