



"""simple docstring"""
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 					a				:



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Optional[int]     			=parent
     SCREAMING_SNAKE_CASE_: Tuple     			=batch_size
     SCREAMING_SNAKE_CASE_: Dict     			=image_size
     SCREAMING_SNAKE_CASE_: int     			=num_channels
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=embeddings_size
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=hidden_sizes
     SCREAMING_SNAKE_CASE_: int     			=depths
     SCREAMING_SNAKE_CASE_: Dict     			=is_training
     SCREAMING_SNAKE_CASE_: Optional[Any]     			=use_labels
     SCREAMING_SNAKE_CASE_: Dict     			=hidden_act
     SCREAMING_SNAKE_CASE_: str     			=num_labels
     SCREAMING_SNAKE_CASE_: Any     			=scope
     SCREAMING_SNAKE_CASE_: Tuple     			=len(lowerCamelCase_		)



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


     '''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_: Any     			=None
     if self.use_labels:
        SCREAMING_SNAKE_CASE_: Optional[int]     			=ids_tensor([self.batch_size]       ,		self.num_labels		)

     SCREAMING_SNAKE_CASE_: Optional[int]     			=self.get_config()

     return config, pixel_values, labels



  def 	lowerCamelCase__       (						self :					Tuple		)     ->					Optional[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 	lowerCamelCase__       (						self :					Tuple       ,		lowerCAmelCase :					Union[str, Any]       ,		lowerCAmelCase :					Dict       ,		lowerCAmelCase :					Union[str, Any]		)     ->					Any:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=TFResNetModel(config=lowerCamelCase_		)
     SCREAMING_SNAKE_CASE_: Dict     			=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 	lowerCamelCase__       (						self :					Union[str, Any]       ,		lowerCAmelCase :					Optional[int]       ,		lowerCAmelCase :					Any       ,		lowerCAmelCase :					Optional[Any]		)     ->					List[Any]:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[Any]     			=self.num_labels
     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=TFResNetForImageClassification(lowerCamelCase_		)
     SCREAMING_SNAKE_CASE_: Optional[int]     			=model(lowerCamelCase_       ,		labels=lowerCamelCase_		)
     self.parent.assertEqual(result.logits.shape       ,		(self.batch_size, self.num_labels)		)





  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					Tuple:


     '''simple docstring'''




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







@require_tf
class 					a				(  a__	,      a__	,      unittest.TestCase       ):
  UpperCamelCase	:				Union[str, Any]     						=					(TFResNetModel, TFResNetForImageClassification) if is_tf_available() else ()
  UpperCamelCase	:				Any     						=					(
      {"feature-extraction": TFResNetModel, "image-classification": TFResNetForImageClassification}
      if is_tf_available()
      else {}
  )

  UpperCamelCase	:				int     						=					False
  UpperCamelCase	:				List[str]     						=					False
  UpperCamelCase	:				str     						=					False
  UpperCamelCase	:				Tuple     						=					False
  UpperCamelCase	:				Union[str, Any]     						=					False



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


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Tuple     			=TFResNetModelTester(self		)
     SCREAMING_SNAKE_CASE_: str     			=ConfigTester(self       ,		config_class=lowerCamelCase_       ,		has_text_modality=lowerCamelCase_		)



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


     '''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 	lowerCamelCase__       (						self :					List[Any]		)     ->					Any:


     '''simple docstring'''




     return



  @unittest.skip(reason="""ResNet does not use inputs_embeds"""		)
  def 	lowerCamelCase__       (						self :					Optional[int]		)     ->					List[str]:


     '''simple docstring'''




     pass



  @unittest.skip(reason="""ResNet does not support input and output embeddings"""		)
  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					List[str]:


     '''simple docstring'''




     pass



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


     '''simple docstring'''




     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_: Any     			=model_class(lowerCamelCase_		)
        SCREAMING_SNAKE_CASE_: int     			=inspect.signature(model.call		)
        # signature.parameters is an OrderedDict => so arg_names order is deterministic
        SCREAMING_SNAKE_CASE_: Optional[Any]     			=[*signature.parameters.keys()]

        SCREAMING_SNAKE_CASE_: Union[str, Any]     			=["""pixel_values"""]
        self.assertListEqual(arg_names[:1]       ,		lowerCamelCase_		)



  def 	lowerCamelCase__       (						self :					List[str]		)     ->					str:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: int     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*lowerCamelCase_		)



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


     '''simple docstring'''




     def check_hidden_states_output(lowerCAmelCase :					Optional[Any]       ,		lowerCAmelCase :					int       ,		lowerCAmelCase :					List[str]		):
        SCREAMING_SNAKE_CASE_: Optional[Any]     			=model_class(lowerCamelCase_		)
        SCREAMING_SNAKE_CASE_: str     			=model(**self._prepare_for_class(lowerCamelCase_       ,		lowerCamelCase_		)		)

        SCREAMING_SNAKE_CASE_: List[str]     			=outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        SCREAMING_SNAKE_CASE_: str     			=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]       ,		)

     SCREAMING_SNAKE_CASE_			,		SCREAMING_SNAKE_CASE_: Optional[int]     			=self.model_tester.prepare_config_and_inputs_for_common()
     SCREAMING_SNAKE_CASE_: int     			=["""basic""", """bottleneck"""]
     for model_class in self.all_model_classes:
        for layer_type in layers_type:
           SCREAMING_SNAKE_CASE_: Tuple     			=layer_type
           SCREAMING_SNAKE_CASE_: Tuple     			=True
           check_hidden_states_output(lowerCamelCase_       ,		lowerCamelCase_       ,		lowerCamelCase_		)

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           SCREAMING_SNAKE_CASE_: List[str]     			=True

           check_hidden_states_output(lowerCamelCase_       ,		lowerCamelCase_       ,		lowerCamelCase_		)



  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					Tuple:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: Union[str, Any]     			=self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*lowerCamelCase_		)





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


     '''simple docstring'''




     for model_name in TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        SCREAMING_SNAKE_CASE_: Union[str, Any]     			=TFResNetModel.from_pretrained(lowerCamelCase_		)
        self.assertIsNotNone(lowerCamelCase_		)







def      __magic_name__				(							):
   SCREAMING_SNAKE_CASE_: int     			=Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
   return image
@require_tf
@require_vision
class 					a				(  unittest.TestCase       ):



  @cached_property
  def 	lowerCamelCase__       (						self :					List[Any]		)     ->					List[str]:


     '''simple docstring'''




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





  @slow
  def 	lowerCamelCase__       (						self :					Any		)     ->					int:


     '''simple docstring'''




     SCREAMING_SNAKE_CASE_: List[str]     			=TFResNetForImageClassification.from_pretrained(TF_RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]		)

     SCREAMING_SNAKE_CASE_: Optional[int]     			=self.default_image_processor
     SCREAMING_SNAKE_CASE_: Tuple     			=prepare_img()
     SCREAMING_SNAKE_CASE_: str     			=image_processor(images=lowerCamelCase_       ,		return_tensors="""tf"""		)

     # forward pass
     SCREAMING_SNAKE_CASE_: List[str]     			=model(**lowerCamelCase_		)

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

     SCREAMING_SNAKE_CASE_: int     			=tf.constant([-1_1.1_0_6_9, -9.7_8_7_7, -8.3_7_7_7]		)

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








"""simple docstring"""
import argparse

import torch

from transformers import BertForMaskedLM


if __name__ == "__main__":
   _UpperCAmelCase     =			argparse.ArgumentParser(
       description=(
           """Extraction some layers of the full BertForMaskedLM or RObertaForMaskedLM for Transfer Learned"""
           """ Distillation"""
       )
   )
   parser.add_argument("""--model_type""", default="""bert""", choices=["""bert"""])
   parser.add_argument("""--model_name""", default="""bert-base-uncased""", type=str)
   parser.add_argument("""--dump_checkpoint""", default="""serialization_dir/tf_bert-base-uncased_0247911.pth""", type=str)
   parser.add_argument("""--vocab_transform""", action="""store_true""")
   _UpperCAmelCase     =			parser.parse_args()

   if args.model_type == "bert":
      _UpperCAmelCase     =			BertForMaskedLM.from_pretrained(args.model_name)
      _UpperCAmelCase     =			"""bert"""
   else:
      raise ValueError("""args.model_type should be \"bert\".""")

   _UpperCAmelCase     =			model.state_dict()
   _UpperCAmelCase     =			{}

   for w in ["word_embeddings", "position_embeddings"]:
      _UpperCAmelCase     =			state_dict[f"""{prefix}.embeddings.{w}.weight"""]
   for w in ["weight", "bias"]:
      _UpperCAmelCase     =			state_dict[f"""{prefix}.embeddings.LayerNorm.{w}"""]

   _UpperCAmelCase     =			0
   for teacher_idx in [0, 2, 4, 7, 9, 1_1]:
      for w in ["weight", "bias"]:
         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.attention.self.query.{w}"""
         ]
         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.attention.self.key.{w}"""
         ]
         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.attention.self.value.{w}"""
         ]

         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.attention.output.dense.{w}"""
         ]
         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.attention.output.LayerNorm.{w}"""
         ]

         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.intermediate.dense.{w}"""
         ]
         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.output.dense.{w}"""
         ]
         _UpperCAmelCase     =			state_dict[
             f"""{prefix}.encoder.layer.{teacher_idx}.output.LayerNorm.{w}"""
         ]
      std_idx += 1

   _UpperCAmelCase     =			state_dict["""cls.predictions.decoder.weight"""]
   _UpperCAmelCase     =			state_dict["""cls.predictions.bias"""]
   if args.vocab_transform:
      for w in ["weight", "bias"]:
         _UpperCAmelCase     =			state_dict[f"""cls.predictions.transform.dense.{w}"""]
         _UpperCAmelCase     =			state_dict[f"""cls.predictions.transform.LayerNorm.{w}"""]

   print(f"""N layers selected for distillation: {std_idx}""")
   print(f"""Number of params transferred for distillation: {len(compressed_sd.keys())}""")

   print(f"""Save transferred checkpoint to {args.dump_checkpoint}.""")
   torch.save(compressed_sd, args.dump_checkpoint)



