





import inspect
import unittest

from transformers import MobileNetVaConfig
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 transformers import MobileNetVaForImageClassification, MobileNetVaModel
     from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import MobileNetVaImageProcessor

class      A       (	__lowercase						):



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.config_class(**self.inputs_dict       )
         self.parent.assertTrue(hasattr(_lowerCAmelCase		,  "tf_padding"       )       )
         self.parent.assertTrue(hasattr(_lowerCAmelCase		,  "depth_multiplier"       )       )

class      A       :



      def __init__(	self:						str		,  _lowerCAmelCase:						str		,  _lowerCAmelCase:						List[Any]=13		,  _lowerCAmelCase:						Optional[int]=3		,  _lowerCAmelCase:						Any=32		,  _lowerCAmelCase:						Union[str, Any]=0.25		,  _lowerCAmelCase:						Optional[Any]=8		,  _lowerCAmelCase:						Optional[Any]=True		,  _lowerCAmelCase:						int=1024		,  _lowerCAmelCase:						Optional[Any]=32		,  _lowerCAmelCase:						Dict="relu6"		,  _lowerCAmelCase:						List[str]=0.1		,  _lowerCAmelCase:						str=0.02		,  _lowerCAmelCase:						str=True		,  _lowerCAmelCase:						Optional[int]=True		,  _lowerCAmelCase:						Tuple=10		,  _lowerCAmelCase:						int=None		,  )						->    Tuple:
         '''simple docstring'''

         UpperCAmelCase_											=parent
         UpperCAmelCase_											=batch_size
         UpperCAmelCase_											=num_channels
         UpperCAmelCase_											=image_size
         UpperCAmelCase_											=depth_multiplier
         UpperCAmelCase_											=min_depth
         UpperCAmelCase_											=tf_padding
         UpperCAmelCase_											=int(last_hidden_size * depth_multiplier       )
         UpperCAmelCase_											=output_stride
         UpperCAmelCase_											=hidden_act
         UpperCAmelCase_											=classifier_dropout_prob
         UpperCAmelCase_											=use_labels
         UpperCAmelCase_											=is_training
         UpperCAmelCase_											=num_labels
         UpperCAmelCase_											=initializer_range
         UpperCAmelCase_											=scope



      def 	lowerCAmelCase__    (	self:						int       )						->    Any:
         '''simple docstring'''

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

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

         UpperCAmelCase_											=self.get_config()

         return config, pixel_values, labels, pixel_labels



      def 	lowerCAmelCase__    (	self:						int       )						->    Dict:
         '''simple docstring'''

         return MobileNetVaConfig(
             num_channels=self.num_channels		,  image_size=self.image_size		,  depth_multiplier=self.depth_multiplier		,  min_depth=self.min_depth		,  tf_padding=self.tf_padding		,  hidden_act=self.hidden_act		,  classifier_dropout_prob=self.classifier_dropout_prob		,  initializer_range=self.initializer_range		,  )



      def 	lowerCAmelCase__    (	self:						Optional[Any]		,  _lowerCAmelCase:						int		,  _lowerCAmelCase:						Union[str, Any]		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						int       )						->    Optional[int]:
         '''simple docstring'''

         UpperCAmelCase_											=MobileNetVaModel(config=_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=model(_lowerCAmelCase       )
         self.parent.assertEqual(
             result.last_hidden_state.shape		,  (
                 self.batch_size,
                 self.last_hidden_size,
                 self.image_size // self.output_stride,
                 self.image_size // self.output_stride,
             )		,  )



      def 	lowerCAmelCase__    (	self:						Union[str, Any]		,  _lowerCAmelCase:						Dict		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Optional[Any]		,  _lowerCAmelCase:						Any       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.num_labels
         UpperCAmelCase_											=MobileNetVaForImageClassification(_lowerCAmelCase       )
         model.to(_lowerCAmelCase       )
         model.eval()
         UpperCAmelCase_											=model(_lowerCAmelCase		,  labels=_lowerCAmelCase       )
         self.parent.assertEqual(result.logits.shape		,  (self.batch_size, self.num_labels)       )






      def 	lowerCAmelCase__    (	self:						str       )						->    Dict:
         '''simple docstring'''

         UpperCAmelCase_											=self.prepare_config_and_inputs()
         UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_						,       UpperCAmelCase_											=config_and_inputs
         UpperCAmelCase_											={"pixel_values": pixel_values}
         return config, inputs_dict

@require_torch
class      A       (	__lowercase		,      __lowercase		,      unittest.TestCase						):
      _snake_case							     =(MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
      _snake_case							     =(
          {'''feature-extraction''': MobileNetVaModel, '''image-classification''': MobileNetVaForImageClassification}
          if is_torch_available()
          else {}
      )

      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False
      _snake_case							     =False



      def 	lowerCAmelCase__    (	self:						Dict       )						->    Any:
         '''simple docstring'''

         UpperCAmelCase_											=MobileNetVaModelTester(self       )
         UpperCAmelCase_											=MobileNetVaConfigTester(self		,  config_class=_lowerCAmelCase		,  has_text_modality=_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						List[str]       )						->    int:
         '''simple docstring'''

         self.config_tester.run_common_tests()



      @unittest.skip(reason="MobileNetV1 does not use inputs_embeds"       )
      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    int:
         '''simple docstring'''

         pass



      @unittest.skip(reason="MobileNetV1 does not support input and output embeddings"       )
      def 	lowerCAmelCase__    (	self:						int       )						->    Dict:
         '''simple docstring'''

         pass



      @unittest.skip(reason="MobileNetV1 does not output attentions"       )
      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    Union[str, Any]:
         '''simple docstring'''

         pass



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()

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

            UpperCAmelCase_											=["pixel_values"]
            self.assertListEqual(arg_names[:1]		,  _lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    List[str]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*_lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Union[str, Any]       )						->    Union[str, Any]:
         '''simple docstring'''

         def check_hidden_states_output(_lowerCAmelCase:						int		,  _lowerCAmelCase:						List[Any]		,  _lowerCAmelCase:						Tuple       ):
            UpperCAmelCase_											=model_class(_lowerCAmelCase       )
            model.to(_lowerCAmelCase       )
            model.eval()

            with torch.no_grad():
               UpperCAmelCase_											=model(**self._prepare_for_class(_lowerCAmelCase		,  _lowerCAmelCase       )       )

            UpperCAmelCase_											=outputs.hidden_states

            UpperCAmelCase_											=26
            self.assertEqual(len(_lowerCAmelCase       )		,  _lowerCAmelCase       )

         UpperCAmelCase_						,       UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
            UpperCAmelCase_											=True
            check_hidden_states_output(_lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase       )

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

            check_hidden_states_output(_lowerCAmelCase		,  _lowerCAmelCase		,  _lowerCAmelCase       )



      def 	lowerCAmelCase__    (	self:						Optional[Any]       )						->    Optional[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*_lowerCAmelCase       )






      @slow
      def 	lowerCAmelCase__    (	self:						str       )						->    Tuple:
         '''simple docstring'''

         for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            UpperCAmelCase_											=MobileNetVaModel.from_pretrained(_lowerCAmelCase       )
            self.assertIsNotNone(_lowerCAmelCase       )




def     a__ (					):






   '''simple docstring'''






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





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



      @cached_property
      def 	lowerCAmelCase__    (	self:						Optional[int]       )						->    List[Any]:
         '''simple docstring'''

         return (
             MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224"       ) if is_vision_available() else None
         )






      @slow
      def 	lowerCAmelCase__    (	self:						Dict       )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224"       ).to(_lowerCAmelCase       )

         UpperCAmelCase_											=self.default_image_processor
         UpperCAmelCase_											=prepare_img()
         UpperCAmelCase_											=image_processor(images=_lowerCAmelCase		,  return_tensors="pt"       ).to(_lowerCAmelCase       )

         # forward pass
         with torch.no_grad():
            UpperCAmelCase_											=model(**_lowerCAmelCase       )

         # verify the logits
         UpperCAmelCase_											=torch.Size((1, 1001)       )
         self.assertEqual(outputs.logits.shape		,  _lowerCAmelCase       )

         UpperCAmelCase_											=torch.tensor([-4.17_39, -1.12_33, 3.12_05]       ).to(_lowerCAmelCase       )

         self.assertTrue(torch.allclose(outputs.logits[0, :3]		,  _lowerCAmelCase		,  atol=1e-4       )       )











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


__lowercase  :   Union[str, Any]						      =logging.get_logger(__name__)

__lowercase  :   List[Any]						      ={
    """tanreinama/GPTSAN-2.8B-spout_is_uniform""": (
        """https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json"""
    ),
}

class      A       (	__lowercase						):
      _snake_case							     ='''gptsan-japanese'''
      _snake_case							     =[
          '''past_key_values''',
      ]
      _snake_case							     ={
          '''hidden_size''': '''d_model''',
          '''num_attention_heads''': '''num_heads''',
          '''num_hidden_layers''': '''num_layers''',
      }



      def __init__(	self:						Optional[Any]		,  _lowerCAmelCase:						List[Any]=3_6000		,  _lowerCAmelCase:						List[Any]=1280		,  _lowerCAmelCase:						str=1024		,  _lowerCAmelCase:						Any=8192		,  _lowerCAmelCase:						str=4096		,  _lowerCAmelCase:						int=128		,  _lowerCAmelCase:						int=10		,  _lowerCAmelCase:						Dict=0		,  _lowerCAmelCase:						Any=16		,  _lowerCAmelCase:						Optional[int]=16		,  _lowerCAmelCase:						List[Any]=128		,  _lowerCAmelCase:						Tuple=0.0		,  _lowerCAmelCase:						Optional[Any]=1e-5		,  _lowerCAmelCase:						int=False		,  _lowerCAmelCase:						Optional[Any]=0.0		,  _lowerCAmelCase:						str="float32"		,  _lowerCAmelCase:						Dict=False		,  _lowerCAmelCase:						Any=False		,  _lowerCAmelCase:						int=False		,  _lowerCAmelCase:						Union[str, Any]=0.0_02		,  _lowerCAmelCase:						Optional[int]=False		,  _lowerCAmelCase:						int=True		,  _lowerCAmelCase:						List[str]=3_5998		,  _lowerCAmelCase:						Optional[int]=3_5995		,  _lowerCAmelCase:						Dict=3_5999		,  **_lowerCAmelCase:						Optional[Any]		,  )						->    List[Any]:
         '''simple docstring'''

         UpperCAmelCase_											=vocab_size
         UpperCAmelCase_											=max_position_embeddings
         UpperCAmelCase_											=d_model
         UpperCAmelCase_											=d_ff
         UpperCAmelCase_											=d_ext
         UpperCAmelCase_											=d_spout
         UpperCAmelCase_											=num_switch_layers
         UpperCAmelCase_											=num_ext_layers
         UpperCAmelCase_											=num_switch_layers + num_ext_layers
         UpperCAmelCase_											=num_heads
         UpperCAmelCase_											=num_experts
         UpperCAmelCase_											=expert_capacity
         UpperCAmelCase_											=dropout_rate
         UpperCAmelCase_											=layer_norm_epsilon
         UpperCAmelCase_											=router_bias
         UpperCAmelCase_											=router_jitter_noise
         UpperCAmelCase_											=router_dtype
         UpperCAmelCase_											=router_ignore_padding_tokens
         UpperCAmelCase_											=output_hidden_states
         UpperCAmelCase_											=output_attentions
         UpperCAmelCase_											=initializer_factor
         UpperCAmelCase_											=output_router_logits
         UpperCAmelCase_											=use_cache

         super().__init__(
             separator_token_id=_lowerCAmelCase		,  pad_token_id=_lowerCAmelCase		,  eos_token_id=_lowerCAmelCase		,  **_lowerCAmelCase		,  )




