

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  ,  _UpperCAmelCase  ,  ):
   __UpperCAmelCase	:  Dict		=     parent
   __UpperCAmelCase	:  Tuple		=     13
   __UpperCAmelCase	:  Any		=     7
   __UpperCAmelCase	:  Tuple		=     True
   __UpperCAmelCase	:  Any		=     True
   __UpperCAmelCase	:  str		=     True
   __UpperCAmelCase	:  Tuple		=     True
   __UpperCAmelCase	:  List[Any]		=     True
   __UpperCAmelCase	:  Optional[int]		=     99
   __UpperCAmelCase	:  Tuple		=     32
   __UpperCAmelCase	:  str		=     2
   __UpperCAmelCase	:  Optional[int]		=     4
   __UpperCAmelCase	:  str		=     37
   __UpperCAmelCase	:  Dict		=     'gelu'
   __UpperCAmelCase	:  Tuple		=     0.1
   __UpperCAmelCase	:  str		=     0.1
   __UpperCAmelCase	:  int		=     512
   __UpperCAmelCase	:  int		=     16
   __UpperCAmelCase	:  str		=     2
   __UpperCAmelCase	:  Optional[Any]		=     0.0_2
   __UpperCAmelCase	:  Optional[Any]		=     3
   __UpperCAmelCase	:  Any		=     4
   __UpperCAmelCase	:  Dict		=     None
   __UpperCAmelCase	:  Union[str, Any]		=     self.vocab_size - 1




 def     a       (			self							):
   __UpperCAmelCase	:  Union[str, Any]		=     ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size							)

   __UpperCAmelCase	:  List[Any]		=     None
   if self.use_input_mask:
     __UpperCAmelCase	:  List[str]		=     random_attention_mask([self.batch_size, self.seq_length]							)

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

   __UpperCAmelCase	:  int		=     None
   if self.use_mc_token_ids:
     __UpperCAmelCase	:  List[str]		=     ids_tensor([self.batch_size, self.num_choices]  ,  self.seq_length							)

   __UpperCAmelCase	:  Tuple		=     None
   __UpperCAmelCase	:  Any		=     None
   __UpperCAmelCase	:  Any		=     None
   if self.use_labels:
     __UpperCAmelCase	:  Any		=     ids_tensor([self.batch_size]  ,  self.type_sequence_label_size							)
     __UpperCAmelCase	:  Union[str, Any]		=     ids_tensor([self.batch_size, self.seq_length]  ,  self.num_labels							)
     __UpperCAmelCase	:  List[str]		=     ids_tensor([self.batch_size]  ,  self.num_choices							)

   __UpperCAmelCase	:  Any		=     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  ,  )

   __UpperCAmelCase	:  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     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  *_UpperCAmelCase							):
   __UpperCAmelCase	:  List[Any]		=     TFCTRLModel(config=_UpperCAmelCase							)
   __UpperCAmelCase	:  Optional[Any]		=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
   __UpperCAmelCase	:  Dict		=     model(_UpperCAmelCase							)

   __UpperCAmelCase	:  int		=     [input_ids, None, input_mask]  # None is the input for 'past'
   __UpperCAmelCase	:  str		=     model(_UpperCAmelCase							)

   __UpperCAmelCase	:  Optional[int]		=     model(_UpperCAmelCase							)

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




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  *_UpperCAmelCase							):
   __UpperCAmelCase	:  Any		=     TFCTRLLMHeadModel(config=_UpperCAmelCase							)
   __UpperCAmelCase	:  Union[str, Any]		=     {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
   __UpperCAmelCase	:  str		=     model(_UpperCAmelCase							)
   self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)							)




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  *_UpperCAmelCase							):
   __UpperCAmelCase	:  str		=     self.num_labels
   __UpperCAmelCase	:  Union[str, Any]		=     ids_tensor([self.batch_size]  ,  self.type_sequence_label_size							)
   __UpperCAmelCase	:  Optional[int]		=     {
       'input_ids': input_ids,
       'token_type_ids': token_type_ids,
       'labels': sequence_labels,
   }
   __UpperCAmelCase	:  List[Any]		=     TFCTRLForSequenceClassification(_UpperCAmelCase							)
   __UpperCAmelCase	:  str		=     model(_UpperCAmelCase							)
   self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.num_labels)							)




 def     a       (			self							):
   __UpperCAmelCase	:  Dict		=     self.prepare_config_and_inputs()

   (
       (
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			(
       __UpperCAmelCase
   ),			
   )	:  Tuple		=     config_and_inputs

   __UpperCAmelCase	:  Union[str, Any]		=     {'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):
 _lowerCAmelCase									=	(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
 _lowerCAmelCase									=	(TFCTRLLMHeadModel,) if is_tf_available() else ()
 _lowerCAmelCase									=	(
     {
         '''feature-extraction''': TFCTRLModel,
         '''text-classification''': TFCTRLForSequenceClassification,
         '''text-generation''': TFCTRLLMHeadModel,
         '''zero-shot''': TFCTRLForSequenceClassification,
     }
     if is_tf_available()
     else {}
 )
 _lowerCAmelCase									=	False
 _lowerCAmelCase									=	False




 def     a       (			self  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase  ,  _UpperCAmelCase							):
   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     a       (			self							):
   __UpperCAmelCase	:  Optional[Any]		=     TFCTRLModelTester(self							)
   __UpperCAmelCase	:  Optional[Any]		=     ConfigTester(self  ,  config_class=_UpperCAmelCase  ,  n_embd=37							)




 def     a       (			self							):
   self.config_tester.run_common_tests()




 def     a       (			self							):
   __UpperCAmelCase	:  List[Any]		=     self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_ctrl_model(*_UpperCAmelCase							)




 def     a       (			self							):
   __UpperCAmelCase	:  Dict		=     self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_ctrl_lm_head(*_UpperCAmelCase							)




 def     a       (			self							):
   __UpperCAmelCase	:  int		=     self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_ctrl_for_sequence_classification(*_UpperCAmelCase							)




 def     a       (			self							):
   __UpperCAmelCase,			__UpperCAmelCase	:  Tuple		=     self.model_tester.prepare_config_and_inputs_for_common()
   __UpperCAmelCase	:  Dict		=     [TFCTRLLMHeadModel]
   __UpperCAmelCase	:  str		=     [TFCTRLForSequenceClassification]

   for model_class in self.all_model_classes:
     __UpperCAmelCase	:  Union[str, Any]		=     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:
       __UpperCAmelCase	:  List[str]		=     model.get_output_embeddings()
       assert isinstance(_UpperCAmelCase  ,  tf.keras.layers.Layer							)
       __UpperCAmelCase	:  Tuple		=     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:
       __UpperCAmelCase	:  List[str]		=     model.get_output_embeddings()
       assert isinstance(_UpperCAmelCase  ,  tf.keras.layers.Layer							)
       __UpperCAmelCase	:  Union[str, Any]		=     model.get_bias()
       assert name is None
     else:
       __UpperCAmelCase	:  int		=     model.get_output_embeddings()
       assert x is None
       __UpperCAmelCase	:  Optional[Any]		=     model.get_bias()
       assert name is None




 @slow
 def     a       (			self							):
   for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     __UpperCAmelCase	:  str		=     TFCTRLModel.from_pretrained(_UpperCAmelCase							)
     self.assertIsNotNone(_UpperCAmelCase							)



@require_tf
class      lowerCamelCase_ (			unittest.TestCase):




 @slow
 def     a       (			self							):
   __UpperCAmelCase	:  Any		=     TFCTRLLMHeadModel.from_pretrained('ctrl'							)
   __UpperCAmelCase	:  Optional[int]		=     tf.convert_to_tensor([[11_859, 0, 1_611, 8]]  ,  dtype=tf.intaa							)  # Legal the president is
   __UpperCAmelCase	:  str		=     [
       11_859,
       0,
       1_611,
       8,
       5,
       150,
       26_449,
       2,
       19,
       348,
       469,
       3,
       2_595,
       48,
       20_740,
       246_533,
       246_533,
       19,
       30,
       5,
   ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

   __UpperCAmelCase	:  Union[str, Any]		=     model.generate(_UpperCAmelCase  ,  do_sample=_UpperCAmelCase							)
   self.assertListEqual(output_ids[0].numpy().tolist()  ,  _UpperCAmelCase							)








def  _snake_case		(snake_case_					:     int = 100    ,       snake_case_					:     int = 100  )							->		int:
  return max(
      sum(int(snake_case_  ) for x in str(base**power  )  )
      for base in range(snake_case_  )
      for power in range(snake_case_  )  )


# Tests
if __name__ == "__main__":
      import doctest

      doctest.testmod()





