




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)

SCREAMING_SNAKE_CASE__       :      List[str]  =   [
    '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     ):

    '''simple docstring'''



    def     _lowercase     (    self     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__ = None     ,  UpperCamelCase__ = None     )     ->      str:
           lowerCamelCase   :      Tuple			 =					None
           lowerCamelCase   :      str			 =					os.path.abspath(os.path.join("examples"     ,  "by_feature"     )     )
           lowerCamelCase   :      str			 =					os.path.abspath("examples"     )
           for item in os.listdir(UpperCamelCase__     ):
                  if item not in EXCLUDE_EXAMPLES:
                         lowerCamelCase   :      List[str]			 =					os.path.join(UpperCamelCase__     ,  UpperCamelCase__     )
                         if os.path.isfile(UpperCamelCase__     ) and ".py" in item_path:
                                with self.subTest(
                                    tested_script=UpperCamelCase__     ,  feature_script=UpperCamelCase__     ,  tested_section="main()" if parser_only else "training_function()"     ,  ):
                                       lowerCamelCase   :      Any			 =					compare_against_test(
                                           os.path.join(UpperCamelCase__     ,  UpperCamelCase__     )     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )
                                       lowerCamelCase   :      Dict			 =					"\n".join(UpperCamelCase__     )
                                       if special_strings is not None:
                                              for string in special_strings:
                                                     lowerCamelCase   :      Tuple			 =					diff.replace(UpperCamelCase__     ,  ""     )
                                       self.assertEqual(UpperCamelCase__     ,  ""     )







    def     _lowercase     (    self     )     ->      List[Any]:
           self.one_complete_example("complete_nlp_example.py"     ,  UpperCamelCase__     )
           self.one_complete_example("complete_nlp_example.py"     ,  UpperCamelCase__     )







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      Optional[Any]			 =					os.path.abspath(os.path.join("examples"     ,  "cv_example.py"     )     )
           lowerCamelCase   :      Tuple			 =					[
               " " * 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"     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )
           self.one_complete_example("complete_cv_example.py"     ,  UpperCamelCase__     ,  UpperCamelCase__     ,  UpperCamelCase__     )




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

    '''simple docstring'''



    lowerCamelCase_      :	Union[str, Any]	     =      False







    @classmethod
    def     _lowercase     (    cls     )     ->      str:
           super().setUpClass()
           lowerCamelCase   :      str			 =					tempfile.mkdtemp()
           lowerCamelCase   :      Tuple			 =					os.path.join(cls._tmpdir     ,  "default_config.yml"     )

           write_basic_config(save_location=cls.configPath     )
           lowerCamelCase   :      Optional[Any]			 =					["accelerate", "launch", "--config_file", cls.configPath]







    @classmethod
    def     _lowercase     (    cls     )     ->      List[Any]:
           super().tearDownClass()
           shutil.rmtree(cls._tmpdir     )







    def     _lowercase     (    self     )     ->      Union[str, Any]:
           lowerCamelCase   :      Union[str, Any]			 =					F'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps epoch
        --output_dir {self.tmpdir}
        '''.split()
           run_command(self._launch_args + testargs     )
           self.assertTrue(os.path.exists(os.path.join(self.tmpdir     ,  "epoch_0"     )     )     )







    def     _lowercase     (    self     )     ->      List[Any]:
           lowerCamelCase   :      Optional[int]			 =					F'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        '''.split()
           lowerCamelCase   :      Optional[Any]			 =					run_command(self._launch_args + testargs     )
           self.assertTrue(os.path.exists(os.path.join(self.tmpdir     ,  "step_2"     )     )     )







    def     _lowercase     (    self     )     ->      Any:
           lowerCamelCase   :      Optional[int]			 =					F'''
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir     ,  "epoch_0"     )}
        '''.split()
           lowerCamelCase   :      List[Any]			 =					run_command(self._launch_args + testargs     ,  return_stdout=UpperCamelCase__     )
           self.assertNotIn("epoch 0:"     ,  UpperCamelCase__     )
           self.assertIn("epoch 1:"     ,  UpperCamelCase__     )







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







    @slow
    def     _lowercase     (    self     )     ->      Tuple:
           lowerCamelCase   :      Union[str, Any]			 =					"\n        examples/by_feature/cross_validation.py\n        --num_folds 2\n        ".split()
           with mock.patch.dict(os.environ     ,  {"TESTING_MOCKED_DATALOADERS": "0"}     ):
                  lowerCamelCase   :      Tuple			 =					run_command(self._launch_args + testargs     ,  return_stdout=UpperCamelCase__     )
                  lowerCamelCase   :      Optional[int]			 =					re.findall("({.+})"     ,  UpperCamelCase__     )
                  lowerCamelCase   :      Any			 =					[r for r in results if "accuracy" in r][-1]
                  lowerCamelCase   :      List[str]			 =					ast.literal_eval(UpperCamelCase__     )
                  self.assertGreaterEqual(results["accuracy"]     ,  0.75     )







    def     _lowercase     (    self     )     ->      str:
           lowerCamelCase   :      str			 =					["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     )     ->      Optional[int]:
           with tempfile.TemporaryDirectory() as tmpdir:
                  lowerCamelCase   :      Optional[int]			 =					F'''
            examples/by_feature/tracking.py
            --with_tracking
            --project_dir {tmpdir}
            '''.split()
                  run_command(self._launch_args + testargs     )
                  self.assertTrue(os.path.exists(os.path.join(UpperCamelCase__     ,  "tracking"     )     )     )







    def     _lowercase     (    self     )     ->      Optional[int]:
           lowerCamelCase   :      Union[str, Any]			 =					["examples/by_feature/gradient_accumulation.py"]
           run_command(self._launch_args + testargs     )







    def     _lowercase     (    self     )     ->      Any:
           lowerCamelCase   :      Tuple			 =					["examples/by_feature/local_sgd.py"]
           run_command(self._launch_args + testargs     )










'''simple docstring'''


import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


if is_flax_available():
				import jax
				import jax.numpy as jnp

				from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
				from PIL import Image

				from transformers import AutoImageProcessor





class     lowercase__  (						unittest.TestCase					):





							'''simple docstring'''


							def __init__(    self    ,					__snake_case    ,					__snake_case=3    ,					__snake_case=32    ,					__snake_case=3    ,					__snake_case=10    ,					__snake_case=[10, 20, 30, 40]    ,					__snake_case=[1, 1, 2, 1]    ,					__snake_case=True    ,					__snake_case=True    ,					__snake_case="relu"    ,					__snake_case=3    ,					__snake_case=None    ,					):
											_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      parent
											_SCREAMING_SNAKE_CASE :       Dict     		=      batch_size
											_SCREAMING_SNAKE_CASE :       int     		=      image_size
											_SCREAMING_SNAKE_CASE :       Any     		=      num_channels
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      embeddings_size
											_SCREAMING_SNAKE_CASE :       Tuple     		=      hidden_sizes
											_SCREAMING_SNAKE_CASE :       str     		=      depths
											_SCREAMING_SNAKE_CASE :       str     		=      is_training
											_SCREAMING_SNAKE_CASE :       Any     		=      use_labels
											_SCREAMING_SNAKE_CASE :       Dict     		=      hidden_act
											_SCREAMING_SNAKE_CASE :       int     		=      num_labels
											_SCREAMING_SNAKE_CASE :       str     		=      scope
											_SCREAMING_SNAKE_CASE :       Any     		=      len(__snake_case  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

											_SCREAMING_SNAKE_CASE :       Any     		=      self.get_config()

											return config, pixel_values






							def UpperCAmelCase_	(    self  ):
											return RegNetConfig(
											    num_channels=self.num_channels    ,					embeddings_size=self.embeddings_size    ,					hidden_sizes=self.hidden_sizes    ,					depths=self.depths    ,					hidden_act=self.hidden_act    ,					num_labels=self.num_labels    ,					image_size=self.image_size    ,					)






							def UpperCAmelCase_	(    self    ,					__snake_case    ,					__snake_case  ):
											_SCREAMING_SNAKE_CASE :       List[str]     		=      FlaxRegNetModel(config=__snake_case  )
											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      model(__snake_case  )

											# Output shape (b, c, h, w)
											self.parent.assertEqual(
											    result.last_hidden_state.shape    ,					(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)    ,					)






							def UpperCAmelCase_	(    self    ,					__snake_case    ,					__snake_case  ):
											_SCREAMING_SNAKE_CASE :       Tuple     		=      self.num_labels
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      FlaxRegNetForImageClassification(config=__snake_case  )
											_SCREAMING_SNAKE_CASE :       Tuple     		=      model(__snake_case  )
											self.parent.assertEqual(result.logits.shape    ,					(self.batch_size, self.num_labels)  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       List[Any]     		=      config_and_inputs
											_SCREAMING_SNAKE_CASE :       Dict     		=      {"""pixel_values""": pixel_values}
											return config, inputs_dict





@require_flax
class     lowercase__  (						_snake_case     ,     unittest.TestCase					):





							'''simple docstring'''


							A_							:      Dict      	=  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

							A_							:      Union[str, Any]      	=  False
							A_							:      List[str]      	=  False
							A_							:      str      	=  False






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       str     		=      FlaxRegNetModelTester(self  )
											_SCREAMING_SNAKE_CASE :       Any     		=      ConfigTester(self    ,					config_class=__snake_case    ,					has_text_modality=__snake_case  )






							def UpperCAmelCase_	(    self  ):
											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 UpperCAmelCase_	(    self  ):
											return






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       int     		=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*__snake_case  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Tuple     		=      self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*__snake_case  )






							@unittest.skip(reason="""RegNet does not use inputs_embeds"""  )
							def UpperCAmelCase_	(    self  ):
											pass






							@unittest.skip(reason="""RegNet does not support input and output embeddings"""  )
							def UpperCAmelCase_	(    self  ):
											pass






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE :       List[Any]     		=      model_class(__snake_case  )
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      inspect.signature(model.__call__  )
															# signature.parameters is an OrderedDict => so arg_names order is deterministic
															_SCREAMING_SNAKE_CASE :       int     		=      [*signature.parameters.keys()]

															_SCREAMING_SNAKE_CASE :       Tuple     		=      ["""pixel_values"""]
															self.assertListEqual(arg_names[:1]    ,					__snake_case  )






							def UpperCAmelCase_	(    self  ):
											def check_hidden_states_output(__snake_case    ,					__snake_case    ,					__snake_case  ):
															_SCREAMING_SNAKE_CASE :       Any     		=      model_class(__snake_case  )

															_SCREAMING_SNAKE_CASE :       int     		=      model(**self._prepare_for_class(__snake_case    ,					__snake_case  )  )

															_SCREAMING_SNAKE_CASE :       Dict     		=      outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

															_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      self.model_tester.num_stages
															self.assertEqual(len(__snake_case  )    ,					expected_num_stages + 1  )

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

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE :       Optional[int]     		=      True
															check_hidden_states_output(__snake_case    ,					__snake_case    ,					__snake_case  )

															# check that output_hidden_states also work using config
															del inputs_dict["output_hidden_states"]
															_SCREAMING_SNAKE_CASE :       Union[str, Any]     		=      True

															check_hidden_states_output(__snake_case    ,					__snake_case    ,					__snake_case  )






							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE		,       _SCREAMING_SNAKE_CASE :       str     		=      self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															with self.subTest(model_class.__name__  ):
																			_SCREAMING_SNAKE_CASE :       List[Any]     		=      self._prepare_for_class(__snake_case    ,					__snake_case  )
																			_SCREAMING_SNAKE_CASE :       str     		=      model_class(__snake_case  )

																			@jax.jit
																			def model_jitted(__snake_case    ,					**__snake_case  ):
																							return model(pixel_values=__snake_case    ,					**__snake_case  )

																			with self.subTest("""JIT Enabled"""  ):
																							_SCREAMING_SNAKE_CASE :       Any     		=      model_jitted(**__snake_case  ).to_tuple()

																			with self.subTest("""JIT Disabled"""  ):
																							with jax.disable_jit():
																											_SCREAMING_SNAKE_CASE :       Optional[int]     		=      model_jitted(**__snake_case  ).to_tuple()

																			self.assertEqual(len(__snake_case  )    ,					len(__snake_case  )  )
																			for jitted_output, output in zip(__snake_case    ,					__snake_case  ):
																							self.assertEqual(jitted_output.shape    ,					output.shape  )







def 			snake_case_   (  ):







				"""simple docstring"""





				_SCREAMING_SNAKE_CASE :       List[str]     		=      Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""     )
				return image







@require_flax
class     lowercase__  (						unittest.TestCase					):





							'''simple docstring'''


							@cached_property
							def UpperCAmelCase_	(    self  ):
											return AutoImageProcessor.from_pretrained("""facebook/regnet-y-040"""  ) if is_vision_available() else None






							@slow
							def UpperCAmelCase_	(    self  ):
											_SCREAMING_SNAKE_CASE :       Any     		=      FlaxRegNetForImageClassification.from_pretrained("""facebook/regnet-y-040"""  )

											_SCREAMING_SNAKE_CASE :       List[Any]     		=      self.default_image_processor
											_SCREAMING_SNAKE_CASE :       Optional[Any]     		=      prepare_img()
											_SCREAMING_SNAKE_CASE :       Tuple     		=      image_processor(images=__snake_case    ,					return_tensors="""np"""  )

											_SCREAMING_SNAKE_CASE :       str     		=      model(**__snake_case  )

											# verify the logits
											_SCREAMING_SNAKE_CASE :       Tuple     		=      (1, 1000)
											self.assertEqual(outputs.logits.shape    ,					__snake_case  )

											_SCREAMING_SNAKE_CASE :       List[Any]     		=      jnp.array([-0.4180, -1.5051, -3.4836]  )

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




