

'''simple docstring'''







import inspect
import unittest

from transformers import ConvNextVaConfig
from transformers.models.auto import get_values
from transformers.models.auto.modeling_auto import MODEL_FOR_BACKBONE_MAPPING_NAMES, MODEL_MAPPING_NAMES
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_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 ConvNextVaBackbone, ConvNextVaForImageClassification, ConvNextVaModel
      from transformers.models.convnextva.modeling_convnextva import CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor


class        A__   :



    def __init__(       self		,					UpperCamelCase__		,					UpperCamelCase__=13		,					UpperCamelCase__=32		,					UpperCamelCase__=3		,					UpperCamelCase__=4		,					UpperCamelCase__=[10, 20, 30, 40]		,					UpperCamelCase__=[2, 2, 3, 2]		,					UpperCamelCase__=True		,					UpperCamelCase__=True		,					UpperCamelCase__=37		,					UpperCamelCase__="gelu"		,					UpperCamelCase__=10		,					UpperCamelCase__=0.02		,					UpperCamelCase__=["stage2", "stage3", "stage4"]		,					UpperCamelCase__=[2, 3, 4]		,					UpperCamelCase__=None		,					)    ->   Union[str, Any]:





           '''simple docstring'''
           A_						       =   parent
           A_						       =   batch_size
           A_						       =   image_size
           A_						       =   num_channels
           A_						       =   num_stages
           A_						       =   hidden_sizes
           A_						       =   depths
           A_						       =   is_training
           A_						       =   use_labels
           A_						       =   intermediate_size
           A_						       =   hidden_act
           A_						       =   num_labels
           A_						       =   initializer_range
           A_						       =   out_features
           A_						       =   out_indices
           A_						       =   scope



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





           '''simple docstring'''
           A_						       =   floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]							)

           A_						       =   None
           if self.use_labels:
                  A_						       =   ids_tensor([self.batch_size]		,					self.num_labels							)

           A_						       =   self.get_config()

           return config, pixel_values, labels



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           return ConvNextVaConfig(
               num_channels=self.num_channels		,					hidden_sizes=self.hidden_sizes		,					depths=self.depths		,					num_stages=self.num_stages		,					hidden_act=self.hidden_act		,					is_decoder=UpperCamelCase__		,					initializer_range=self.initializer_range		,					out_features=self.out_features		,					out_indices=self.out_indices		,					num_labels=self.num_labels		,					)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   str:





           '''simple docstring'''
           A_						       =   ConvNextVaModel(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)
           # 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        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Any:





           '''simple docstring'''
           A_						       =   ConvNextVaForImageClassification(UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__		,					labels=UpperCamelCase__							)
           self.parent.assertEqual(result.logits.shape		,					(self.batch_size, self.num_labels)							)



    def        snake_case_      (       self		,					UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)    ->   Optional[Any]:





           '''simple docstring'''
           A_						       =   ConvNextVaBackbone(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)

           # verify hidden states
           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
           A_						       =   None
           A_						       =   ConvNextVaBackbone(config=UpperCamelCase__							)
           model.to(UpperCamelCase__							)
           model.eval()
           A_						       =   model(UpperCamelCase__							)

           # 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        snake_case_      (       self							)    ->   List[str]:





           '''simple docstring'''
           A_						       =   self.prepare_config_and_inputs()
           A_       ,      A_       ,      A_						       =   config_and_inputs
           A_						       =   {"""pixel_values""": pixel_values}
           return config, inputs_dict







    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_						       =   self.prepare_config_and_inputs()
           A_       ,      A_       ,      A_						       =   config_and_inputs
           A_						       =   {"""pixel_values""": pixel_values, """labels""": labels}
           return config, inputs_dict


@require_torch
class        A__   (   _snake_case    ,    _snake_case    ,    unittest.TestCase      ):
    lowercase						       =		(
        (
            ConvNextVaModel,
            ConvNextVaForImageClassification,
            ConvNextVaBackbone,
        )
        if is_torch_available()
        else ()
    )
    lowercase						       =		(
        {"feature-extraction": ConvNextVaModel, "image-classification": ConvNextVaForImageClassification}
        if is_torch_available()
        else {}
    )

    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False
    lowercase						       =		False



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





           '''simple docstring'''
           A_						       =   ConvNextVaModelTester(self							)
           A_						       =   ConfigTester(self		,					config_class=UpperCamelCase__		,					has_text_modality=UpperCamelCase__		,					hidden_size=37							)



    def        snake_case_      (       self							)    ->   Tuple:





           '''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        snake_case_      (       self							)    ->   Optional[int]:





           '''simple docstring'''
           return



    @unittest.skip(reason="""ConvNextV2 does not use inputs_embeds"""							)
    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           pass



    @unittest.skip(reason="""ConvNextV2 does not support input and output embeddings"""							)
    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           pass



    @unittest.skip(reason="""ConvNextV2 does not use feedforward chunking"""							)
    def        snake_case_      (       self							)    ->   Optional[Any]:





           '''simple docstring'''
           pass



    def        snake_case_      (       self							)    ->   str:





           '''simple docstring'''
           if not self.model_tester.is_training:
                  return

           for model_class in self.all_model_classes:
                  A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_with_labels()
                  A_						       =   True

                  if model_class.__name__ in [
                      *get_values(UpperCamelCase__							),
                      *get_values(UpperCamelCase__							),
                  ]:
                         continue

                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.train()
                  A_						       =   self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__		,					return_labels=UpperCamelCase__							)
                  A_						       =   model(**UpperCamelCase__							).loss
                  loss.backward()



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





           '''simple docstring'''
           if not self.model_tester.is_training:
                  return

           for model_class in self.all_model_classes:
                  A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_with_labels()
                  A_						       =   False
                  A_						       =   True

                  if (
                      model_class.__name__
                      in [*get_values(UpperCamelCase__							), *get_values(UpperCamelCase__							)]
                      or not model_class.supports_gradient_checkpointing
                  ):
                         continue

                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.gradient_checkpointing_enable()
                  model.train()
                  A_						       =   self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__		,					return_labels=UpperCamelCase__							)
                  A_						       =   model(**UpperCamelCase__							).loss
                  loss.backward()



    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

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

                  A_						       =   ["""pixel_values"""]
                  self.assertListEqual(arg_names[:1]		,					UpperCamelCase__							)



    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*UpperCamelCase__							)



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





           '''simple docstring'''
           def check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							):
                  A_						       =   model_class(UpperCamelCase__							)
                  model.to(UpperCamelCase__							)
                  model.eval()

                  with torch.no_grad():
                         A_						       =   model(**self._prepare_for_class(UpperCamelCase__		,					UpperCamelCase__							)							)

                  A_						       =   outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

                  A_						       =   self.model_tester.num_stages
                  self.assertEqual(len(UpperCamelCase__							)		,					expected_num_stages + 1							)

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

           A_       ,      A_						       =   self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
                  A_						       =   True
                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)

                  # check that output_hidden_states also work using config
                  del inputs_dict["output_hidden_states"]
                  A_						       =   True

                  check_hidden_states_output(UpperCamelCase__		,					UpperCamelCase__		,					UpperCamelCase__							)



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





           '''simple docstring'''
           A_						       =   self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_for_image_classification(*UpperCamelCase__							)







    @slow
    def        snake_case_      (       self							)    ->   Any:





           '''simple docstring'''
           for model_name in CONVNEXTV2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  A_						       =   ConvNextVaModel.from_pretrained(UpperCamelCase__							)
                  self.assertIsNotNone(UpperCamelCase__							)

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







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



    @cached_property
    def        snake_case_      (       self							)    ->   Dict:





           '''simple docstring'''
           return AutoImageProcessor.from_pretrained("""facebook/convnextv2-tiny-1k-224"""							) if is_vision_available() else None







    @slow
    def        snake_case_      (       self							)    ->   int:





           '''simple docstring'''
           A_						       =   ConvNextVaForImageClassification.from_pretrained("""facebook/convnextv2-tiny-1k-224"""							).to(UpperCamelCase__							)

           A_						       =   self.default_image_processor
           A_						       =   prepare_img()
           A_						       =   preprocessor(images=UpperCamelCase__		,					return_tensors="""pt"""							).to(UpperCamelCase__							)

           # forward pass
           with torch.no_grad():
                  A_						       =   model(**UpperCamelCase__							)

           # verify the logits
           A_						       =   torch.Size((1, 1000)							)
           self.assertEqual(outputs.logits.shape		,					UpperCamelCase__							)

           A_						       =   torch.tensor([0.9996, 0.1966, -0.4386]							).to(UpperCamelCase__							)
           self.assertTrue(torch.allclose(outputs.logits[0, :3]		,					UpperCamelCase__		,					atol=1e-4							)							)



'''simple docstring'''







# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import subprocess

from packaging.version import Version, parse

from accelerate.commands.config.config_args import default_config_file, load_config_from_file


__lowerCamelCase									=     '''Run commands across TPU VMs for initial setup before running `accelerate launch`.'''
def    UpperCAmelCase__     (   UpperCAmelCase__=None					)      ->      List[str]:
       if subparsers is not None:
              A_						       =   subparsers.add_parser("""tpu-config""",							description=_description					)
       else:
              A_						       =   argparse.ArgumentParser("""Accelerate tpu-config command""",							description=_description					)
       # Core arguments
       A_						       =   parser.add_argument_group(
           """Config Arguments""",							"""Arguments that can be configured through `accelerate config`."""					)
       config_args.add_argument(
           """--config_file""",							type=UpperCAmelCase__,							default=UpperCAmelCase__,							help="""Path to the config file to use for accelerate.""",							)
       config_args.add_argument(
           """--tpu_name""",							default=UpperCAmelCase__,							help="""The name of the TPU to use. If not specified, will use the TPU specified in the config file.""",							)
       config_args.add_argument(
           """--tpu_zone""",							default=UpperCAmelCase__,							help="""The zone of the TPU to use. If not specified, will use the zone specified in the config file.""",							)
       A_						       =   parser.add_argument_group("""TPU Arguments""",							"""Arguments for options ran inside the TPU."""					)
       pod_args.add_argument(
           """--use_alpha""",							action="""store_true""",							help="""Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`.""",							)
       pod_args.add_argument(
           """--command_file""",							default=UpperCAmelCase__,							help="""The path to the file containing the commands to run on the pod on startup.""",							)
       pod_args.add_argument(
           """--command""",							action="""append""",							nargs="""+""",							help="""A command to run on the pod. Can be passed multiple times.""",							)
       pod_args.add_argument(
           """--install_accelerate""",							action="""store_true""",							help="""Whether to install accelerate on the pod. Defaults to False.""",							)
       pod_args.add_argument(
           """--accelerate_version""",							default="""latest""",							help="""The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify 'dev' to install from GitHub.""",							)
       pod_args.add_argument(
           """--debug""",							action="""store_true""",							help="""If set, will print the command that would be run instead of running it."""					)

       if subparsers is not None:
              parser.set_defaults(func=UpperCAmelCase__					)
       return parser
def    UpperCAmelCase__     (   UpperCAmelCase__					)      ->      List[Any]:
       A_						       =   None

       # Get the default from the config file if it exists.
       if args.config_file is not None or os.path.isfile(UpperCAmelCase__					):
              A_						       =   load_config_from_file(args.config_file					)
              if not args.command_file and defaults.command_file is not None and not args.command:
                     A_						       =   defaults.command_file
              if not args.command and defaults.commands is not None:
                     A_						       =   defaults.commands
              if not args.tpu_name:
                     A_						       =   defaults.tpu_name
              if not args.tpu_zone:
                     A_						       =   defaults.tpu_zone
       if args.accelerate_version == "dev":
              A_						       =   """git+https://github.com/huggingface/accelerate.git"""
       elif args.accelerate_version == "latest":
              A_						       =   """accelerate -U"""
       elif isinstance(parse(args.accelerate_version					),							UpperCAmelCase__					):
              A_						       =   F'''accelerate=={args.accelerate_version}'''

       if not args.command_file and not args.command:
              raise ValueError("""You must specify either a command file or a command to run on the pod."""					)

       if args.command_file:
              with open(args.command_file,							"""r"""					) as f:
                     A_						       =   [f.read().splitlines()]

    # To turn list of lists into list of strings
       if isinstance(args.command[0],							UpperCAmelCase__					):
              A_						       =   [line for cmd in args.command for line in cmd]
       # Default to the shared folder and install accelerate
       A_						       =   ["""cd /usr/share"""]
       if args.install_accelerate:
              new_cmd += [F'''pip install {args.accelerate_version}''']
       new_cmd += args.command
       A_						       =   """; """.join(UpperCAmelCase__					)

       # Then send it to gcloud
       # Eventually try to use google-api-core to do this instead of subprocess
       A_						       =   ["""gcloud"""]
       if args.use_alpha:
              cmd += ["alpha"]
       cmd += [
           "compute",
           "tpus",
           "tpu-vm",
           "ssh",
           args.tpu_name,
           "--zone",
           args.tpu_zone,
           "--command",
           args.command,
           "--worker",
           "all",
       ]
       if args.debug:
              print(F'''Running {" ".join(UpperCAmelCase__					)}'''					)
              return
       subprocess.run(UpperCAmelCase__					)
       print("""Successfully setup pod."""					)







def    UpperCAmelCase__     (   )      ->      int:
       A_						       =   tpu_command_parser()
       A_						       =   parser.parse_args()

       tpu_command_launcher(UpperCAmelCase__					)
