code: 
  
  
  
  
  
  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 (    _SCREAMING_SNAKE_CASE    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        def __init__( self				,						UpperCAmelCase				,						)       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[Any]        =       parent
          __snake_case   :				Tuple        =       13
          __snake_case   :				str        =       7
          __snake_case   :				Optional[Any]        =       True
          __snake_case   :				str        =       True
          __snake_case   :				Dict        =       True
          __snake_case   :				str        =       True
          __snake_case   :				Dict        =       True
          __snake_case   :				int        =       99
          __snake_case   :				Optional[Any]        =       32
          __snake_case   :				Union[str, Any]        =       2
          __snake_case   :				Any        =       4
          __snake_case   :				Any        =       37
          __snake_case   :				str        =       "gelu"
          __snake_case   :				Optional[int]        =       0.1
          __snake_case   :				List[str]        =       0.1
          __snake_case   :				Tuple        =       512
          __snake_case   :				Optional[Any]        =       16
          __snake_case   :				List[Any]        =       2
          __snake_case   :				List[str]        =       0.02
          __snake_case   :				List[Any]        =       3
          __snake_case   :				int        =       4
          __snake_case   :				int        =       None
          __snake_case   :				Any        =       self.vocab_size - 1
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Dict        =       ids_tensor([self.batch_size, self.seq_length]				,						self.vocab_size   )
  
          __snake_case   :				List[str]        =       None
          if self.use_input_mask:
            __snake_case   :				Any        =       random_attention_mask([self.batch_size, self.seq_length]   )
  
          __snake_case   :				List[Any]        =       None
          if self.use_token_type_ids:
            __snake_case   :				Any        =       ids_tensor([self.batch_size, self.seq_length]				,						self.type_vocab_size   )
  
          __snake_case   :				Optional[Any]        =       None
          if self.use_mc_token_ids:
            __snake_case   :				Union[str, Any]        =       ids_tensor([self.batch_size, self.num_choices]				,						self.seq_length   )
  
          __snake_case   :				List[Any]        =       None
          __snake_case   :				Tuple        =       None
          __snake_case   :				int        =       None
          if self.use_labels:
            __snake_case   :				List[Any]        =       ids_tensor([self.batch_size]				,						self.type_sequence_label_size   )
            __snake_case   :				List[Any]        =       ids_tensor([self.batch_size, self.seq_length]				,						self.num_labels   )
            __snake_case   :				Optional[int]        =       ids_tensor([self.batch_size]				,						self.num_choices   )
  
          __snake_case   :				str        =       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   :				Union[str, Any]        =       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				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						*UpperCAmelCase   )       ->   List[str]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Union[str, Any]        =       TFCTRLModel(config=UpperCamelCase__   )
          __snake_case   :				Dict        =       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          __snake_case   :				List[str]        =       model(UpperCamelCase__   )
  
          __snake_case   :				int        =       [input_ids, None, input_mask]  # None is the input for 'past'
          __snake_case   :				List[Any]        =       model(UpperCamelCase__   )
  
          __snake_case   :				Tuple        =       model(UpperCamelCase__   )
  
          self.parent.assertEqual(result.last_hidden_state.shape				,						(self.batch_size, self.seq_length, self.hidden_size)   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						*UpperCAmelCase   )       ->   str:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[int]        =       TFCTRLLMHeadModel(config=UpperCamelCase__   )
          __snake_case   :				Optional[Any]        =       {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          __snake_case   :				List[Any]        =       model(UpperCamelCase__   )
          self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.seq_length, self.vocab_size)   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						*UpperCAmelCase   )       ->   List[str]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Tuple        =       self.num_labels
          __snake_case   :				List[str]        =       ids_tensor([self.batch_size]				,						self.type_sequence_label_size   )
          __snake_case   :				str        =       {
              "input_ids": input_ids,
              "token_type_ids": token_type_ids,
              "labels": sequence_labels,
          }
          __snake_case   :				List[str]        =       TFCTRLForSequenceClassification(UpperCamelCase__   )
          __snake_case   :				Tuple        =       model(UpperCamelCase__   )
          self.parent.assertEqual(result.logits.shape				,						(self.batch_size, self.num_labels)   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   int:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Tuple        =       self.prepare_config_and_inputs()
  
          (
              (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,   (
              __snake_case 
          )    ,    
          )   :				Any        =       config_and_inputs
  
          __snake_case   :				Tuple        =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
          return config, inputs_dict
  
  
  
  
  @require_tf
  class 				_lowerCamelCase (    _SCREAMING_SNAKE_CASE ,   _SCREAMING_SNAKE_CASE ,   unittest.TestCase    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        UpperCAmelCase_						:      Optional[Any]       =(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
        UpperCAmelCase_						:      Tuple       =(TFCTRLLMHeadModel,) if is_tf_available() else ()
        UpperCAmelCase_						:      List[str]       =(
            {
                'feature-extraction': TFCTRLModel,
                'text-classification': TFCTRLForSequenceClassification,
                'text-generation': TFCTRLLMHeadModel,
                'zero-shot': TFCTRLForSequenceClassification,
            }
            if is_tf_available()
            else {}
        )
        UpperCAmelCase_						:      Dict       =False
        UpperCAmelCase_						:      Optional[Any]       =False
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase				,						UpperCAmelCase   )       ->   Optional[int]:
  
  
  
  
  
  
          '''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   )       ->   Union[str, Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				str        =       TFCTRLModelTester(self   )
          __snake_case   :				Tuple        =       ConfigTester(self				,						config_class=UpperCamelCase__				,						n_embd=37   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Any:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          self.config_tester.run_common_tests()
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   List[str]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				int        =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_model(*UpperCamelCase__   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   List[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Optional[Any]        =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_lm_head(*UpperCamelCase__   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Dict:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				int        =       self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_for_sequence_classification(*UpperCamelCase__   )
  
  
  
  
  
  
  
        def 						UpperCAmelCase		( self   )       ->   Tuple:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case    ,   __snake_case   :				Optional[int]        =       self.model_tester.prepare_config_and_inputs_for_common()
          __snake_case   :				int        =       [TFCTRLLMHeadModel]
          __snake_case   :				Dict        =       [TFCTRLForSequenceClassification]
  
          for model_class in self.all_model_classes:
            __snake_case   :				Dict        =       model_class(UpperCamelCase__   )
            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   :				int        =       model.get_output_embeddings()
              assert isinstance(UpperCamelCase__				,						tf.keras.layers.Layer   )
              __snake_case   :				Union[str, Any]        =       model.get_bias()
              assert isinstance(UpperCamelCase__				,						UpperCamelCase__   )
              for k, v in name.items():
                assert isinstance(UpperCamelCase__				,						tf.Variable   )
            elif model_class in list_other_models_with_output_ebd:
              __snake_case   :				Optional[Any]        =       model.get_output_embeddings()
              assert isinstance(UpperCamelCase__				,						tf.keras.layers.Layer   )
              __snake_case   :				Optional[Any]        =       model.get_bias()
              assert name is None
            else:
              __snake_case   :				List[str]        =       model.get_output_embeddings()
              assert x is None
              __snake_case   :				Optional[Any]        =       model.get_bias()
              assert name is None
  
  
  
  
  
  
  
        @slow
        def 						UpperCAmelCase		( self   )       ->   Optional[Any]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __snake_case   :				List[Any]        =       TFCTRLModel.from_pretrained(UpperCamelCase__   )
            self.assertIsNotNone(UpperCamelCase__   )
  
  
  
  
  @require_tf
  class 				_lowerCamelCase (    unittest.TestCase    ):
  
  
  
        """simple docstring"""
  
  
  
  
  
  
        @slow
        def 						UpperCAmelCase		( self   )       ->   Optional[int]:
  
  
  
  
  
  
          '''simple docstring'''
  
  
  
  
  
  
          __snake_case   :				Tuple        =       TFCTRLLMHeadModel.from_pretrained("ctrl"   )
          __snake_case   :				Tuple        =       tf.convert_to_tensor([[11859, 0, 1611, 8]]				,						dtype=tf.intaa   )  # Legal the president is
          __snake_case   :				str        =       [
              11859,
              0,
              1611,
              8,
              5,
              150,
              26449,
              2,
              19,
              348,
              469,
              3,
              2595,
              48,
              20740,
              246533,
              246533,
              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   :				Optional[Any]        =       model.generate(UpperCamelCase__				,						do_sample=UpperCamelCase__   )
          self.assertListEqual(output_ids[0].numpy().tolist()				,						UpperCamelCase__   )
  
  
style_context: 
  
  
  
  
  
  def       lowerCAmelCase__(	lowercase       :    str				) ->    int:
    __snake_case   :				List[str]        =       str(lowercase				).strip()
    if not oct_string:
      raise ValueError("Empty string was passed to the function"				)
    __snake_case   :				int        =       oct_string[0] == "-"
    if is_negative:
      __snake_case   :				Union[str, Any]        =       oct_string[1:]
    if not oct_string.isdigit() or not all(0 <= int(lowercase				) <= 7 for char in oct_string				):
      raise ValueError("Non-octal value was passed to the function"				)
    __snake_case   :				List[Any]        =       0
    for char in oct_string:
      __snake_case   :				Optional[Any]        =       8 * decimal_number + int(lowercase				)
    if is_negative:
      __snake_case   :				Any        =       -decimal_number
    return decimal_number
  
  
  if __name__ == "__main__":
       from doctest import testmod
  
       testmod()
  
  