





'''simple docstring'''

import unittest

from transformers import (
    MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
    Pipeline,
    ZeroShotClassificationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, nested_simplify, require_tf, require_torch, slow

from .test_pipelines_common import ANY


# These 2 model types require different inputs than those of the usual text models.
a   	=    {"LayoutLMv2Config", "LayoutLMv3Config"}







@is_pipeline_test
class __a			( unittest.TestCase      ):
   __UpperCamelCase :			Optional[Any]						   =						MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING
   __UpperCamelCase :			str						   =						TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING

   if model_mapping is not None:
      __UpperCamelCase :			List[str]						   =						{config: model for config, model in model_mapping.items() if config.__name__ not in _TO_SKIP}
   if tf_model_mapping is not None:
      __UpperCamelCase :			Any						   =						{
          config: model for config, model in tf_model_mapping.items() if config.__name__ not in _TO_SKIP
      }





   def   UpperCAmelCase__  (       self					:      Any					,lowerCamelCase					:      Tuple					,lowerCamelCase					:      int					,lowerCamelCase					:      Optional[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							ZeroShotClassificationPipeline(
          model=lowerCamelCase					,tokenizer=lowerCamelCase					,candidate_labels=["""polics""", """health"""]     )
      return classifier, ["Who are you voting for in 2020?", "My stomach hurts."]





   def   UpperCAmelCase__  (       self					:      Optional[int]					,lowerCamelCase					:      Tuple					,lowerCamelCase					:      Tuple     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							classifier("""Who are you voting for in 2020?"""					,candidate_labels="""politics"""     )
      self.assertEqual(lowerCamelCase					,{"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     )]}     )

      # No kwarg
      __SCREAMING_SNAKE_CASE   				=							classifier("""Who are you voting for in 2020?"""					,["""politics"""]     )
      self.assertEqual(lowerCamelCase					,{"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     )]}     )

      __SCREAMING_SNAKE_CASE   				=							classifier("""Who are you voting for in 2020?"""					,candidate_labels=["""politics"""]     )
      self.assertEqual(lowerCamelCase					,{"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     )]}     )

      __SCREAMING_SNAKE_CASE   				=							classifier("""Who are you voting for in 2020?"""					,candidate_labels="""politics, public health"""     )
      self.assertEqual(
          lowerCamelCase					,{"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )]}     )
      self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]     )     )					,1.0     )

      __SCREAMING_SNAKE_CASE   				=							classifier("""Who are you voting for in 2020?"""					,candidate_labels=["""politics""", """public health"""]     )
      self.assertEqual(
          lowerCamelCase					,{"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )]}     )
      self.assertAlmostEqual(sum(nested_simplify(outputs["""scores"""]     )     )					,1.0     )

      __SCREAMING_SNAKE_CASE   				=							classifier(
          """Who are you voting for in 2020?"""					,candidate_labels="""politics"""					,hypothesis_template="""This text is about {}"""     )
      self.assertEqual(lowerCamelCase					,{"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     )]}     )

      # https://github.com/huggingface/transformers/issues/13846
      __SCREAMING_SNAKE_CASE   				=							classifier(["""I am happy"""]					,["""positive""", """negative"""]     )
      self.assertEqual(
          lowerCamelCase					,[
              {"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )]}
              for i in range(1     )
          ]					,)
      __SCREAMING_SNAKE_CASE   				=							classifier(["""I am happy""", """I am sad"""]					,["""positive""", """negative"""]     )
      self.assertEqual(
          lowerCamelCase					,[
              {"""sequence""": ANY(lowerCamelCase     ), """labels""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )], """scores""": [ANY(lowerCamelCase     ), ANY(lowerCamelCase     )]}
              for i in range(2     )
          ]					,)

      with self.assertRaises(lowerCamelCase     ):
         classifier(""""""					,candidate_labels="""politics"""     )

      with self.assertRaises(lowerCamelCase     ):
         classifier(lowerCamelCase					,candidate_labels="""politics"""     )

      with self.assertRaises(lowerCamelCase     ):
         classifier("""Who are you voting for in 2020?"""					,candidate_labels=""""""     )

      with self.assertRaises(lowerCamelCase     ):
         classifier("""Who are you voting for in 2020?"""					,candidate_labels=lowerCamelCase     )

      with self.assertRaises(lowerCamelCase     ):
         classifier(
             """Who are you voting for in 2020?"""					,candidate_labels="""politics"""					,hypothesis_template="""Not formatting template"""					,)

      with self.assertRaises(lowerCamelCase     ):
         classifier(
             """Who are you voting for in 2020?"""					,candidate_labels="""politics"""					,hypothesis_template=lowerCamelCase					,)

      self.run_entailment_id(lowerCamelCase     )





   def   UpperCAmelCase__  (       self					:      Union[str, Any]					,lowerCamelCase					:      Pipeline     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier.model.config
      __SCREAMING_SNAKE_CASE   				=							config.labelaid
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier.entailment_id

      __SCREAMING_SNAKE_CASE   				=							{"""LABEL_0""": 0, """LABEL_1""": 1, """LABEL_2""": 2}
      self.assertEqual(zero_shot_classifier.entailment_id					,-1     )

      __SCREAMING_SNAKE_CASE   				=							{"""entailment""": 0, """neutral""": 1, """contradiction""": 2}
      self.assertEqual(zero_shot_classifier.entailment_id					,0     )

      __SCREAMING_SNAKE_CASE   				=							{"""ENTAIL""": 0, """NON-ENTAIL""": 1}
      self.assertEqual(zero_shot_classifier.entailment_id					,0     )

      __SCREAMING_SNAKE_CASE   				=							{"""ENTAIL""": 2, """NEUTRAL""": 1, """CONTR""": 0}
      self.assertEqual(zero_shot_classifier.entailment_id					,2     )

      __SCREAMING_SNAKE_CASE   				=							original_labelaid
      self.assertEqual(lowerCamelCase					,zero_shot_classifier.entailment_id     )





   @require_torch
   def   UpperCAmelCase__  (       self					:      List[str]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							pipeline(
          """zero-shot-classification"""					,model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""					,framework="""pt"""					,)
      # There was a regression in 4.10 for this
      # Adding a test so we don't make the mistake again.
      # https://github.com/huggingface/transformers/issues/13381#issuecomment-912343499
      zero_shot_classifier(
          """Who are you voting for in 2020?""" * 100					,candidate_labels=["""politics""", """public health""", """science"""]     )





   @require_torch
   def   UpperCAmelCase__  (       self					:      List[str]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							pipeline(
          """zero-shot-classification"""					,model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""					,framework="""pt"""					,)
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier(
          """Who are you voting for in 2020?"""					,candidate_labels=["""politics""", """public health""", """science"""]     )

      self.assertEqual(
          nested_simplify(lowerCamelCase     )					,{
              """sequence""": """Who are you voting for in 2020?""",
              """labels""": ["""science""", """public health""", """politics"""],
              """scores""": [0.333, 0.333, 0.333],
          }					,)





   @require_tf
   def   UpperCAmelCase__  (       self					:      List[str]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							pipeline(
          """zero-shot-classification"""					,model="""sshleifer/tiny-distilbert-base-cased-distilled-squad"""					,framework="""tf"""					,)
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier(
          """Who are you voting for in 2020?"""					,candidate_labels=["""politics""", """public health""", """science"""]     )

      self.assertEqual(
          nested_simplify(lowerCamelCase     )					,{
              """sequence""": """Who are you voting for in 2020?""",
              """labels""": ["""science""", """public health""", """politics"""],
              """scores""": [0.333, 0.333, 0.333],
          }					,)





   @slow
   @require_torch
   def   UpperCAmelCase__  (       self					:      Tuple     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							pipeline("""zero-shot-classification"""					,model="""roberta-large-mnli"""					,framework="""pt"""     )
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier(
          """Who are you voting for in 2020?"""					,candidate_labels=["""politics""", """public health""", """science"""]     )

      self.assertEqual(
          nested_simplify(lowerCamelCase     )					,{
              """sequence""": """Who are you voting for in 2020?""",
              """labels""": ["""politics""", """public health""", """science"""],
              """scores""": [0.976, 0.015, 0.009],
          }					,)
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier(
          """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
          """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
          """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
          """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
          """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
          """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
          """ English-to-German translation task, improving over the existing best results, including ensembles by"""
          """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
          """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
          """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
          """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
          """ large and limited training data."""					,candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]					,multi_label=lowerCamelCase					,)
      self.assertEqual(
          nested_simplify(lowerCamelCase     )					,{
              """sequence""": (
                  """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
                  """ networks in an encoder-decoder configuration. The best performing models also connect the"""
                  """ encoder and decoder through an attention mechanism. We propose a new simple network"""
                  """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
                  """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
                  """ superior in quality while being more parallelizable and requiring significantly less time to"""
                  """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
                  """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
                  """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
                  """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
                  """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
                  """ other tasks by applying it successfully to English constituency parsing both with large and"""
                  """ limited training data."""
              ),
              """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
              """scores""": [0.817, 0.713, 0.018, 0.018],
          }					,)





   @slow
   @require_tf
   def   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							pipeline("""zero-shot-classification"""					,model="""roberta-large-mnli"""					,framework="""tf"""     )
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier(
          """Who are you voting for in 2020?"""					,candidate_labels=["""politics""", """public health""", """science"""]     )

      self.assertEqual(
          nested_simplify(lowerCamelCase     )					,{
              """sequence""": """Who are you voting for in 2020?""",
              """labels""": ["""politics""", """public health""", """science"""],
              """scores""": [0.976, 0.015, 0.009],
          }					,)
      __SCREAMING_SNAKE_CASE   				=							zero_shot_classifier(
          """The dominant sequence transduction models are based on complex recurrent or convolutional neural networks"""
          """ in an encoder-decoder configuration. The best performing models also connect the encoder and decoder"""
          """ through an attention mechanism. We propose a new simple network architecture, the Transformer, based"""
          """ solely on attention mechanisms, dispensing with recurrence and convolutions entirely. Experiments on two"""
          """ machine translation tasks show these models to be superior in quality while being more parallelizable"""
          """ and requiring significantly less time to train. Our model achieves 28.4 BLEU on the WMT 2014"""
          """ English-to-German translation task, improving over the existing best results, including ensembles by"""
          """ over 2 BLEU. On the WMT 2014 English-to-French translation task, our model establishes a new"""
          """ single-model state-of-the-art BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small"""
          """ fraction of the training costs of the best models from the literature. We show that the Transformer"""
          """ generalizes well to other tasks by applying it successfully to English constituency parsing both with"""
          """ large and limited training data."""					,candidate_labels=["""machine learning""", """statistics""", """translation""", """vision"""]					,multi_label=lowerCamelCase					,)
      self.assertEqual(
          nested_simplify(lowerCamelCase     )					,{
              """sequence""": (
                  """The dominant sequence transduction models are based on complex recurrent or convolutional neural"""
                  """ networks in an encoder-decoder configuration. The best performing models also connect the"""
                  """ encoder and decoder through an attention mechanism. We propose a new simple network"""
                  """ architecture, the Transformer, based solely on attention mechanisms, dispensing with recurrence"""
                  """ and convolutions entirely. Experiments on two machine translation tasks show these models to be"""
                  """ superior in quality while being more parallelizable and requiring significantly less time to"""
                  """ train. Our model achieves 28.4 BLEU on the WMT 2014 English-to-German translation task,"""
                  """ improving over the existing best results, including ensembles by over 2 BLEU. On the WMT 2014"""
                  """ English-to-French translation task, our model establishes a new single-model state-of-the-art"""
                  """ BLEU score of 41.8 after training for 3.5 days on eight GPUs, a small fraction of the training"""
                  """ costs of the best models from the literature. We show that the Transformer generalizes well to"""
                  """ other tasks by applying it successfully to English constituency parsing both with large and"""
                  """ limited training data."""
              ),
              """labels""": ["""translation""", """machine learning""", """vision""", """statistics"""],
              """scores""": [0.817, 0.713, 0.018, 0.018],
          }					,)












'''simple docstring'''

import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
        CTRLForSequenceClassification,
        CTRLLMHeadModel,
        CTRLModel,
    )







class __a			:





   def __init__(       self					:      Union[str, Any]					,lowerCamelCase					:      Optional[int]					,lowerCamelCase					:      Tuple=14					,lowerCamelCase					:      Optional[Any]=7					,lowerCamelCase					:      str=True					,lowerCamelCase					:      List[str]=True					,lowerCamelCase					:      Dict=True					,lowerCamelCase					:      Any=True					,lowerCamelCase					:      int=True					,lowerCamelCase					:      Dict=99					,lowerCamelCase					:      Dict=32					,lowerCamelCase					:      Optional[Any]=5					,lowerCamelCase					:      Tuple=4					,lowerCamelCase					:      Optional[int]=37					,lowerCamelCase					:      Optional[int]="gelu"					,lowerCamelCase					:      Optional[Any]=0.1					,lowerCamelCase					:      Tuple=0.1					,lowerCamelCase					:      Dict=512					,lowerCamelCase					:      int=16					,lowerCamelCase					:      Union[str, Any]=2					,lowerCamelCase					:      Tuple=0.02					,lowerCamelCase					:      str=3					,lowerCamelCase					:      Union[str, Any]=4					,lowerCamelCase					:      Any=None					,):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							parent
      __SCREAMING_SNAKE_CASE   				=							batch_size
      __SCREAMING_SNAKE_CASE   				=							seq_length
      __SCREAMING_SNAKE_CASE   				=							is_training
      __SCREAMING_SNAKE_CASE   				=							use_token_type_ids
      __SCREAMING_SNAKE_CASE   				=							use_input_mask
      __SCREAMING_SNAKE_CASE   				=							use_labels
      __SCREAMING_SNAKE_CASE   				=							use_mc_token_ids
      __SCREAMING_SNAKE_CASE   				=							vocab_size
      __SCREAMING_SNAKE_CASE   				=							hidden_size
      __SCREAMING_SNAKE_CASE   				=							num_hidden_layers
      __SCREAMING_SNAKE_CASE   				=							num_attention_heads
      __SCREAMING_SNAKE_CASE   				=							intermediate_size
      __SCREAMING_SNAKE_CASE   				=							hidden_act
      __SCREAMING_SNAKE_CASE   				=							hidden_dropout_prob
      __SCREAMING_SNAKE_CASE   				=							attention_probs_dropout_prob
      __SCREAMING_SNAKE_CASE   				=							max_position_embeddings
      __SCREAMING_SNAKE_CASE   				=							type_vocab_size
      __SCREAMING_SNAKE_CASE   				=							type_sequence_label_size
      __SCREAMING_SNAKE_CASE   				=							initializer_range
      __SCREAMING_SNAKE_CASE   				=							num_labels
      __SCREAMING_SNAKE_CASE   				=							num_choices
      __SCREAMING_SNAKE_CASE   				=							scope
      __SCREAMING_SNAKE_CASE   				=							self.vocab_size - 1





   def   UpperCAmelCase__  (       self					:      Dict     ):

      '''simple docstring'''

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

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

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

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

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

      __SCREAMING_SNAKE_CASE   				=							self.get_config()

      __SCREAMING_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   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      return 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					,)





   def   UpperCAmelCase__  (       self					:      Tuple					,lowerCamelCase					:      Optional[Any]					,lowerCamelCase					:      Tuple					,lowerCamelCase					:      Any					,lowerCamelCase					:      List[str]					,lowerCamelCase					:      str					,*lowerCamelCase					:      Tuple     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							CTRLModel(config=lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()

      model(lowerCamelCase					,token_type_ids=lowerCamelCase					,head_mask=lowerCamelCase     )
      model(lowerCamelCase					,token_type_ids=lowerCamelCase     )
      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase     )
      self.parent.assertEqual(result.last_hidden_state.shape					,(self.batch_size, self.seq_length, self.hidden_size)     )
      self.parent.assertEqual(len(result.past_key_values     )					,config.n_layer     )





   def   UpperCAmelCase__  (       self					:      Optional[int]					,lowerCamelCase					:      int					,lowerCamelCase					:      Optional[Any]					,lowerCamelCase					:      Optional[Any]					,lowerCamelCase					:      int					,lowerCamelCase					:      Dict					,*lowerCamelCase					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							CTRLLMHeadModel(lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()

      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase					,token_type_ids=lowerCamelCase					,labels=lowerCamelCase     )
      self.parent.assertEqual(result.loss.shape					,()     )
      self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.seq_length, self.vocab_size)     )





   def   UpperCAmelCase__  (       self					:      List[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.prepare_config_and_inputs()

      (
          (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  (
          __SCREAMING_SNAKE_CASE
      )				,  
      )   				=							config_and_inputs

      __SCREAMING_SNAKE_CASE   				=							{"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

      return config, inputs_dict





   def   UpperCAmelCase__  (       self					:      Dict					,lowerCamelCase					:      int					,lowerCamelCase					:      str					,lowerCamelCase					:      Optional[Any]					,lowerCamelCase					:      Optional[Any]					,*lowerCamelCase					:      Optional[Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.num_labels
      __SCREAMING_SNAKE_CASE   				=							CTRLForSequenceClassification(lowerCamelCase     )
      model.to(lowerCamelCase     )
      model.eval()
      __SCREAMING_SNAKE_CASE   				=							ids_tensor([self.batch_size]					,self.type_sequence_label_size     )
      __SCREAMING_SNAKE_CASE   				=							model(lowerCamelCase					,token_type_ids=lowerCamelCase					,labels=lowerCamelCase     )
      self.parent.assertEqual(result.logits.shape					,(self.batch_size, self.num_labels)     )







@require_torch
class __a			( _snake_case, _snake_case, _snake_case, unittest.TestCase      ):
   __UpperCamelCase :			str						   =						(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
   __UpperCamelCase :			Dict						   =						(CTRLLMHeadModel,) if is_torch_available() else ()
   __UpperCamelCase :			int						   =						(
       {
           'feature-extraction': CTRLModel,
           'text-classification': CTRLForSequenceClassification,
           'text-generation': CTRLLMHeadModel,
           'zero-shot': CTRLForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   __UpperCamelCase :			List[str]						   =						True
   __UpperCamelCase :			Dict						   =						False
   __UpperCamelCase :			Tuple						   =						False





   def   UpperCAmelCase__  (       self					:      Dict					,lowerCamelCase					:      Any					,lowerCamelCase					:      str					,lowerCamelCase					:      List[str]					,lowerCamelCase					:      int					,lowerCamelCase					:      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   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							CTRLModelTester(self     )
      __SCREAMING_SNAKE_CASE   				=							ConfigTester(self					,config_class=lowerCamelCase					,n_embd=37     )





   def   UpperCAmelCase__  (       self					:      List[str]     ):

      '''simple docstring'''

      super().tearDown()
      # clean-up as much as possible GPU memory occupied by PyTorch
      gc.collect()
      torch.cuda.empty_cache()





   def   UpperCAmelCase__  (       self					:      Optional[int]     ):

      '''simple docstring'''

      self.config_tester.run_common_tests()





   def   UpperCAmelCase__  (       self					:      Dict     ):

      '''simple docstring'''

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





   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(*lowerCamelCase     )





   @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""     )
   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      pass





   @slow
   def   UpperCAmelCase__  (       self					:      List[str]     ):

      '''simple docstring'''

      for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __SCREAMING_SNAKE_CASE   				=							CTRLModel.from_pretrained(lowerCamelCase     )
         self.assertIsNotNone(lowerCamelCase     )





   @unittest.skip("""The model doesn't support left padding"""     )  # and it's not used enough to be worth fixing :)
   def   UpperCAmelCase__  (       self					:      Tuple     ):

      '''simple docstring'''

      pass







@require_torch
class __a			( unittest.TestCase      ):





   def   UpperCAmelCase__  (       self					:      int     ):

      '''simple docstring'''

      super().tearDown()
      # clean-up as much as possible GPU memory occupied by PyTorch
      gc.collect()
      torch.cuda.empty_cache()





   @slow
   def   UpperCAmelCase__  (       self					:      Union[str, Any]     ):

      '''simple docstring'''

      __SCREAMING_SNAKE_CASE   				=							CTRLLMHeadModel.from_pretrained("""ctrl"""     )
      model.to(lowerCamelCase     )
      __SCREAMING_SNAKE_CASE   				=							torch.tensor(
          [[1_1859, 0, 1611, 8]]					,dtype=torch.long					,device=lowerCamelCase     )  # Legal the president is
      __SCREAMING_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

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





