


'''simple docstring'''



import logging
import os
import sys
from dataclasses import dataclass, field
from typing import Optional

from seqaseq_trainer import SeqaSeqTrainer
from seqaseq_training_args import SeqaSeqTrainingArguments

import transformers
from transformers import (
    AutoConfig,
    AutoModelForSeqaSeqLM,
    AutoTokenizer,
    HfArgumentParser,
    MBartTokenizer,
    MBartTokenizerFast,
    set_seed,
)
from transformers.trainer_utils import EvaluationStrategy, is_main_process
from transformers.training_args import ParallelMode
from utils import (
    SeqaSeqDataCollator,
    SeqaSeqDataset,
    assert_all_frozen,
    build_compute_metrics_fn,
    check_output_dir,
    freeze_embeds,
    freeze_params,
    lmap,
    save_json,
    use_task_specific_params,
    write_txt_file,
)


__UpperCAmelCase    :int				       =     logging.getLogger(__name__)



@dataclass
class 			a       :


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Any							     =     field(
         metadata={"help": "Path to pretrained model or model identifier from huggingface.co/models"} )
     SCREAMING_SNAKE_CASE :				Union[str, Any]							     =     field(
         default=UpperCAmelCase__			,						metadata={"help": "Pretrained config name or path if not the same as model_name"} )
     SCREAMING_SNAKE_CASE :				List[str]							     =     field(
         default=UpperCAmelCase__			,						metadata={"help": "Pretrained tokenizer name or path if not the same as model_name"} )
     SCREAMING_SNAKE_CASE :				Union[str, Any]							     =     field(
         default=UpperCAmelCase__			,						metadata={"help": "Where do you want to store the pretrained models downloaded from huggingface.co"}			,						)
     SCREAMING_SNAKE_CASE :				List[Any]							     =     field(default=UpperCAmelCase__			,						metadata={"help": "Whether tp freeze the encoder."} )
     SCREAMING_SNAKE_CASE :				Optional[Any]							     =     field(default=UpperCAmelCase__			,						metadata={"help": "Whether  to freeze the embeddings."} )



@dataclass
class 			a       :


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Any							     =     field(
         metadata={"help": "The input data dir. Should contain the .tsv files (or other data files) for the task."} )
     SCREAMING_SNAKE_CASE :				Dict							     =     field(
         default="summarization"			,						metadata={"help": "Task name, summarization (or summarization_{dataset} for pegasus) or translation"}			,						)
     SCREAMING_SNAKE_CASE :				Optional[Any]							     =     field(
         default=1_0_2_4			,						metadata={
             "help": (
                 "The maximum total input sequence length after tokenization. Sequences longer "
                 "than this will be truncated, sequences shorter will be padded."
             )
         }			,						)
     SCREAMING_SNAKE_CASE :				Optional[Any]							     =     field(
         default=1_2_8			,						metadata={
             "help": (
                 "The maximum total sequence length for target text after tokenization. Sequences longer "
                 "than this will be truncated, sequences shorter will be padded."
             )
         }			,						)
     SCREAMING_SNAKE_CASE :				int							     =     field(
         default=1_4_2			,						metadata={
             "help": (
                 "The maximum total sequence length for validation target text after tokenization. Sequences longer "
                 "than this will be truncated, sequences shorter will be padded. "
                 "This argument is also used to override the ``max_length`` param of ``model.generate``, which is used "
                 "during ``evaluate`` and ``predict``."
             )
         }			,						)
     SCREAMING_SNAKE_CASE :				int							     =     field(
         default=1_4_2			,						metadata={
             "help": (
                 "The maximum total sequence length for test target text after tokenization. Sequences longer "
                 "than this will be truncated, sequences shorter will be padded."
             )
         }			,						)
     SCREAMING_SNAKE_CASE :				Union[str, Any]							     =     field(default=-1			,						metadata={"help": "# training examples. -1 means use all."} )
     SCREAMING_SNAKE_CASE :				Tuple							     =     field(default=-1			,						metadata={"help": "# validation examples. -1 means use all."} )
     SCREAMING_SNAKE_CASE :				Any							     =     field(default=-1			,						metadata={"help": "# test examples. -1 means use all."} )
     SCREAMING_SNAKE_CASE :				str							     =     field(default=UpperCAmelCase__			,						metadata={"help": "Source language id for translation."} )
     SCREAMING_SNAKE_CASE :				Union[str, Any]							     =     field(default=UpperCAmelCase__			,						metadata={"help": "Target language id for translation."} )
     SCREAMING_SNAKE_CASE :				Dict							     =     field(default=UpperCAmelCase__			,						metadata={"help": "# num_beams to use for evaluation."} )
     SCREAMING_SNAKE_CASE :				Dict							     =     field(
         default=UpperCAmelCase__			,						metadata={"help": "If only pad tokens should be ignored. This assumes that `config.pad_token_id` is defined."}			,						)




def 			_a			(		_lowercase      :			Optional[int]    ,      _lowercase      :			List[Any]    ,      _lowercase      :			Union[str, Any]						):





     '''simple docstring'''






     logger.info(F'***** {split} metrics *****'						)
     for key in sorted(metrics.keys()						):
          logger.info(F'  {key} = {metrics[key]}'						)
     save_json(UpperCAmelCase__    ,      os.path.join(UpperCAmelCase__    ,      F'{split}_results.json'						)						)




def 			_a			(		):





     '''simple docstring'''






     __UpperCAmelCase			:				Union[str, Any]													=    HfArgumentParser((ModelArguments, DataTrainingArguments, SeqaSeqTrainingArguments)						)

     if len(sys.argv						) == 2 and sys.argv[1].endswith('''.json'''						):
          # If we pass only one argument to the script and it's the path to a json file,
          # let's parse it to get our arguments.
          __UpperCAmelCase							,     __UpperCAmelCase							,     __UpperCAmelCase			:				List[Any]													=    parser.parse_json_file(json_file=os.path.abspath(sys.argv[1]						)						)
     else:
          __UpperCAmelCase							,     __UpperCAmelCase							,     __UpperCAmelCase			:				str													=    parser.parse_args_into_dataclasses()

     check_output_dir(UpperCAmelCase__						)

     # Setup logging
     logging.basicConfig(
         format='''%(asctime)s - %(levelname)s - %(name)s - %(message)s'''    ,      datefmt='''%m/%d/%Y %H:%M:%S'''    ,      level=logging.INFO if training_args.local_rank in [-1, 0] else logging.WARN    ,      )
     logger.warning(
         '''Process rank: %s, device: %s, n_gpu: %s, distributed training: %s, 16-bits training: %s'''    ,      training_args.local_rank    ,      training_args.device    ,      training_args.n_gpu    ,      bool(training_args.parallel_mode == ParallelMode.DISTRIBUTED						)    ,      training_args.fpaa    ,      )
     transformers.utils.logging.enable_default_handler()
     transformers.utils.logging.enable_explicit_format()
     # Set the verbosity to info of the Transformers logger (on main process only):
     if is_main_process(training_args.local_rank						):
          transformers.utils.logging.set_verbosity_info()
     logger.info('''Training/evaluation parameters %s'''    ,      UpperCAmelCase__						)

     # Set seed
     set_seed(training_args.seed						)

     # Load pretrained model and tokenizer
     #
     # Distributed training:
     # The .from_pretrained methods guarantee that only one local process can concurrently
     # download model & vocab.

     __UpperCAmelCase			:				int													=    AutoConfig.from_pretrained(
         model_args.config_name if model_args.config_name else model_args.model_name_or_path    ,      cache_dir=model_args.cache_dir    ,      )

     __UpperCAmelCase			:				str													=    ('''encoder_layerdrop''', '''decoder_layerdrop''', '''dropout''', '''attention_dropout''')
     for p in extra_model_params:
          if getattr(UpperCAmelCase__    ,      UpperCAmelCase__    ,      UpperCAmelCase__						):
               assert hasattr(UpperCAmelCase__    ,      UpperCAmelCase__						), F'({config.__class__.__name__}) doesn\'t have a `{p}` attribute'
               setattr(UpperCAmelCase__    ,      UpperCAmelCase__    ,      getattr(UpperCAmelCase__    ,      UpperCAmelCase__						)						)

     __UpperCAmelCase			:				Optional[int]													=    AutoTokenizer.from_pretrained(
         model_args.tokenizer_name if model_args.tokenizer_name else model_args.model_name_or_path    ,      cache_dir=model_args.cache_dir    ,      )
     __UpperCAmelCase			:				List[str]													=    AutoModelForSeqaSeqLM.from_pretrained(
         model_args.model_name_or_path    ,      from_tf='''.ckpt''' in model_args.model_name_or_path    ,      config=UpperCAmelCase__    ,      cache_dir=model_args.cache_dir    ,      )

     # use task specific params
     use_task_specific_params(UpperCAmelCase__    ,      data_args.task						)

     # set num_beams for evaluation
     if data_args.eval_beams is None:
          __UpperCAmelCase			:				List[str]													=    model.config.num_beams

     # set decoder_start_token_id for MBart
     if model.config.decoder_start_token_id is None and isinstance(UpperCAmelCase__    ,      (MBartTokenizer, MBartTokenizerFast)						):
          assert (
              data_args.tgt_lang is not None and data_args.src_lang is not None
          ), "mBart requires --tgt_lang and --src_lang"
          if isinstance(UpperCAmelCase__    ,      UpperCAmelCase__						):
               __UpperCAmelCase			:				Union[str, Any]													=    tokenizer.lang_code_to_id[data_args.tgt_lang]
          else:
               __UpperCAmelCase			:				List[str]													=    tokenizer.convert_tokens_to_ids(data_args.tgt_lang						)

     if model_args.freeze_embeds:
          freeze_embeds(UpperCAmelCase__						)
     if model_args.freeze_encoder:
          freeze_params(model.get_encoder()						)
          assert_all_frozen(model.get_encoder()						)

     __UpperCAmelCase			:				int													=    SeqaSeqDataset

     # Get datasets
     __UpperCAmelCase			:				Optional[Any]													=    (
         dataset_class(
             UpperCAmelCase__    ,      type_path='''train'''    ,      data_dir=data_args.data_dir    ,      n_obs=data_args.n_train    ,      max_target_length=data_args.max_target_length    ,      max_source_length=data_args.max_source_length    ,      prefix=model.config.prefix or ''''''    ,      )
         if training_args.do_train
         else None
     )
     __UpperCAmelCase			:				Any													=    (
         dataset_class(
             UpperCAmelCase__    ,      type_path='''val'''    ,      data_dir=data_args.data_dir    ,      n_obs=data_args.n_val    ,      max_target_length=data_args.val_max_target_length    ,      max_source_length=data_args.max_source_length    ,      prefix=model.config.prefix or ''''''    ,      )
         if training_args.do_eval or training_args.evaluation_strategy != EvaluationStrategy.NO
         else None
     )
     __UpperCAmelCase			:				Optional[int]													=    (
         dataset_class(
             UpperCAmelCase__    ,      type_path='''test'''    ,      data_dir=data_args.data_dir    ,      n_obs=data_args.n_test    ,      max_target_length=data_args.test_max_target_length    ,      max_source_length=data_args.max_source_length    ,      prefix=model.config.prefix or ''''''    ,      )
         if training_args.do_predict
         else None
     )

     # Initialize our Trainer
     __UpperCAmelCase			:				Union[str, Any]													=    (
         build_compute_metrics_fn(data_args.task    ,      UpperCAmelCase__						) if training_args.predict_with_generate else None
     )
     __UpperCAmelCase			:				Any													=    SeqaSeqTrainer(
         model=UpperCAmelCase__    ,      args=UpperCAmelCase__    ,      data_args=UpperCAmelCase__    ,      train_dataset=UpperCAmelCase__    ,      eval_dataset=UpperCAmelCase__    ,      data_collator=SeqaSeqDataCollator(
             UpperCAmelCase__    ,      UpperCAmelCase__    ,      model.config.decoder_start_token_id    ,      training_args.tpu_num_cores						)    ,      compute_metrics=UpperCAmelCase__    ,      tokenizer=UpperCAmelCase__    ,      )

     __UpperCAmelCase			:				str													=    {}
     # Training
     if training_args.do_train:
          logger.info('''*** Train ***'''						)

          __UpperCAmelCase			:				Any													=    trainer.train(
              model_path=model_args.model_name_or_path if os.path.isdir(model_args.model_name_or_path						) else None						)
          __UpperCAmelCase			:				str													=    train_result.metrics
          __UpperCAmelCase			:				Optional[Any]													=    data_args.n_train

          trainer.save_model()  # this also saves the tokenizer

          if trainer.is_world_process_zero():
               handle_metrics('''train'''    ,      UpperCAmelCase__    ,      training_args.output_dir						)
               all_metrics.update(UpperCAmelCase__						)

               # Need to save the state, since Trainer.save_model saves only the tokenizer with the model
               trainer.state.save_to_json(os.path.join(training_args.output_dir    ,      '''trainer_state.json'''						)						)

               # For convenience, we also re-save the tokenizer to the same directory,
               # so that you can share your model easily on huggingface.co/models =)
               tokenizer.save_pretrained(training_args.output_dir						)

                # Evaluation
     if training_args.do_eval:
          logger.info('''*** Evaluate ***'''						)

          __UpperCAmelCase			:				Union[str, Any]													=    trainer.evaluate(metric_key_prefix='''val'''						)
          __UpperCAmelCase			:				Union[str, Any]													=    data_args.n_val
          __UpperCAmelCase			:				str													=    round(metrics['''val_loss''']    ,      4						)

          if trainer.is_world_process_zero():
               handle_metrics('''val'''    ,      UpperCAmelCase__    ,      training_args.output_dir						)
               all_metrics.update(UpperCAmelCase__						)

     if training_args.do_predict:
          logger.info('''*** Predict ***'''						)

          __UpperCAmelCase			:				List[Any]													=    trainer.predict(test_dataset=UpperCAmelCase__    ,      metric_key_prefix='''test'''						)
          __UpperCAmelCase			:				Optional[Any]													=    test_output.metrics
          __UpperCAmelCase			:				List[str]													=    data_args.n_test

          if trainer.is_world_process_zero():
               __UpperCAmelCase			:				int													=    round(metrics['''test_loss''']    ,      4						)
               handle_metrics('''test'''    ,      UpperCAmelCase__    ,      training_args.output_dir						)
               all_metrics.update(UpperCAmelCase__						)

               if training_args.predict_with_generate:
                    __UpperCAmelCase			:				Any													=    tokenizer.batch_decode(
                        test_output.predictions    ,      skip_special_tokens=UpperCAmelCase__    ,      clean_up_tokenization_spaces=UpperCAmelCase__						)
                    __UpperCAmelCase			:				str													=    lmap(str.strip    ,      UpperCAmelCase__						)
                    write_txt_file(UpperCAmelCase__    ,      os.path.join(training_args.output_dir    ,      '''test_generations.txt'''						)						)

     if trainer.is_world_process_zero():
          save_json(UpperCAmelCase__    ,      os.path.join(training_args.output_dir    ,      '''all_results.json'''						)						)

     return all_metrics




def 			_a			(		_lowercase      :			Union[str, Any]						):





     '''simple docstring'''






     main()


if __name__ == "__main__":
      main()



'''simple docstring'''



from typing import Optional, Tuple, Union

import tensorflow as tf

from ...activations_tf import ACTaFN
from ...file_utils import add_code_sample_docstrings, add_start_docstrings, add_start_docstrings_to_model_forward
from ...modeling_tf_outputs import (
    TFBaseModelOutputWithNoAttention,
    TFBaseModelOutputWithPoolingAndNoAttention,
    TFSequenceClassifierOutput,
)
from ...modeling_tf_utils import TFPreTrainedModel, TFSequenceClassificationLoss, keras_serializable, unpack_inputs
from ...tf_utils import shape_list
from ...utils import logging
from .configuration_regnet import RegNetConfig


__UpperCAmelCase    :Optional[Any]				       =     logging.get_logger(__name__)

# General docstring
__UpperCAmelCase    :List[Any]				       =     "RegNetConfig"

# Base docstring
__UpperCAmelCase    :List[Any]				       =     "facebook/regnet-y-040"
__UpperCAmelCase    :Union[str, Any]				       =     [1, 1_0_8_8, 7, 7]

# Image classification docstring
__UpperCAmelCase    :int				       =     "facebook/regnet-y-040"
__UpperCAmelCase    :Optional[Any]				       =     "tabby, tabby cat"

__UpperCAmelCase    :Dict				       =     [
    "facebook/regnet-y-040",
    # See all regnet models at https://huggingface.co/models?filter=regnet
]



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	str							,				snake_case  :	int							,				snake_case  :	int = 3							,				snake_case  :	int = 1							,				snake_case  :	int = 1							,				snake_case  :	Optional[str] = "relu"							,				**snake_case  :	Any							,				)		->							Union[str, Any]:
          super().__init__(**snake_case    )
          # The padding and conv has been verified in
          # https://colab.research.google.com/gist/sayakpaul/854bc10eeaf21c9ee2119e0b9f3841a7/scratchpad.ipynb
          __UpperCAmelCase			:				Union[str, Any]													=    tf.keras.layers.ZeroPaddingaD(padding=kernel_size // 2    )
          __UpperCAmelCase			:				List[Any]													=    tf.keras.layers.ConvaD(
              filters=snake_case							,				kernel_size=snake_case							,				strides=snake_case							,				padding='''VALID'''							,				groups=snake_case							,				use_bias=snake_case							,				name='''convolution'''							,				)
          __UpperCAmelCase			:				List[Any]													=    tf.keras.layers.BatchNormalization(epsilon=1E-5							,				momentum=0.9							,				name='''normalization'''    )
          __UpperCAmelCase			:				Any													=    ACTaFN[activation] if activation is not None else tf.identity






     def 				lowerCamelCase__				(       self  :	Any							,				snake_case  :	List[str]    )		->							int:
          __UpperCAmelCase			:				Tuple													=    self.convolution(self.padding(snake_case    )    )
          __UpperCAmelCase			:				List[Any]													=    self.normalization(snake_case    )
          __UpperCAmelCase			:				Optional[Any]													=    self.activation(snake_case    )
          return hidden_state



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	str							,				snake_case  :	RegNetConfig							,				**snake_case  :	Tuple    )		->							int:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				List[str]													=    config.num_channels
          __UpperCAmelCase			:				Optional[int]													=    TFRegNetConvLayer(
              out_channels=config.embedding_size							,				kernel_size=3							,				stride=2							,				activation=config.hidden_act							,				name='''embedder'''							,				)






     def 				lowerCamelCase__				(       self  :	Optional[int]							,				snake_case  :	Dict    )		->							int:
          __UpperCAmelCase			:				int													=    shape_list(snake_case    )[1]
          if tf.executing_eagerly() and num_channels != self.num_channels:
               raise ValueError(
                   '''Make sure that the channel dimension of the pixel values match with the one set in the configuration.'''    )

          # When running on CPU, `tf.keras.layers.Conv2D` doesn't support `NCHW` format.
          # So change the input format from `NCHW` to `NHWC`.
          # shape = (batch_size, in_height, in_width, in_channels=num_channels)
          __UpperCAmelCase			:				Dict													=    tf.transpose(snake_case							,				perm=(0, 2, 3, 1)    )
          __UpperCAmelCase			:				List[str]													=    self.embedder(snake_case    )
          return hidden_state



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	Any							,				snake_case  :	int							,				snake_case  :	int = 2							,				**snake_case  :	Tuple    )		->							str:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				str													=    tf.keras.layers.ConvaD(
              filters=snake_case							,				kernel_size=1							,				strides=snake_case							,				use_bias=snake_case							,				name='''convolution'''    )
          __UpperCAmelCase			:				int													=    tf.keras.layers.BatchNormalization(epsilon=1E-5							,				momentum=0.9							,				name='''normalization'''    )






     def 				lowerCamelCase__				(       self  :	str							,				snake_case  :	tf.Tensor							,				snake_case  :	bool = False    )		->							tf.Tensor:
          return self.normalization(self.convolution(snake_case    )							,				training=snake_case    )



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	Tuple							,				snake_case  :	int							,				snake_case  :	int							,				**snake_case  :	Tuple    )		->							List[Any]:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				List[Any]													=    tf.keras.layers.GlobalAveragePoolingaD(keepdims=snake_case							,				name='''pooler'''    )
          __UpperCAmelCase			:				Dict													=    [
              tf.keras.layers.ConvaD(filters=snake_case							,				kernel_size=1							,				activation='''relu'''							,				name='''attention.0'''    ),
              tf.keras.layers.ConvaD(filters=snake_case							,				kernel_size=1							,				activation='''sigmoid'''							,				name='''attention.2'''    ),
          ]






     def 				lowerCamelCase__				(       self  :	Optional[int]							,				snake_case  :	Tuple    )		->							Union[str, Any]:
          # [batch_size, h, w, num_channels] -> [batch_size, 1, 1, num_channels]
          __UpperCAmelCase			:				str													=    self.pooler(snake_case    )
          for layer_module in self.attention:
               __UpperCAmelCase			:				int													=    layer_module(snake_case    )
          __UpperCAmelCase			:				List[Any]													=    hidden_state * pooled
          return hidden_state



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	Any							,				snake_case  :	RegNetConfig							,				snake_case  :	int							,				snake_case  :	int							,				snake_case  :	int = 1							,				**snake_case  :	int    )		->							int:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				Any													=    in_channels != out_channels or stride != 1
          __UpperCAmelCase			:				Optional[int]													=    max(1							,				out_channels // config.groups_width    )
          __UpperCAmelCase			:				Optional[int]													=    (
              TFRegNetShortCut(snake_case							,				stride=snake_case							,				name='''shortcut'''    )
              if should_apply_shortcut
              else tf.keras.layers.Activation('''linear'''							,				name='''shortcut'''    )
          )
          # `self.layers` instead of `self.layer` because that is a reserved argument.
          __UpperCAmelCase			:				List[Any]													=    [
              TFRegNetConvLayer(snake_case							,				kernel_size=1							,				activation=config.hidden_act							,				name='''layer.0'''    ),
              TFRegNetConvLayer(
                  snake_case							,				stride=snake_case							,				groups=snake_case							,				activation=config.hidden_act							,				name='''layer.1'''    ),
              TFRegNetConvLayer(snake_case							,				kernel_size=1							,				activation=snake_case							,				name='''layer.2'''    ),
          ]
          __UpperCAmelCase			:				Union[str, Any]													=    ACTaFN[config.hidden_act]






     def 				lowerCamelCase__				(       self  :	Union[str, Any]							,				snake_case  :	Optional[Any]    )		->							List[str]:
          __UpperCAmelCase			:				Union[str, Any]													=    hidden_state
          for layer_module in self.layers:
               __UpperCAmelCase			:				Any													=    layer_module(snake_case    )
          __UpperCAmelCase			:				Tuple													=    self.shortcut(snake_case    )
          hidden_state += residual
          __UpperCAmelCase			:				Optional[int]													=    self.activation(snake_case    )
          return hidden_state



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	List[str]							,				snake_case  :	RegNetConfig							,				snake_case  :	int							,				snake_case  :	int							,				snake_case  :	int = 1							,				**snake_case  :	List[str]    )		->							Optional[int]:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				List[str]													=    in_channels != out_channels or stride != 1
          __UpperCAmelCase			:				Optional[Any]													=    max(1							,				out_channels // config.groups_width    )
          __UpperCAmelCase			:				Any													=    (
              TFRegNetShortCut(snake_case							,				stride=snake_case							,				name='''shortcut'''    )
              if should_apply_shortcut
              else tf.keras.layers.Activation('''linear'''							,				name='''shortcut'''    )
          )
          __UpperCAmelCase			:				List[str]													=    [
              TFRegNetConvLayer(snake_case							,				kernel_size=1							,				activation=config.hidden_act							,				name='''layer.0'''    ),
              TFRegNetConvLayer(
                  snake_case							,				stride=snake_case							,				groups=snake_case							,				activation=config.hidden_act							,				name='''layer.1'''    ),
              TFRegNetSELayer(snake_case							,				reduced_channels=int(round(in_channels / 4    )    )							,				name='''layer.2'''    ),
              TFRegNetConvLayer(snake_case							,				kernel_size=1							,				activation=snake_case							,				name='''layer.3'''    ),
          ]
          __UpperCAmelCase			:				Dict													=    ACTaFN[config.hidden_act]






     def 				lowerCamelCase__				(       self  :	Optional[Any]							,				snake_case  :	Tuple    )		->							Any:
          __UpperCAmelCase			:				Optional[int]													=    hidden_state
          for layer_module in self.layers:
               __UpperCAmelCase			:				Any													=    layer_module(snake_case    )
          __UpperCAmelCase			:				int													=    self.shortcut(snake_case    )
          hidden_state += residual
          __UpperCAmelCase			:				Optional[int]													=    self.activation(snake_case    )
          return hidden_state



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	Optional[int]							,				snake_case  :	RegNetConfig							,				snake_case  :	int							,				snake_case  :	int							,				snake_case  :	int = 2							,				snake_case  :	int = 2							,				**snake_case  :	str    )		->							Optional[Any]:
          super().__init__(**snake_case    )

          __UpperCAmelCase			:				str													=    TFRegNetXLayer if config.layer_type == '''x''' else TFRegNetYLayer
          __UpperCAmelCase			:				str													=    [
              # downsampling is done in the first layer with stride of 2
              layer(snake_case							,				snake_case							,				snake_case							,				stride=snake_case							,				name='''layers.0'''    ),
              *[layer(snake_case							,				snake_case							,				snake_case							,				name=f'layers.{i+1}'    ) for i in range(depth - 1    )],
          ]






     def 				lowerCamelCase__				(       self  :	List[str]							,				snake_case  :	Any    )		->							List[Any]:
          for layer_module in self.layers:
               __UpperCAmelCase			:				Optional[Any]													=    layer_module(snake_case    )
          return hidden_state



class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     def __init__(       self  :	Any							,				snake_case  :	RegNetConfig							,				**snake_case  :	int    )		->							str:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				Dict													=    []
          # based on `downsample_in_first_stage`, the first layer of the first stage may or may not downsample the input
          self.stages.append(
              TFRegNetStage(
                  snake_case							,				config.embedding_size							,				config.hidden_sizes[0]							,				stride=2 if config.downsample_in_first_stage else 1							,				depth=config.depths[0]							,				name='''stages.0'''							,				)    )
          __UpperCAmelCase			:				Optional[Any]													=    zip(config.hidden_sizes							,				config.hidden_sizes[1:]    )
          for i, ((in_channels, out_channels), depth) in enumerate(zip(snake_case							,				config.depths[1:]    )    ):
               self.stages.append(TFRegNetStage(snake_case							,				snake_case							,				snake_case							,				depth=snake_case							,				name=f'stages.{i+1}'    )    )






     def 				lowerCamelCase__				(       self  :	int							,				snake_case  :	tf.Tensor							,				snake_case  :	bool = False							,				snake_case  :	bool = True    )		->							TFBaseModelOutputWithNoAttention:
          __UpperCAmelCase			:				Any													=    () if output_hidden_states else None

          for stage_module in self.stages:
               if output_hidden_states:
                    __UpperCAmelCase			:				Any													=    hidden_states + (hidden_state,)

               __UpperCAmelCase			:				List[Any]													=    stage_module(snake_case    )

          if output_hidden_states:
               __UpperCAmelCase			:				Optional[Any]													=    hidden_states + (hidden_state,)

          if not return_dict:
               return tuple(v for v in [hidden_state, hidden_states] if v is not None    )

          return TFBaseModelOutputWithNoAttention(last_hidden_state=snake_case							,				hidden_states=snake_case    )



@keras_serializable
class 			a       (	tf.keras.layers.Layer ):


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				Any							     =     RegNetConfig

     def __init__(       self  :	Dict							,				snake_case  :	str							,				**snake_case  :	Optional[int]    )		->							Any:
          super().__init__(**snake_case    )
          __UpperCAmelCase			:				List[Any]													=    config
          __UpperCAmelCase			:				List[str]													=    TFRegNetEmbeddings(snake_case							,				name='''embedder'''    )
          __UpperCAmelCase			:				List[str]													=    TFRegNetEncoder(snake_case							,				name='''encoder'''    )
          __UpperCAmelCase			:				List[Any]													=    tf.keras.layers.GlobalAveragePoolingaD(keepdims=snake_case							,				name='''pooler'''    )






     @unpack_inputs
     def 				lowerCamelCase__				(       self  :	Dict							,				snake_case  :	tf.Tensor							,				snake_case  :	Optional[bool] = None							,				snake_case  :	Optional[bool] = None							,				snake_case  :	bool = False							,				)		->							TFBaseModelOutputWithPoolingAndNoAttention:
          __UpperCAmelCase			:				Dict													=    (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          __UpperCAmelCase			:				Optional[Any]													=    return_dict if return_dict is not None else self.config.use_return_dict

          __UpperCAmelCase			:				Optional[Any]													=    self.embedder(snake_case							,				training=snake_case    )

          __UpperCAmelCase			:				Optional[int]													=    self.encoder(
              snake_case							,				output_hidden_states=snake_case							,				return_dict=snake_case							,				training=snake_case    )

          __UpperCAmelCase			:				List[str]													=    encoder_outputs[0]
          __UpperCAmelCase			:				str													=    self.pooler(snake_case    )

          # Change to NCHW output format have uniformity in the modules
          __UpperCAmelCase			:				Optional[Any]													=    tf.transpose(snake_case							,				perm=(0, 3, 1, 2)    )
          __UpperCAmelCase			:				str													=    tf.transpose(snake_case							,				perm=(0, 3, 1, 2)    )

          # Change the other hidden state outputs to NCHW as well
          if output_hidden_states:
               __UpperCAmelCase			:				Dict													=    tuple([tf.transpose(snake_case							,				perm=(0, 3, 1, 2)    ) for h in encoder_outputs[1]]    )

          if not return_dict:
               return (last_hidden_state, pooled_output) + encoder_outputs[1:]

          return TFBaseModelOutputWithPoolingAndNoAttention(
              last_hidden_state=snake_case							,				pooler_output=snake_case							,				hidden_states=hidden_states if output_hidden_states else encoder_outputs.hidden_states							,				)



class 			a       (	_a ):


     """simple docstring"""






     SCREAMING_SNAKE_CASE :				str							     =     RegNetConfig
     SCREAMING_SNAKE_CASE :				Tuple							     =     "regnet"
     SCREAMING_SNAKE_CASE :				List[Any]							     =     "pixel_values"

     @property
     def 				lowerCamelCase__				(       self  :	int    )		->							List[str]:
          return {"pixel_values": tf.TensorSpec(shape=(None, self.config.num_channels, 224, 224)							,				dtype=tf.floataa    )}



__UpperCAmelCase    :Optional[int]				       =     r"\n    Parameters:\n    This model is a Tensorflow\n    [tf.keras.layers.Layer](https://www.tensorflow.org/api_docs/python/tf/keras/layers/Layer) sub-class. Use it as a\n    regular Tensorflow Module and refer to the Tensorflow documentation for all matter related to general usage and\n    behavior.\n        config ([`RegNetConfig`]): Model configuration class with all the parameters of the model.\n            Initializing with a config file does not load the weights associated with the model, only the\n            configuration. Check out the [`~TFPreTrainedModel.from_pretrained`] method to load the model weights.\n"

__UpperCAmelCase    :List[Any]				       =     r"\n    Args:\n        pixel_values (`tf.Tensor` of shape `(batch_size, num_channels, height, width)`):\n            Pixel values. Pixel values can be obtained using [`AutoImageProcessor`]. See\n            [`ConveNextImageProcessor.__call__`] for details.\n        output_hidden_states (`bool`, *optional*):\n            Whether or not to return the hidden states of all layers. See `hidden_states` under returned tensors for\n            more detail.\n        return_dict (`bool`, *optional*):\n            Whether or not to return a [`~utils.ModelOutput`] instead of a plain tuple.\n"



@add_start_docstrings(
    "The bare RegNet model outputting raw features without any specific head on top."			,						_a			,						)
class 			a       (	_a ):


     """simple docstring"""






     def __init__(       self  :	List[Any]							,				snake_case  :	RegNetConfig							,				*snake_case  :	Optional[int]							,				**snake_case  :	List[str]    )		->							Tuple:
          super().__init__(snake_case							,				*snake_case							,				**snake_case    )
          __UpperCAmelCase			:				Dict													=    TFRegNetMainLayer(snake_case							,				name='''regnet'''    )






     @unpack_inputs
     @add_start_docstrings_to_model_forward(snake_case    )
     @add_code_sample_docstrings(
         checkpoint=_CHECKPOINT_FOR_DOC							,				output_type=snake_case							,				config_class=_CONFIG_FOR_DOC							,				modality='''vision'''							,				expected_output=_EXPECTED_OUTPUT_SHAPE							,				)
     def 				lowerCamelCase__				(       self  :	Tuple							,				snake_case  :	tf.Tensor							,				snake_case  :	Optional[bool] = None							,				snake_case  :	Optional[bool] = None							,				snake_case  :	str=False							,				)		->							Union[TFBaseModelOutputWithPoolingAndNoAttention, Tuple[tf.Tensor]]:
          __UpperCAmelCase			:				List[Any]													=    (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          __UpperCAmelCase			:				List[str]													=    return_dict if return_dict is not None else self.config.use_return_dict

          __UpperCAmelCase			:				Dict													=    self.regnet(
              pixel_values=snake_case							,				output_hidden_states=snake_case							,				return_dict=snake_case							,				training=snake_case							,				)
          if not return_dict:
               return (outputs[0],) + outputs[1:]

          return TFBaseModelOutputWithPoolingAndNoAttention(
              last_hidden_state=outputs.last_hidden_state							,				pooler_output=outputs.pooler_output							,				hidden_states=outputs.hidden_states							,				)



@add_start_docstrings(
    "\n    RegNet Model with an image classification head on top (a linear layer on top of the pooled features), e.g. for\n    ImageNet.\n    "			,						_a			,						)
class 			a       (	_a			,						_a ):


     """simple docstring"""






     def __init__(       self  :	Tuple							,				snake_case  :	RegNetConfig							,				*snake_case  :	Optional[Any]							,				**snake_case  :	List[Any]    )		->							List[Any]:
          super().__init__(snake_case							,				*snake_case							,				**snake_case    )
          __UpperCAmelCase			:				List[Any]													=    config.num_labels
          __UpperCAmelCase			:				Optional[int]													=    TFRegNetMainLayer(snake_case							,				name='''regnet'''    )
          # classification head
          __UpperCAmelCase			:				str													=    [
              tf.keras.layers.Flatten(),
              tf.keras.layers.Dense(config.num_labels							,				name='''classifier.1'''    ) if config.num_labels > 0 else tf.identity,
          ]






     @unpack_inputs
     @add_start_docstrings_to_model_forward(snake_case    )
     @add_code_sample_docstrings(
         checkpoint=_IMAGE_CLASS_CHECKPOINT							,				output_type=snake_case							,				config_class=_CONFIG_FOR_DOC							,				expected_output=_IMAGE_CLASS_EXPECTED_OUTPUT							,				)
     def 				lowerCamelCase__				(       self  :	Tuple							,				snake_case  :	tf.Tensor = None							,				snake_case  :	tf.Tensor = None							,				snake_case  :	bool = None							,				snake_case  :	bool = None							,				snake_case  :	Tuple=False							,				)		->							Union[TFSequenceClassifierOutput, Tuple[tf.Tensor]]:
          __UpperCAmelCase			:				Optional[int]													=    (
              output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
          )
          __UpperCAmelCase			:				Optional[Any]													=    return_dict if return_dict is not None else self.config.use_return_dict

          __UpperCAmelCase			:				Optional[int]													=    self.regnet(
              snake_case							,				output_hidden_states=snake_case							,				return_dict=snake_case							,				training=snake_case    )

          __UpperCAmelCase			:				str													=    outputs.pooler_output if return_dict else outputs[1]

          __UpperCAmelCase			:				Tuple													=    self.classifier[0](snake_case    )
          __UpperCAmelCase			:				Tuple													=    self.classifier[1](snake_case    )

          __UpperCAmelCase			:				Any													=    None if labels is None else self.hf_compute_loss(labels=snake_case							,				logits=snake_case    )

          if not return_dict:
               __UpperCAmelCase			:				List[Any]													=    (logits,) + outputs[2:]
               return ((loss,) + output) if loss is not None else output

          return TFSequenceClassifierOutput(loss=snake_case							,				logits=snake_case							,				hidden_states=outputs.hidden_states    )