





"""simple docstring"""
import inspect
import os

import torch
from transformers import AutoModel
from transformers.testing_utils import mockenv_context
from transformers.trainer_utils import set_seed

import accelerate
from accelerate.accelerator import Accelerator
from accelerate.state import AcceleratorState
from accelerate.test_utils.testing import (
    AccelerateTestCase,
    TempDirTestCase,
    execute_subprocess_async,
    require_cuda,
    require_fsdp,
    require_multi_gpu,
    slow,
)
from accelerate.utils.constants import (
    FSDP_AUTO_WRAP_POLICY,
    FSDP_BACKWARD_PREFETCH,
    FSDP_SHARDING_STRATEGY,
    FSDP_STATE_DICT_TYPE,
)
from accelerate.utils.dataclasses import FullyShardedDataParallelPlugin
from accelerate.utils.other import patch_environment


set_seed(42)

__UpperCamelCase     :						Optional[int]						       =  '''bert-base-cased'''
__UpperCamelCase     :						List[Any]						       =  '''fp16'''
__UpperCamelCase     :						Any						       =  '''bf16'''
__UpperCamelCase     :						Dict						       =  [FPaa, BFaa]







@require_fsdp
@require_cuda
class       a							(   a__  ):



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  super().setUp()

  lowerCAmelCase    				= dict(
      ACCELERATE_USE_FSDP='true'				,       MASTER_ADDR='localhost'				,       MASTER_PORT='10999'				,       RANK='0'				,       LOCAL_RANK='0'				,       WORLD_SIZE='1'				,       )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from torch.distributed.fsdp.fully_sharded_data_parallel import ShardingStrategy

  for i, strategy in enumerate(_snake_case     ):
   lowerCAmelCase    				= self.dist_env.copy()
   lowerCAmelCase    				= F'{i + 1}'
   lowerCAmelCase    				= strategy
   with mockenv_context(**_snake_case     ):
    lowerCAmelCase    				= FullyShardedDataParallelPlugin()
    self.assertEqual(fsdp_plugin.sharding_strategy				,       ShardingStrategy(i + 1     )     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from torch.distributed.fsdp.fully_sharded_data_parallel import BackwardPrefetch

  for i, prefetch_policy in enumerate(_snake_case     ):
   lowerCAmelCase    				= self.dist_env.copy()
   lowerCAmelCase    				= prefetch_policy
   with mockenv_context(**_snake_case     ):
    lowerCAmelCase    				= FullyShardedDataParallelPlugin()
    if prefetch_policy == "NO_PREFETCH":
     self.assertIsNone(fsdp_plugin.backward_prefetch     )
    else:
     self.assertEqual(fsdp_plugin.backward_prefetch				,       BackwardPrefetch(i + 1     )     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from torch.distributed.fsdp.fully_sharded_data_parallel import StateDictType

  for i, state_dict_type in enumerate(_snake_case     ):
   lowerCAmelCase    				= self.dist_env.copy()
   lowerCAmelCase    				= state_dict_type
   with mockenv_context(**_snake_case     ):
    lowerCAmelCase    				= FullyShardedDataParallelPlugin()
    self.assertEqual(fsdp_plugin.state_dict_type				,       StateDictType(i + 1     )     )
    if state_dict_type == "FULL_STATE_DICT":
     self.assertTrue(fsdp_plugin.state_dict_config.offload_to_cpu     )
     self.assertTrue(fsdp_plugin.state_dict_config.ranka_only     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= AutoModel.from_pretrained(_snake_case     )
  for policy in FSDP_AUTO_WRAP_POLICY:
   lowerCAmelCase    				= self.dist_env.copy()
   lowerCAmelCase    				= policy
   if policy == "TRANSFORMER_BASED_WRAP":
    lowerCAmelCase    				= 'BertLayer'
   elif policy == "SIZE_BASED_WRAP":
    lowerCAmelCase    				= '2000'
   with mockenv_context(**_snake_case     ):
    lowerCAmelCase    				= FullyShardedDataParallelPlugin()
    fsdp_plugin.set_auto_wrap_policy(_snake_case     )
    if policy == "NO_WRAP":
     self.assertIsNone(fsdp_plugin.auto_wrap_policy     )
    else:
     self.assertIsNotNone(fsdp_plugin.auto_wrap_policy     )

  lowerCAmelCase    				= self.dist_env.copy()
  lowerCAmelCase    				= 'TRANSFORMER_BASED_WRAP'
  lowerCAmelCase    				= 'T5Layer'
  with mockenv_context(**_snake_case     ):
   lowerCAmelCase    				= FullyShardedDataParallelPlugin()
   with self.assertRaises(_snake_case     ) as cm:
    fsdp_plugin.set_auto_wrap_policy(_snake_case     )
   self.assertTrue('Could not find the transformer layer class to wrap in the model.' in str(cm.exception     )     )

  lowerCAmelCase    				= self.dist_env.copy()
  lowerCAmelCase    				= 'SIZE_BASED_WRAP'
  lowerCAmelCase    				= '0'
  with mockenv_context(**_snake_case     ):
   lowerCAmelCase    				= FullyShardedDataParallelPlugin()
   fsdp_plugin.set_auto_wrap_policy(_snake_case     )
   self.assertIsNone(fsdp_plugin.auto_wrap_policy     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from torch.distributed.fsdp.fully_sharded_data_parallel import MixedPrecision
  from torch.distributed.fsdp.sharded_grad_scaler import ShardedGradScaler

  for mp_dtype in dtypes:
   lowerCAmelCase    				= self.dist_env.copy()
   lowerCAmelCase    				= mp_dtype
   with mockenv_context(**_snake_case     ):
    lowerCAmelCase    				= Accelerator()
    if mp_dtype == "fp16":
     lowerCAmelCase    				= torch.floataa
    elif mp_dtype == "bf16":
     lowerCAmelCase    				= torch.bfloataa
    lowerCAmelCase    				= MixedPrecision(param_dtype=_snake_case				,       reduce_dtype=_snake_case				,       buffer_dtype=_snake_case     )
    self.assertEqual(accelerator.state.fsdp_plugin.mixed_precision_policy				,       _snake_case     )
    if mp_dtype == FPaa:
     self.assertTrue(isinstance(accelerator.scaler				,       _snake_case     )     )
    elif mp_dtype == BFaa:
     self.assertIsNone(accelerator.scaler     )
    AcceleratorState._reset_state(_snake_case     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  from torch.distributed.fsdp.fully_sharded_data_parallel import CPUOffload

  for flag in [True, False]:
   lowerCAmelCase    				= self.dist_env.copy()
   lowerCAmelCase    				= str(_snake_case     ).lower()
   with mockenv_context(**_snake_case     ):
    lowerCAmelCase    				= FullyShardedDataParallelPlugin()
    self.assertEqual(fsdp_plugin.cpu_offload				,       CPUOffload(offload_params=_snake_case     )     )







@require_fsdp
@require_multi_gpu
@slow
class       a							(   a__  ):



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  super().setUp()
  lowerCAmelCase    				= 0.82
  lowerCAmelCase    				= [
      'fsdp_shard_grad_op_transformer_based_wrap',
      'fsdp_full_shard_transformer_based_wrap',
  ]
  lowerCAmelCase    				= {
      'multi_gpu_fp16': 32_00,
      'fsdp_shard_grad_op_transformer_based_wrap_fp16': 20_00,
      'fsdp_full_shard_transformer_based_wrap_fp16': 19_00,
      # Disabling below test as it overwhelms the RAM memory usage
      # on CI self-hosted runner leading to tests getting killed.
      # "fsdp_full_shard_cpu_offload_transformer_based_wrap_fp32": 1500,  # fp16 was leading to indefinite hang
  }
  lowerCAmelCase    				= 1_60
  lowerCAmelCase    				= 1_60

  lowerCAmelCase    				= inspect.getfile(accelerate.test_utils     )
  lowerCAmelCase    				= os.path.sep.join(mod_file.split(os.path.sep     )[:-1] + ['scripts', 'external_deps']     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= os.path.join(self.test_scripts_folder				,       'test_performance.py'     )
  lowerCAmelCase    				= ['accelerate', 'launch', '--num_processes=2', '--num_machines=1', '--machine_rank=0', '--use_fsdp']
  for config in self.performance_configs:
   lowerCAmelCase    				= cmd.copy()
   for i, strategy in enumerate(_snake_case     ):
    if strategy.lower() in config:
     cmd_config.append(F'--fsdp_sharding_strategy={i+1}'     )
     break

   if "fp32" in config:
    cmd_config.append('--mixed_precision=no'     )
   else:
    cmd_config.append('--mixed_precision=fp16'     )

   if "cpu_offload" in config:
    cmd_config.append('--fsdp_offload_params=True'     )

   for policy in FSDP_AUTO_WRAP_POLICY:
    if policy.lower() in config:
     cmd_config.append(F'--fsdp_auto_wrap_policy={policy}'     )
     break

   if policy == "TRANSFORMER_BASED_WRAP":
    cmd_config.append('--fsdp_transformer_layer_cls_to_wrap=BertLayer'     )
   elif policy == "SIZE_BASED_WRAP":
    cmd_config.append('--fsdp_min_num_params=2000'     )

   cmd_config.extend(
       [
           self.test_file_path,
           F'--output_dir={self.tmpdir}',
           F'--performance_lower_bound={self.performance_lower_bound}',
       ]     )
   with patch_environment(omp_num_threads=1     ):
    execute_subprocess_async(_snake_case				,       env=os.environ.copy()     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= os.path.join(self.test_scripts_folder				,       'test_checkpointing.py'     )
  lowerCAmelCase    				= [
      'accelerate',
      'launch',
      '--num_processes=2',
      '--num_machines=1',
      '--machine_rank=0',
      '--use_fsdp',
      '--mixed_precision=fp16',
      '--fsdp_transformer_layer_cls_to_wrap=BertLayer',
  ]

  for i, strategy in enumerate(_snake_case     ):
   lowerCAmelCase    				= cmd.copy()
   cmd_config.append(F'--fsdp_sharding_strategy={i+1}'     )
   if strategy != "FULL_SHARD":
    continue
   lowerCAmelCase    				= len(_snake_case     )
   for state_dict_type in FSDP_STATE_DICT_TYPE:
    lowerCAmelCase    				= cmd_config[:state_dict_config_index]
    cmd_config.append(F'--fsdp_state_dict_type={state_dict_type}'     )
    cmd_config.extend(
        [
            self.test_file_path,
            F'--output_dir={self.tmpdir}',
            '--partial_train_epoch=1',
        ]     )
    with patch_environment(omp_num_threads=1     ):
     execute_subprocess_async(_snake_case				,       env=os.environ.copy()     )

    lowerCAmelCase    				= cmd_config[:-1]
    lowerCAmelCase    				= os.path.join(self.tmpdir				,       'epoch_0'     )
    cmd_config.extend(
        [
            F'--resume_from_checkpoint={resume_from_checkpoint}',
        ]     )
    with patch_environment(omp_num_threads=1     ):
     execute_subprocess_async(_snake_case				,       env=os.environ.copy()     )



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  lowerCAmelCase    				= os.path.join(self.test_scripts_folder				,       'test_peak_memory_usage.py'     )
  lowerCAmelCase    				= [
      'accelerate',
      'launch',
      '--num_processes=2',
      '--num_machines=1',
      '--machine_rank=0',
  ]
  for spec, peak_mem_upper_bound in self.peak_memory_usage_upper_bound.items():
   lowerCAmelCase    				= cmd.copy()
   if "fp16" in spec:
    cmd_config.extend(['--mixed_precision=fp16']     )
   else:
    cmd_config.extend(['--mixed_precision=no']     )

   if "multi_gpu" in spec:
    continue
   else:
    cmd_config.extend(['--use_fsdp']     )
    for i, strategy in enumerate(_snake_case     ):
     if strategy.lower() in spec:
      cmd_config.append(F'--fsdp_sharding_strategy={i+1}'     )
      break

    if "cpu_offload" in spec:
     cmd_config.append('--fsdp_offload_params=True'     )

    for policy in FSDP_AUTO_WRAP_POLICY:
     if policy.lower() in spec:
      cmd_config.append(F'--fsdp_auto_wrap_policy={policy}'     )
      break

    if policy == "TRANSFORMER_BASED_WRAP":
     cmd_config.append('--fsdp_transformer_layer_cls_to_wrap=BertLayer'     )
    elif policy == "SIZE_BASED_WRAP":
     cmd_config.append('--fsdp_min_num_params=2000'     )

   cmd_config.extend(
       [
           self.test_file_path,
           F'--output_dir={self.tmpdir}',
           F'--peak_memory_upper_bound={peak_mem_upper_bound}',
           F'--n_train={self.n_train}',
           F'--n_val={self.n_val}',
       ]     )
   with patch_environment(omp_num_threads=1     ):
    execute_subprocess_async(_snake_case				,       env=os.environ.copy()     )












"""simple docstring"""
import numpy as np
import pandas as pd
from sklearn.preprocessing import Normalizer
from sklearn.svm import SVR
from statsmodels.tsa.statespace.sarimax import SARIMAX

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list			):
 lowerCAmelCase    				= np.array([[1, item, train_mtch[i]] for i, item in enumerate(_UpperCAmelCase			)]			)
 lowerCAmelCase    				= np.array(_UpperCAmelCase			)
 lowerCAmelCase    				= np.dot(np.dot(np.linalg.inv(np.dot(x.transpose()  ,     _UpperCAmelCase			)			)  ,     x.transpose()			)  ,     _UpperCAmelCase			)
 return abs(beta[0] + test_dt[0] * beta[1] + test_mtch[0] + beta[2]			)

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list			):
 lowerCAmelCase    				= (1, 2, 1)
 lowerCAmelCase    				= (1, 1, 0, 7)
 lowerCAmelCase    				= SARIMAX(
     _UpperCAmelCase  ,     exog=_UpperCAmelCase  ,     order=_UpperCAmelCase  ,     seasonal_order=_UpperCAmelCase			)
 lowerCAmelCase    				= model.fit(disp=_UpperCAmelCase  ,     maxiter=600  ,     method='nm'			)
 lowerCAmelCase    				= model_fit.predict(1  ,     len(_UpperCAmelCase			)  ,     exog=[test_match]			)
 return result[0]

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    list			):
 lowerCAmelCase    				= SVR(kernel='rbf'  ,     C=1  ,     gamma=0.1  ,     epsilon=0.1			)
 regressor.fit(_UpperCAmelCase  ,     _UpperCAmelCase			)
 lowerCAmelCase    				= regressor.predict(_UpperCAmelCase			)
 return y_pred[0]

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    list			):
 train_user.sort()
 lowerCAmelCase    				= np.percentile(_UpperCAmelCase  ,     25			)
 lowerCAmelCase    				= np.percentile(_UpperCAmelCase  ,     75			)
 lowerCAmelCase    				= qa - qa
 lowerCAmelCase    				= qa - (iqr * 0.1)
 return low_lim

def 		_SCREAMING_SNAKE_CASE			(_UpperCAmelCase					:    list  ,     _UpperCAmelCase					:    float			):
 lowerCAmelCase    				= 0
 lowerCAmelCase    				= 0
 for i in list_vote:
  if i > actual_result:
   lowerCAmelCase    				= not_safe + 1
  else:
   if abs(abs(_UpperCAmelCase			) - abs(_UpperCAmelCase			)			) <= 0.1:
    safe += 1
   else:
    not_safe += 1
 return safe > not_safe


if __name__ == "__main__":
       # data_input_df = pd.read_csv("ex_data.csv", header=None)
       __UpperCamelCase     :						Optional[Any]						       =  [[1_8231, 0.0, 1], [2_2621, 1.0, 2], [1_5675, 0.0, 3], [2_3583, 1.0, 4]]
       __UpperCamelCase     :						Any						       =  pd.DataFrame(
           data_input, columns=['''total_user''', '''total_even''', '''days''']
       )
       __UpperCamelCase     :						Dict						       =  Normalizer().fit_transform(data_input_df.values)
       # split data
       __UpperCamelCase     :						Dict						       =  normalize_df[:, 2].tolist()
       __UpperCamelCase     :						Union[str, Any]						       =  normalize_df[:, 0].tolist()
       __UpperCamelCase     :						List[str]						       =  normalize_df[:, 1].tolist()

       # for svr (input variable = total date and total match)
       __UpperCamelCase     :						Optional[int]						       =  normalize_df[:, [1, 2]].tolist()
       __UpperCamelCase     :						Tuple						       =  x[: len(x) - 1]
       __UpperCamelCase     :						Any						       =  x[len(x) - 1 :]

       # for linear regression & sarimax
       __UpperCamelCase     :						str						       =  total_date[: len(total_date) - 1]
       __UpperCamelCase     :						Union[str, Any]						       =  total_user[: len(total_user) - 1]
       __UpperCamelCase     :						List[Any]						       =  total_match[: len(total_match) - 1]

       __UpperCamelCase     :						Optional[Any]						       =  total_date[len(total_date) - 1 :]
       __UpperCamelCase     :						str						       =  total_user[len(total_user) - 1 :]
       __UpperCamelCase     :						str						       =  total_match[len(total_match) - 1 :]

       # voting system with forecasting
       __UpperCamelCase     :						Any						       =  [
           linear_regression_prediction(
               trn_date, trn_user, trn_match, tst_date, tst_match
           ),
           sarimax_predictor(trn_user, trn_match, tst_match),
           support_vector_regressor(x_train, x_test, trn_user),
       ]

       # check the safety of today's data
       __UpperCamelCase     :						List[str]						       =  '''''' if data_safety_checker(res_vote, tst_user) else '''not '''
       print('''Today\'s data is {not_str}safe.''')





