






'''simple docstring'''
from __future__ import annotations

import inspect
import unittest

import numpy as np

from transformers import DeiTConfig
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 (
     TFDeiTForImageClassification,
     TFDeiTForImageClassificationWithTeacher,
     TFDeiTForMaskedImageModeling,
     TFDeiTModel,
 )
 from transformers.models.deit.modeling_tf_deit import TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
 from PIL import Image

 from transformers import DeiTImageProcessor


class 							_UpperCAmelCase    :




    def __init__(      self	:							int	,  a	:							Dict	,  a	:							Optional[Any]=1_3	,  a	:							str=3_0	,  a	:							Dict=2	,  a	:							List[Any]=3	,  a	:							Optional[int]=True	,  a	:							Tuple=True	,  a	:							List[Any]=3_2	,  a	:							int=2	,  a	:							List[str]=4	,  a	:							Dict=3_7	,  a	:							Optional[Any]="gelu"	,  a	:							List[str]=0.1	,  a	:							Dict=0.1	,  a	:							List[Any]=1_0	,  a	:							Union[str, Any]=0.02	,  a	:							Union[str, Any]=3	,  a	:							int=None	,  a	:							Any=2	,  ):






       '''simple docstring'''






       lowercase_						:     Any							     =  parent
       lowercase_						:     Union[str, Any]							     =  batch_size
       lowercase_						:     Any							     =  image_size
       lowercase_						:     str							     =  patch_size
       lowercase_						:     int							     =  num_channels
       lowercase_						:     str							     =  is_training
       lowercase_						:     str							     =  use_labels
       lowercase_						:     List[Any]							     =  hidden_size
       lowercase_						:     List[str]							     =  num_hidden_layers
       lowercase_						:     Dict							     =  num_attention_heads
       lowercase_						:     Optional[int]							     =  intermediate_size
       lowercase_						:     str							     =  hidden_act
       lowercase_						:     Union[str, Any]							     =  hidden_dropout_prob
       lowercase_						:     List[str]							     =  attention_probs_dropout_prob
       lowercase_						:     List[str]							     =  type_sequence_label_size
       lowercase_						:     List[Any]							     =  initializer_range
       lowercase_						:     str							     =  scope
       lowercase_						:     Tuple							     =  encoder_stride

       # in DeiT, the seq length equals the number of patches + 2 (we add 2 for the [CLS] and distilation tokens)
       lowercase_						:     Any							     =  (image_size // patch_size) ** 2
       lowercase_						:     str							     =  num_patches + 2




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

       lowercase_						:     List[Any]							     =  None
       if self.use_labels:
          lowercase_						:     int							     =  ids_tensor([self.batch_size]	,  self.type_sequence_label_size      )

       lowercase_						:     Optional[Any]							     =  self.get_config()

       return config, pixel_values, labels




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       return DeiTConfig(
           image_size=self.image_size	,  patch_size=self.patch_size	,  num_channels=self.num_channels	,  hidden_size=self.hidden_size	,  num_hidden_layers=self.num_hidden_layers	,  num_attention_heads=self.num_attention_heads	,  intermediate_size=self.intermediate_size	,  hidden_act=self.hidden_act	,  hidden_dropout_prob=self.hidden_dropout_prob	,  attention_probs_dropout_prob=self.attention_probs_dropout_prob	,  is_decoder=a	,  initializer_range=self.initializer_range	,  encoder_stride=self.encoder_stride	,  )




    def 					lowerCAmelCase__					(      self	:							Any	,  a	:							List[str]	,  a	:							str	,  a	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  TFDeiTModel(config=a      )
       lowercase_						:     int							     =  model(a      )
       self.parent.assertEqual(result.last_hidden_state.shape	,  (self.batch_size, self.seq_length, self.hidden_size)      )




    def 					lowerCAmelCase__					(      self	:							Dict	,  a	:							Optional[int]	,  a	:							List[Any]	,  a	:							Union[str, Any]      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  TFDeiTForMaskedImageModeling(config=a      )
       lowercase_						:     Union[str, Any]							     =  model(a      )
       self.parent.assertEqual(
           result.reconstruction.shape	,  (self.batch_size, self.num_channels, self.image_size, self.image_size)      )

       # test greyscale images
       lowercase_						:     Dict							     =  1
       lowercase_						:     Tuple							     =  TFDeiTForMaskedImageModeling(a      )

       lowercase_						:     Tuple							     =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       lowercase_						:     Dict							     =  model(a      )
       self.parent.assertEqual(result.reconstruction.shape	,  (self.batch_size, 1, self.image_size, self.image_size)      )




    def 					lowerCAmelCase__					(      self	:							int	,  a	:							List[str]	,  a	:							List[str]	,  a	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     str							     =  self.type_sequence_label_size
       lowercase_						:     Union[str, Any]							     =  TFDeiTForImageClassification(a      )
       lowercase_						:     int							     =  model(a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)      )

       # test greyscale images
       lowercase_						:     Dict							     =  1
       lowercase_						:     str							     =  TFDeiTForImageClassification(a      )

       lowercase_						:     Tuple							     =  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]      )
       lowercase_						:     Any							     =  model(a	,  labels=a      )
       self.parent.assertEqual(result.logits.shape	,  (self.batch_size, self.type_sequence_label_size)      )





    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






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


@require_tf
class 							_UpperCAmelCase    (		snake_case					,       snake_case					,       unittest.TestCase      ):
    __lowerCamelCase: Tuple									=     (
        (
            TFDeiTModel,
            TFDeiTForImageClassification,
            TFDeiTForImageClassificationWithTeacher,
            TFDeiTForMaskedImageModeling,
        )
        if is_tf_available()
        else ()
    )
    __lowerCamelCase: Tuple									=     (
        {
            'feature-extraction': TFDeiTModel,
            'image-classification': (TFDeiTForImageClassification, TFDeiTForImageClassificationWithTeacher),
        }
        if is_tf_available()
        else {}
    )

    __lowerCamelCase: Union[str, Any]									=     False
    __lowerCamelCase: Any									=     False
    __lowerCamelCase: List[str]									=     False
    __lowerCamelCase: Optional[Any]									=     False




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     int							     =  TFDeiTModelTester(self      )
       lowercase_						:     Any							     =  ConfigTester(self	,  config_class=a	,  has_text_modality=a	,  hidden_size=3_7      )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       self.config_tester.run_common_tests()




    @unittest.skip(reason="DeiT does not use inputs_embeds"      )
    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       pass




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Tuple							     =  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          lowercase_						:     Dict							     =  model_class(a      )
          self.assertIsInstance(model.get_input_embeddings()	,  (tf.keras.layers.Layer)      )
          lowercase_						:     Optional[int]							     =  model.get_output_embeddings()
          self.assertTrue(x is None or isinstance(a	,  tf.keras.layers.Dense      )      )




    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_     ,		lowercase_						:     Union[str, Any]							     =  self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
          lowercase_						:     Any							     =  model_class(a      )
          lowercase_						:     str							     =  inspect.signature(model.call      )
          # signature.parameters is an OrderedDict => so arg_names order is deterministic
          lowercase_						:     int							     =  [*signature.parameters.keys()]

          lowercase_						:     Dict							     =  ["pixel_values"]
          self.assertListEqual(arg_names[:1]	,  a      )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*a      )




    def 					lowerCAmelCase__					(      self	:							str      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_image_modeling(*a      )




    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     int							     =  self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*a      )




    def 					lowerCAmelCase__					(      self	:							Any	,  a	:							int	,  a	:							Union[str, Any]	,  a	:							Any=False      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  super()._prepare_for_class(a	,  a	,  return_labels=a      )

       if return_labels:
          if "labels" in inputs_dict and "labels" not in inspect.signature(model_class.call      ).parameters:
             del inputs_dict["labels"]

       return inputs_dict





    @slow
    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       for model_name in TF_DEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
          lowercase_						:     List[Any]							     =  TFDeiTModel.from_pretrained(a      )
          self.assertIsNotNone(a      )




def 		__SCREAMING_SNAKE_CASE       (	):


   """simple docstring"""




   lowercase_						:     str							     =  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
   return image





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




    @cached_property
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       return (
           DeiTImageProcessor.from_pretrained("facebook/deit-base-distilled-patch16-224"      )
           if is_vision_available()
           else None
       )





    @slow
    def 					lowerCAmelCase__					(      self	:							List[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  TFDeiTForImageClassificationWithTeacher.from_pretrained("facebook/deit-base-distilled-patch16-224"      )

       lowercase_						:     Union[str, Any]							     =  self.default_image_processor
       lowercase_						:     Tuple							     =  prepare_img()
       lowercase_						:     Optional[Any]							     =  image_processor(images=a	,  return_tensors="tf"      )

       # forward pass
       lowercase_						:     int							     =  model(**a      )

       # verify the logits
       lowercase_						:     Union[str, Any]							     =  tf.TensorShape((1, 1_0_0_0)      )
       self.assertEqual(outputs.logits.shape	,  a      )

       lowercase_						:     Optional[Any]							     =  tf.constant([-1.0266, 0.1912, -1.2861]      )

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












'''simple docstring'''
import inspect
import os
import unittest
from pathlib import Path

import torch

import accelerate
from accelerate.test_utils import execute_subprocess_async
from accelerate.test_utils.testing import run_command


class 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: List[Any]									=     inspect.getfile(accelerate.test_utils      )
    __lowerCamelCase: List[Any]									=     os.path.sep.join(mod_file.split(os.path.sep      )[:-1] + ['scripts', 'test_cli.py']      )

    __lowerCamelCase: Optional[Any]									=     ['accelerate', 'launch']
    __lowerCamelCase: List[str]									=     Path.home() / '.cache/huggingface/accelerate'
    __lowerCamelCase: Dict									=     'default_config.yaml'
    __lowerCamelCase: Union[str, Any]									=     config_folder / config_file
    __lowerCamelCase: Tuple									=     config_folder / '_default_config.yaml'

    __lowerCamelCase: Union[str, Any]									=     Path('tests/test_configs'      )




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							List[Any]      ):






       '''simple docstring'''






       if cls.config_path.is_file():
          cls.config_path.rename(cls.changed_path      )




    @classmethod
    def 					lowerCAmelCase__					(      cls	:							str      ):






       '''simple docstring'''






       if cls.changed_path.is_file():
          cls.changed_path.rename(cls.config_path      )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[str]							     =  self.base_cmd
       if torch.cuda.is_available() and (torch.cuda.device_count() > 1):
          cmd += ["--multi_gpu"]
       execute_subprocess_async(cmd + [self.test_file_path]	,  env=os.environ.copy()      )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       for config in sorted(self.test_config_path.glob("**/*.yaml"      )      ):
          with self.subTest(config_file=a      ):
             execute_subprocess_async(
                 self.base_cmd + ["--config_file", str(a      ), self.test_file_path]	,  env=os.environ.copy()      )





    def 					lowerCAmelCase__					(      self	:							List[str]      ):






       '''simple docstring'''






       execute_subprocess_async(["accelerate", "test"]	,  env=os.environ.copy()      )


class 							_UpperCAmelCase    (		unittest.TestCase      ):
    __lowerCamelCase: Tuple									=     'test-tpu'
    __lowerCamelCase: Dict									=     'us-central1-a'
    __lowerCamelCase: List[str]									=     'ls'
    __lowerCamelCase: Optional[int]									=     ['accelerate', 'tpu-config']
    __lowerCamelCase: Optional[Any]									=     'cd /usr/share'
    __lowerCamelCase: Any									=     'tests/test_samples/test_command_file.sh'
    __lowerCamelCase: Tuple									=     'Running gcloud compute tpus tpu-vm ssh'




    def 					lowerCAmelCase__					(      self	:							Tuple      ):






       '''simple docstring'''






       lowercase_						:     str							     =  run_command(
           self.cmd
           + ["--command", self.command, "--tpu_zone", self.tpu_zone, "--tpu_name", self.tpu_name, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[int]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/0_12_0.yaml",
               "--command",
               self.command,
               "--tpu_zone",
               self.tpu_zone,
               "--tpu_name",
               self.tpu_name,
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							int      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--debug"]	,  return_stdout=a      )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Any							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--command", self.command, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Optional[Any]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/latest.yaml",
               "--command",
               self.command,
               "--command",
               "echo \"Hello World\"",
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; ls; echo \"Hello World\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Any      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + ["--config_file", "tests/test_configs/latest.yaml", "--command_file", self.command_file, "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     List[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/0_12_0.yaml",
               "--command_file",
               self.command_file,
               "--tpu_zone",
               self.tpu_zone,
               "--tpu_name",
               self.tpu_name,
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




    def 					lowerCAmelCase__					(      self	:							Dict      ):






       '''simple docstring'''






       lowercase_						:     Union[str, Any]							     =  run_command(
           self.cmd + ["--config_file", "tests/test_configs/latest.yaml", "--install_accelerate", "--debug"]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate -U; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )





    def 					lowerCAmelCase__					(      self	:							Optional[int]      ):






       '''simple docstring'''






       lowercase_						:     Optional[Any]							     =  run_command(
           self.cmd
           + [
               "--config_file",
               "tests/test_configs/latest.yaml",
               "--install_accelerate",
               "--accelerate_version",
               "12.0.0",
               "--debug",
           ]	,  return_stdout=a	,  )
       self.assertIn(
           f"""{self.gcloud} test-tpu --zone us-central1-a --command {self.base_output}; pip install accelerate==12.0.0; echo \"hello world\"; echo \"this is a second command\" --worker all"""	,  a	,  )




