




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		:				List[str]       			=		False
__lowerCAmelCase		:				str       			=		False




def 			__magic_name__				( A		:					Namespace   ):






			'''simple docstring'''







			return TrainCommand(A   )






class     snake_case__							(snake_case_   ):
				"""simple docstring"""



				@staticmethod
				def 							__UpperCAmelCase							(      __lowerCamelCase				:     List[Any]							)					->     Optional[int]:
							a    =			parser.add_parser("train"					,      help="CLI tool to train a model on a task."							)

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

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





				def __init__(      self				:     Any					,      __lowerCamelCase				:     Union[str, Any]							)					->     int:
							a    =			logging.get_logger("transformers-cli/training"							)

							a    =			"tf" if is_tf_available() else "torch"

							os.makedirs(args.output					,      exist_ok=UpperCamelCase__							)
							a    =			args.output

							a    =			args.column_label
							a    =			args.column_text
							a    =			args.column_id

							self.logger.info(f"""Loading {args.task} pipeline for {args.model}"""							)
							if args.task == "text_classification":
										a    =			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    =			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    =			None
							if args.validation_data:
										self.logger.info(f"""Loading validation dataset from {args.validation_data}"""							)
										a    =			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    =			args.validation_split
							a    =			args.train_batch_size
							a    =			args.valid_batch_size
							a    =			args.learning_rate
							a    =			args.adam_epsilon





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




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





				def 							__UpperCAmelCase							(      self				:     List[str]							)					->     Union[str, Any]:
							raise NotImplementedError





				def 							__UpperCAmelCase							(      self				:     Any							)					->     Optional[int]:
							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"""



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__snake_case   			=    logging.get_logger(__name__)

__snake_case   			=    {
    """facebook/xmod-base""": """https://huggingface.co/facebook/xmod-base/resolve/main/config.json""",
    """facebook/xmod-large-prenorm""": """https://huggingface.co/facebook/xmod-large-prenorm/resolve/main/config.json""",
    """facebook/xmod-base-13-125k""": """https://huggingface.co/facebook/xmod-base-13-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-125k""": """https://huggingface.co/facebook/xmod-base-30-125k/resolve/main/config.json""",
    """facebook/xmod-base-30-195k""": """https://huggingface.co/facebook/xmod-base-30-195k/resolve/main/config.json""",
    """facebook/xmod-base-60-125k""": """https://huggingface.co/facebook/xmod-base-60-125k/resolve/main/config.json""",
    """facebook/xmod-base-60-265k""": """https://huggingface.co/facebook/xmod-base-60-265k/resolve/main/config.json""",
    """facebook/xmod-base-75-125k""": """https://huggingface.co/facebook/xmod-base-75-125k/resolve/main/config.json""",
    """facebook/xmod-base-75-269k""": """https://huggingface.co/facebook/xmod-base-75-269k/resolve/main/config.json""",
}







class _lowerCAmelCase				(      snake_case_	):
   __UpperCAmelCase :       Optional[Any]							  =  '''xmod'''







   def __init__(	self						,	UpperCamelCase__=3_0522						,	UpperCamelCase__=768						,	UpperCamelCase__=12						,	UpperCamelCase__=12						,	UpperCamelCase__=3072						,	UpperCamelCase__="gelu"						,	UpperCamelCase__=0.1						,	UpperCamelCase__=0.1						,	UpperCamelCase__=512						,	UpperCamelCase__=2						,	UpperCamelCase__=0.02						,	UpperCamelCase__=1e-12						,	UpperCamelCase__=1						,	UpperCamelCase__=0						,	UpperCamelCase__=2						,	UpperCamelCase__="absolute"						,	UpperCamelCase__=True						,	UpperCamelCase__=None						,	UpperCamelCase__=False						,	UpperCamelCase__=2						,	UpperCamelCase__=False						,	UpperCamelCase__=True						,	UpperCamelCase__=True						,	UpperCamelCase__=("en_XX",)						,	UpperCamelCase__=None						,	**UpperCamelCase__						,	)	->		int:

       '''simple docstring'''




       super().__init__(pad_token_id=UpperCamelCase__						,	bos_token_id=UpperCamelCase__						,	eos_token_id=UpperCamelCase__						,	**UpperCamelCase__						)

       snake_case    :      List[Any]      					=    vocab_size
       snake_case    :      List[Any]      					=    hidden_size
       snake_case    :      Union[str, Any]      					=    num_hidden_layers
       snake_case    :      Any      					=    num_attention_heads
       snake_case    :      List[str]      					=    hidden_act
       snake_case    :      Union[str, Any]      					=    intermediate_size
       snake_case    :      int      					=    hidden_dropout_prob
       snake_case    :      Any      					=    attention_probs_dropout_prob
       snake_case    :      Optional[int]      					=    max_position_embeddings
       snake_case    :      Tuple      					=    type_vocab_size
       snake_case    :      List[str]      					=    initializer_range
       snake_case    :      int      					=    layer_norm_eps
       snake_case    :      Optional[Any]      					=    position_embedding_type
       snake_case    :      int      					=    use_cache
       snake_case    :      Dict      					=    classifier_dropout
       snake_case    :      Dict      					=    pre_norm
       snake_case    :      Union[str, Any]      					=    adapter_reduction_factor
       snake_case    :      Any      					=    adapter_layer_norm
       snake_case    :      Optional[int]      					=    adapter_reuse_layer_norm
       snake_case    :      List[Any]      					=    ln_before_adapter
       snake_case    :      str      					=    list(UpperCamelCase__						)
       snake_case    :      int      					=    default_language







class _lowerCAmelCase				(      snake_case_	):







   @property
   def  lowerCamelCase		(	self						)	->		Mapping[str, Mapping[int, str]]:

       '''simple docstring'''




       if self.task == "multiple-choice":
           snake_case    :      Optional[int]      					=    {0: "batch", 1: "choice", 2: "sequence"}
       else:
           snake_case    :      Optional[Any]      					=    {0: "batch", 1: "sequence"}
       return OrderedDict(
           [
               ("input_ids", dynamic_axis),
               ("attention_mask", dynamic_axis),
           ]						)





