






'''simple docstring'''
from typing import Tuple, Union

from ...modeling_outputs import BackboneOutput
from ...modeling_utils import PreTrainedModel
from ...utils import is_timm_available, is_torch_available, requires_backends
from ...utils.backbone_utils import BackboneMixin
from .configuration_timm_backbone import TimmBackboneConfig


if is_timm_available():
  import timm


if is_torch_available():
  from torch import Tensor






class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase	,				lowerCamelCase				):
 snake_case_  							=							"""pixel_values"""
 snake_case_  							=							False
 snake_case_  							=							TimmBackboneConfig




 def __init__(      self	:							Tuple					,      __lowercase	:							Dict					,      **__lowercase	:							List[str]      )	->						Any:
       requires_backends(self					,      '''timm'''      )
       super().__init__(UpperCamelCase_      )
       SCREAMING_SNAKE_CASE__     :				str									=config

       if config.backbone is None:
             raise ValueError('''backbone is not set in the config. Please set it to a timm model name.'''      )

       if config.backbone not in timm.list_models():
             raise ValueError(F"backbone {config.backbone} is not supported by timm."      )

       if hasattr(UpperCamelCase_					,      '''out_features'''      ) and config.out_features is not None:
             raise ValueError('''out_features is not supported by TimmBackbone. Please use out_indices instead.'''      )

       SCREAMING_SNAKE_CASE__     :				Optional[int]									=getattr(UpperCamelCase_					,      '''use_pretrained_backbone'''					,      UpperCamelCase_      )
       if pretrained is None:
             raise ValueError('''use_pretrained_backbone is not set in the config. Please set it to True or False.'''      )

       # We just take the final layer by default. This matches the default for the transformers models.
       SCREAMING_SNAKE_CASE__     :				int									=config.out_indices if getattr(UpperCamelCase_					,      '''out_indices'''					,      UpperCamelCase_      ) is not None else (-1,)

       SCREAMING_SNAKE_CASE__     :				Any									=timm.create_model(
           config.backbone					,      pretrained=UpperCamelCase_					,      features_only=config.features_only					,      in_chans=config.num_channels					,      out_indices=UpperCamelCase_					,      **UpperCamelCase_					,      )
       # These are used to control the output of the model when called. If output_hidden_states is True, then
       # return_layers is modified to include all layers.
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=self._backbone.return_layers
       SCREAMING_SNAKE_CASE__     :				Any									={layer['module']: str(UpperCamelCase_      ) for i, layer in enumerate(self._backbone.feature_info.info      )}
       super()._init_backbone(UpperCamelCase_      )




 @classmethod
 def      __magic_name__	(      cls	:							int					,      __lowercase	:							List[Any]					,      *__lowercase	:							str					,      **__lowercase	:							Dict      )	->						List[Any]:
       requires_backends(cls					,      ['''vision''', '''timm''']      )
       from ...models.timm_backbone import TimmBackboneConfig

       SCREAMING_SNAKE_CASE__     :				Dict									=kwargs.pop('''config'''					,      TimmBackboneConfig()      )

       SCREAMING_SNAKE_CASE__     :				List[Any]									=kwargs.pop('''use_timm_backbone'''					,      UpperCamelCase_      )
       if not use_timm:
             raise ValueError('''use_timm_backbone must be True for timm backbones'''      )

       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=kwargs.pop('''num_channels'''					,      config.num_channels      )
       SCREAMING_SNAKE_CASE__     :				Dict									=kwargs.pop('''features_only'''					,      config.features_only      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=kwargs.pop('''use_pretrained_backbone'''					,      config.use_pretrained_backbone      )
       SCREAMING_SNAKE_CASE__     :				Any									=kwargs.pop('''out_indices'''					,      config.out_indices      )
       SCREAMING_SNAKE_CASE__     :				Optional[int]									=TimmBackboneConfig(
           backbone=UpperCamelCase_					,      num_channels=UpperCamelCase_					,      features_only=UpperCamelCase_					,      use_pretrained_backbone=UpperCamelCase_					,      out_indices=UpperCamelCase_					,      )
       return super()._from_config(UpperCamelCase_					,      **UpperCamelCase_      )




 def      __magic_name__	(      self	:							str					,      __lowercase	:							Union[str, Any]      )	->						int:
       pass




 def      __magic_name__	(      self	:							Union[str, Any]					,      __lowercase	:							Optional[int]					,      __lowercase	:							Union[str, Any]=None					,      __lowercase	:							int=None					,      __lowercase	:							Dict=None					,      **__lowercase	:							Union[str, Any]      )	->						Union[BackboneOutput, Tuple[Tensor, ...]]:
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=return_dict if return_dict is not None else self.config.use_return_dict
       SCREAMING_SNAKE_CASE__     :				List[Any]									=(
           output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
       )
       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=output_attentions if output_attentions is not None else self.config.output_attentions

       if output_attentions:
             raise ValueError('''Cannot output attentions for timm backbones at the moment'''      )

       if output_hidden_states:
             # We modify the return layers to include all the stages of the backbone
             SCREAMING_SNAKE_CASE__     :				Tuple									=self._all_layers
             SCREAMING_SNAKE_CASE__     :				Any									=self._backbone(UpperCamelCase_					,      **UpperCamelCase_      )
             SCREAMING_SNAKE_CASE__     :				List[Any]									=self._return_layers
             SCREAMING_SNAKE_CASE__     :				Tuple									=tuple(hidden_states[i] for i in self.out_indices      )
       else:
             SCREAMING_SNAKE_CASE__     :				List[Any]									=self._backbone(UpperCamelCase_					,      **UpperCamelCase_      )
             SCREAMING_SNAKE_CASE__     :				Tuple									=None

       SCREAMING_SNAKE_CASE__     :				Dict									=tuple(UpperCamelCase_      )
       SCREAMING_SNAKE_CASE__     :				Tuple									=tuple(UpperCamelCase_      ) if hidden_states is not None else None

       if not return_dict:
             SCREAMING_SNAKE_CASE__     :				List[str]									=(feature_maps,)
             if output_hidden_states:
                   SCREAMING_SNAKE_CASE__     :				Tuple									=output + (hidden_states,)
             return output

       return BackboneOutput(feature_maps=UpperCamelCase_					,      hidden_states=UpperCamelCase_					,      attentions=UpperCamelCase_      )













'''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
a_					= False
a_					= False





def  _a(  UpperCamelCase__				:						Namespace							):




      '''simple docstring'''






      return TrainCommand(UpperCamelCase__							)







class      __SCREAMING_SNAKE_CASE      (			lowerCamelCase				):




 @staticmethod
 def      __magic_name__	(      __lowercase	:							ArgumentParser      )	->						Any:
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=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	:							Tuple					,      __lowercase	:							Namespace      )	->						List[str]:
       SCREAMING_SNAKE_CASE__     :				Tuple									=logging.get_logger('''transformers-cli/training'''      )

       SCREAMING_SNAKE_CASE__     :				int									='''tf''' if is_tf_available() else '''torch'''

       os.makedirs(args.output					,      exist_ok=__lowercase      )
       SCREAMING_SNAKE_CASE__     :				Any									=args.output

       SCREAMING_SNAKE_CASE__     :				str									=args.column_label
       SCREAMING_SNAKE_CASE__     :				List[Any]									=args.column_text
       SCREAMING_SNAKE_CASE__     :				Tuple									=args.column_id

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

       SCREAMING_SNAKE_CASE__     :				Optional[Any]									=args.validation_split
       SCREAMING_SNAKE_CASE__     :				List[Any]									=args.train_batch_size
       SCREAMING_SNAKE_CASE__     :				Any									=args.valid_batch_size
       SCREAMING_SNAKE_CASE__     :				Union[str, Any]									=args.learning_rate
       SCREAMING_SNAKE_CASE__     :				int									=args.adam_epsilon




 def      __magic_name__	(      self	:							Any      )	->						str:
       if self.framework == "tf":
             return self.run_tf()
       return self.run_torch()




 def      __magic_name__	(      self	:							Optional[int]      )	->						Tuple:
       raise NotImplementedError




 def      __magic_name__	(      self	:							Dict      )	->						List[Any]:
       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      )