



"""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 			snake_case_    ( A_   :						Union[str, Any]=None		):


       '''simple docstring'''





       if subparsers is not None:
              _lowerCamelCase		:							Optional[Any]            =					subparsers.add_parser('''tpu-config''',							description=_description		)
       else:
              _lowerCamelCase		:							Any            =					argparse.ArgumentParser('''Accelerate tpu-config command''',							description=_description		)
       # Core arguments
       _lowerCamelCase		:							Optional[int]            =					parser.add_argument_group(
           '''Config Arguments''',							'''Arguments that can be configured through `accelerate config`.'''		)
       config_args.add_argument(
           '''--config_file''',							type=A_,							default=A_,							help='''Path to the config file to use for accelerate.''',							)
       config_args.add_argument(
           '''--tpu_name''',							default=A_,							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=A_,							help='''The zone of the TPU to use. If not specified, will use the zone specified in the config file.''',							)
       _lowerCamelCase		:							Optional[Any]            =					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=A_,							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=A_		)
       return parser






def 			snake_case_    ( A_   :						List[Any]		):


       '''simple docstring'''





       _lowerCamelCase		:							Optional[Any]            =					None

       # Get the default from the config file if it exists.
       if args.config_file is not None or os.path.isfile(A_		):
              _lowerCamelCase		:							int            =					load_config_from_file(args.config_file		)
              if not args.command_file and defaults.command_file is not None and not args.command:
                     _lowerCamelCase		:							List[Any]            =					defaults.command_file
              if not args.command and defaults.commands is not None:
                     _lowerCamelCase		:							int            =					defaults.commands
              if not args.tpu_name:
                     _lowerCamelCase		:							List[Any]            =					defaults.tpu_name
              if not args.tpu_zone:
                     _lowerCamelCase		:							Optional[int]            =					defaults.tpu_zone
       if args.accelerate_version == "dev":
              _lowerCamelCase		:							Any            =					'''git+https://github.com/huggingface/accelerate.git'''
       elif args.accelerate_version == "latest":
              _lowerCamelCase		:							Optional[int]            =					'''accelerate -U'''
       elif isinstance(parse(args.accelerate_version		),							A_		):
              _lowerCamelCase		:							Optional[int]            =					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:
                     _lowerCamelCase		:							Optional[Any]            =					[f.read().splitlines()]

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

       # Then send it to gcloud
       # Eventually try to use google-api-core to do this instead of subprocess
       _lowerCamelCase		:							Optional[int]            =					['''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(A_		)}'''		)
              return
       subprocess.run(A_		)
       print('''Successfully setup pod.'''		)







def 			snake_case_    ( ):


       '''simple docstring'''





       _lowerCamelCase		:							List[str]            =					tpu_command_parser()
       _lowerCamelCase		:							Union[str, Any]            =					parser.parse_args()

       tpu_command_launcher(A_		)










import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ChineseCLIPImageProcessor






class 					SCREAMING_SNAKE_CASE_  (       unittest.TestCase ):
       """simple docstring"""


       def __init__(				self	:Any,      snake_case	:Optional[int],      snake_case	:Optional[Any]=7,      snake_case	:str=3,      snake_case	:Optional[int]=18,      snake_case	:str=30,      snake_case	:List[Any]=400,      snake_case	:Any=True,      snake_case	:Dict=None,      snake_case	:Any=True,      snake_case	:Dict=None,      snake_case	:List[Any]=True,      snake_case	:Optional[int]=[0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3],      snake_case	:Any=[0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1],      snake_case	:int=True,      ):



          """simple docstring"""

          _lowercase              =size if size is not None else {'height': 224, 'width': 224}
          _lowercase              =crop_size if crop_size is not None else {'height': 18, 'width': 18}
          _lowercase              =parent
          _lowercase              =batch_size
          _lowercase              =num_channels
          _lowercase              =image_size
          _lowercase              =min_resolution
          _lowercase              =max_resolution
          _lowercase              =do_resize
          _lowercase              =size
          _lowercase              =do_center_crop
          _lowercase              =crop_size
          _lowercase              =do_normalize
          _lowercase              =image_mean
          _lowercase              =image_std
          _lowercase              =do_convert_rgb





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          return {
              "do_resize": self.do_resize,
              "size": self.size,
              "do_center_crop": self.do_center_crop,
              "crop_size": self.crop_size,
              "do_normalize": self.do_normalize,
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_convert_rgb": self.do_convert_rgb,
          }





       def 				UpperCamelCase__						(				self	:Dict,      snake_case	:List[Any]=False,      snake_case	:Any=False,      snake_case	:Union[str, Any]=False):



          """simple docstring"""

          assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

          if equal_resolution:
             _lowercase              =[]
             for i in range(self.batch_size):
                image_inputs.append(
                    np.random.randint(
                        255,      size=(self.num_channels, self.max_resolution, self.max_resolution),      dtype=np.uinta))
          else:
             _lowercase              =[]
             for i in range(self.batch_size):
                _lowercase       ,       _lowercase              =np.random.choice(np.arange(self.min_resolution,      self.max_resolution),      2)
                image_inputs.append(np.random.randint(255,      size=(self.num_channels, width, height),      dtype=np.uinta))

          if not numpify and not torchify:
             # PIL expects the channel dimension as last dimension
             _lowercase              =[Image.fromarray(np.moveaxis(snake_case,      0,      -1)) for x in image_inputs]

          if torchify:
             _lowercase              =[torch.from_numpy(snake_case) for x in image_inputs]

          return image_inputs






@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		List[Any]					=ChineseCLIPImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          _lowercase              =ChineseCLIPImageProcessingTester(self,      do_center_crop=snake_case)





       @property
       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_resize'))
          self.assertTrue(hasattr(snake_case,      'size'))
          self.assertTrue(hasattr(snake_case,      'do_center_crop'))
          self.assertTrue(hasattr(snake_case,      'center_crop'))
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'image_mean'))
          self.assertTrue(hasattr(snake_case,      'image_std'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:List[str]):



          """simple docstring"""

          _lowercase              =self.image_processing_class.from_dict(self.image_processor_dict)
          self.assertEqual(image_processor.size,      {'height': 224, 'width': 224})
          self.assertEqual(image_processor.crop_size,      {'height': 18, 'width': 18})

          _lowercase              =self.image_processing_class.from_dict(self.image_processor_dict,      size=42,      crop_size=84)
          self.assertEqual(image_processor.size,      {'shortest_edge': 42})
          self.assertEqual(image_processor.crop_size,      {'height': 84, 'width': 84})





       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          pass





       def 				UpperCamelCase__						(				self	:Any):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =image_processing(image_inputs[0],      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )





       def 				UpperCamelCase__						(				self	:List[str]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random numpy tensors
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case,      numpify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      np.ndarray)

          # Test not batched input
          _lowercase              =image_processing(image_inputs[0],      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )





       def 				UpperCamelCase__						(				self	:Optional[int]):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PyTorch tensors
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case,      torchify=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      torch.Tensor)

          # Test not batched input
          _lowercase              =image_processing(image_inputs[0],      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  1,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  self.image_processor_tester.batch_size,
                  self.image_processor_tester.num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )






@require_torch
@require_vision
class 					SCREAMING_SNAKE_CASE_  (       _a  ,						unittest.TestCase ):
       """simple docstring"""


       __lowerCAmelCase    :		Optional[Any]					=ChineseCLIPImageProcessor if is_vision_available() else None





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          _lowercase              =ChineseCLIPImageProcessingTester(self,      num_channels=4,      do_center_crop=snake_case)
          _lowercase              =3





       @property
       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          return self.image_processor_tester.prepare_image_processor_dict()





       def 				UpperCamelCase__						(				self	:Tuple):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          self.assertTrue(hasattr(snake_case,      'do_resize'))
          self.assertTrue(hasattr(snake_case,      'size'))
          self.assertTrue(hasattr(snake_case,      'do_center_crop'))
          self.assertTrue(hasattr(snake_case,      'center_crop'))
          self.assertTrue(hasattr(snake_case,      'do_normalize'))
          self.assertTrue(hasattr(snake_case,      'image_mean'))
          self.assertTrue(hasattr(snake_case,      'image_std'))
          self.assertTrue(hasattr(snake_case,      'do_convert_rgb'))





       def 				UpperCamelCase__						(				self	:Dict):



          """simple docstring"""

          pass





       def 				UpperCamelCase__						(				self	:str):



          """simple docstring"""

          _lowercase              =self.image_processing_class(**self.image_processor_dict)
          # create random PIL images
          _lowercase              =self.image_processor_tester.prepare_inputs(equal_resolution=snake_case)
          for image in image_inputs:
             self.assertIsInstance(snake_case,      Image.Image)

          # Test not batched input
          _lowercase              =image_processing(image_inputs[0],      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  1,
                  self.expected_encoded_image_num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )

          # Test batched
          _lowercase              =image_processing(snake_case,      return_tensors='pt').pixel_values
          self.assertEqual(
              encoded_images.shape,      (
                  self.image_processor_tester.batch_size,
                  self.expected_encoded_image_num_channels,
                  self.image_processor_tester.crop_size['height'],
                  self.image_processor_tester.crop_size['width'],
              ),      )


