





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)

_UpperCAmelCase        =				[
    '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 							_UpperCamelCase					(	unittest.TestCase     ):

       def lowercase (		self: List[str]					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: Optional[int]					,   _SCREAMING_SNAKE_CASE: List[str] = None					,   _SCREAMING_SNAKE_CASE: Optional[int] = None		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  None
              UpperCamelCase_          =  os.path.abspath(os.path.join("examples"					,   "by_feature"		)		)
              UpperCamelCase_          =  os.path.abspath("examples"		)
              for item in os.listdir(_SCREAMING_SNAKE_CASE		):
                     if item not in EXCLUDE_EXAMPLES:
                            UpperCamelCase_          =  os.path.join(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)
                            if os.path.isfile(_SCREAMING_SNAKE_CASE		) and ".py" in item_path:
                                   with self.subTest(
                                       tested_script=_SCREAMING_SNAKE_CASE					,   feature_script=_SCREAMING_SNAKE_CASE					,   tested_section="main()" if parser_only else "training_function()"					,   ):
                                          UpperCamelCase_          =  compare_against_test(
                                              os.path.join(_SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)
                                          UpperCamelCase_          =  '''\n'''.join(_SCREAMING_SNAKE_CASE		)
                                          if special_strings is not None:
                                                 for string in special_strings:
                                                        UpperCamelCase_          =  diff.replace(_SCREAMING_SNAKE_CASE					,   ""		)
                                          self.assertEqual(_SCREAMING_SNAKE_CASE					,   ""		)

       def lowercase (		self: Dict		)       -> Optional[Any]:




              """simple docstring"""


              self.one_complete_example("complete_nlp_example.py"					,   _SCREAMING_SNAKE_CASE		)
              self.one_complete_example("complete_nlp_example.py"					,   _SCREAMING_SNAKE_CASE		)



       def lowercase (		self: int		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  os.path.abspath(os.path.join("examples"					,   "cv_example.py"		)		)
              UpperCamelCase_          =  [
                  ''' ''' * 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"					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)
              self.one_complete_example("complete_cv_example.py"					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE					,   _SCREAMING_SNAKE_CASE		)



@mock.patch.dict(os.environ  ,						{'''TESTING_MOCKED_DATALOADERS''': '''1'''}     )
class 							_UpperCamelCase					(	UpperCamelCase_     ):
       _UpperCamelCase				:					Optional[int]         =   False

       @classmethod
       def lowercase (		cls: Optional[Any]		)       -> List[Any]:




              """simple docstring"""


              super().setUpClass()
              UpperCamelCase_          =  tempfile.mkdtemp()
              UpperCamelCase_          =  os.path.join(cls._tmpdir					,   "default_config.yml"		)

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

       @classmethod
       def lowercase (		cls: List[Any]		)       -> List[Any]:




              """simple docstring"""


              super().tearDownClass()
              shutil.rmtree(cls._tmpdir		)

       def lowercase (		self: Optional[int]		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  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 lowercase (		self: Any		)       -> Dict:




              """simple docstring"""


              UpperCamelCase_          =  f'''\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '''.split()
              UpperCamelCase_          =  run_command(self._launch_args + testargs		)
              self.assertTrue(os.path.exists(os.path.join(self.tmpdir					,   "step_2"		)		)		)

       def lowercase (		self: int		)       -> Dict:




              """simple docstring"""


              UpperCamelCase_          =  f'''\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir					,   "epoch_0"		)}\n        '''.split()
              UpperCamelCase_          =  run_command(self._launch_args + testargs					,   return_stdout=_SCREAMING_SNAKE_CASE		)
              self.assertNotIn("epoch 0:"					,   _SCREAMING_SNAKE_CASE		)
              self.assertIn("epoch 1:"					,   _SCREAMING_SNAKE_CASE		)

       def lowercase (		self: str		)       -> List[Any]:




              """simple docstring"""


              UpperCamelCase_          =  f'''\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir					,   "step_2"		)}\n        '''.split()
              UpperCamelCase_          =  run_command(self._launch_args + testargs					,   return_stdout=_SCREAMING_SNAKE_CASE		)
              if torch.cuda.is_available():
                     UpperCamelCase_          =  torch.cuda.device_count()
              else:
                     UpperCamelCase_          =  1
              if num_processes > 1:
                     self.assertNotIn("epoch 0:"					,   _SCREAMING_SNAKE_CASE		)
                     self.assertIn("epoch 1:"					,   _SCREAMING_SNAKE_CASE		)
              else:
                     self.assertIn("epoch 0:"					,   _SCREAMING_SNAKE_CASE		)
                     self.assertIn("epoch 1:"					,   _SCREAMING_SNAKE_CASE		)

       @slow
       def lowercase (		self: Tuple		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  '''
        examples/by_feature/cross_validation.py
        --num_folds 2
        '''.split()
              with mock.patch.dict(os.environ					,   {"TESTING_MOCKED_DATALOADERS": "0"}		):
                     UpperCamelCase_          =  run_command(self._launch_args + testargs					,   return_stdout=_SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  re.findall("({.+})"					,   _SCREAMING_SNAKE_CASE		)
                     UpperCamelCase_          =  [r for r in results if '''accuracy''' in r][-1]
                     UpperCamelCase_          =  ast.literal_eval(_SCREAMING_SNAKE_CASE		)
                     self.assertGreaterEqual(results["accuracy"]					,   0.75		)

       def lowercase (		self: List[str]		)       -> List[Any]:




              """simple docstring"""


              UpperCamelCase_          =  ['''examples/by_feature/multi_process_metrics.py''']
              run_command(self._launch_args + testargs		)

       @require_trackers
       @mock.patch.dict(os.environ					,   {"WANDB_MODE": "offline"}		)
       def lowercase (		self: Dict		)       -> int:




              """simple docstring"""


              with tempfile.TemporaryDirectory() as tmpdir:
                     UpperCamelCase_          =  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(_SCREAMING_SNAKE_CASE					,   "tracking"		)		)		)

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




              """simple docstring"""


              UpperCamelCase_          =  ['''examples/by_feature/gradient_accumulation.py''']
              run_command(self._launch_args + testargs		)



       def lowercase (		self: int		)       -> List[Any]:




              """simple docstring"""


              UpperCamelCase_          =  ['''examples/by_feature/local_sgd.py''']
              run_command(self._launch_args + testargs		)








import unittest

from transformers import MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING, is_vision_available, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
     from PIL import Image
else:



     class 							_UpperCamelCase					:

            @staticmethod
            def lowercase (		*_SCREAMING_SNAKE_CASE: Optional[int]					,   **_SCREAMING_SNAKE_CASE: Dict		)       -> Union[str, Any]:




                   """simple docstring"""


                   pass



@is_pipeline_test
@require_vision
@require_torch
class 							_UpperCamelCase					(	unittest.TestCase     ):
       _UpperCamelCase				:					str         =   MODEL_FOR_ZERO_SHOT_OBJECT_DETECTION_MAPPING

       def lowercase (		self: int					,   _SCREAMING_SNAKE_CASE: str					,   _SCREAMING_SNAKE_CASE: int					,   _SCREAMING_SNAKE_CASE: Optional[Any]		)       -> Any:




              """simple docstring"""


              UpperCamelCase_          =  pipeline(
                  "zero-shot-object-detection"					,   model="hf-internal-testing/tiny-random-owlvit-object-detection"		)

              UpperCamelCase_          =  [
                  {
                      "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                      "candidate_labels": ["cat", "remote", "couch"],
                  }
              ]
              return object_detector, examples

       def lowercase (		self: Optional[Any]					,   _SCREAMING_SNAKE_CASE: Tuple					,   _SCREAMING_SNAKE_CASE: Tuple		)       -> Optional[int]:




              """simple docstring"""


              UpperCamelCase_          =  object_detector(examples[0]					,   threshold=0.0		)

              UpperCamelCase_          =  len(_SCREAMING_SNAKE_CASE		)
              self.assertGreater(_SCREAMING_SNAKE_CASE					,   0		)
              self.assertEqual(
                  _SCREAMING_SNAKE_CASE					,   [
                      {
                          "score": ANY(_SCREAMING_SNAKE_CASE		),
                          "label": ANY(_SCREAMING_SNAKE_CASE		),
                          "box": {"xmin": ANY(_SCREAMING_SNAKE_CASE		), "ymin": ANY(_SCREAMING_SNAKE_CASE		), "xmax": ANY(_SCREAMING_SNAKE_CASE		), "ymax": ANY(_SCREAMING_SNAKE_CASE		)},
                      }
                      for i in range(_SCREAMING_SNAKE_CASE		)
                  ]					,   )

       @require_tf
       @unittest.skip("Zero Shot Object Detection not implemented in TF"		)
       def lowercase (		self: Tuple		)       -> List[str]:




              """simple docstring"""


              pass

       @require_torch
       def lowercase (		self: Union[str, Any]		)       -> str:




              """simple docstring"""


              UpperCamelCase_          =  pipeline(
                  "zero-shot-object-detection"					,   model="hf-internal-testing/tiny-random-owlvit-object-detection"		)

              UpperCamelCase_          =  object_detector(
                  "./tests/fixtures/tests_samples/COCO/000000039769.png"					,   candidate_labels=["cat", "remote", "couch"]					,   threshold=0.64					,   )

              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE					,   decimals=4		)					,   [
                      {"score": 0.72_35, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                      {"score": 0.72_18, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                      {"score": 0.71_84, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                      {"score": 0.67_48, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                      {"score": 0.66_56, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                      {"score": 0.66_14, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                      {"score": 0.64_56, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                      {"score": 0.6_42, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
                      {"score": 0.64_19, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                  ]					,   )

              UpperCamelCase_          =  object_detector(
                  [
                      {
                          "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
                          "candidate_labels": ["cat", "remote", "couch"],
                      }
                  ]					,   threshold=0.64					,   )

              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE					,   decimals=4		)					,   [
                      [
                          {"score": 0.72_35, "label": "cat", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                          {"score": 0.72_18, "label": "remote", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                          {"score": 0.71_84, "label": "couch", "box": {"xmin": 204, "ymin": 167, "xmax": 232, "ymax": 190}},
                          {"score": 0.67_48, "label": "remote", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                          {"score": 0.66_56, "label": "cat", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                          {"score": 0.66_14, "label": "couch", "box": {"xmin": 571, "ymin": 83, "xmax": 598, "ymax": 103}},
                          {"score": 0.64_56, "label": "remote", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                          {"score": 0.6_42, "label": "remote", "box": {"xmin": 67, "ymin": 274, "xmax": 93, "ymax": 297}},
                          {"score": 0.64_19, "label": "cat", "box": {"xmin": 494, "ymin": 105, "xmax": 521, "ymax": 127}},
                      ]
                  ]					,   )

       @require_torch
       @slow
       def lowercase (		self: List[str]		)       -> List[str]:




              """simple docstring"""


              UpperCamelCase_          =  pipeline("zero-shot-object-detection"		)

              UpperCamelCase_          =  object_detector(
                  "http://images.cocodataset.org/val2017/000000039769.jpg"					,   candidate_labels=["cat", "remote", "couch"]					,   )
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE					,   decimals=4		)					,   [
                      {"score": 0.28_68, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                      {"score": 0.2_77, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                      {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                      {"score": 0.14_74, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                      {"score": 0.12_08, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
                  ]					,   )

              UpperCamelCase_          =  object_detector(
                  [
                      {
                          "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                          "candidate_labels": ["cat", "remote", "couch"],
                      },
                      {
                          "image": "http://images.cocodataset.org/val2017/000000039769.jpg",
                          "candidate_labels": ["cat", "remote", "couch"],
                      },
                  ]					,   )
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE					,   decimals=4		)					,   [
                      [
                          {"score": 0.28_68, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                          {"score": 0.2_77, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                          {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                          {"score": 0.14_74, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                          {"score": 0.12_08, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
                      ],
                      [
                          {"score": 0.28_68, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                          {"score": 0.2_77, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                          {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                          {"score": 0.14_74, "label": "remote", "box": {"xmin": 335, "ymin": 74, "xmax": 371, "ymax": 187}},
                          {"score": 0.12_08, "label": "couch", "box": {"xmin": 4, "ymin": 0, "xmax": 642, "ymax": 476}},
                      ],
                  ]					,   )

       @require_tf
       @unittest.skip("Zero Shot Object Detection not implemented in TF"		)
       def lowercase (		self: List[Any]		)       -> str:




              """simple docstring"""


              pass

       @require_torch
       @slow
       def lowercase (		self: Any		)       -> int:




              """simple docstring"""


              UpperCamelCase_          =  0.2
              UpperCamelCase_          =  pipeline("zero-shot-object-detection"		)

              UpperCamelCase_          =  object_detector(
                  "http://images.cocodataset.org/val2017/000000039769.jpg"					,   candidate_labels=["cat", "remote", "couch"]					,   threshold=_SCREAMING_SNAKE_CASE					,   )
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE					,   decimals=4		)					,   [
                      {"score": 0.28_68, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                      {"score": 0.2_77, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                      {"score": 0.25_37, "label": "cat", "box": {"xmin": 1, "ymin": 55, "xmax": 315, "ymax": 472}},
                  ]					,   )



       @require_torch
       @slow
       def lowercase (		self: Dict		)       -> Tuple:




              """simple docstring"""


              UpperCamelCase_          =  2
              UpperCamelCase_          =  pipeline("zero-shot-object-detection"		)

              UpperCamelCase_          =  object_detector(
                  "http://images.cocodataset.org/val2017/000000039769.jpg"					,   candidate_labels=["cat", "remote", "couch"]					,   top_k=_SCREAMING_SNAKE_CASE					,   )
              self.assertEqual(
                  nested_simplify(_SCREAMING_SNAKE_CASE					,   decimals=4		)					,   [
                      {"score": 0.28_68, "label": "cat", "box": {"xmin": 324, "ymin": 20, "xmax": 640, "ymax": 373}},
                      {"score": 0.2_77, "label": "remote", "box": {"xmin": 40, "ymin": 72, "xmax": 177, "ymax": 115}},
                  ]					,   )

