


import ast
import os
import re
import shutil
import tempfile
import unittest
from unittest import mock

import torch

from accelerate.test_utils.examples import compare_against_test
from accelerate.test_utils.testing import TempDirTestCase, require_trackers, run_command, slow
from accelerate.utils import write_basic_config


# DataLoaders built from `test_samples/MRPC` for quick testing
# Should mock `{script_name}.get_dataloaders` via:
# @mock.patch("{script_name}.get_dataloaders", mocked_dataloaders)

lowercase_	   =     [
    'cross_validation.py',
    'gradient_accumulation.py',
    'local_sgd.py',
    'multi_process_metrics.py',
    'memory.py',
    'automatic_gradient_accumulation.py',
    'fsdp_with_peak_mem_tracking.py',
    'deepspeed_with_config_support.py',
    'megatron_lm_gpt_pretraining.py',
]


class 	A_ (	unittest.TestCase   ):




      '''simple docstring'''


      def 					_snake_case     (    self:	Any						,				a:	str						,				a:	bool						,				a:	str = None						,				a:	list = None    ):
       __lowerCamelCase		:							Optional[Any]		 =						None
       __lowerCamelCase		:							Tuple		 =						os.path.abspath(os.path.join('examples'						,				'by_feature'    )    )
       __lowerCamelCase		:							Dict		 =						os.path.abspath('examples'    )
       for item in os.listdir(a    ):
        if item not in EXCLUDE_EXAMPLES:
         __lowerCamelCase		:							int		 =						os.path.join(a						,				a    )
         if os.path.isfile(a    ) and ".py" in item_path:
          with self.subTest(
              tested_script=a						,				feature_script=a						,				tested_section='main()' if parser_only else 'training_function()'						,				):
           __lowerCamelCase		:							str		 =						compare_against_test(
               os.path.join(a						,				a    )						,				a						,				a						,				a    )
           __lowerCamelCase		:							Tuple		 =						'\n'.join(a    )
           if special_strings is not None:
            for string in special_strings:
             __lowerCamelCase		:							Dict		 =						diff.replace(a						,				''    )
           self.assertEqual(a						,				''    )




      def 					_snake_case     (    self:	int    ):
       self.one_complete_example('complete_nlp_example.py'						,				a    )
       self.one_complete_example('complete_nlp_example.py'						,				a    )




      def 					_snake_case     (    self:	Optional[Any]    ):
       __lowerCamelCase		:							List[Any]		 =						os.path.abspath(os.path.join('examples'						,				'cv_example.py'    )    )
       __lowerCamelCase		:							Optional[int]		 =						[
           ' ' * 16 + '{\n\n',
           ' ' * 20 + '"accuracy": eval_metric["accuracy"],\n\n',
           ' ' * 20 + '"f1": eval_metric["f1"],\n\n',
           ' ' * 20 + '"train_loss": total_loss.item() / len(train_dataloader),\n\n',
           ' ' * 20 + '"epoch": epoch,\n\n',
           ' ' * 16 + '},\n\n',
           ' ' * 16 + 'step=epoch,\n',
           ' ' * 12,
           ' ' * 8 + 'for step, batch in enumerate(active_dataloader):\n',
       ]
       self.one_complete_example('complete_cv_example.py'						,				a						,				a						,				a    )
       self.one_complete_example('complete_cv_example.py'						,				a						,				a						,				a    )



@mock.patch.dict(os.environ ,				{"""TESTING_MOCKED_DATALOADERS""": """1"""}   )
class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       False




      @classmethod
      def 					_snake_case     (    cls:	List[str]    ):
       super().setUpClass()
       __lowerCamelCase		:							str		 =						tempfile.mkdtemp()
       __lowerCamelCase		:							Any		 =						os.path.join(cls._tmpdir						,				'default_config.yml'    )

       write_basic_config(save_location=cls.configPath    )
       __lowerCamelCase		:							int		 =						['accelerate', 'launch', '--config_file', cls.configPath]




      @classmethod
      def 					_snake_case     (    cls:	Optional[int]    ):
       super().tearDownClass()
       shutil.rmtree(cls._tmpdir    )




      def 					_snake_case     (    self:	Union[str, Any]    ):
       __lowerCamelCase		:							Optional[int]		 =						F'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps epoch\n        --output_dir {self.tmpdir}\n        '.split()
       run_command(self._launch_args + testargs    )
       self.assertTrue(os.path.exists(os.path.join(self.tmpdir						,				'epoch_0'    )    )    )




      def 					_snake_case     (    self:	Tuple    ):
       __lowerCamelCase		:							Optional[int]		 =						F'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
       __lowerCamelCase		:							List[str]		 =						run_command(self._launch_args + testargs    )
       self.assertTrue(os.path.exists(os.path.join(self.tmpdir						,				'step_2'    )    )    )




      def 					_snake_case     (    self:	Dict    ):
       __lowerCamelCase		:							Optional[int]		 =						F'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir						,				"epoch_0"    )}\n        '.split()
       __lowerCamelCase		:							Optional[Any]		 =						run_command(self._launch_args + testargs						,				return_stdout=a    )
       self.assertNotIn('epoch 0:'						,				a    )
       self.assertIn('epoch 1:'						,				a    )




      def 					_snake_case     (    self:	List[str]    ):
       __lowerCamelCase		:							Tuple		 =						F'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir						,				"step_2"    )}\n        '.split()
       __lowerCamelCase		:							Optional[Any]		 =						run_command(self._launch_args + testargs						,				return_stdout=a    )
       if torch.cuda.is_available():
        __lowerCamelCase		:							Union[str, Any]		 =						torch.cuda.device_count()
       else:
        __lowerCamelCase		:							str		 =						1
       if num_processes > 1:
        self.assertNotIn('epoch 0:'						,				a    )
        self.assertIn('epoch 1:'						,				a    )
       else:
        self.assertIn('epoch 0:'						,				a    )
        self.assertIn('epoch 1:'						,				a    )




      @slow
      def 					_snake_case     (    self:	Any    ):
       __lowerCamelCase		:							Optional[int]		 =						'\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        '.split()
       with mock.patch.dict(os.environ						,				{'TESTING_MOCKED_DATALOADERS': '0'}    ):
        __lowerCamelCase		:							Optional[int]		 =						run_command(self._launch_args + testargs						,				return_stdout=a    )
        __lowerCamelCase		:							Union[str, Any]		 =						re.findall('({.+})'						,				a    )
        __lowerCamelCase		:							Dict		 =						[r for r in results if 'accuracy' in r][-1]
        __lowerCamelCase		:							str		 =						ast.literal_eval(a    )
        self.assertGreaterEqual(results['accuracy']						,				0.7_5    )




      def 					_snake_case     (    self:	Optional[Any]    ):
       __lowerCamelCase		:							List[Any]		 =						['examples/by_feature/multi_process_metrics.py']
       run_command(self._launch_args + testargs    )




      @require_trackers
      @mock.patch.dict(os.environ						,				{'WANDB_MODE': 'offline'}    )
      def 					_snake_case     (    self:	Tuple    ):
       with tempfile.TemporaryDirectory() as tmpdir:
        __lowerCamelCase		:							Union[str, Any]		 =						F'\n            examples/by_feature/tracking.py\n            --with_tracking\n            --project_dir {tmpdir}\n            '.split()
        run_command(self._launch_args + testargs    )
        self.assertTrue(os.path.exists(os.path.join(a						,				'tracking'    )    )    )




      def 					_snake_case     (    self:	Any    ):
       __lowerCamelCase		:							Optional[Any]		 =						['examples/by_feature/gradient_accumulation.py']
       run_command(self._launch_args + testargs    )




      def 					_snake_case     (    self:	Union[str, Any]    ):
       __lowerCamelCase		:							str		 =						['examples/by_feature/local_sgd.py']
       run_command(self._launch_args + testargs    )





import copy
import os
from collections import OrderedDict
from typing import TYPE_CHECKING, Any, Dict, Mapping, Optional, Union


if TYPE_CHECKING:
       from ...processing_utils import ProcessorMixin
       from ...utils import TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowercase_	   =     logging.get_logger(__name__)

lowercase_	   =     {
    'google/owlvit-base-patch32': 'https://huggingface.co/google/owlvit-base-patch32/resolve/main/config.json',
    'google/owlvit-base-patch16': 'https://huggingface.co/google/owlvit-base-patch16/resolve/main/config.json',
    'google/owlvit-large-patch14': 'https://huggingface.co/google/owlvit-large-patch14/resolve/main/config.json',
}


class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit_text_model"""




      def __init__(    self:	Optional[int]						,				a:	Dict=4_9408						,				a:	Optional[Any]=512						,				a:	Dict=2048						,				a:	Optional[Any]=12						,				a:	Tuple=8						,				a:	Union[str, Any]=16						,				a:	str="quick_gelu"						,				a:	List[Any]=1e-5						,				a:	Dict=0.0						,				a:	Optional[int]=0.0_2						,				a:	Dict=1.0						,				a:	Any=0						,				a:	Union[str, Any]=4_9406						,				a:	Any=4_9407						,				**a:	Dict						,				):
       super().__init__(pad_token_id=a						,				bos_token_id=a						,				eos_token_id=a						,				**a    )

       __lowerCamelCase		:							List[Any]		 =						vocab_size
       __lowerCamelCase		:							int		 =						hidden_size
       __lowerCamelCase		:							Optional[Any]		 =						intermediate_size
       __lowerCamelCase		:							List[Any]		 =						num_hidden_layers
       __lowerCamelCase		:							Any		 =						num_attention_heads
       __lowerCamelCase		:							Union[str, Any]		 =						max_position_embeddings
       __lowerCamelCase		:							List[Any]		 =						hidden_act
       __lowerCamelCase		:							List[str]		 =						layer_norm_eps
       __lowerCamelCase		:							Tuple		 =						attention_dropout
       __lowerCamelCase		:							Optional[int]		 =						initializer_range
       __lowerCamelCase		:							Tuple		 =						initializer_factor




      @classmethod
      def 					_snake_case     (    cls:	Dict						,				a:	Union[str, os.PathLike]						,				**a:	Optional[int]    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						cls.get_config_dict(a						,				**a    )

       # get the text config dict if we are loading from OwlViTConfig
       if config_dict.get('model_type'    ) == "owlvit":
        __lowerCamelCase		:							Optional[Any]		 =						config_dict['text_config']

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit_vision_model"""




      def __init__(    self:	int						,				a:	Tuple=768						,				a:	int=3072						,				a:	List[str]=12						,				a:	Optional[Any]=12						,				a:	Optional[int]=3						,				a:	Optional[int]=768						,				a:	Optional[Any]=32						,				a:	Optional[int]="quick_gelu"						,				a:	Union[str, Any]=1e-5						,				a:	Union[str, Any]=0.0						,				a:	Union[str, Any]=0.0_2						,				a:	int=1.0						,				**a:	Union[str, Any]						,				):
       super().__init__(**a    )

       __lowerCamelCase		:							str		 =						hidden_size
       __lowerCamelCase		:							Tuple		 =						intermediate_size
       __lowerCamelCase		:							Dict		 =						num_hidden_layers
       __lowerCamelCase		:							Optional[Any]		 =						num_attention_heads
       __lowerCamelCase		:							int		 =						num_channels
       __lowerCamelCase		:							Optional[Any]		 =						image_size
       __lowerCamelCase		:							Tuple		 =						patch_size
       __lowerCamelCase		:							List[str]		 =						hidden_act
       __lowerCamelCase		:							Tuple		 =						layer_norm_eps
       __lowerCamelCase		:							List[Any]		 =						attention_dropout
       __lowerCamelCase		:							Tuple		 =						initializer_range
       __lowerCamelCase		:							List[Any]		 =						initializer_factor




      @classmethod
      def 					_snake_case     (    cls:	Optional[int]						,				a:	Union[str, os.PathLike]						,				**a:	int    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							Dict		 =						cls.get_config_dict(a						,				**a    )

       # get the vision config dict if we are loading from OwlViTConfig
       if config_dict.get('model_type'    ) == "owlvit":
        __lowerCamelCase		:							Any		 =						config_dict['vision_config']

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      __snake_case 	=       """owlvit"""
      __snake_case 	=       True




      def __init__(    self:	Dict						,				a:	int=None						,				a:	str=None						,				a:	Tuple=512						,				a:	Tuple=2.6_5_9_2						,				a:	int=True						,				**a:	int						,				):
       super().__init__(**a    )

       if text_config is None:
        __lowerCamelCase		:							List[str]		 =						{}
        logger.info('text_config is None. Initializing the OwlViTTextConfig with default values.'    )

       if vision_config is None:
        __lowerCamelCase		:							str		 =						{}
        logger.info('vision_config is None. initializing the OwlViTVisionConfig with default values.'    )

       __lowerCamelCase		:							List[Any]		 =						OwlViTTextConfig(**a    )
       __lowerCamelCase		:							str		 =						OwlViTVisionConfig(**a    )

       __lowerCamelCase		:							Union[str, Any]		 =						projection_dim
       __lowerCamelCase		:							Tuple		 =						logit_scale_init_value
       __lowerCamelCase		:							Dict		 =						return_dict
       __lowerCamelCase		:							Tuple		 =						1.0




      @classmethod
      def 					_snake_case     (    cls:	str						,				a:	Union[str, os.PathLike]						,				**a:	List[Any]    ):
       cls._set_token_in_kwargs(a    )

       __lowerCamelCase ,					__lowerCamelCase		:							List[Any]		 =						cls.get_config_dict(a						,				**a    )

       if "model_type" in config_dict and hasattr(cls						,				'model_type'    ) and config_dict["model_type"] != cls.model_type:
        logger.warning(
            F'You are using a model of type {config_dict["model_type"]} to instantiate a model of type '
            F'{cls.model_type}. This is not supported for all configurations of models and can yield errors.'    )

       return cls.from_dict(a						,				**a    )




      @classmethod
      def 					_snake_case     (    cls:	Tuple						,				a:	Dict						,				a:	Dict						,				**a:	str    ):
       __lowerCamelCase		:							List[str]		 =						{}
       __lowerCamelCase		:							List[str]		 =						text_config
       __lowerCamelCase		:							Optional[int]		 =						vision_config

       return cls.from_dict(a						,				**a    )




      def 					_snake_case     (    self:	Optional[int]    ):
       __lowerCamelCase		:							Optional[Any]		 =						copy.deepcopy(self.__dict__    )
       __lowerCamelCase		:							List[Any]		 =						self.text_config.to_dict()
       __lowerCamelCase		:							List[str]		 =						self.vision_config.to_dict()
       __lowerCamelCase		:							Optional[Any]		 =						self.__class__.model_type
       return output



class 	A_ (	__UpperCamelCase   ):




      '''simple docstring'''


      @property
      def 					_snake_case     (    self:	str    ):
       return OrderedDict(
           [
               ('input_ids', {0: 'batch', 1: 'sequence'}),
               ('pixel_values', {0: 'batch', 1: 'num_channels', 2: 'height', 3: 'width'}),
               ('attention_mask', {0: 'batch', 1: 'sequence'}),
           ]    )




      @property
      def 					_snake_case     (    self:	Dict    ):
       return OrderedDict(
           [
               ('logits_per_image', {0: 'batch'}),
               ('logits_per_text', {0: 'batch'}),
               ('text_embeds', {0: 'batch'}),
               ('image_embeds', {0: 'batch'}),
           ]    )




      @property
      def 					_snake_case     (    self:	int    ):
       return 1e-4




      def 					_snake_case     (    self:	Any						,				a:	"ProcessorMixin"						,				a:	int = -1						,				a:	int = -1						,				a:	Optional["TensorType"] = None						,				):
       __lowerCamelCase		:							List[str]		 =						super().generate_dummy_inputs(
           processor.tokenizer						,				batch_size=a						,				seq_length=a						,				framework=a    )
       __lowerCamelCase		:							int		 =						super().generate_dummy_inputs(
           processor.image_processor						,				batch_size=a						,				framework=a    )
       return {**text_input_dict, **image_input_dict}




      @property
      def 					_snake_case     (    self:	int    ):
       return 14

