"""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 			UpperCamelCase						(     unittest.TestCase    ):
   def      _UpperCAmelCase  (     self	,__UpperCamelCase	,__UpperCamelCase	,__UpperCamelCase = None	,__UpperCamelCase = None     )      -> Tuple:


        '''simple docstring'''


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


        '''simple docstring'''


        self.one_complete_example('complete_nlp_example.py'	,lowerCAmelCase_     )
        self.one_complete_example('complete_nlp_example.py'	,lowerCAmelCase_     )







   def      _UpperCAmelCase  (     self     )      -> Optional[Any]:


        '''simple docstring'''


        lowercase_      :							str						=      os.path.abspath(os.path.join('examples'	,'cv_example.py'     )     )
        lowercase_      :							List[str]						=      [
            " " * 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'	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_     )
        self.one_complete_example('complete_cv_example.py'	,lowerCAmelCase_	,lowerCAmelCase_	,lowerCAmelCase_     )







@mock.patch.dict(os.environ		, {'TESTING_MOCKED_DATALOADERS': '1'}    )
class 			UpperCamelCase						(     __A    ):
   lowercase				=    False
   @classmethod
   def      _UpperCAmelCase  (     cls     )      -> str:


        '''simple docstring'''


        super().setUpClass()
        lowercase_      :							Optional[int]						=      tempfile.mkdtemp()
        lowercase_      :							List[str]						=      os.path.join(cls._tmpdir	,'default_config.yml'     )

        write_basic_config(save_location=cls.configPath     )
        lowercase_      :							Tuple						=      ["accelerate", "launch", "--config_file", cls.configPath]
   @classmethod
   def      _UpperCAmelCase  (     cls     )      -> Tuple:


        '''simple docstring'''


        super().tearDownClass()
        shutil.rmtree(cls._tmpdir     )
   def      _UpperCAmelCase  (     self     )      -> Dict:


        '''simple docstring'''


        lowercase_      :							Optional[int]						=      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      _UpperCAmelCase  (     self     )      -> Optional[int]:


        '''simple docstring'''


        lowercase_      :							List[Any]						=      f'''
        examples/by_feature/checkpointing.py
        --checkpointing_steps 1
        --output_dir {self.tmpdir}
        '''.split()
        lowercase_      :							List[str]						=      run_command(self._launch_args + testargs     )
        self.assertTrue(os.path.exists(os.path.join(self.tmpdir	,'step_2'     )     )     )
   def      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


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


        '''simple docstring'''


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


        '''simple docstring'''


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


        '''simple docstring'''


        lowercase_      :							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      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


        with tempfile.TemporaryDirectory() as tmpdir:
             lowercase_      :							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(lowerCAmelCase_	,'tracking'     )     )     )
   def      _UpperCAmelCase  (     self     )      -> Tuple:


        '''simple docstring'''


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







   def      _UpperCAmelCase  (     self     )      -> List[Any]:


        '''simple docstring'''


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









"""simple docstring"""
from typing import List, Optional, Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCamelCase_							      = logging.get_logger(__name__)

lowerCamelCase_							      = {
    '''huggingface/informer-tourism-monthly''': (
        '''https://huggingface.co/huggingface/informer-tourism-monthly/resolve/main/config.json'''
    ),
    # See all Informer models at https://huggingface.co/models?filter=informer
}







class      UpperCamelCase_			(__A	):
	__magic_name__       =							'''informer'''
	__magic_name__       =							{
	    '''hidden_size''': '''d_model''',
	    '''num_attention_heads''': '''encoder_attention_heads''',
	    '''num_hidden_layers''': '''encoder_layers''',
	}





	def __init__( self   :					Optional[Any]      ,      lowerCAmelCase_   :					Optional[int] = None      ,      lowerCAmelCase_   :					Optional[int] = None      ,      lowerCAmelCase_   :					str = "student_t"      ,      lowerCAmelCase_   :					str = "nll"      ,      lowerCAmelCase_   :					int = 1      ,      lowerCAmelCase_   :					List[int] = None      ,      lowerCAmelCase_   :					Optional[Union[str, bool]] = "mean"      ,      lowerCAmelCase_   :					int = 0      ,      lowerCAmelCase_   :					int = 0      ,      lowerCAmelCase_   :					int = 0      ,      lowerCAmelCase_   :					int = 0      ,      lowerCAmelCase_   :					Optional[List[int]] = None      ,      lowerCAmelCase_   :					Optional[List[int]] = None      ,      lowerCAmelCase_   :					int = 64      ,      lowerCAmelCase_   :					int = 32      ,      lowerCAmelCase_   :					int = 32      ,      lowerCAmelCase_   :					int = 2      ,      lowerCAmelCase_   :					int = 2      ,      lowerCAmelCase_   :					int = 2      ,      lowerCAmelCase_   :					int = 2      ,      lowerCAmelCase_   :					bool = True      ,      lowerCAmelCase_   :					str = "gelu"      ,      lowerCAmelCase_   :					float = 0.0_5      ,      lowerCAmelCase_   :					float = 0.1      ,      lowerCAmelCase_   :					float = 0.1      ,      lowerCAmelCase_   :					float = 0.1      ,      lowerCAmelCase_   :					float = 0.1      ,      lowerCAmelCase_   :					int = 100      ,      lowerCAmelCase_   :					float = 0.0_2      ,      lowerCAmelCase_   :					List[Any]=True      ,      lowerCAmelCase_   :					str = "prob"      ,      lowerCAmelCase_   :					int = 5      ,      lowerCAmelCase_   :					bool = True      ,      **lowerCAmelCase_   :					Tuple      ,      )		->     Tuple:
				# time series specific configuration
				UpperCAmelCase_	:					str     	=   prediction_length
				UpperCAmelCase_	:					Tuple     	=   context_length or prediction_length
				UpperCAmelCase_	:					Any     	=   distribution_output
				UpperCAmelCase_	:					Union[str, Any]     	=   loss
				UpperCAmelCase_	:					Any     	=   input_size
				UpperCAmelCase_	:					int     	=   num_time_features
				UpperCAmelCase_	:					Union[str, Any]     	=   lags_sequence if lags_sequence is not None else [1, 2, 3, 4, 5, 6, 7]
				UpperCAmelCase_	:					List[Any]     	=   scaling
				UpperCAmelCase_	:					List[str]     	=   num_dynamic_real_features
				UpperCAmelCase_	:					int     	=   num_static_real_features
				UpperCAmelCase_	:					Any     	=   num_static_categorical_features

				# set cardinality
				if cardinality and num_static_categorical_features > 0:
							if len(lowerCAmelCase_		) != num_static_categorical_features:
										raise ValueError(
										    "The cardinality should be a list of the same length as `num_static_categorical_features`"		)
							UpperCAmelCase_	:					Optional[Any]     	=   cardinality
				else:
							UpperCAmelCase_	:					Optional[int]     	=   [0]

				# set embedding_dimension
				if embedding_dimension and num_static_categorical_features > 0:
							if len(lowerCAmelCase_		) != num_static_categorical_features:
										raise ValueError(
										    "The embedding dimension should be a list of the same length as `num_static_categorical_features`"		)
							UpperCAmelCase_	:					Any     	=   embedding_dimension
				else:
							UpperCAmelCase_	:					Optional[Any]     	=   [min(50      ,      (cat + 1) // 2		) for cat in self.cardinality]

				UpperCAmelCase_	:					Dict     	=   num_parallel_samples

				# Transformer architecture configuration
				UpperCAmelCase_	:					Optional[Any]     	=   input_size * len(self.lags_sequence		) + self._number_of_features
				UpperCAmelCase_	:					List[Any]     	=   d_model
				UpperCAmelCase_	:					List[str]     	=   encoder_attention_heads
				UpperCAmelCase_	:					List[str]     	=   decoder_attention_heads
				UpperCAmelCase_	:					Union[str, Any]     	=   encoder_ffn_dim
				UpperCAmelCase_	:					Union[str, Any]     	=   decoder_ffn_dim
				UpperCAmelCase_	:					Tuple     	=   encoder_layers
				UpperCAmelCase_	:					List[Any]     	=   decoder_layers

				UpperCAmelCase_	:					List[str]     	=   dropout
				UpperCAmelCase_	:					Optional[Any]     	=   attention_dropout
				UpperCAmelCase_	:					Any     	=   activation_dropout
				UpperCAmelCase_	:					Union[str, Any]     	=   encoder_layerdrop
				UpperCAmelCase_	:					Optional[int]     	=   decoder_layerdrop

				UpperCAmelCase_	:					Union[str, Any]     	=   activation_function
				UpperCAmelCase_	:					int     	=   init_std

				UpperCAmelCase_	:					Optional[Any]     	=   use_cache

				# Informer
				UpperCAmelCase_	:					int     	=   attention_type
				UpperCAmelCase_	:					List[str]     	=   sampling_factor
				UpperCAmelCase_	:					Union[str, Any]     	=   distil

				super().__init__(is_encoder_decoder=lowerCAmelCase_      ,      **lowerCAmelCase_		)





	@property
	def 	_SCREAMING_SNAKE_CASE ( self   :					List[Any]		)		->     int:
				return (
				    sum(self.embedding_dimension		)
				    + self.num_dynamic_real_features
				    + self.num_time_features
				    + self.num_static_real_features
				    + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
				)



