





"""simple docstring"""





import numpy as np

from transformers import BatchFeature
from transformers.testing_utils import require_tf, require_torch

from .test_feature_extraction_common import FeatureExtractionSavingTestMixin

class 						lowercase   (   _UpperCAmelCase   ):
      # to overwrite at feature extractactor specific tests
      _SCREAMING_SNAKE_CASE							      = None
      _SCREAMING_SNAKE_CASE							      = None





      @property
      def        _snake_case     (			self					)						->	Union[str, Any]:
            return self.feat_extract_tester.prepare_feat_extract_dict()





      def        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            self.assertTrue(hasattr(lowercase ,		"""feature_size"""					)					)
            self.assertTrue(hasattr(lowercase ,		"""sampling_rate"""					)					)
            self.assertTrue(hasattr(lowercase ,		"""padding_value"""					)					)





      def        _snake_case     (			self					)						->	Tuple:
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common()
            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)

            self.assertTrue(all(len(lowercase					) == len(lowercase					) for x, y in zip(lowercase ,		processed_features[input_name]					)					)					)

            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common(equal_length=lowercase					)
            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs} ,		tensor_type="""np"""					)

            lowerCAmelCase    		=			processed_features[input_name]

            if len(batch_features_input.shape					) < 3:
                  lowerCAmelCase    		=			batch_features_input[:, :, None]

            self.assertTrue(
                batch_features_input.shape
                == (self.feat_extract_tester.batch_size, len(speech_inputs[0]					), self.feat_extract_tester.feature_size)					)





      @require_torch
      def        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common(equal_length=lowercase					)
            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs} ,		tensor_type="""pt"""					)

            lowerCAmelCase    		=			processed_features[input_name]

            if len(batch_features_input.shape					) < 3:
                  lowerCAmelCase    		=			batch_features_input[:, :, None]

            self.assertTrue(
                batch_features_input.shape
                == (self.feat_extract_tester.batch_size, len(speech_inputs[0]					), self.feat_extract_tester.feature_size)					)





      @require_tf
      def        _snake_case     (			self					)						->	Tuple:
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common(equal_length=lowercase					)
            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs} ,		tensor_type="""tf"""					)

            lowerCAmelCase    		=			processed_features[input_name]

            if len(batch_features_input.shape					) < 3:
                  lowerCAmelCase    		=			batch_features_input[:, :, None]

            self.assertTrue(
                batch_features_input.shape
                == (self.feat_extract_tester.batch_size, len(speech_inputs[0]					), self.feat_extract_tester.feature_size)					)





      def        _snake_case     (			self ,		lowercase=False					)						->	Tuple:
            def _inputs_have_equal_length(lowercase					):
                  lowerCAmelCase    		=			len(input[0]					)
                  for input_slice in input[1:]:
                        if len(lowercase					) != length:
                              return False
                  return True

            def _inputs_are_equal(lowercase ,		lowercase					):
                  if len(lowercase					) != len(lowercase					):
                        return False

                  for input_slice_a, input_slice_a in zip(lowercase ,		lowercase					):
                        if not np.allclose(np.asarray(lowercase					) ,		np.asarray(lowercase					) ,		atol=1e-3					):
                              return False
                  return True

            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common(numpify=lowercase					)
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)

            lowerCAmelCase    		=			self.feat_extract_tester.seq_length_diff
            lowerCAmelCase    		=			self.feat_extract_tester.max_seq_length + pad_diff
            lowerCAmelCase    		=			self.feat_extract_tester.min_seq_length
            lowerCAmelCase    		=			self.feat_extract_tester.batch_size
            lowerCAmelCase    		=			self.feat_extract_tester.feature_size

            # test padding for List[int] + numpy
            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding=lowercase					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest"""					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[-1]					)					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		return_tensors="""np"""					)
            lowerCAmelCase    		=			input_a[input_name]

            # max_length parameter has to be provided when setting `padding="max_length"`
            with self.assertRaises(lowercase					):
                  feat_extract.pad(lowercase ,		padding="""max_length"""					)[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=lowercase ,		return_tensors="""np"""					)
            lowerCAmelCase    		=			input_a[input_name]

            self.assertFalse(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(_inputs_are_equal(lowercase ,		lowercase					)					)
            self.assertTrue(len(input_a[0]					) == pad_min_length					)
            self.assertTrue(len(input_a[1]					) == pad_min_length + pad_diff					)
            self.assertTrue(input_a.shape[:2] == (batch_size, len(input_a[0]					))					)
            self.assertTrue(input_a.shape[:2] == (batch_size, pad_max_length)					)

            if feature_size > 1:
                  self.assertTrue(input_a.shape[2] == input_a.shape[2] == feature_size					)

            # test padding for `pad_to_multiple_of` for List[int] + numpy
            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		pad_to_multiple_of=10					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		pad_to_multiple_of=10					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		pad_to_multiple_of=10 ,		max_length=lowercase					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		pad_to_multiple_of=10 ,		max_length=lowercase ,		return_tensors="""np""" ,		)
            lowerCAmelCase    		=			input_a[input_name]

            self.assertTrue(all(len(lowercase					) % 10 == 0 for x in input_a					)					)
            self.assertTrue(_inputs_are_equal(lowercase ,		lowercase					)					)

            lowerCAmelCase    		=			pad_max_length if pad_max_length % 10 == 0 else (pad_max_length // 10 + 1) * 10
            self.assertTrue(all(len(lowercase					) == expected_mult_pad_length for x in input_a					)					)
            self.assertEqual(input_a.shape[:2] ,		(batch_size, expected_mult_pad_length)					)

            if feature_size > 1:
                  self.assertTrue(input_a.shape[2] == feature_size					)

            # Check padding value is correct
            lowerCAmelCase    		=			(np.ones(self.feat_extract_tester.feature_size					) * feat_extract.padding_value).sum()
            self.assertTrue(
                abs(np.asarray(input_a[0]					)[pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length)					)
                < 1e-3					)
            self.assertTrue(
                abs(
                    np.asarray(input_a[1]					)[pad_min_length + pad_diff :].sum()
                    - padding_vector_sum * (pad_max_length - pad_min_length - pad_diff)					)
                < 1e-3					)
            self.assertTrue(
                abs(
                    np.asarray(input_a[2]					)[pad_min_length + 2 * pad_diff :].sum()
                    - padding_vector_sum * (pad_max_length - pad_min_length - 2 * pad_diff)					)
                < 1e-3					)
            self.assertTrue(
                abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (pad_max_length - pad_min_length)					) < 1e-3					)
            self.assertTrue(
                abs(input_a[0, pad_min_length:].sum() - padding_vector_sum * (expected_mult_pad_length - pad_min_length)					)
                < 1e-3					)





      def        _snake_case     (			self ,		lowercase=False					)						->	Optional[Any]:
            def _inputs_have_equal_length(lowercase					):
                  lowerCAmelCase    		=			len(input[0]					)
                  for input_slice in input[1:]:
                        if len(lowercase					) != length:
                              return False
                  return True

            def _inputs_are_equal(lowercase ,		lowercase					):
                  if len(lowercase					) != len(lowercase					):
                        return False

                  for input_slice_a, input_slice_a in zip(lowercase ,		lowercase					):
                        if not np.allclose(np.asarray(lowercase					) ,		np.asarray(lowercase					) ,		atol=1e-3					):
                              return False
                  return True

            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common(numpify=lowercase					)
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)

            # truncate to smallest
            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[0]					) ,		truncation=lowercase					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[0]					)					)
            lowerCAmelCase    		=			input_a[input_name]

            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertFalse(_inputs_have_equal_length(lowercase					)					)

            # truncate to smallest with np
            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[0]					) ,		return_tensors="""np""" ,		truncation=lowercase ,		)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[0]					) ,		return_tensors="""np"""					)
            lowerCAmelCase    		=			input_a[input_name]

            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(input_a.shape[1] == len(speech_inputs[0]					)					)

            # since truncation forces padding to be smaller than longest input
            # function can't return `np.ndarray`, but has to return list
            self.assertFalse(_inputs_have_equal_length(lowercase					)					)

            # truncate to middle
            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[1]					) ,		truncation=lowercase ,		return_tensors="""np""" ,		)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[1]					) ,		truncation=lowercase					)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[1]					) ,		return_tensors="""np"""					)
            lowerCAmelCase    		=			input_a[input_name]

            self.assertTrue(input_a.shape[1] == len(speech_inputs[1]					)					)
            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(_inputs_are_equal(lowercase ,		lowercase					)					)

            # since truncation forces padding to be smaller than longest input
            # function can't return `np.ndarray`, but has to return list
            self.assertFalse(_inputs_have_equal_length(lowercase					)					)
            self.assertTrue(len(input_a[-1]					) == len(speech_inputs[-1]					)					)

            # padding has to be max_length when setting `truncation=True`
            with self.assertRaises(lowercase					):
                  feat_extract.pad(lowercase ,		truncation=lowercase					)[input_name]

            # padding has to be max_length when setting `truncation=True`
            with self.assertRaises(lowercase					):
                  feat_extract.pad(lowercase ,		padding="""longest""" ,		truncation=lowercase					)[input_name]

            # padding has to be max_length when setting `truncation=True`
            with self.assertRaises(lowercase					):
                  feat_extract.pad(lowercase ,		padding="""longest""" ,		truncation=lowercase					)[input_name]

            # max_length parameter has to be provided when setting `truncation=True` and padding="max_length"
            with self.assertRaises(lowercase					):
                  feat_extract.pad(lowercase ,		padding="""max_length""" ,		truncation=lowercase					)[input_name]

            # test truncation for `pad_to_multiple_of` for List[int] + numpy
            lowerCAmelCase    		=			12
            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[0]					) ,		pad_to_multiple_of=lowercase ,		truncation=lowercase ,		)
            lowerCAmelCase    		=			input_a[input_name]

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=len(speech_inputs[0]					) ,		pad_to_multiple_of=lowercase ,		)
            lowerCAmelCase    		=			input_a[input_name]

            # retrieve expected_length as multiple of pad_to_multiple_of
            lowerCAmelCase    		=			len(speech_inputs[0]					)
            if expected_length % pad_to_multiple_of != 0:
                  lowerCAmelCase    		=			((len(speech_inputs[0]					) // pad_to_multiple_of) + 1) * pad_to_multiple_of

            self.assertTrue(len(input_a[0]					) == expected_length					)
            self.assertTrue(_inputs_have_equal_length(lowercase					)					)
            self.assertFalse(_inputs_have_equal_length(lowercase					)					)





      def        _snake_case     (			self					)						->	Dict:
            self._check_padding(numpify=lowercase					)





      def        _snake_case     (			self					)						->	str:
            self._check_padding(numpify=lowercase					)





      def        _snake_case     (			self					)						->	Optional[int]:
            self._check_truncation(numpify=lowercase					)





      def        _snake_case     (			self					)						->	Any:
            self._check_truncation(numpify=lowercase					)





      @require_torch
      def        _snake_case     (			self					)						->	Optional[Any]:
            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common()
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		return_tensors="""np"""					)[input_name]
            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		return_tensors="""pt"""					)[input_name]

            self.assertTrue(abs(input_np.astype(np.floataa					).sum() - input_pt.numpy().astype(np.floataa					).sum()					) < 1e-2					)





      @require_tf
      def        _snake_case     (			self					)						->	Optional[Any]:
            lowerCAmelCase    		=			self.feature_extraction_class(**self.feat_extract_dict					)
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common()
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		return_tensors="""np"""					)[input_name]
            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		return_tensors="""tf"""					)[input_name]

            self.assertTrue(abs(input_np.astype(np.floataa					).sum() - input_tf.numpy().astype(np.floataa					).sum()					) < 1e-2					)





      def        _snake_case     (			self					)						->	List[Any]:
            lowerCAmelCase    		=			self.feat_extract_dict
            lowerCAmelCase    		=			True
            lowerCAmelCase    		=			self.feature_extraction_class(**lowercase					)
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common()
            lowerCAmelCase    		=			[len(lowercase					) for x in speech_inputs]
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)

            lowerCAmelCase    		=			feat_extract.pad(lowercase ,		padding="""longest""" ,		return_tensors="""np"""					)
            self.assertIn("""attention_mask""" ,		lowercase					)
            self.assertListEqual(list(processed.attention_mask.shape					) ,		list(processed[input_name].shape[:2]					)					)
            self.assertListEqual(processed.attention_mask.sum(-1					).tolist() ,		lowercase					)





      def        _snake_case     (			self					)						->	Optional[int]:
            lowerCAmelCase    		=			self.feat_extract_dict
            lowerCAmelCase    		=			True
            lowerCAmelCase    		=			self.feature_extraction_class(**lowercase					)
            lowerCAmelCase    		=			self.feat_extract_tester.prepare_inputs_for_common()
            lowerCAmelCase    		=			[len(lowercase					) for x in speech_inputs]
            lowerCAmelCase    		=			feat_extract.model_input_names[0]

            lowerCAmelCase    		=			BatchFeature({input_name: speech_inputs}					)
            lowerCAmelCase    		=			min(lowercase					)

            lowerCAmelCase    		=			feat_extract.pad(
                lowercase ,		padding="""max_length""" ,		max_length=lowercase ,		truncation=lowercase ,		return_tensors="""np"""					)
            self.assertIn("""attention_mask""" ,		lowercase					)
            self.assertListEqual(
                list(processed_pad.attention_mask.shape					) ,		[processed_pad[input_name].shape[0], max_length]					)
            self.assertListEqual(
                processed_pad.attention_mask[:, :max_length].sum(-1					).tolist() ,		[max_length for x in speech_inputs]					)













"""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)

SCREAMING_SNAKE_CASE__   =  [
    "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   ):





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





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





      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			os.path.abspath(os.path.join("""examples""" ,		"""cv_example.py"""					)					)
            lowerCAmelCase    		=			[
                """ """ * 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""" ,		lowercase ,		lowercase ,		lowercase					)
            self.one_complete_example("""complete_cv_example.py""" ,		lowercase ,		lowercase ,		lowercase					)







@mock.patch.dict(os.environ	,    {'TESTING_MOCKED_DATALOADERS': '1'}   )
class 						lowercase   (   _UpperCAmelCase   ):
      _SCREAMING_SNAKE_CASE							      = False





      @classmethod
      def        _snake_case     (			cls					)						->	Optional[int]:
            super().setUpClass()
            lowerCAmelCase    		=			tempfile.mkdtemp()
            lowerCAmelCase    		=			os.path.join(cls._tmpdir ,		"""default_config.yml"""					)

            write_basic_config(save_location=cls.configPath					)
            lowerCAmelCase    		=			["""accelerate""", """launch""", """--config_file""", cls.configPath]





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





      def        _snake_case     (			self					)						->	str:
            lowerCAmelCase    		=			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					)						->	Optional[int]:
            lowerCAmelCase    		=			f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
            lowerCAmelCase    		=			run_command(self._launch_args + testargs					)
            self.assertTrue(os.path.exists(os.path.join(self.tmpdir ,		"""step_2"""					)					)					)





      def        _snake_case     (			self					)						->	List[str]:
            lowerCAmelCase    		=			f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir ,		"epoch_0"					)}\n        '.split()
            lowerCAmelCase    		=			run_command(self._launch_args + testargs ,		return_stdout=lowercase					)
            self.assertNotIn("""epoch 0:""" ,		lowercase					)
            self.assertIn("""epoch 1:""" ,		lowercase					)





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





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





      def        _snake_case     (			self					)						->	int:
            lowerCAmelCase    		=			["""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					)						->	Any:
            with tempfile.TemporaryDirectory() as tmpdir:
                  lowerCAmelCase    		=			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(lowercase ,		"""tracking"""					)					)					)





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





      def        _snake_case     (			self					)						->	int:
            lowerCAmelCase    		=			["""examples/by_feature/local_sgd.py"""]
            run_command(self._launch_args + testargs					)






