



"""simple docstring"""







import inspect
import unittest

from transformers import RegNetConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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 RegNetForImageClassification, RegNetModel
     from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     from PIL import Image

     from transformers import AutoImageProcessor







class 				UpperCamelCase__       :




      """simple docstring"""




      def __init__(				self    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Tuple   ,      SCREAMING_SNAKE_CASE_    :				Dict=3   ,      SCREAMING_SNAKE_CASE_    :				str=3_2   ,      SCREAMING_SNAKE_CASE_    :				List[str]=3   ,      SCREAMING_SNAKE_CASE_    :				Dict=1_0   ,      SCREAMING_SNAKE_CASE_    :				Any=[1_0, 2_0, 3_0, 4_0]   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=[1, 1, 2, 1]   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=True   ,      SCREAMING_SNAKE_CASE_    :				str=True   ,      SCREAMING_SNAKE_CASE_    :				Tuple="relu"   ,      SCREAMING_SNAKE_CASE_    :				List[str]=3   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=None   ,      ):
        lowerCAmelCase_       :    Union[str, Any]								=			parent
        lowerCAmelCase_       :    int								=			batch_size
        lowerCAmelCase_       :    List[str]								=			image_size
        lowerCAmelCase_       :    int								=			num_channels
        lowerCAmelCase_       :    Dict								=			embeddings_size
        lowerCAmelCase_       :    Optional[Any]								=			hidden_sizes
        lowerCAmelCase_       :    List[str]								=			depths
        lowerCAmelCase_       :    Any								=			is_training
        lowerCAmelCase_       :    Tuple								=			use_labels
        lowerCAmelCase_       :    Optional[int]								=			hidden_act
        lowerCAmelCase_       :    Tuple								=			num_labels
        lowerCAmelCase_       :    Union[str, Any]								=			scope
        lowerCAmelCase_       :    Optional[Any]								=			len(__snake_case	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				int	):
        lowerCAmelCase_       :    Optional[int]								=			floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

        lowerCAmelCase_       :    Any								=			self.get_config()

        return config, pixel_values, labels



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        return RegNetConfig(
            num_channels=self.num_channels   ,      embeddings_size=self.embeddings_size   ,      hidden_sizes=self.hidden_sizes   ,      depths=self.depths   ,      hidden_act=self.hidden_act   ,      num_labels=self.num_labels   ,      )



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]   ,      SCREAMING_SNAKE_CASE_    :				str   ,      SCREAMING_SNAKE_CASE_    :				List[Any]   ,      SCREAMING_SNAKE_CASE_    :				Dict	):
        lowerCAmelCase_       :    str								=			RegNetModel(config=__snake_case	)
        model.to(__snake_case	)
        model.eval()
        lowerCAmelCase_       :    List[Any]								=			model(__snake_case	)
        # expected last hidden states: B, C, H // 32, W // 32
        self.parent.assertEqual(
            result.last_hidden_state.shape   ,      (self.batch_size, self.hidden_sizes[-1], self.image_size // 3_2, self.image_size // 3_2)   ,      )



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]	):
        lowerCAmelCase_       :    Optional[int]								=			self.num_labels
        lowerCAmelCase_       :    List[str]								=			RegNetForImageClassification(__snake_case	)
        model.to(__snake_case	)
        model.eval()
        lowerCAmelCase_       :    List[str]								=			model(__snake_case   ,      labels=__snake_case	)
        self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.num_labels)	)





      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        lowerCAmelCase_       :    Optional[Any]								=			self.prepare_config_and_inputs()
        lowerCAmelCase_						,lowerCAmelCase_						,lowerCAmelCase_       :    Dict								=			config_and_inputs
        lowerCAmelCase_       :    Optional[Any]								=			{'pixel_values': pixel_values}
        return config, inputs_dict







@require_torch
class 				UpperCamelCase__       (     A__, A__, unittest.TestCase  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					(RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
      _SCREAMING_SNAKE_CASE      =					(
          {"""feature-extraction""": RegNetModel, """image-classification""": RegNetForImageClassification}
          if is_torch_available()
          else {}
      )

      _SCREAMING_SNAKE_CASE      =					False
      _SCREAMING_SNAKE_CASE      =					False
      _SCREAMING_SNAKE_CASE      =					False
      _SCREAMING_SNAKE_CASE      =					False



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        lowerCAmelCase_       :    Optional[Any]								=			RegNetModelTester(self	)
        lowerCAmelCase_       :    Optional[int]								=			ConfigTester(self   ,      config_class=__snake_case   ,      has_text_modality=__snake_case	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        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 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        return



      @unittest.skip(reason='RegNet does not use inputs_embeds'	)
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Dict	):
        pass



      @unittest.skip(reason='RegNet does not support input and output embeddings'	)
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        pass



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Union[str, Any]	):
        lowerCAmelCase_						,lowerCAmelCase_       :    Any								=			self.model_tester.prepare_config_and_inputs_for_common()

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

          lowerCAmelCase_       :    str								=			['pixel_values']
          self.assertListEqual(arg_names[:1]   ,      __snake_case	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        lowerCAmelCase_       :    Tuple								=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*__snake_case	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				Any	):
        lowerCAmelCase_						,lowerCAmelCase_       :    Optional[int]								=			self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
          lowerCAmelCase_       :    List[Any]								=			model_class(config=__snake_case	)
          for name, module in model.named_modules():
            if isinstance(__snake_case   ,      (nn.BatchNormad, nn.GroupNorm)	):
              self.assertTrue(
                  torch.all(module.weight == 1	)   ,      msg=F"Parameter {name} of model {model_class} seems not properly initialized"   ,      )
              self.assertTrue(
                  torch.all(module.bias == 0	)   ,      msg=F"Parameter {name} of model {model_class} seems not properly initialized"   ,      )



      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        def check_hidden_states_output(SCREAMING_SNAKE_CASE_    :				int   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				str	):
          lowerCAmelCase_       :    Optional[int]								=			model_class(__snake_case	)
          model.to(__snake_case	)
          model.eval()

          with torch.no_grad():
            lowerCAmelCase_       :    Optional[Any]								=			model(**self._prepare_for_class(__snake_case   ,      __snake_case	)	)

          lowerCAmelCase_       :    Any								=			outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

          lowerCAmelCase_       :    Dict								=			self.model_tester.num_stages
          self.assertEqual(len(__snake_case	)   ,      expected_num_stages + 1	)

          # RegNet's feature maps are of shape (batch_size, num_channels, height, width)
          self.assertListEqual(
              list(hidden_states[0].shape[-2:]	)   ,      [self.model_tester.image_size // 2, self.model_tester.image_size // 2]   ,      )

        lowerCAmelCase_						,lowerCAmelCase_       :    Any								=			self.model_tester.prepare_config_and_inputs_for_common()
        lowerCAmelCase_       :    Optional[Any]								=			['basic', 'bottleneck']
        for model_class in self.all_model_classes:
          for layer_type in layers_type:
            lowerCAmelCase_       :    Tuple								=			layer_type
            lowerCAmelCase_       :    Tuple								=			True
            check_hidden_states_output(__snake_case   ,      __snake_case   ,      __snake_case	)

            # check that output_hidden_states also work using config
            del inputs_dict["output_hidden_states"]
            lowerCAmelCase_       :    Union[str, Any]								=			True

            check_hidden_states_output(__snake_case   ,      __snake_case   ,      __snake_case	)



      def 					SCREAMING_SNAKE_CASE__			(				self    :				str	):
        lowerCAmelCase_       :    Optional[Any]								=			self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*__snake_case	)





      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Tuple	):
        for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowerCAmelCase_       :    Optional[int]								=			RegNetModel.from_pretrained(__snake_case	)
          self.assertIsNotNone(__snake_case	)




def     UpperCamelCase_   (   )						->       Any:

  """simple docstring"""




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







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




      """simple docstring"""




      @cached_property
      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        return (
            AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
            if is_vision_available()
            else None
        )





      @slow
      def 					SCREAMING_SNAKE_CASE__			(				self    :				Optional[Any]	):
        lowerCAmelCase_       :    Optional[Any]								=			RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	).to(__snake_case	)

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

        # forward pass
        with torch.no_grad():
          lowerCAmelCase_       :    int								=			model(**__snake_case	)

        # verify the logits
        lowerCAmelCase_       :    Any								=			torch.Size((1, 1_0_0_0)	)
        self.assertEqual(outputs.logits.shape   ,      __snake_case	)

        lowerCAmelCase_       :    Optional[Any]								=			torch.tensor([-0.41_80, -1.50_51, -3.48_36]	).to(__snake_case	)

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










"""simple docstring"""







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


lowercase__		:    List[Any]				   =     logging.get_logger(__name__)

lowercase__		:    List[str]				   =     {
    """s-JoL/Open-Llama-V1""": """https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json""",
}







class 				UpperCamelCase__       (     lowercase_  ):




      """simple docstring"""




      _SCREAMING_SNAKE_CASE      =					"""open-llama"""



      def __init__(				self    :				Optional[int]   ,      SCREAMING_SNAKE_CASE_    :				Tuple=1_0_0_0_0_0   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=4_0_9_6   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=1_1_0_0_8   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=3_2   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=3_2   ,      SCREAMING_SNAKE_CASE_    :				str="silu"   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=2_0_4_8   ,      SCREAMING_SNAKE_CASE_    :				Union[str, Any]=0.02   ,      SCREAMING_SNAKE_CASE_    :				int=1E-6   ,      SCREAMING_SNAKE_CASE_    :				List[str]=True   ,      SCREAMING_SNAKE_CASE_    :				Dict=0   ,      SCREAMING_SNAKE_CASE_    :				Dict=1   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=2   ,      SCREAMING_SNAKE_CASE_    :				str=False   ,      SCREAMING_SNAKE_CASE_    :				Optional[int]=True   ,      SCREAMING_SNAKE_CASE_    :				List[Any]=0.1   ,      SCREAMING_SNAKE_CASE_    :				Optional[Any]=0.1   ,      SCREAMING_SNAKE_CASE_    :				Dict=True   ,      SCREAMING_SNAKE_CASE_    :				Any=True   ,      SCREAMING_SNAKE_CASE_    :				Tuple=None   ,      **SCREAMING_SNAKE_CASE_    :				Optional[int]   ,      ):
        lowerCAmelCase_       :    Union[str, Any]								=			vocab_size
        lowerCAmelCase_       :    int								=			max_position_embeddings
        lowerCAmelCase_       :    Union[str, Any]								=			hidden_size
        lowerCAmelCase_       :    Optional[Any]								=			intermediate_size
        lowerCAmelCase_       :    List[Any]								=			num_hidden_layers
        lowerCAmelCase_       :    Optional[int]								=			num_attention_heads
        lowerCAmelCase_       :    List[Any]								=			hidden_act
        lowerCAmelCase_       :    List[Any]								=			initializer_range
        lowerCAmelCase_       :    List[str]								=			rms_norm_eps
        lowerCAmelCase_       :    List[Any]								=			use_cache
        lowerCAmelCase_       :    Optional[int]								=			kwargs.pop(
            'use_memorry_efficient_attention'   ,      SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    List[Any]								=			hidden_dropout_prob
        lowerCAmelCase_       :    List[str]								=			attention_dropout_prob
        lowerCAmelCase_       :    Tuple								=			use_stable_embedding
        lowerCAmelCase_       :    Optional[Any]								=			shared_input_output_embedding
        lowerCAmelCase_       :    Optional[Any]								=			rope_scaling
        self._rope_scaling_validation()

        super().__init__(
            pad_token_id=SCREAMING_SNAKE_CASE_   ,      bos_token_id=SCREAMING_SNAKE_CASE_   ,      eos_token_id=SCREAMING_SNAKE_CASE_   ,      tie_word_embeddings=SCREAMING_SNAKE_CASE_   ,      **SCREAMING_SNAKE_CASE_   ,      )





      def 					SCREAMING_SNAKE_CASE__			(				self    :				List[str]	):
        if self.rope_scaling is None:
          return

        if not isinstance(self.rope_scaling   ,      SCREAMING_SNAKE_CASE_	) or len(self.rope_scaling	) != 2:
          raise ValueError(
              '`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '
              F"got {self.rope_scaling}"	)
        lowerCAmelCase_       :    int								=			self.rope_scaling.get('type'   ,      SCREAMING_SNAKE_CASE_	)
        lowerCAmelCase_       :    Union[str, Any]								=			self.rope_scaling.get('factor'   ,      SCREAMING_SNAKE_CASE_	)
        if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
          raise ValueError(
              F"`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"	)
        if rope_scaling_factor is None or not isinstance(SCREAMING_SNAKE_CASE_   ,      SCREAMING_SNAKE_CASE_	) or rope_scaling_factor <= 1.0:
          raise ValueError(F"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"	)





