






'''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					):




      """simple docstring"""




      def 					A__   (			self      :		str  ,   UpperCAmelCase      :		str  ,   UpperCAmelCase      :		bool  ,   UpperCAmelCase      :		str = None  ,   UpperCAmelCase      :		list = None		)	->	Optional[int]:


       '''simple docstring'''

       lowercase       : Optional[Any]						=None
       lowercase       : Any						=os.path.abspath(os.path.join('''examples'''  ,   '''by_feature'''		)		)
       lowercase       : List[str]						=os.path.abspath('''examples'''		)
       for item in os.listdir(UpperCAmelCase		):
        if item not in EXCLUDE_EXAMPLES:
         lowercase       : Union[str, Any]						=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()'''  ,   ):
           lowercase       : List[Any]						=compare_against_test(
               os.path.join(UpperCAmelCase  ,   UpperCAmelCase		)  ,   UpperCAmelCase  ,   UpperCAmelCase  ,   UpperCAmelCase		)
           lowercase       : Optional[int]						='''\n'''.join(UpperCAmelCase		)
           if special_strings is not None:
            for string in special_strings:
             lowercase       : Optional[Any]						=diff.replace(UpperCAmelCase  ,   ''''''		)
           self.assertEqual(UpperCAmelCase  ,   ''''''		)






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


       '''simple docstring'''

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






      def 					A__   (			self      :		str		)	->	Any:


       '''simple docstring'''

       lowercase       : str						=os.path.abspath(os.path.join('''examples'''  ,   '''cv_example.py'''		)		)
       lowercase       : 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'''  ,   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_       (     __A					):




      """simple docstring"""




      UpperCamelCase_             =      False






      @classmethod
      def 					A__   (			cls      :		Optional[Any]		)	->	List[Any]:


       '''simple docstring'''

       super().setUpClass()
       lowercase       : List[str]						=tempfile.mkdtemp()
       lowercase       : Union[str, Any]						=os.path.join(cls._tmpdir  ,   '''default_config.yml'''		)

       write_basic_config(save_location=cls.configPath		)
       lowercase       : Any						=['''accelerate''', '''launch''', '''--config_file''', cls.configPath]






      @classmethod
      def 					A__   (			cls      :		Optional[Any]		)	->	Tuple:


       '''simple docstring'''

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






      def 					A__   (			self      :		List[str]		)	->	Tuple:


       '''simple docstring'''

       lowercase       : Dict						=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 					A__   (			self      :		int		)	->	Union[str, Any]:


       '''simple docstring'''

       lowercase       : Dict						=f'\n        examples/by_feature/checkpointing.py\n        --checkpointing_steps 1\n        --output_dir {self.tmpdir}\n        '.split()
       lowercase       : str						=run_command(self._launch_args + testargs		)
       self.assertTrue(os.path.exists(os.path.join(self.tmpdir  ,   '''step_2'''		)		)		)






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


       '''simple docstring'''

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






      def 					A__   (			self      :		str		)	->	List[Any]:


       '''simple docstring'''

       lowercase       : List[str]						=f'\n        examples/by_feature/checkpointing.py\n        --resume_from_checkpoint {os.path.join(self.tmpdir  ,   "step_2"		)}\n        '.split()
       lowercase       : Tuple						=run_command(self._launch_args + testargs  ,   return_stdout=UpperCAmelCase		)
       if torch.cuda.is_available():
        lowercase       : List[Any]						=torch.cuda.device_count()
       else:
        lowercase       : Dict						=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 					A__   (			self      :		Any		)	->	Union[str, Any]:


       '''simple docstring'''

       lowercase       : List[Any]						='''
        examples/by_feature/cross_validation.py
        --num_folds 2
        '''.split()
       with mock.patch.dict(os.environ  ,   {'''TESTING_MOCKED_DATALOADERS''': '''0'''}		):
        lowercase       : List[str]						=run_command(self._launch_args + testargs  ,   return_stdout=UpperCAmelCase		)
        lowercase       : List[Any]						=re.findall('''({.+})'''  ,   UpperCAmelCase		)
        lowercase       : Tuple						=[r for r in results if '''accuracy''' in r][-1]
        lowercase       : Union[str, Any]						=ast.literal_eval(UpperCAmelCase		)
        self.assertGreaterEqual(results['''accuracy''']  ,   0.7_5		)






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


       '''simple docstring'''

       with tempfile.TemporaryDirectory() as tmpdir:
        lowercase       : Tuple						=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(UpperCAmelCase  ,   '''tracking'''		)		)		)






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


       '''simple docstring'''

       lowercase       : Optional[Any]						=['''examples/by_feature/gradient_accumulation.py''']
       run_command(self._launch_args + testargs		)






      def 					A__   (			self      :		Dict		)	->	Tuple:


       '''simple docstring'''

       lowercase       : List[str]						=['''examples/by_feature/local_sgd.py''']
       run_command(self._launch_args + testargs		)








'''simple docstring'''





import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
     CTRLForSequenceClassification,
     CTRLLMHeadModel,
     CTRLModel,
 )


class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=14  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=32  ,UpperCAmelCase_=5  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,)	->				Dict:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    seq_length
           lowercase__									=    is_training
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_input_mask
           lowercase__									=    use_labels
           lowercase__									=    use_mc_token_ids
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    num_choices
           lowercase__									=    scope
           lowercase__									=    self.vocab_size - 1



       def 				_a   (					self   )	->				Any:
           lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

           lowercase__									=    None
           if self.use_input_mask:
               lowercase__									=    random_attention_mask([self.batch_size, self.seq_length]   )

           lowercase__									=    None
           if self.use_token_type_ids:
               lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.type_vocab_size   )

           lowercase__									=    None
           if self.use_mc_token_ids:
               lowercase__									=    ids_tensor([self.batch_size, self.num_choices]  ,self.seq_length   )

           lowercase__									=    None
           lowercase__									=    None
           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
               lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.num_labels   )
               lowercase__									=    ids_tensor([self.batch_size]  ,self.num_choices   )

           lowercase__									=    self.get_config()

           lowercase__									=    ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,2   )

           return (
               config,
               input_ids,
               input_mask,
               head_mask,
               token_type_ids,
               mc_token_ids,
               sequence_labels,
               token_labels,
               choice_labels,
           )



       def 				_a   (					self   )	->				Optional[int]:
           return CTRLConfig(
               vocab_size=self.vocab_size  ,n_embd=self.hidden_size  ,n_layer=self.num_hidden_layers  ,n_head=self.num_attention_heads  ,n_positions=self.max_position_embeddings  ,pad_token_id=self.pad_token_id  ,)



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    CTRLModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,head_mask=UpperCAmelCase_   )
           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_   )
           self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)   )
           self.parent.assertEqual(len(result.past_key_values   )  ,config.n_layer   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    CTRLLMHeadModel(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.loss.shape  ,()   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.prepare_config_and_inputs()

           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs

           lowercase__									=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

           return config, inputs_dict







       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    self.num_labels
           lowercase__									=    CTRLForSequenceClassification(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Union[str, Any]     =       (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       lowerCAmelCase__	:Dict     =       (CTRLLMHeadModel,) if is_torch_available() else ()
       lowerCAmelCase__	:int     =       (
           {
               "feature-extraction": CTRLModel,
               "text-classification": CTRLForSequenceClassification,
               "text-generation": CTRLLMHeadModel,
               "zero-shot": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__	:str     =       True
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:List[Any]     =       False



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Tuple:
           if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
               # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
               # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
               # config could not be created.
               return True

           return False



       def 				_a   (					self   )	->				str:
           lowercase__									=    CTRLModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,n_embd=37   )



       def 				_a   (					self   )	->				List[Any]:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()



       def 				_a   (					self   )	->				Tuple:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase_   )



       @unittest.skip("Will be fixed soon by reducing the size of the model used for common tests."   )
       def 				_a   (					self   )	->				Tuple:
           pass



       @slow
       def 				_a   (					self   )	->				List[str]:
           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    CTRLModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )







       @unittest.skip("The model doesn't support left padding"   )  # and it's not used enough to be worth fixing :)
       def 				_a   (					self   )	->				Tuple:
           pass





@require_torch
class 					snake_case			(unittest.TestCase ):



       def 				_a   (					self   )	->				int:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()







       @slow
       def 				_a   (					self   )	->				int:
           lowercase__									=    CTRLLMHeadModel.from_pretrained("ctrl"   )
           model.to(UpperCAmelCase_   )
           lowercase__									=    torch.tensor(
               [[11_859, 0, 1_611, 8]]  ,dtype=torch.long  ,device=UpperCAmelCase_   )  # Legal the president is
           lowercase__									=    [
               11_859,
               0,
               1_611,
               8,
               5,
               150,
               26_449,
               2,
               19,
               348,
               469,
               3,
               2_595,
               48,
               20_740,
               246_533,
               246_533,
               19,
               30,
               5,
           ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

           lowercase__									=    model.generate(UpperCAmelCase_  ,do_sample=UpperCAmelCase_   )
           self.assertListEqual(output_ids[0].tolist()  ,UpperCAmelCase_   )




