
"""simple docstring"""





import inspect
import unittest

from transformers import ResNetConfig
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_backbone_common import BackboneTesterMixin
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 ResNetBackbone, ResNetForImageClassification, ResNetModel
    from transformers.models.resnet.modeling_resnet import RESNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import AutoImageProcessor


class A_      :




  '''simple docstring'''







  def __init__(						self							,  lowercase_							,  lowercase_=3							,  lowercase_=32							,  lowercase_=3							,  lowercase_=10							,  lowercase_=[10, 20, 30, 40]							,  lowercase_=[1, 1, 2, 1]							,  lowercase_=True							,  lowercase_=True							,  lowercase_="relu"							,  lowercase_=3							,  lowercase_=None							,  lowercase_=["stage2", "stage3", "stage4"]							,  lowercase_=[2, 3, 4]							,  ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       parent
     UpperCAmelCase_   :   Dict     	=       batch_size
     UpperCAmelCase_   :   Tuple     	=       image_size
     UpperCAmelCase_   :   List[Any]     	=       num_channels
     UpperCAmelCase_   :   Tuple     	=       embeddings_size
     UpperCAmelCase_   :   List[str]     	=       hidden_sizes
     UpperCAmelCase_   :   Tuple     	=       depths
     UpperCAmelCase_   :   List[Any]     	=       is_training
     UpperCAmelCase_   :   Dict     	=       use_labels
     UpperCAmelCase_   :   List[str]     	=       hidden_act
     UpperCAmelCase_   :   Union[str, Any]     	=       num_labels
     UpperCAmelCase_   :   List[str]     	=       scope
     UpperCAmelCase_   :   Optional[Any]     	=       len(_lowercase    )
     UpperCAmelCase_   :   List[Any]     	=       out_features
     UpperCAmelCase_   :   Optional[int]     	=       out_indices







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

     UpperCAmelCase_   :   List[str]     	=       None
     if self.use_labels:
        UpperCAmelCase_   :   Optional[int]     	=       ids_tensor([self.batch_size]							,  self.num_labels    )

     UpperCAmelCase_   :   int     	=       self.get_config()

     return config, pixel_values, labels







  def 			UpperCamelCase__   (						self    ):

     """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							,  out_features=self.out_features							,  out_indices=self.out_indices							,  )







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       ResNetModel(config=_lowercase    )
     model.to(_lowercase    )
     model.eval()
     UpperCAmelCase_   :   List[Any]     	=       model(_lowercase    )
     # 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							,  lowercase_							,  lowercase_							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[str]     	=       self.num_labels
     UpperCAmelCase_   :   List[str]     	=       ResNetForImageClassification(_lowercase    )
     model.to(_lowercase    )
     model.eval()
     UpperCAmelCase_   :   int     	=       model(_lowercase							,  labels=_lowercase    )
     self.parent.assertEqual(result.logits.shape							,  (self.batch_size, self.num_labels)    )







  def 			UpperCamelCase__   (						self							,  lowercase_							,  lowercase_							,  lowercase_    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       ResNetBackbone(config=_lowercase    )
     model.to(_lowercase    )
     model.eval()
     UpperCAmelCase_   :   Any     	=       model(_lowercase    )

     # verify feature maps
     self.parent.assertEqual(len(result.feature_maps    )							,  len(config.out_features    )    )
     self.parent.assertListEqual(list(result.feature_maps[0].shape    )							,  [self.batch_size, self.hidden_sizes[1], 4, 4]    )

     # verify channels
     self.parent.assertEqual(len(model.channels    )							,  len(config.out_features    )    )
     self.parent.assertListEqual(model.channels							,  config.hidden_sizes[1:]    )

     # verify backbone works with out_features=None
     UpperCAmelCase_   :   int     	=       None
     UpperCAmelCase_   :   int     	=       ResNetBackbone(config=_lowercase    )
     model.to(_lowercase    )
     model.eval()
     UpperCAmelCase_   :   Union[str, Any]     	=       model(_lowercase    )

     # verify feature maps
     self.parent.assertEqual(len(result.feature_maps    )							,  1    )
     self.parent.assertListEqual(list(result.feature_maps[0].shape    )							,  [self.batch_size, self.hidden_sizes[-1], 1, 1]    )

     # verify channels
     self.parent.assertEqual(len(model.channels    )							,  1    )
     self.parent.assertListEqual(model.channels							,  [config.hidden_sizes[-1]]    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   int     	=       self.prepare_config_and_inputs()
     UpperCAmelCase_   :   Tuple     	=       config_and_inputs
     UpperCAmelCase_   :   List[Any]     	=       {'pixel_values': pixel_values}
     return config, inputs_dict




@require_torch
class A_      (UpperCAmelCase_							,UpperCAmelCase_							,unittest.TestCase ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		Optional[Any]    	=						(
      (
          ResNetModel,
          ResNetForImageClassification,
          ResNetBackbone,
      )
      if is_torch_available()
      else ()
  )
  SCREAMING_SNAKE_CASE__     :		str    	=						(
      {'feature-extraction': ResNetModel, 'image-classification': ResNetForImageClassification}
      if is_torch_available()
      else {}
  )

  SCREAMING_SNAKE_CASE__     :		Optional[int]    	=						True
  SCREAMING_SNAKE_CASE__     :		Optional[Any]    	=						False
  SCREAMING_SNAKE_CASE__     :		Optional[Any]    	=						False
  SCREAMING_SNAKE_CASE__     :		Tuple    	=						False
  SCREAMING_SNAKE_CASE__     :		List[str]    	=						False







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       ResNetModelTester(self    )
     UpperCAmelCase_   :   str     	=       ConfigTester(self							,  config_class=_lowercase							,  has_text_modality=_lowercase    )







  def 			UpperCamelCase__   (						self    ):

     """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    ):

     """simple docstring"""
     return







  @unittest.skip(reason="ResNet does not use inputs_embeds"    )
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     pass







  @unittest.skip(reason="ResNet does not support input and output embeddings"    )
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     pass







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[int]     	=       self.model_tester.prepare_config_and_inputs_for_common()

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

        UpperCAmelCase_   :   str     	=       ['pixel_values']
        self.assertListEqual(arg_names[:1]							,  _lowercase    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Optional[Any]     	=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*_lowercase    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Union[str, Any]     	=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*_lowercase    )







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   Dict     	=       self.model_tester.prepare_config_and_inputs_for_common()

     for model_class in self.all_model_classes:
        UpperCAmelCase_   :   Optional[int]     	=       model_class(config=_lowercase    )
        for name, module in model.named_modules():
           if isinstance(_lowercase							,  (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 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     def check_hidden_states_output(lowercase_							,  lowercase_							,  lowercase_    ):
        UpperCAmelCase_   :   Optional[int]     	=       model_class(_lowercase    )
        model.to(_lowercase    )
        model.eval()

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

        UpperCAmelCase_   :   Optional[Any]     	=       outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

        UpperCAmelCase_   :   Dict     	=       self.model_tester.num_stages
        self.assertEqual(len(_lowercase    )							,  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_   :   List[str]     	=       self.model_tester.prepare_config_and_inputs_for_common()
     UpperCAmelCase_   :   Dict     	=       ['basic', 'bottleneck']
     for model_class in self.all_model_classes:
        for layer_type in layers_type:
           UpperCAmelCase_   :   Any     	=       layer_type
           UpperCAmelCase_   :   str     	=       True
           check_hidden_states_output(_lowercase							,  _lowercase							,  _lowercase    )

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

           check_hidden_states_output(_lowercase							,  _lowercase							,  _lowercase    )







  @unittest.skip(reason="ResNet does not use feedforward chunking"    )
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     pass







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*_lowercase    )







  @slow
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     for model_name in RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
        UpperCAmelCase_   :   Any     	=       ResNetModel.from_pretrained(_lowercase    )
        self.assertIsNotNone(_lowercase    )







def       __a	(						):
   UpperCAmelCase_   :   Union[str, Any]     	=       Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"    )
   return image


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




  '''simple docstring'''







  @cached_property
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     return (
         AutoImageProcessor.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]    )
         if is_vision_available()
         else None
     )







  @slow
  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   int     	=       ResNetForImageClassification.from_pretrained(RESNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]    ).to(_lowercase    )

     UpperCAmelCase_   :   Any     	=       self.default_image_processor
     UpperCAmelCase_   :   Tuple     	=       prepare_img()
     UpperCAmelCase_   :   str     	=       image_processor(images=_lowercase							,  return_tensors="pt"    ).to(_lowercase    )

     # forward pass
     with torch.no_grad():
        UpperCAmelCase_   :   Optional[int]     	=       model(**_lowercase    )

     # verify the logits
     UpperCAmelCase_   :   Optional[int]     	=       torch.Size((1, 1000)    )
     self.assertEqual(outputs.logits.shape							,  _lowercase    )

     UpperCAmelCase_   :   str     	=       torch.tensor([-11.10_69, -9.78_77, -8.37_77]    ).to(_lowercase    )

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




@require_torch
class A_      (UpperCAmelCase_							,unittest.TestCase ):




  '''simple docstring'''
  SCREAMING_SNAKE_CASE__     :		List[Any]    	=						(ResNetBackbone,) if is_torch_available() else ()
  SCREAMING_SNAKE_CASE__     :		str    	=						False
  SCREAMING_SNAKE_CASE__     :		Optional[int]    	=						ResNetConfig







  def 			UpperCamelCase__   (						self    ):

     """simple docstring"""
     UpperCAmelCase_   :   List[Any]     	=       ResNetModelTester(self    )










'''simple docstring'''

import importlib
import sys
from argparse import REMAINDER, ArgumentParser
from pathlib import Path

import torch_xla.distributed.xla_multiprocessing as xmp






def 						_UpperCAmelCase     (	)      ->      Optional[Any]:





						"""simple docstring"""
						lowercase_   : List[str]   	=      ArgumentParser(
						    description=(
						        'PyTorch TPU distributed training launch helper utility that will spawn up multiple distributed processes'
						    )				)

						# Optional arguments for the launch helper
						parser.add_argument('--num_cores'    ,      type=a    ,      default=1    ,      help='Number of TPU cores to use (1 or 8).'				)

						# positional
						parser.add_argument(
						    'training_script'    ,      type=a    ,      help=(
						        'The full path to the single TPU training '
						        'program/script to be launched in parallel, '
						        'followed by all the arguments for the '
						        'training script'
						    )    ,      )

						# rest from the training program
						parser.add_argument('training_script_args'    ,      nargs=a				)

						return parser.parse_args()






def 						_UpperCAmelCase     (	)      ->      str:





						"""simple docstring"""
						lowercase_   : Optional[int]   	=      parse_args()

						# Import training_script as a module.
						lowercase_   : str   	=      Path(args.training_script				)
						sys.path.append(str(script_fpath.parent.resolve()				)				)
						lowercase_   : int   	=      script_fpath.stem
						lowercase_   : List[Any]   	=      importlib.import_module(a				)

						# Patch sys.argv
						lowercase_   : List[str]   	=      [args.training_script] + args.training_script_args + ['--tpu_num_cores', str(args.num_cores				)]

						xmp.spawn(mod._mp_fn    ,      args=()    ,      nprocs=args.num_cores				)


if __name__ == "__main__":
						main()
