




'''simple docstring'''



from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import TensorType, is_torch_available, logging


UpperCAmelCase_				:     Any     			=						logging.get_logger(__name__)

UpperCAmelCase_				:     Tuple     			=						{
    '''google/bigbird-pegasus-large-arxiv''': (
        '''https://huggingface.co/google/bigbird-pegasus-large-arxiv/resolve/main/config.json'''
    ),
    '''google/bigbird-pegasus-large-pubmed''': (
        '''https://huggingface.co/google/bigbird-pegasus-large-pubmed/resolve/main/config.json'''
    ),
    '''google/bigbird-pegasus-large-bigpatent''': (
        '''https://huggingface.co/google/bigbird-pegasus-large-bigpatent/resolve/main/config.json'''
    ),
    # See all BigBirdPegasus models at https://huggingface.co/models?filter=bigbird_pegasus
}





class 			lowerCAmelCase      (       __lowerCAmelCase):
       __lowercase     :					Tuple       				=	'''bigbird_pegasus'''
       __lowercase     :					Optional[Any]       				=	['''past_key_values''']
       __lowercase     :					Dict       				=	{
           '''num_attention_heads''': '''encoder_attention_heads''',
           '''hidden_size''': '''d_model''',
           '''attention_probs_dropout_prob''': '''attention_dropout''',
       }


       def __init__(      self			,     __SCREAMING_SNAKE_CASE=9_6103			,     __SCREAMING_SNAKE_CASE=4096			,     __SCREAMING_SNAKE_CASE=16			,     __SCREAMING_SNAKE_CASE=4096			,     __SCREAMING_SNAKE_CASE=16			,     __SCREAMING_SNAKE_CASE=16			,     __SCREAMING_SNAKE_CASE=4096			,     __SCREAMING_SNAKE_CASE=16			,     __SCREAMING_SNAKE_CASE=0.0			,     __SCREAMING_SNAKE_CASE=0.0			,     __SCREAMING_SNAKE_CASE=True			,     __SCREAMING_SNAKE_CASE=True			,     __SCREAMING_SNAKE_CASE="gelu_new"			,     __SCREAMING_SNAKE_CASE=1024			,     __SCREAMING_SNAKE_CASE=0.1			,     __SCREAMING_SNAKE_CASE=0.0			,     __SCREAMING_SNAKE_CASE=0.0			,     __SCREAMING_SNAKE_CASE=0.02			,     __SCREAMING_SNAKE_CASE=2			,     __SCREAMING_SNAKE_CASE=0.0			,     __SCREAMING_SNAKE_CASE=True			,     __SCREAMING_SNAKE_CASE=0			,     __SCREAMING_SNAKE_CASE=2			,     __SCREAMING_SNAKE_CASE=1			,     __SCREAMING_SNAKE_CASE="block_sparse"			,     __SCREAMING_SNAKE_CASE=64			,     __SCREAMING_SNAKE_CASE=3			,     __SCREAMING_SNAKE_CASE=False			,     **__SCREAMING_SNAKE_CASE			,     )					->     Tuple:



            '''simple docstring'''




            __snake_case			    =     vocab_size
            __snake_case			    =     max_position_embeddings
            __snake_case			    =     d_model
            __snake_case			    =     encoder_ffn_dim
            __snake_case			    =     encoder_layers
            __snake_case			    =     encoder_attention_heads
            __snake_case			    =     decoder_ffn_dim
            __snake_case			    =     decoder_layers
            __snake_case			    =     decoder_attention_heads
            __snake_case			    =     dropout
            __snake_case			    =     attention_dropout
            __snake_case			    =     activation_dropout
            __snake_case			    =     activation_function
            __snake_case			    =     init_std
            __snake_case			    =     encoder_layerdrop
            __snake_case			    =     decoder_layerdrop
            __snake_case			    =     classifier_dropout
            __snake_case			    =     use_cache
            __snake_case			    =     encoder_layers
            __snake_case			    =     scale_embedding  # scale factor will be sqrt(d_model) if True

            # extra config
            __snake_case			    =     attention_type
            __snake_case			    =     block_size
            __snake_case			    =     num_random_blocks
            __snake_case			    =     use_bias

            super().__init__(
                pad_token_id=__SCREAMING_SNAKE_CASE			,     bos_token_id=__SCREAMING_SNAKE_CASE			,     eos_token_id=__SCREAMING_SNAKE_CASE			,     is_encoder_decoder=__SCREAMING_SNAKE_CASE			,     decoder_start_token_id=__SCREAMING_SNAKE_CASE			,     **__SCREAMING_SNAKE_CASE			,     )





class 			lowerCAmelCase      (       __lowerCAmelCase):


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



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case			    =     OrderedDict(
                     [
                         ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ]     )

                 if self.use_past:
                      __snake_case			    =     {0: '''batch'''}
                      __snake_case			    =     {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
                 else:
                      __snake_case			    =     {0: '''batch''', 1: '''decoder_sequence'''}
                      __snake_case			    =     {0: '''batch''', 1: '''decoder_sequence'''}

                 if self.use_past:
                      self.fill_with_past_key_values_(__SCREAMING_SNAKE_CASE			,     direction='''inputs'''     )
            elif self.task == "causal-lm":
                 # TODO: figure this case out.
                 __snake_case			    =     OrderedDict(
                     [
                         ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ]     )
                 if self.use_past:
                      __snake_case    ,			__snake_case			    =     self.num_layers
                      for i in range(__SCREAMING_SNAKE_CASE     ):
                           __snake_case			    =     {0: '''batch''', 2: '''past_sequence + sequence'''}
                           __snake_case			    =     {0: '''batch''', 2: '''past_sequence + sequence'''}
            else:
                 __snake_case			    =     OrderedDict(
                     [
                         ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                         ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
                         ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
                     ]     )

            return common_inputs


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



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case			    =     super().outputs
            else:
                 __snake_case			    =     super(__SCREAMING_SNAKE_CASE			,     self     ).outputs
                 if self.use_past:
                      __snake_case    ,			__snake_case			    =     self.num_layers
                      for i in range(__SCREAMING_SNAKE_CASE     ):
                           __snake_case			    =     {0: '''batch''', 2: '''past_sequence + sequence'''}
                           __snake_case			    =     {0: '''batch''', 2: '''past_sequence + sequence'''}
            return common_outputs


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = False			,     __SCREAMING_SNAKE_CASE = None			,     )					->     Mapping[str, Any]:



            '''simple docstring'''




            __snake_case			    =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )

            # Generate decoder inputs
            __snake_case			    =     seq_length if not self.use_past else 1
            __snake_case			    =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )
            __snake_case			    =     {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
            __snake_case			    =     dict(**__SCREAMING_SNAKE_CASE			,     **__SCREAMING_SNAKE_CASE     )

            if self.use_past:
                 if not is_torch_available():
                      raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''     )
                 else:
                      import torch
                 __snake_case    ,			__snake_case			    =     common_inputs['''input_ids'''].shape
                 __snake_case			    =     common_inputs['''decoder_input_ids'''].shape[1]
                 __snake_case    ,			__snake_case			    =     self.num_attention_heads
                 __snake_case			    =     (
                     batch,
                     num_encoder_attention_heads,
                     encoder_seq_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )
                 __snake_case			    =     decoder_seq_length + 3
                 __snake_case			    =     (
                     batch,
                     num_decoder_attention_heads,
                     decoder_past_length,
                     self._config.hidden_size // num_decoder_attention_heads,
                 )

                 __snake_case			    =     torch.cat(
                     [common_inputs['''decoder_attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )]			,     dim=1     )

                 __snake_case			    =     []
                 # If the number of encoder and decoder layers are present in the model configuration, both are considered
                 __snake_case    ,			__snake_case			    =     self.num_layers
                 __snake_case			    =     min(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )
                 __snake_case			    =     max(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     ) - min_num_layers
                 __snake_case			    =     '''encoder''' if num_encoder_layers > num_decoder_layers else '''decoder'''

                 for _ in range(__SCREAMING_SNAKE_CASE     ):
                      common_inputs["past_key_values"].append(
                          (
                              torch.zeros(__SCREAMING_SNAKE_CASE     ),
                              torch.zeros(__SCREAMING_SNAKE_CASE     ),
                              torch.zeros(__SCREAMING_SNAKE_CASE     ),
                              torch.zeros(__SCREAMING_SNAKE_CASE     ),
                          )     )
                 # TODO: test this.
                 __snake_case			    =     encoder_shape if remaining_side_name == '''encoder''' else decoder_shape
                 for _ in range(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     ):
                      common_inputs["past_key_values"].append((torch.zeros(__SCREAMING_SNAKE_CASE     ), torch.zeros(__SCREAMING_SNAKE_CASE     ))     )
            return common_inputs


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = False			,     __SCREAMING_SNAKE_CASE = None			,     )					->     Mapping[str, Any]:



            '''simple docstring'''




            __snake_case			    =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )

            if self.use_past:
                 if not is_torch_available():
                      raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''     )
                 else:
                      import torch
                 __snake_case    ,			__snake_case			    =     common_inputs['''input_ids'''].shape
                 # Not using the same length for past_key_values
                 __snake_case			    =     seqlen + 2
                 __snake_case    ,			__snake_case			    =     self.num_layers
                 __snake_case    ,			__snake_case			    =     self.num_attention_heads
                 __snake_case			    =     (
                     batch,
                     num_encoder_attention_heads,
                     past_key_values_length,
                     self._config.hidden_size // num_encoder_attention_heads,
                 )

                 __snake_case			    =     common_inputs['''attention_mask'''].dtype
                 __snake_case			    =     torch.cat(
                     [common_inputs['''attention_mask'''], torch.ones(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     dtype=__SCREAMING_SNAKE_CASE     )]			,     dim=1     )
                 __snake_case			    =     [
                     (torch.zeros(__SCREAMING_SNAKE_CASE     ), torch.zeros(__SCREAMING_SNAKE_CASE     )) for _ in range(__SCREAMING_SNAKE_CASE     )
                 ]
            return common_inputs


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = False			,     __SCREAMING_SNAKE_CASE = None			,     )					->     Mapping[str, Any]:



            '''simple docstring'''




            __snake_case			    =     compute_effective_axis_dimension(
                __SCREAMING_SNAKE_CASE			,     fixed_dimension=OnnxConfig.default_fixed_batch			,     num_token_to_add=0     )

            # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
            __snake_case			    =     tokenizer.num_special_tokens_to_add(__SCREAMING_SNAKE_CASE     )
            __snake_case			    =     compute_effective_axis_dimension(
                __SCREAMING_SNAKE_CASE			,     fixed_dimension=OnnxConfig.default_fixed_sequence			,     num_token_to_add=__SCREAMING_SNAKE_CASE     )

            # Generate dummy inputs according to compute batch and sequence
            __snake_case			    =     [''' '''.join([tokenizer.unk_token]     ) * seq_length] * batch_size
            __snake_case			    =     dict(tokenizer(__SCREAMING_SNAKE_CASE			,     return_tensors=__SCREAMING_SNAKE_CASE     )     )
            return common_inputs


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = -1			,     __SCREAMING_SNAKE_CASE = False			,     __SCREAMING_SNAKE_CASE = None			,     )					->     Mapping[str, Any]:



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case			    =     self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                     __SCREAMING_SNAKE_CASE			,     batch_size=__SCREAMING_SNAKE_CASE			,     seq_length=__SCREAMING_SNAKE_CASE			,     is_pair=__SCREAMING_SNAKE_CASE			,     framework=__SCREAMING_SNAKE_CASE     )

            elif self.task == "causal-lm":
                 __snake_case			    =     self._generate_dummy_inputs_for_causal_lm(
                     __SCREAMING_SNAKE_CASE			,     batch_size=__SCREAMING_SNAKE_CASE			,     seq_length=__SCREAMING_SNAKE_CASE			,     is_pair=__SCREAMING_SNAKE_CASE			,     framework=__SCREAMING_SNAKE_CASE     )
            else:
                 __snake_case			    =     self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                     __SCREAMING_SNAKE_CASE			,     batch_size=__SCREAMING_SNAKE_CASE			,     seq_length=__SCREAMING_SNAKE_CASE			,     is_pair=__SCREAMING_SNAKE_CASE			,     framework=__SCREAMING_SNAKE_CASE     )

            return common_inputs




       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     str:



            '''simple docstring'''




            if self.task in ["default", "seq2seq-lm"]:
                 __snake_case			    =     super()._flatten_past_key_values_(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )
            else:
                 __snake_case			    =     super(__SCREAMING_SNAKE_CASE			,     self     )._flatten_past_key_values_(
                     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )












'''simple docstring'''



from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
     import tensorflow as tf

     from transformers.models.ctrl.modeling_tf_ctrl import (
         TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
         TFCTRLForSequenceClassification,
         TFCTRLLMHeadModel,
         TFCTRLModel,
     )





class 			lowerCAmelCase      (       __lowerCAmelCase):


       def __init__(      self			,     __SCREAMING_SNAKE_CASE			,     )					->     Any:



            '''simple docstring'''




            __snake_case			    =     parent
            __snake_case			    =     13
            __snake_case			    =     7
            __snake_case			    =     True
            __snake_case			    =     True
            __snake_case			    =     True
            __snake_case			    =     True
            __snake_case			    =     True
            __snake_case			    =     99
            __snake_case			    =     32
            __snake_case			    =     2
            __snake_case			    =     4
            __snake_case			    =     37
            __snake_case			    =     '''gelu'''
            __snake_case			    =     0.1
            __snake_case			    =     0.1
            __snake_case			    =     512
            __snake_case			    =     16
            __snake_case			    =     2
            __snake_case			    =     0.02
            __snake_case			    =     3
            __snake_case			    =     4
            __snake_case			    =     None
            __snake_case			    =     self.vocab_size - 1


       def 					lowerCAmelCase					(      self     )					->     Any:



            '''simple docstring'''




            __snake_case			    =     ids_tensor([self.batch_size, self.seq_length]			,     self.vocab_size     )

            __snake_case			    =     None
            if self.use_input_mask:
                 __snake_case			    =     random_attention_mask([self.batch_size, self.seq_length]     )

            __snake_case			    =     None
            if self.use_token_type_ids:
                 __snake_case			    =     ids_tensor([self.batch_size, self.seq_length]			,     self.type_vocab_size     )

            __snake_case			    =     None
            if self.use_mc_token_ids:
                 __snake_case			    =     ids_tensor([self.batch_size, self.num_choices]			,     self.seq_length     )

            __snake_case			    =     None
            __snake_case			    =     None
            __snake_case			    =     None
            if self.use_labels:
                 __snake_case			    =     ids_tensor([self.batch_size]			,     self.type_sequence_label_size     )
                 __snake_case			    =     ids_tensor([self.batch_size, self.seq_length]			,     self.num_labels     )
                 __snake_case			    =     ids_tensor([self.batch_size]			,     self.num_choices     )

            __snake_case			    =     CTRLConfig(
                vocab_size=self.vocab_size			,     n_embd=self.hidden_size			,     n_layer=self.num_hidden_layers			,     n_head=self.num_attention_heads			,     n_positions=self.max_position_embeddings			,     pad_token_id=self.pad_token_id			,     )

            __snake_case			    =     ids_tensor([self.num_hidden_layers, self.num_attention_heads]			,     2     )

            return (
                config,
                input_ids,
                input_mask,
                head_mask,
                token_type_ids,
                mc_token_ids,
                sequence_labels,
                token_labels,
                choice_labels,
            )


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     *__SCREAMING_SNAKE_CASE     )					->     List[Any]:



            '''simple docstring'''




            __snake_case			    =     TFCTRLModel(config=__SCREAMING_SNAKE_CASE     )
            __snake_case			    =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )

            __snake_case			    =     [input_ids, None, input_mask]  # None is the input for 'past'
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )

            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )

            self.parent.assertEqual(result.last_hidden_state.shape			,     (self.batch_size, self.seq_length, self.hidden_size)     )


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     *__SCREAMING_SNAKE_CASE     )					->     int:



            '''simple docstring'''




            __snake_case			    =     TFCTRLLMHeadModel(config=__SCREAMING_SNAKE_CASE     )
            __snake_case			    =     {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )
            self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.seq_length, self.vocab_size)     )


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     *__SCREAMING_SNAKE_CASE     )					->     List[str]:



            '''simple docstring'''




            __snake_case			    =     self.num_labels
            __snake_case			    =     ids_tensor([self.batch_size]			,     self.type_sequence_label_size     )
            __snake_case			    =     {
                '''input_ids''': input_ids,
                '''token_type_ids''': token_type_ids,
                '''labels''': sequence_labels,
            }
            __snake_case			    =     TFCTRLForSequenceClassification(__SCREAMING_SNAKE_CASE     )
            __snake_case			    =     model(__SCREAMING_SNAKE_CASE     )
            self.parent.assertEqual(result.logits.shape			,     (self.batch_size, self.num_labels)     )




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



            '''simple docstring'''




            __snake_case			    =     self.prepare_config_and_inputs()

            (
                (
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			(
                __snake_case
            )    ,			
            )			    =     config_and_inputs

            __snake_case			    =     {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
            return config, inputs_dict





@require_tf
class 			lowerCAmelCase      (       __lowerCAmelCase ,      __lowerCAmelCase ,      unittest.TestCase):
       __lowercase     :					str       				=	(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
       __lowercase     :					Tuple       				=	(TFCTRLLMHeadModel,) if is_tf_available() else ()
       __lowercase     :					Optional[Any]       				=	(
           {
               '''feature-extraction''': TFCTRLModel,
               '''text-classification''': TFCTRLForSequenceClassification,
               '''text-generation''': TFCTRLLMHeadModel,
               '''zero-shot''': TFCTRLForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       __lowercase     :					List[Any]       				=	False
       __lowercase     :					Optional[int]       				=	False


       def 					lowerCAmelCase					(      self			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )					->     Dict:



            '''simple docstring'''




            if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
                 # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
                 # `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
                 # config could not be created.
                 return True

            return False


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



            '''simple docstring'''




            __snake_case			    =     TFCTRLModelTester(self     )
            __snake_case			    =     ConfigTester(self			,     config_class=__SCREAMING_SNAKE_CASE			,     n_embd=37     )


       def 					lowerCAmelCase					(      self     )					->     Dict:



            '''simple docstring'''




            self.config_tester.run_common_tests()


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



            '''simple docstring'''




            __snake_case			    =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_ctrl_model(*__SCREAMING_SNAKE_CASE     )


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



            '''simple docstring'''




            __snake_case			    =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_ctrl_lm_head(*__SCREAMING_SNAKE_CASE     )


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



            '''simple docstring'''




            __snake_case			    =     self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_ctrl_for_sequence_classification(*__SCREAMING_SNAKE_CASE     )


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



            '''simple docstring'''




            __snake_case    ,			__snake_case			    =     self.model_tester.prepare_config_and_inputs_for_common()
            __snake_case			    =     [TFCTRLLMHeadModel]
            __snake_case			    =     [TFCTRLForSequenceClassification]

            for model_class in self.all_model_classes:
                 __snake_case			    =     model_class(__SCREAMING_SNAKE_CASE     )
                 model.build()  # may be needed for the get_bias() call below
                 assert isinstance(model.get_input_embeddings()			,     tf.keras.layers.Layer     )

                 if model_class in list_lm_models:
                      __snake_case			    =     model.get_output_embeddings()
                      assert isinstance(__SCREAMING_SNAKE_CASE			,     tf.keras.layers.Layer     )
                      __snake_case			    =     model.get_bias()
                      assert isinstance(__SCREAMING_SNAKE_CASE			,     __SCREAMING_SNAKE_CASE     )
                      for k, v in name.items():
                           assert isinstance(__SCREAMING_SNAKE_CASE			,     tf.Variable     )
                 elif model_class in list_other_models_with_output_ebd:
                      __snake_case			    =     model.get_output_embeddings()
                      assert isinstance(__SCREAMING_SNAKE_CASE			,     tf.keras.layers.Layer     )
                      __snake_case			    =     model.get_bias()
                      assert name is None
                 else:
                      __snake_case			    =     model.get_output_embeddings()
                      assert x is None
                      __snake_case			    =     model.get_bias()
                      assert name is None




       @slow
       def 					lowerCAmelCase					(      self     )					->     Union[str, Any]:



            '''simple docstring'''




            for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                 __snake_case			    =     TFCTRLModel.from_pretrained(__SCREAMING_SNAKE_CASE     )
                 self.assertIsNotNone(__SCREAMING_SNAKE_CASE     )





@require_tf
class 			lowerCAmelCase      (       unittest.TestCase):


       @slow
       def 					lowerCAmelCase					(      self     )					->     Tuple:



            '''simple docstring'''




            __snake_case			    =     TFCTRLLMHeadModel.from_pretrained('''ctrl'''     )
            __snake_case			    =     tf.convert_to_tensor([[1_1859, 0, 1611, 8]]			,     dtype=tf.intaa     )  # Legal the president is
            __snake_case			    =     [
                1_1859,
                0,
                1611,
                8,
                5,
                150,
                2_6449,
                2,
                19,
                348,
                469,
                3,
                2595,
                48,
                2_0740,
                24_6533,
                24_6533,
                19,
                30,
                5,
            ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

            __snake_case			    =     model.generate(__SCREAMING_SNAKE_CASE			,     do_sample=__SCREAMING_SNAKE_CASE     )
            self.assertListEqual(output_ids[0].numpy().tolist()			,     __SCREAMING_SNAKE_CASE     )






