

"""simple docstring"""





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)

lowerCamelCase      =		[
    """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    lowercase__     (					unittest.TestCase   ):







       '''simple docstring'''


       def   lowercase__       (							self    :						str	,       _UpperCAmelCase    :						str	,       _UpperCAmelCase    :						bool	,       _UpperCAmelCase    :						str = None	,       _UpperCAmelCase    :						list = None					)				->		str:


             '''simple docstring'''




             UpperCAmelCase_    				=							None
             UpperCAmelCase_    				=							os.path.abspath(os.path.join("examples"	,       "by_feature"					)					)
             UpperCAmelCase_    				=							os.path.abspath("examples"					)
             for item in os.listdir(_UpperCAmelCase					):
                   if item not in EXCLUDE_EXAMPLES:
                         UpperCAmelCase_    				=							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()"	,       ):
                                     UpperCAmelCase_    				=							compare_against_test(
                                         os.path.join(_UpperCAmelCase	,       _UpperCAmelCase					)	,       _UpperCAmelCase	,       _UpperCAmelCase	,       _UpperCAmelCase					)
                                     UpperCAmelCase_    				=							"\n".join(_UpperCAmelCase					)
                                     if special_strings is not None:
                                           for string in special_strings:
                                                 UpperCAmelCase_    				=							diff.replace(_UpperCAmelCase	,       ""					)
                                     self.assertEqual(_UpperCAmelCase	,       ""					)



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


             '''simple docstring'''




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







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


             '''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"	,       _UpperCAmelCase	,       _UpperCAmelCase	,       _UpperCAmelCase					)
             self.one_complete_example("complete_cv_example.py"	,       _UpperCAmelCase	,       _UpperCAmelCase	,       _UpperCAmelCase					)



@mock.patch.dict(os.environ       ,      {'''TESTING_MOCKED_DATALOADERS''': '''1'''}   )
class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  False



       @classmethod
       def   lowercase__       (							cls    :						Tuple					)				->		str:


             '''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]					)				->		Union[str, Any]:


             '''simple docstring'''




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



       def   lowercase__       (							self    :						Tuple					)				->		Union[str, Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							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    :						Tuple					)				->		List[str]:


             '''simple docstring'''




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



       def   lowercase__       (							self    :						Optional[Any]					)				->		Tuple:


             '''simple docstring'''




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



       def   lowercase__       (							self    :						List[Any]					)				->		Optional[Any]:


             '''simple docstring'''




             UpperCAmelCase_    				=							F"""
        examples/by_feature/checkpointing.py
        --resume_from_checkpoint {os.path.join(self.tmpdir	,       'step_2'					)}
        """.split()
             UpperCAmelCase_    				=							run_command(self._launch_args + testargs	,       return_stdout=_UpperCAmelCase					)
             if torch.cuda.is_available():
                   UpperCAmelCase_    				=							torch.cuda.device_count()
             else:
                   UpperCAmelCase_    				=							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    :						List[Any]					)				->		str:


             '''simple docstring'''




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



       def   lowercase__       (							self    :						Optional[int]					)				->		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    :						Optional[Any]					)				->		str:


             '''simple docstring'''




             with tempfile.TemporaryDirectory() as tmpdir:
                   UpperCAmelCase_    				=							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[Any]					)				->		Optional[int]:


             '''simple docstring'''




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







       def   lowercase__       (							self    :						Optional[Any]					)				->		str:


             '''simple docstring'''




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









"""simple docstring"""





from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


if is_vision_available():
  import PIL


lowerCamelCase      =		logging.get_logger(__name__)
class    lowercase__     (					SCREAMING_SNAKE_CASE   ):







       '''simple docstring'''


       UpperCamelCase		=  ['''pixel_values''']



       def __init__(							self    :						str	,       _UpperCAmelCase    :						bool = True	,       _UpperCAmelCase    :						Dict[str, int] = None	,       _UpperCAmelCase    :						float = None	,       _UpperCAmelCase    :						PILImageResampling = PILImageResampling.BILINEAR	,       _UpperCAmelCase    :						bool = True	,       _UpperCAmelCase    :						Union[int, float] = 1 / 255	,       _UpperCAmelCase    :						bool = True	,       _UpperCAmelCase    :						Optional[Union[float, List[float]]] = None	,       _UpperCAmelCase    :						Optional[Union[float, List[float]]] = None	,       **_UpperCAmelCase    :						Optional[int]	,       )				->		None:


             '''simple docstring'''




             super().__init__(**_UpperCAmelCase					)
             UpperCAmelCase_    				=							size if size is not None else {"shortest_edge": 384}
             UpperCAmelCase_    				=							get_size_dict(_UpperCAmelCase	,       default_to_square=_UpperCAmelCase					)

             UpperCAmelCase_    				=							do_resize
             UpperCAmelCase_    				=							size
             # Default value set here for backwards compatibility where the value in config is None
             UpperCAmelCase_    				=							crop_pct if crop_pct is not None else 224 / 256
             UpperCAmelCase_    				=							resample
             UpperCAmelCase_    				=							do_rescale
             UpperCAmelCase_    				=							rescale_factor
             UpperCAmelCase_    				=							do_normalize
             UpperCAmelCase_    				=							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
             UpperCAmelCase_    				=							image_std if image_std is not None else IMAGENET_STANDARD_STD



       def   lowercase__       (							self    :						Union[str, Any]	,       _UpperCAmelCase    :						np.ndarray	,       _UpperCAmelCase    :						Dict[str, int]	,       _UpperCAmelCase    :						float	,       _UpperCAmelCase    :						PILImageResampling = PILImageResampling.BICUBIC	,       _UpperCAmelCase    :						Optional[Union[str, ChannelDimension]] = None	,       **_UpperCAmelCase    :						Optional[int]	,       )				->		np.ndarray:


             '''simple docstring'''




             UpperCAmelCase_    				=							get_size_dict(_UpperCAmelCase	,       default_to_square=_UpperCAmelCase					)
             if "shortest_edge" not in size:
                   raise ValueError(F"""Size dictionary must contain 'shortest_edge' key. Got {size.keys()}"""					)
             UpperCAmelCase_    				=							size["shortest_edge"]

             if shortest_edge < 384:
                   # maintain same ratio, resizing shortest edge to shortest_edge/crop_pct
                   UpperCAmelCase_    				=							int(shortest_edge / crop_pct					)
                   UpperCAmelCase_    				=							get_resize_output_image_size(_UpperCAmelCase	,       size=_UpperCAmelCase	,       default_to_square=_UpperCAmelCase					)
                   UpperCAmelCase_    				=							resize(image=_UpperCAmelCase	,       size=_UpperCAmelCase	,       resample=_UpperCAmelCase	,       data_format=_UpperCAmelCase	,       **_UpperCAmelCase					)
                   # then crop to (shortest_edge, shortest_edge)
                   return center_crop(image=_UpperCAmelCase	,       size=(shortest_edge, shortest_edge)	,       data_format=_UpperCAmelCase	,       **_UpperCAmelCase					)
             else:
                   # warping (no cropping) when evaluated at 384 or larger
                   return resize(
                       _UpperCAmelCase	,       size=(shortest_edge, shortest_edge)	,       resample=_UpperCAmelCase	,       data_format=_UpperCAmelCase	,       **_UpperCAmelCase					)



       def   lowercase__       (							self    :						Tuple	,       _UpperCAmelCase    :						np.ndarray	,       _UpperCAmelCase    :						Union[int, float]	,       _UpperCAmelCase    :						Optional[Union[str, ChannelDimension]] = None	,       **_UpperCAmelCase    :						List[Any]	,       )				->		Any:


             '''simple docstring'''




             return rescale(_UpperCAmelCase	,       scale=_UpperCAmelCase	,       data_format=_UpperCAmelCase	,       **_UpperCAmelCase					)



       def   lowercase__       (							self    :						Dict	,       _UpperCAmelCase    :						np.ndarray	,       _UpperCAmelCase    :						Union[float, List[float]]	,       _UpperCAmelCase    :						Union[float, List[float]]	,       _UpperCAmelCase    :						Optional[Union[str, ChannelDimension]] = None	,       **_UpperCAmelCase    :						List[str]	,       )				->		np.ndarray:


             '''simple docstring'''




             return normalize(_UpperCAmelCase	,       mean=_UpperCAmelCase	,       std=_UpperCAmelCase	,       data_format=_UpperCAmelCase	,       **_UpperCAmelCase					)







       def   lowercase__       (							self    :						Union[str, Any]	,       _UpperCAmelCase    :						ImageInput	,       _UpperCAmelCase    :						bool = None	,       _UpperCAmelCase    :						Dict[str, int] = None	,       _UpperCAmelCase    :						float = None	,       _UpperCAmelCase    :						PILImageResampling = None	,       _UpperCAmelCase    :						bool = None	,       _UpperCAmelCase    :						float = None	,       _UpperCAmelCase    :						bool = None	,       _UpperCAmelCase    :						Optional[Union[float, List[float]]] = None	,       _UpperCAmelCase    :						Optional[Union[float, List[float]]] = None	,       _UpperCAmelCase    :						Optional[Union[str, TensorType]] = None	,       _UpperCAmelCase    :						ChannelDimension = ChannelDimension.FIRST	,       **_UpperCAmelCase    :						Optional[int]	,       )				->		PIL.Image.Image:


             '''simple docstring'''




             UpperCAmelCase_    				=							do_resize if do_resize is not None else self.do_resize
             UpperCAmelCase_    				=							crop_pct if crop_pct is not None else self.crop_pct
             UpperCAmelCase_    				=							resample if resample is not None else self.resample
             UpperCAmelCase_    				=							do_rescale if do_rescale is not None else self.do_rescale
             UpperCAmelCase_    				=							rescale_factor if rescale_factor is not None else self.rescale_factor
             UpperCAmelCase_    				=							do_normalize if do_normalize is not None else self.do_normalize
             UpperCAmelCase_    				=							image_mean if image_mean is not None else self.image_mean
             UpperCAmelCase_    				=							image_std if image_std is not None else self.image_std

             UpperCAmelCase_    				=							size if size is not None else self.size
             UpperCAmelCase_    				=							get_size_dict(_UpperCAmelCase	,       default_to_square=_UpperCAmelCase					)

             UpperCAmelCase_    				=							make_list_of_images(_UpperCAmelCase					)

             if not valid_images(_UpperCAmelCase					):
                   raise ValueError(
                       "Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, "
                       "torch.Tensor, tf.Tensor or jax.ndarray."					)

             if do_resize and size is None or resample is None:
                   raise ValueError("Size and resample must be specified if do_resize is True."					)

             if do_resize and size["shortest_edge"] < 384 and crop_pct is None:
                   raise ValueError("crop_pct must be specified if size < 384."					)

             if do_rescale and rescale_factor is None:
                   raise ValueError("Rescale factor must be specified if do_rescale is True."					)

             if do_normalize and (image_mean is None or image_std is None):
                   raise ValueError("Image mean and std must be specified if do_normalize is True."					)

             # All transformations expect numpy arrays.
             UpperCAmelCase_    				=							[to_numpy_array(_UpperCAmelCase					) for image in images]

             if do_resize:
                   UpperCAmelCase_    				=							[self.resize(image=_UpperCAmelCase	,       size=_UpperCAmelCase	,       crop_pct=_UpperCAmelCase	,       resample=_UpperCAmelCase					) for image in images]

             if do_rescale:
                   UpperCAmelCase_    				=							[self.rescale(image=_UpperCAmelCase	,       scale=_UpperCAmelCase					) for image in images]

             if do_normalize:
                   UpperCAmelCase_    				=							[self.normalize(image=_UpperCAmelCase	,       mean=_UpperCAmelCase	,       std=_UpperCAmelCase					) for image in images]

             UpperCAmelCase_    				=							[to_channel_dimension_format(_UpperCAmelCase	,       _UpperCAmelCase					) for image in images]

             UpperCAmelCase_    				=							{"pixel_values": images}
             return BatchFeature(data=_UpperCAmelCase	,       tensor_type=_UpperCAmelCase					)




