"""simple docstring"""
import os
from argparse import ArgumentParser, Namespace

from ..data import SingleSentenceClassificationProcessor as Processor
from ..pipelines import TextClassificationPipeline
from ..utils import is_tf_available, is_torch_available, logging
from . import BaseTransformersCLICommand


if not is_tf_available() and not is_torch_available():
 raise RuntimeError('At least one of PyTorch or TensorFlow 2.0+ should be installed to use CLI training')

# TF training parameters
lowercase__ 	=     False
lowercase__ 	=     False







def 						__a       (     _SCREAMING_SNAKE_CASE       )     ->List[str]:
  return TrainCommand(_SCREAMING_SNAKE_CASE       )




class        __snake_case	(					__lowerCAmelCase    ):



      @staticmethod
      def   lowerCamelCase_       (						lowercase)     ->      Dict:





        '''simple docstring'''





        a__:					Dict      							=     parser.add_parser('train'   ,      help='CLI tool to train a model on a task.')

        train_parser.add_argument(
            '--train_data'   ,      type=lowercase   ,      required=lowercase   ,      help='path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences.'   ,      )
        train_parser.add_argument(
            '--column_label'   ,      type=lowercase   ,      default=0   ,      help='Column of the dataset csv file with example labels.')
        train_parser.add_argument(
            '--column_text'   ,      type=lowercase   ,      default=1   ,      help='Column of the dataset csv file with example texts.')
        train_parser.add_argument(
            '--column_id'   ,      type=lowercase   ,      default=2   ,      help='Column of the dataset csv file with example ids.')
        train_parser.add_argument(
            '--skip_first_row'   ,      action='store_true'   ,      help='Skip the first row of the csv file (headers).')

        train_parser.add_argument('--validation_data'   ,      type=lowercase   ,      default=''   ,      help='path to validation dataset.')
        train_parser.add_argument(
            '--validation_split'   ,      type=lowercase   ,      default=0.1   ,      help='if validation dataset is not provided, fraction of train dataset to use as validation dataset.'   ,      )

        train_parser.add_argument('--output'   ,      type=lowercase   ,      default='./'   ,      help='path to saved the trained model.')

        train_parser.add_argument(
            '--task'   ,      type=lowercase   ,      default='text_classification'   ,      help='Task to train the model on.')
        train_parser.add_argument(
            '--model'   ,      type=lowercase   ,      default='bert-base-uncased'   ,      help='Model\'s name or path to stored model.')
        train_parser.add_argument('--train_batch_size'   ,      type=lowercase   ,      default=32   ,      help='Batch size for training.')
        train_parser.add_argument('--valid_batch_size'   ,      type=lowercase   ,      default=64   ,      help='Batch size for validation.')
        train_parser.add_argument('--learning_rate'   ,      type=lowercase   ,      default=3e-5   ,      help='Learning rate.')
        train_parser.add_argument('--adam_epsilon'   ,      type=lowercase   ,      default=1e-08   ,      help='Epsilon for Adam optimizer.')
        train_parser.set_defaults(func=lowercase)



      def __init__(						self   ,      lowercase)     ->      int:





        '''simple docstring'''





        a__:					List[Any]      							=     logging.get_logger('transformers-cli/training')

        a__:					Tuple      							=     'tf' if is_tf_available() else 'torch'

        os.makedirs(args.output   ,      exist_ok=lowercase)
        a__:					Optional[Any]      							=     args.output

        a__:					Union[str, Any]      							=     args.column_label
        a__:					Tuple      							=     args.column_text
        a__:					int      							=     args.column_id

        self.logger.info(f'Loading {args.task} pipeline for {args.model}')
        if args.task == "text_classification":
          a__:					Tuple      							=     TextClassificationPipeline.from_pretrained(args.model)
        elif args.task == "token_classification":
          raise NotImplementedError
        elif args.task == "question_answering":
          raise NotImplementedError

        self.logger.info(f'Loading dataset from {args.train_data}')
        a__:					Union[str, Any]      							=     Processor.create_from_csv(
            args.train_data   ,      column_label=args.column_label   ,      column_text=args.column_text   ,      column_id=args.column_id   ,      skip_first_row=args.skip_first_row   ,      )
        a__:					List[str]      							=     None
        if args.validation_data:
          self.logger.info(f'Loading validation dataset from {args.validation_data}')
          a__:					Optional[Any]      							=     Processor.create_from_csv(
              args.validation_data   ,      column_label=args.column_label   ,      column_text=args.column_text   ,      column_id=args.column_id   ,      skip_first_row=args.skip_first_row   ,      )

        a__:					str      							=     args.validation_split
        a__:					Optional[int]      							=     args.train_batch_size
        a__:					Tuple      							=     args.valid_batch_size
        a__:					str      							=     args.learning_rate
        a__:					Optional[Any]      							=     args.adam_epsilon



      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





        if self.framework == "tf":
          return self.run_tf()
        return self.run_torch()



      def   lowerCamelCase_       (						self)     ->      Any:





        '''simple docstring'''





        raise NotImplementedError



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





        '''simple docstring'''





        self.pipeline.fit(
            self.train_dataset   ,      validation_data=self.valid_dataset   ,      validation_split=self.validation_split   ,      learning_rate=self.learning_rate   ,      adam_epsilon=self.adam_epsilon   ,      train_batch_size=self.train_batch_size   ,      valid_batch_size=self.valid_batch_size   ,      )

        # Save trained pipeline
        self.pipeline.save_pretrained(self.output)



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

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



      def   lowerCamelCase_       (						self   ,      lowercase   ,      lowercase   ,      lowercase = None   ,      lowercase = None)     ->      Union[str, Any]:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      int:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      Optional[int]:





        '''simple docstring'''





        a__:					Dict      							=     os.path.abspath(os.path.join('examples'   ,      'cv_example.py'))
        a__:					Tuple      							=     [
            ' ' * 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        __snake_case	(					__lowerCAmelCase    ):
      a__				=		False



      @classmethod
      def   lowerCamelCase_       (						cls)     ->      List[str]:





        '''simple docstring'''





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

        write_basic_config(save_location=cls.configPath)
        a__:					List[str]      							=     ['accelerate', 'launch', '--config_file', cls.configPath]



      @classmethod
      def   lowerCamelCase_       (						cls)     ->      List[str]:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      List[str]:





        '''simple docstring'''





        a__:					Optional[Any]      							=     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   lowerCamelCase_       (						self)     ->      Union[str, Any]:





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      str:





        '''simple docstring'''





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



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





        '''simple docstring'''





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





        '''simple docstring'''





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



      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        a__:					str      							=     ['examples/by_feature/multi_process_metrics.py']
        run_command(self._launch_args + testargs)



      @require_trackers
      @mock.patch.dict(os.environ   ,      {'WANDB_MODE': 'offline'})
      def   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        with tempfile.TemporaryDirectory() as tmpdir:
          a__:					List[str]      							=     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   lowerCamelCase_       (						self)     ->      Tuple:





        '''simple docstring'''





        a__:					int      							=     ['examples/by_feature/gradient_accumulation.py']
        run_command(self._launch_args + testargs)



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





        '''simple docstring'''





        a__:					Union[str, Any]      							=     ['examples/by_feature/local_sgd.py']
        run_command(self._launch_args + testargs)


