


from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import ResNetConfig
from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import cached_property, is_tf_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
       import tensorflow as tf

       from transformers import TFResNetForImageClassification, TFResNetModel
       from transformers.models.resnet.modeling_tf_resnet import TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor

class 					__magic_name__		:


     def __init__(					self							:   Optional[Any]							,		lowerCamelCase__							:   Dict							,		lowerCamelCase__							:   int=3							,		lowerCamelCase__							:   Any=32							,		lowerCamelCase__							:   Optional[Any]=3							,		lowerCamelCase__							:   Dict=10							,		lowerCamelCase__							:   Tuple=[10, 20, 30, 40]							,		lowerCamelCase__							:   int=[1, 1, 2, 1]							,		lowerCamelCase__							:   str=True							,		lowerCamelCase__							:   Union[str, Any]=True							,		lowerCamelCase__							:   int="relu"							,		lowerCamelCase__							:   Union[str, Any]=3							,		lowerCamelCase__							:   List[str]=None							,		)     ->					Optional[int]:





      '''simple docstring'''






      UpperCamelCase__    :			List[Any]      				=      parent
      UpperCamelCase__    :			List[Any]      				=      batch_size
      UpperCamelCase__    :			int      				=      image_size
      UpperCamelCase__    :			List[str]      				=      num_channels
      UpperCamelCase__    :			Optional[Any]      				=      embeddings_size
      UpperCamelCase__    :			str      				=      hidden_sizes
      UpperCamelCase__    :			str      				=      depths
      UpperCamelCase__    :			Optional[int]      				=      is_training
      UpperCamelCase__    :			List[Any]      				=      use_labels
      UpperCamelCase__    :			str      				=      hidden_act
      UpperCamelCase__    :			str      				=      num_labels
      UpperCamelCase__    :			Union[str, Any]      				=      scope
      UpperCamelCase__    :			List[Any]      				=      len(__lowerCAmelCase	)


     def    UpperCAmelCase__		(					self							:   str	)     ->					Optional[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			int      				=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

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

      UpperCamelCase__    :			Dict      				=      self.get_config()

      return config, pixel_values, labels


     def    UpperCAmelCase__		(					self							:   List[str]	)     ->					List[Any]:





      '''simple docstring'''






      return ResNetConfig(
          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							,		image_size=self.image_size							,		)


     def    UpperCAmelCase__		(					self							:   Union[str, Any]							,		lowerCamelCase__							:   List[str]							,		lowerCamelCase__							:   Tuple							,		lowerCamelCase__							:   List[Any]	)     ->					str:





      '''simple docstring'''






      UpperCamelCase__    :			List[str]      				=      TFResNetModel(config=__lowerCAmelCase	)
      UpperCamelCase__    :			Optional[Any]      				=      model(__lowerCAmelCase	)
      # 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 // 32, self.image_size // 32)							,		)


     def    UpperCAmelCase__		(					self							:   str							,		lowerCamelCase__							:   Tuple							,		lowerCamelCase__							:   Optional[Any]							,		lowerCamelCase__							:   List[Any]	)     ->					List[str]:





      '''simple docstring'''






      UpperCamelCase__    :			int      				=      self.num_labels
      UpperCamelCase__    :			Tuple      				=      TFResNetForImageClassification(__lowerCAmelCase	)
      UpperCamelCase__    :			Tuple      				=      model(__lowerCAmelCase							,		labels=__lowerCAmelCase	)
      self.parent.assertEqual(result.logits.shape							,		(self.batch_size, self.num_labels)	)


     def    UpperCAmelCase__		(					self							:   Dict	)     ->					List[str]:





      '''simple docstring'''






      UpperCamelCase__    :			Tuple      				=      self.prepare_config_and_inputs()
      UpperCamelCase__    :			Optional[Any]      				=      config_and_inputs
      UpperCamelCase__    :			List[Any]      				=      {'''pixel_values''': pixel_values}
      return config, inputs_dict



@require_tf
class 					__magic_name__		(   a__    , a__    , unittest.TestCase):
     A:						Dict								=					(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
     A:						Dict								=					(
         {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
         if is_tf_available()
         else {}
     )

     A:						str								=					False
     A:						str								=					False
     A:						Union[str, Any]								=					False
     A:						Optional[int]								=					False
     A:						Any								=					False


     def    UpperCAmelCase__		(					self							:   int	)     ->					Dict:





      '''simple docstring'''






      UpperCamelCase__    :			Any      				=      TFResNetModelTester(self	)
      UpperCamelCase__    :			Dict      				=      ConfigTester(self							,		config_class=__lowerCAmelCase							,		has_text_modality=__lowerCAmelCase	)


     def    UpperCAmelCase__		(					self							:   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    UpperCAmelCase__		(					self							:   Dict	)     ->					Any:





      '''simple docstring'''






      return


     @unittest.skip(reason='''ResNet does not use inputs_embeds'''	)
     def    UpperCAmelCase__		(					self							:   str	)     ->					Union[str, Any]:





      '''simple docstring'''






      pass


     @unittest.skip(reason='''ResNet does not support input and output embeddings'''	)
     def    UpperCAmelCase__		(					self							:   Union[str, Any]	)     ->					Tuple:





      '''simple docstring'''






      pass


     def    UpperCAmelCase__		(					self							:   Tuple	)     ->					Union[str, Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Dict      				=      self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
       UpperCamelCase__    :			Tuple      				=      model_class(__lowerCAmelCase	)
       UpperCamelCase__    :			Tuple      				=      inspect.signature(model.call	)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCamelCase__    :			Optional[Any]      				=      [*signature.parameters.keys()]

       UpperCamelCase__    :			Any      				=      ['''pixel_values''']
       self.assertListEqual(arg_names[:1]							,		__lowerCAmelCase	)


     def    UpperCAmelCase__		(					self							:   Optional[Any]	)     ->					int:





      '''simple docstring'''






      UpperCamelCase__    :			Dict      				=      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*__lowerCAmelCase	)


     def    UpperCAmelCase__		(					self							:   List[Any]	)     ->					List[str]:





      '''simple docstring'''






      def check_hidden_states_output(lowerCamelCase__							:   List[str]							,		lowerCamelCase__							:   List[str]							,		lowerCamelCase__							:   Optional[Any]	):
       UpperCamelCase__    :			Optional[int]      				=      model_class(__lowerCAmelCase	)
       UpperCamelCase__    :			Tuple      				=      model(**self._prepare_for_class(__lowerCAmelCase							,		__lowerCAmelCase	)	)

       UpperCamelCase__    :			Union[str, Any]      				=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

       UpperCamelCase__    :			Union[str, Any]      				=      self.model_tester.num_stages
       self.assertEqual(len(__lowerCAmelCase	)							,		expected_num_stages + 1	)

       # ResNet'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 // 4, self.model_tester.image_size // 4]							,		)

      UpperCamelCase__    :			Optional[Any]      				=      self.model_tester.prepare_config_and_inputs_for_common()
      UpperCamelCase__    :			Union[str, Any]      				=      ['''basic''', '''bottleneck''']
      for model_class in self.all_model_classes:
       for layer_type in layers_type:
        UpperCamelCase__    :			str      				=      layer_type
        UpperCamelCase__    :			Optional[int]      				=      True
        check_hidden_states_output(__lowerCAmelCase							,		__lowerCAmelCase							,		__lowerCAmelCase	)

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

        check_hidden_states_output(__lowerCAmelCase							,		__lowerCAmelCase							,		__lowerCAmelCase	)


     def    UpperCAmelCase__		(					self							:   List[Any]	)     ->					List[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Optional[int]      				=      self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_for_image_classification(*__lowerCAmelCase	)


     @slow
     def    UpperCAmelCase__		(					self							:   Union[str, Any]	)     ->					Union[str, Any]:





      '''simple docstring'''






      for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCamelCase__    :			str      				=      TFResNetModel.from_pretrained(__lowerCAmelCase	)
       self.assertIsNotNone(__lowerCAmelCase	)



def 	_a     ( ):







 """simple docstring"""



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






@require_tf
@require_vision
class 					__magic_name__		(   unittest.TestCase):


     @cached_property
     def    UpperCAmelCase__		(					self							:   int	)     ->					List[str]:





      '''simple docstring'''






      return (
          AutoImageProcessor.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)
          if is_vision_available()
          else None
      )


     @slow
     def    UpperCAmelCase__		(					self							:   List[str]	)     ->					List[Any]:





      '''simple docstring'''






      UpperCamelCase__    :			Union[str, Any]      				=      TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]	)

      UpperCamelCase__    :			Any      				=      self.default_image_processor
      UpperCamelCase__    :			List[Any]      				=      prepare_img()
      UpperCamelCase__    :			Optional[int]      				=      image_processor(images=__lowerCAmelCase							,		return_tensors='''tf'''	)

      # forward pass
      UpperCamelCase__    :			Optional[Any]      				=      model(**__lowerCAmelCase	)

      # verify the logits
      UpperCamelCase__    :			Optional[Any]      				=      tf.TensorShape((1, 1000)	)
      self.assertEqual(outputs.logits.shape							,		__lowerCAmelCase	)

      UpperCamelCase__    :			Any      				=      tf.constant([-11.1069, -9.7877, -8.3777]	)

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



'''simple docstring'''

import argparse

import pytorch_lightning as pl
import torch
from torch import nn

from transformers import LongformerForQuestionAnswering, LongformerModel



class 			UpperCAmelCase  (						pl.LightningModule					):





       '''simple docstring'''



       def __init__(						self	,   __lowerCAmelCase )  ->  List[str]:
        super().__init__()
        lowercase__				:    List[str]  =					model
        lowercase__				:    Dict  =					2
        lowercase__				:    Any  =					nn.Linear(self.model.config.hidden_size	,   self.num_labels )


       def _lowerCAmelCase(						self )  ->  str:
        pass


def 			__UpperCamelCase       (   UpperCAmelCase		,     UpperCAmelCase		,     UpperCAmelCase     ):
 # load longformer model from model identifier
 lowercase__				:    Dict  =					LongformerModel.from_pretrained(UpperCAmelCase     )
 lowercase__				:    List[str]  =					LightningModel(UpperCAmelCase     )

 lowercase__				:    List[Any]  =					torch.load(UpperCAmelCase		,     map_location=torch.device('''cpu'''     )     )
 lightning_model.load_state_dict(ckpt['''state_dict''']     )

 # init longformer question answering model
 lowercase__				:    Optional[int]  =					LongformerForQuestionAnswering.from_pretrained(UpperCAmelCase     )

 # transfer weights
 longformer_for_qa.longformer.load_state_dict(lightning_model.model.state_dict()     )
 longformer_for_qa.qa_outputs.load_state_dict(lightning_model.qa_outputs.state_dict()     )
 longformer_for_qa.eval()

 # save model
 longformer_for_qa.save_pretrained(UpperCAmelCase     )

 print(F"""Conversion successful. Model saved under {pytorch_dump_folder_path}"""     )


if __name__ == "__main__":
  __a: List[Any]          =   argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      """--longformer_model""",
      default=None,
      type=str,
      required=True,
      help="""model identifier of longformer. Should be either `longformer-base-4096` or `longformer-large-4096`.""",
  )
  parser.add_argument(
      """--longformer_question_answering_ckpt_path""",
      default=None,
      type=str,
      required=True,
      help="""Path the official PyTorch Lightning Checkpoint.""",
  )
  parser.add_argument(
      """--pytorch_dump_folder_path""", default=None, type=str, required=True, help="""Path to the output PyTorch model."""
  )
  __a: Tuple          =   parser.parse_args()
  convert_longformer_qa_checkpoint_to_pytorch(
      args.longformer_model, args.longformer_question_answering_ckpt_path, args.pytorch_dump_folder_path
  )

