

import argparse

# utilities to import the model weights and config file
import os
import pickle as pkl

# PyTorch + new model classes
import torch
from torch import nn

from transformers import AutoTokenizer, MegaConfig, MegaForMaskedLM


# import the EncoderLayer class used to pretrain
# !! NOTE !! this requires the version of fairseq that is built when you install the Mega source
try:
  from fairseq.modules.mega_layer import MegaEncoderLayer
except ImportError:
  raise ImportError("""You need to install the version of fairseq from the Mega repo!""")







class     a__    (				nn.Module    ):







  """simple docstring"""




  def __init__(     self      :	Dict			,			UpperCAmelCase__      :	List[Any]			,			UpperCAmelCase__      :	Union[str, Any]			,			UpperCAmelCase__      :	Dict						)   ->Tuple:
        """simple docstring"""







        super().__init__()
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				mega_args
        SCREAMING_SNAKE_CASE	: str					       =				nn.Embedding(UpperCAmelCase__			,			self.mega_args.encoder_embed_dim						)
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				nn.ModuleList([MegaEncoderLayer(self.mega_args						) for _ in range(UpperCAmelCase__						)]						)
        SCREAMING_SNAKE_CASE	: Any					       =				depth




  def 	_lowercase (     self      :	Any			,			UpperCAmelCase__      :	Any			,			UpperCAmelCase__      :	Any			,			UpperCAmelCase__      :	str=True			,			UpperCAmelCase__      :	List[Any]=0						)   ->int:
        """simple docstring"""







        if batch_first:
              SCREAMING_SNAKE_CASE	: int					       =				input_ids.T

        # to make things more confusing, Mega expects the attention mask to
        # be (batch, time), but with values of 0 (normal token) and 1 (ignore token)
        # which is the opposite of what HF returns
        if ignore_mask_value == 0:
              SCREAMING_SNAKE_CASE	: List[str]					       =				1 - attention_mask

        # get token embeddings from IDs
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				self.embedding_layer(UpperCAmelCase__						)

        # pass through the Mega layers
        # input is (time, batch, encoder dim) and output is the same
        for encoder in self.encoders:
              SCREAMING_SNAKE_CASE	: Any					       =				encoder(UpperCAmelCase__			,			UpperCAmelCase__						)

        # return according to the shape specified
        if batch_first:
              # (T, B, H) --> (B, T, H)
              return torch.transpose(UpperCAmelCase__			,			0			,			1						)
        else:
              return embeds







class     a__    (				nn.Module    ):







  """simple docstring"""




  def __init__(     self      :	List[Any]			,			UpperCAmelCase__      :	Any			,			UpperCAmelCase__      :	Union[str, Any]			,			UpperCAmelCase__      :	List[str]						)   ->Optional[Any]:
        """simple docstring"""







        super().__init__()
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				MegaLM(UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: str					       =				nn.Linear(mega_args.encoder_embed_dim			,			UpperCAmelCase__						)
        SCREAMING_SNAKE_CASE	: List[str]					       =				nn.Dropout(p=0.1						)




  def 	_lowercase (     self      :	Any			,			UpperCAmelCase__      :	int			,			UpperCAmelCase__      :	List[str]			,			UpperCAmelCase__      :	str=True			,			UpperCAmelCase__      :	List[Any]=0						)   ->List[str]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: str					       =				self.mega(UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__						)
        return self.mlm_head(self.dropout(UpperCAmelCase__						)						)






def    __lowercase (       _A  ,      _A  ,      _A     )       ->  Dict:
      with open(os.path.join(_A  ,      """model_args.pkl"""     )  ,      """rb"""     ) as f:
            SCREAMING_SNAKE_CASE	: Tuple					       =				pkl.load(_A     )

      # load the original encoder
      SCREAMING_SNAKE_CASE	: List[Any]					       =				OriginalMegaForMaskedLM(**_A     ).eval()

      # load its weights
      print(
          """Original Mega encoder:"""  ,      original_mlm.mega.load_state_dict(
              torch.load(os.path.join(_A  ,      """encoder_weights.pt"""     )  ,      map_location="""cpu"""     )     )  ,      )
      print(
          """Original Mega MLM layer:"""  ,      original_mlm.mlm_head.load_state_dict(
              torch.load(os.path.join(_A  ,      """mlm_head_weights.pt"""     )  ,      map_location="""cpu"""     )     )  ,      )

      # create a new config from the old one
      SCREAMING_SNAKE_CASE	: List[str]					       =				MegaConfig(
          num_hidden_layers=mega_original_args["""depth"""]  ,      vocab_size=mega_original_args["""vocab_size"""]  ,      hidden_size=mega_original_args["""mega_args"""].encoder_embed_dim  ,      shared_representation_size=mega_original_args["""mega_args"""].encoder_z_dim  ,      intermediate_size=mega_original_args["""mega_args"""].encoder_hidden_dim  ,      ema_projection_size=mega_original_args["""mega_args"""].encoder_n_dim  ,      dropout_prob=mega_original_args["""mega_args"""].dropout  ,      attention_probs_dropout_prob=mega_original_args["""mega_args"""].attention_dropout  ,      hidden_dropout_prob=mega_original_args["""mega_args"""].hidden_dropout  ,      activation=mega_original_args["""mega_args"""].activation_fn  ,      attention_activation=mega_original_args["""mega_args"""].attention_activation_fn  ,      bidirectional=mega_original_args["""mega_args"""].bidirectional  ,      use_chunking=mega_original_args["""mega_args"""].encoder_chunk_size > 0  ,      chunk_size=mega_original_args["""mega_args"""].encoder_chunk_size  ,      truncation=mega_original_args["""mega_args"""].truncation_length  ,      normalization_type=mega_original_args["""mega_args"""].normalization_type  ,      normalize_before_mega=_A  ,      norm_affine=_A  ,      use_feature_dropout=mega_original_args["""mega_args"""].feature_dropout  ,      relative_positional_bias=mega_original_args["""mega_args"""].rel_pos_bias  ,      max_positions=mega_original_args["""mega_args"""].max_source_positions  ,      nffn_hidden_size=mega_original_args["""mega_args"""].encoder_ffn_embed_dim  ,      normalize_before_ffn=mega_original_args["""mega_args"""].normalize_before  ,      nffn_activation_dropout_prob=0.0  ,      add_token_type_embeddings=_A  ,      add_lm_hidden_dense_layer=_A  ,      )

      SCREAMING_SNAKE_CASE	: List[Any]					       =				MegaForMaskedLM(_A     ).eval()

      # the originl checkpoint just uses nn.Embedding for the word embeddings
      # we use a wrapper module for embeddings to add support for positional embeddings
      SCREAMING_SNAKE_CASE	: Dict					       =				original_mlm.mega.embedding_layer.weight

      # modify the state dictionary of the original checkpoint to account for naming issues in the Hugging Face
      # ecosystem -- any names containing "beta" or "gamma" aren't safe to use and are renamed upon _load_pretrained,
      # also renaming previously confusing parameter names
      SCREAMING_SNAKE_CASE	: Optional[Any]					       =				original_mlm.mega.encoders.state_dict()
      SCREAMING_SNAKE_CASE	: List[Any]					       =				{}
      for module_name in original_state_dict.keys():
            SCREAMING_SNAKE_CASE	: Optional[int]					       =				None
            # have to handle gamma, beta, and alpha differently due to their use
            # in multiple modules within the original repository;
            # beta is used in EMA, MovingAverageGatedAttention, and RotaryRelativePositionalBias, and must be renamed due to flax/tf weights
            # the EMA sublayer was renamed from "move" to "ema_gate" for readability, so that is also done here
            if "beta" in module_name:
                  # EMA sub-layers were always called "move" in the original repo
                  if "move.beta" in module_name:
                        SCREAMING_SNAKE_CASE	: List[Any]					       =				module_name.replace("""move.beta"""  ,      """ema_gate.ema_expansion_matrix"""     )
                  elif "mega_layer.beta" in module_name:
                        SCREAMING_SNAKE_CASE	: List[Any]					       =				module_name.replace("""beta"""  ,      """qk_bias"""     )
                  else:
                        SCREAMING_SNAKE_CASE	: Any					       =				module_name.replace("""beta"""  ,      """b_param"""     )
        # beta is used in EMA and MovingAverageGatedAttention, and must be renamed due to flax/tf weights
            elif "gamma" in module_name:
                  if "move.gamma" in module_name:
                        SCREAMING_SNAKE_CASE	: Tuple					       =				module_name.replace("""move.gamma"""  ,      """ema_gate.kernel_projection_matrix"""     )
                  elif "mega_layer.gamma" in module_name:
                        SCREAMING_SNAKE_CASE	: List[str]					       =				module_name.replace("""gamma"""  ,      """qk_weight"""     )
                  else:
                        SCREAMING_SNAKE_CASE	: List[str]					       =				module_name.replace("""gamma"""  ,      """g_param"""     )
        # alpha is used in EMA and positional bias; renaming to improve readability
            elif "move.alpha" in module_name:
                  SCREAMING_SNAKE_CASE	: str					       =				module_name.replace("""move.alpha"""  ,      """ema_gate.decay_factor"""     )
            # delta is only used in EMA; renaming to improve readability
            elif "move.delta" in module_name:
                  SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				module_name.replace("""move.delta"""  ,      """ema_gate.damping_factor"""     )
            # omega is only used in EMA; renaming to improve readability
            elif "omega" in module_name:
                  SCREAMING_SNAKE_CASE	: str					       =				module_name.replace("""move.omega"""  ,      """ema_gate.residual_weight"""     )

            if new_module_name:
                  SCREAMING_SNAKE_CASE	: List[str]					       =				new_module_name

      if len(_A     ) != 0:
            print(F"Renaming these keys: {updated_keys.keys()}"     )
      else:
            print("""No need to rename state dict entries"""     )
      for old, new in updated_keys.items():
            SCREAMING_SNAKE_CASE	: Optional[Any]					       =				original_state_dict.pop(_A     )

      # now attempt to load the state dictionary with updated names
      # note that we now call it `mega.layers` instead of `mega.encoders` due to hugging face style
      print("""HF Mega encoder:"""  ,      hf_mlm.mega.layers.load_state_dict(_A     )     )

      # load the MLM head weights directly
      print(
          """HF Mega MLM layer:"""  ,      hf_mlm.mlm_head.load_state_dict(
              torch.load(os.path.join(_A  ,      """mlm_head_weights.pt"""     )  ,      map_location="""cpu"""     )     )  ,      )

      # test on a randomly generated input sequence
      SCREAMING_SNAKE_CASE	: Any					       =				torch.randint(0  ,      hf_config.vocab_size  ,      size=(4, 256)     )
      SCREAMING_SNAKE_CASE	: List[Any]					       =				torch.ones_like(_A     )
      # mask a few tokens to make sure masking is applied appropriately :)
      SCREAMING_SNAKE_CASE	: str					       =				0

      # run forward passes
      SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				original_mlm(_A  ,      _A  ,      batch_first=_A  ,      ignore_mask_value=0     )
      SCREAMING_SNAKE_CASE	: List[Any]					       =				hf_mlm(_A  ,      _A     )[0]

      # print shapes and diff
      print(F"original output {original_output.shape}"     )
      print(F"hf output {hf_output.shape}"     )
      print(F"max diff: {(original_output - hf_output).max()}"     )  # 0.0
      SCREAMING_SNAKE_CASE	: Any					       =				torch.allclose(_A  ,      _A  ,      atol=1e-3     )

      if success:
            print("""Yay!"""     )
            hf_mlm.save_pretrained(_A     )
      else:
            raise RuntimeError(F"Something's broken :(\nOriginal:\n{original_output}\n\nHF\n{hf_output}\n{hf_mlm}"     )

      if includes_tokenizer:
            print("""Transferring tokenizer"""     )
            SCREAMING_SNAKE_CASE	: Dict					       =				AutoTokenizer.from_pretrained(_A     )
            tokenizer.save_pretrained(_A     )


if __name__ == "__main__":
  UpperCAmelCase__	:       List[Any]			      =      argparse.ArgumentParser()

  parser.add_argument(
      """--pretrained_checkpoint_path""",
      default=None,
      type=str,
      required=True,
      help="""Point to the directory containing your model weights using the official Mega repo""",
  )

  parser.add_argument(
      """--output_path""", default=None, type=str, required=True, help="""Location to save the Hugging Face version"""
  )

  parser.add_argument(
      """--includes_tokenizer""",
      action="""store_true""",
      help="""Use this flag if there is a Hugging Face tokenizer in the original checkpoint repo""",
  )

  UpperCAmelCase__	:       str			      =      parser.parse_args()

  convert_checkpoint_to_huggingface(args.pretrained_checkpoint_path, args.output_path, args.includes_tokenizer)


import inspect
import unittest

from transformers import EfficientNetConfig
from transformers.testing_utils import is_pipeline_test, 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 EfficientNetForImageClassification, EfficientNetModel
  from transformers.models.efficientnet.modeling_efficientnet import EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor







class     a__    :







  """simple docstring"""




  def __init__(     self      :	Any			,			UpperCAmelCase__      :	Union[str, Any]			,			UpperCAmelCase__      :	List[str]=1_3			,			UpperCAmelCase__      :	Any=3_2			,			UpperCAmelCase__      :	Union[str, Any]=3			,			UpperCAmelCase__      :	List[str]=[3, 3, 5]			,			UpperCAmelCase__      :	int=[3_2, 1_6, 2_4]			,			UpperCAmelCase__      :	Dict=[1_6, 2_4, 4_0]			,			UpperCAmelCase__      :	Optional[Any]=[1, 1, 2]			,			UpperCAmelCase__      :	List[Any]=[1, 1, 2]			,			UpperCAmelCase__      :	str=[1, 6, 6]			,			UpperCAmelCase__      :	List[Any]=True			,			UpperCAmelCase__      :	int=True			,			UpperCAmelCase__      :	Any=3_7			,			UpperCAmelCase__      :	Any="gelu"			,			UpperCAmelCase__      :	List[Any]=1_0			,			)   ->Dict:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[int]					       =				parent
        SCREAMING_SNAKE_CASE	: Any					       =				batch_size
        SCREAMING_SNAKE_CASE	: List[Any]					       =				image_size
        SCREAMING_SNAKE_CASE	: str					       =				num_channels
        SCREAMING_SNAKE_CASE	: int					       =				kernel_sizes
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				in_channels
        SCREAMING_SNAKE_CASE	: List[str]					       =				out_channels
        SCREAMING_SNAKE_CASE	: int					       =				strides
        SCREAMING_SNAKE_CASE	: List[str]					       =				num_block_repeats
        SCREAMING_SNAKE_CASE	: Tuple					       =				expand_ratios
        SCREAMING_SNAKE_CASE	: Dict					       =				is_training
        SCREAMING_SNAKE_CASE	: Optional[int]					       =				hidden_act
        SCREAMING_SNAKE_CASE	: int					       =				num_labels
        SCREAMING_SNAKE_CASE	: Dict					       =				use_labels


  def 	_lowercase (     self      :	Any						)   ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

        SCREAMING_SNAKE_CASE	: Dict					       =				None
        if self.use_labels:
              SCREAMING_SNAKE_CASE	: Optional[Any]					       =				ids_tensor([self.batch_size]			,			self.num_labels						)

        SCREAMING_SNAKE_CASE	: Dict					       =				self.get_config()
        return config, pixel_values, labels


  def 	_lowercase (     self      :	Tuple						)   ->Optional[int]:
        """simple docstring"""







        return EfficientNetConfig(
            num_channels=self.num_channels			,			kernel_sizes=self.kernel_sizes			,			in_channels=self.in_channels			,			out_channels=self.out_channels			,			strides=self.strides			,			num_block_repeats=self.num_block_repeats			,			expand_ratios=self.expand_ratios			,			hidden_act=self.hidden_act			,			num_labels=self.num_labels			,			)


  def 	_lowercase (     self      :	List[Any]			,			UpperCAmelCase__      :	str			,			UpperCAmelCase__      :	int			,			UpperCAmelCase__      :	Optional[Any]						)   ->Union[str, Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: List[Any]					       =				EfficientNetModel(config=UpperCAmelCase__						)
        model.to(UpperCAmelCase__						)
        model.eval()
        SCREAMING_SNAKE_CASE	: str					       =				model(UpperCAmelCase__						)
        # expected last hidden states: B, C, H // 4, W // 4
        self.parent.assertEqual(
            result.last_hidden_state.shape			,			(self.batch_size, config.hidden_dim, self.image_size // 4, self.image_size // 4)			,			)


  def 	_lowercase (     self      :	Optional[int]			,			UpperCAmelCase__      :	List[Any]			,			UpperCAmelCase__      :	Optional[int]			,			UpperCAmelCase__      :	Union[str, Any]						)   ->int:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				EfficientNetForImageClassification(UpperCAmelCase__						)
        model.to(UpperCAmelCase__						)
        model.eval()
        SCREAMING_SNAKE_CASE	: Any					       =				model(UpperCAmelCase__			,			labels=UpperCAmelCase__						)
        self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)						)




  def 	_lowercase (     self      :	Dict						)   ->str:
        """simple docstring"""







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







@require_torch
class     a__    (				UpperCAmelCase		,     UpperCAmelCase		,     unittest.TestCase    ):







  """simple docstring"""




  UpperCAmelCase__ :	Tuple         =(EfficientNetModel, EfficientNetForImageClassification) if is_torch_available() else ()
  UpperCAmelCase__ :	Optional[int]         =(
      {"""feature-extraction""": EfficientNetModel, """image-classification""": EfficientNetForImageClassification}
      if is_torch_available()
      else {}
  )

  UpperCAmelCase__ :	List[str]         =False
  UpperCAmelCase__ :	Any         =False
  UpperCAmelCase__ :	Union[str, Any]         =False
  UpperCAmelCase__ :	Dict         =False
  UpperCAmelCase__ :	List[str]         =False


  def 	_lowercase (     self      :	Optional[Any]						)   ->Tuple:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				EfficientNetModelTester(self						)
        SCREAMING_SNAKE_CASE	: Any					       =				ConfigTester(
            self			,			config_class=UpperCAmelCase__			,			has_text_modality=UpperCAmelCase__			,			hidden_size=3_7						)


  def 	_lowercase (     self      :	Optional[int]						)   ->List[Any]:
        """simple docstring"""







        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 	_lowercase (     self      :	List[Any]						)   ->Optional[int]:
        """simple docstring"""







        return


  @unittest.skip(reason="""EfficientNet does not use inputs_embeds"""						)
  def 	_lowercase (     self      :	Dict						)   ->Tuple:
        """simple docstring"""







        pass


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







        pass


  @unittest.skip(reason="""EfficientNet does not use feedforward chunking"""						)
  def 	_lowercase (     self      :	Optional[Any]						)   ->Any:
        """simple docstring"""







        pass


  def 	_lowercase (     self      :	str						)   ->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE       ,		SCREAMING_SNAKE_CASE	: Optional[Any]					       =				self.model_tester.prepare_config_and_inputs_for_common()

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

              SCREAMING_SNAKE_CASE	: Dict					       =				["""pixel_values"""]
              self.assertListEqual(arg_names[:1]			,			UpperCAmelCase__						)


  def 	_lowercase (     self      :	Optional[int]						)   ->Optional[Any]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Optional[int]					       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*UpperCAmelCase__						)


  def 	_lowercase (     self      :	List[Any]						)   ->int:
        """simple docstring"""







        def check_hidden_states_output(UpperCAmelCase__      :	Optional[Any]			,			UpperCAmelCase__      :	Any			,			UpperCAmelCase__      :	Dict						):
              SCREAMING_SNAKE_CASE	: Optional[Any]					       =				model_class(UpperCAmelCase__						)
              model.to(UpperCAmelCase__						)
              model.eval()

              with torch.no_grad():
                    SCREAMING_SNAKE_CASE	: Any					       =				model(**self._prepare_for_class(UpperCAmelCase__			,			UpperCAmelCase__						)						)

              SCREAMING_SNAKE_CASE	: Optional[int]					       =				outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states
              SCREAMING_SNAKE_CASE	: Any					       =				sum(config.num_block_repeats						) * 4
              self.assertEqual(len(UpperCAmelCase__						)			,			UpperCAmelCase__						)

              # EfficientNet'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]			,			)

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

        for model_class in self.all_model_classes:
              SCREAMING_SNAKE_CASE	: Optional[Any]					       =				True
              check_hidden_states_output(UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__						)

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

              check_hidden_states_output(UpperCAmelCase__			,			UpperCAmelCase__			,			UpperCAmelCase__						)


  def 	_lowercase (     self      :	Union[str, Any]						)   ->Optional[int]:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Tuple					       =				self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase__						)


  @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""						)
  def 	_lowercase (     self      :	List[str]						)   ->Optional[Any]:
        """simple docstring"""







        pass


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







        for model_name in EFFICIENTNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              SCREAMING_SNAKE_CASE	: str					       =				EfficientNetModel.from_pretrained(UpperCAmelCase__						)
              self.assertIsNotNone(UpperCAmelCase__						)




  @is_pipeline_test
  @require_vision
  @slow
  def 	_lowercase (     self      :	Any						)   ->Any:
        """simple docstring"""







        super().test_pipeline_image_classification()






def    __lowercase (       )       ->  List[Any]:
      SCREAMING_SNAKE_CASE	: List[str]					       =				Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
      return image







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







  """simple docstring"""




  @cached_property
  def 	_lowercase (     self      :	List[str]						)   ->Tuple:
        """simple docstring"""







        return AutoImageProcessor.from_pretrained("""google/efficientnet-b7"""						) if is_vision_available() else None




  @slow
  def 	_lowercase (     self      :	List[Any]						)   ->Any:
        """simple docstring"""







        SCREAMING_SNAKE_CASE	: Tuple					       =				EfficientNetForImageClassification.from_pretrained("""google/efficientnet-b7"""						).to(UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: int					       =				self.default_image_processor
        SCREAMING_SNAKE_CASE	: List[Any]					       =				prepare_img()
        SCREAMING_SNAKE_CASE	: Optional[Any]					       =				image_processor(images=UpperCAmelCase__			,			return_tensors="""pt"""						).to(UpperCAmelCase__						)

        # forward pass
        with torch.no_grad():
              SCREAMING_SNAKE_CASE	: Optional[Any]					       =				model(**UpperCAmelCase__						)

        # verify the logits
        SCREAMING_SNAKE_CASE	: Union[str, Any]					       =				torch.Size((1, 1_0_0_0)						)
        self.assertEqual(outputs.logits.shape			,			UpperCAmelCase__						)

        SCREAMING_SNAKE_CASE	: Any					       =				torch.tensor([0.00_01, 0.00_02, 0.00_02]						).to(UpperCAmelCase__						)
        self.assertTrue(torch.allclose(outputs.logits[0, :3]			,			UpperCAmelCase__			,			atol=1e-4						)						)




