





'''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
lowerCAmelCase     :   str       		=      False
lowerCAmelCase     :   int       		=      False






def      __A						(							a_						:  Namespace	):
		return TrainCommand(a_	)

class 		lowerCamelCase  (      __a       ):



	@staticmethod
	def   _lowerCamelCase    (   a_	):
			lowerCAmelCase     :    Tuple						=   parser.add_parser("train"		,      help="CLI tool to train a model on a task."	)

			train_parser.add_argument(
			    "--train_data"		,      type=A__		,      required=A__		,      help="path to train (and optionally evaluation) dataset as a csv with tab separated labels and sentences."		,      )
			train_parser.add_argument(
			    "--column_label"		,      type=A__		,      default=0		,      help="Column of the dataset csv file with example labels."	)
			train_parser.add_argument(
			    "--column_text"		,      type=A__		,      default=1		,      help="Column of the dataset csv file with example texts."	)
			train_parser.add_argument(
			    "--column_id"		,      type=A__		,      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=A__		,      default=""		,      help="path to validation dataset."	)
			train_parser.add_argument(
			    "--validation_split"		,      type=A__		,      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=A__		,      default="./"		,      help="path to saved the trained model."	)

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



	def __init__(   self		,      a_	):
			lowerCAmelCase     :    Optional[Any]						=   logging.get_logger("transformers-cli/training"	)

			lowerCAmelCase     :    Optional[int]						=   "tf" if is_tf_available() else "torch"

			os.makedirs(args.output		,      exist_ok=A__	)
			lowerCAmelCase     :    Union[str, Any]						=   args.output

			lowerCAmelCase     :    List[str]						=   args.column_label
			lowerCAmelCase     :    List[str]						=   args.column_text
			lowerCAmelCase     :    Optional[int]						=   args.column_id

			self.logger.info(F'''Loading {args.task} pipeline for {args.model}'''	)
			if args.task == "text_classification":
					lowerCAmelCase     :    Optional[int]						=   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}'''	)
			lowerCAmelCase     :    List[str]						=   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		,      )
			lowerCAmelCase     :    List[Any]						=   None
			if args.validation_data:
					self.logger.info(F'''Loading validation dataset from {args.validation_data}'''	)
					lowerCAmelCase     :    Dict						=   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		,      )

			lowerCAmelCase     :    List[Any]						=   args.validation_split
			lowerCAmelCase     :    Optional[int]						=   args.train_batch_size
			lowerCAmelCase     :    Optional[Any]						=   args.valid_batch_size
			lowerCAmelCase     :    Dict						=   args.learning_rate
			lowerCAmelCase     :    Tuple						=   args.adam_epsilon



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



	def   _lowerCamelCase    (   self	):
			raise NotImplementedError





	def   _lowerCamelCase    (   self	):
			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 os
import tempfile
import unittest
from pathlib import Path

from transformers import AutoConfig, is_tf_available
from transformers.testing_utils import require_tf


if is_tf_available():
					import tensorflow as tf

					from transformers import TensorFlowBenchmark, TensorFlowBenchmarkArguments
@require_tf
class 		lowerCamelCase  (      unittest.TestCase       ):



	def   _lowerCamelCase    (   self		,      a_	):
			for model_result in results.values():
					for batch_size, sequence_length in zip(model_result["bs"]		,      model_result["ss"]	):
							lowerCAmelCase     :    Tuple						=   model_result["result"][batch_size][sequence_length]
							self.assertIsNotNone(a_	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Optional[int]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Tuple						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      eager_mode=a_		,      multi_process=a_		,      )
			lowerCAmelCase     :    Optional[Any]						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    Optional[Any]						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Union[str, Any]						=   "sgugger/tiny-distilbert-classification"
			lowerCAmelCase     :    Tuple						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      only_pretrain_model=a_		,      )
			lowerCAmelCase     :    Any						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Union[str, Any]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    str						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[str]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Union[str, Any]						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Optional[Any]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      eager_mode=a_		,      multi_process=a_		,      )
			lowerCAmelCase     :    Any						=   TensorFlowBenchmark(a_		,      [config]	)
			lowerCAmelCase     :    Union[str, Any]						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    int						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Union[str, Any]						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Union[str, Any]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    Tuple						=   TensorFlowBenchmark(a_		,      [config]	)
			lowerCAmelCase     :    Any						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_train_result	)
			self.check_results_dict_not_empty(results.memory_train_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    List[Any]						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    Union[str, Any]						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Dict						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    Dict						=   TensorFlowBenchmark(a_		,      [config]	)
			lowerCAmelCase     :    int						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_train_result	)
			self.check_results_dict_not_empty(results.memory_train_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    str						=   "patrickvonplaten/t5-tiny-random"
			lowerCAmelCase     :    Tuple						=   AutoConfig.from_pretrained(a_	)
			lowerCAmelCase     :    Dict						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      multi_process=a_		,      )
			lowerCAmelCase     :    List[str]						=   TensorFlowBenchmark(a_		,      configs=[config]	)
			lowerCAmelCase     :    List[Any]						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	@unittest.skipIf(is_tf_available() and len(tf.config.list_physical_devices("GPU"	)	) == 0		,      "Cannot do xla on CPU."	)
	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    str						=   "sshleifer/tiny-gpt2"
			lowerCAmelCase     :    List[Any]						=   TensorFlowBenchmarkArguments(
			    models=[MODEL_ID]		,      training=a_		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      use_xla=a_		,      multi_process=a_		,      )
			lowerCAmelCase     :    Any						=   TensorFlowBenchmark(a_	)
			lowerCAmelCase     :    str						=   benchmark.run()
			self.check_results_dict_not_empty(results.time_inference_result	)
			self.check_results_dict_not_empty(results.memory_inference_result	)



	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Tuple						=   "sshleifer/tiny-gpt2"
			with tempfile.TemporaryDirectory() as tmp_dir:
					lowerCAmelCase     :    Optional[int]						=   TensorFlowBenchmarkArguments(
					    models=[MODEL_ID]		,      inference=a_		,      save_to_csv=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      inference_time_csv_file=os.path.join(a_		,      "inf_time.csv"	)		,      inference_memory_csv_file=os.path.join(a_		,      "inf_mem.csv"	)		,      env_info_csv_file=os.path.join(a_		,      "env.csv"	)		,      multi_process=a_		,      )
					lowerCAmelCase     :    Optional[Any]						=   TensorFlowBenchmark(a_	)
					benchmark.run()
					self.assertTrue(Path(os.path.join(a_		,      "inf_time.csv"	)	).exists()	)
					self.assertTrue(Path(os.path.join(a_		,      "inf_mem.csv"	)	).exists()	)
					self.assertTrue(Path(os.path.join(a_		,      "env.csv"	)	).exists()	)





	def   _lowerCamelCase    (   self	):
			lowerCAmelCase     :    Any						=   "sshleifer/tiny-gpt2"

			def _check_summary_is_not_empty(a_	):
					self.assertTrue(hasattr(a_		,      "sequential"	)	)
					self.assertTrue(hasattr(a_		,      "cumulative"	)	)
					self.assertTrue(hasattr(a_		,      "current"	)	)
					self.assertTrue(hasattr(a_		,      "total"	)	)

			with tempfile.TemporaryDirectory() as tmp_dir:
					lowerCAmelCase     :    int						=   TensorFlowBenchmarkArguments(
					    models=[MODEL_ID]		,      inference=a_		,      sequence_lengths=[8]		,      batch_sizes=[1]		,      log_filename=os.path.join(a_		,      "log.txt"	)		,      log_print=a_		,      trace_memory_line_by_line=a_		,      eager_mode=a_		,      multi_process=a_		,      )
					lowerCAmelCase     :    Optional[Any]						=   TensorFlowBenchmark(a_	)
					lowerCAmelCase     :    Tuple						=   benchmark.run()
					_check_summary_is_not_empty(result.inference_summary	)
					self.assertTrue(Path(os.path.join(a_		,      "log.txt"	)	).exists()	)



