


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     _lowercase				:


       """simple docstring"""




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


             '''simple docstring'''



             lowerCamelCase__	:       List[Any]					=						parent
             lowerCamelCase__	:       Dict					=						batch_size
             lowerCamelCase__	:       Union[str, Any]					=						seq_length
             lowerCamelCase__	:       List[str]					=						is_training
             lowerCamelCase__	:       int					=						use_token_type_ids
             lowerCamelCase__	:       Dict					=						use_input_mask
             lowerCamelCase__	:       Any					=						use_labels
             lowerCamelCase__	:       List[str]					=						use_mc_token_ids
             lowerCamelCase__	:       str					=						vocab_size
             lowerCamelCase__	:       int					=						hidden_size
             lowerCamelCase__	:       Union[str, Any]					=						num_hidden_layers
             lowerCamelCase__	:       Dict					=						num_attention_heads
             lowerCamelCase__	:       Union[str, Any]					=						intermediate_size
             lowerCamelCase__	:       Optional[Any]					=						hidden_act
             lowerCamelCase__	:       List[str]					=						hidden_dropout_prob
             lowerCamelCase__	:       Tuple					=						attention_probs_dropout_prob
             lowerCamelCase__	:       Tuple					=						max_position_embeddings
             lowerCamelCase__	:       Any					=						type_vocab_size
             lowerCamelCase__	:       List[str]					=						type_sequence_label_size
             lowerCamelCase__	:       int					=						initializer_range
             lowerCamelCase__	:       str					=						num_labels
             lowerCamelCase__	:       str					=						num_choices
             lowerCamelCase__	:       Tuple					=						scope
             lowerCamelCase__	:       str					=						self.vocab_size - 1





       def 		lowerCAmelCase  (     self							:    Any ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[Any]					=						ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size )

             lowerCamelCase__	:       Dict					=						None
             if self.use_input_mask:
                   lowerCamelCase__	:       str					=						random_attention_mask([self.batch_size, self.seq_length] )

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

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

             lowerCamelCase__	:       Optional[Any]					=						None
             lowerCamelCase__	:       List[Any]					=						None
             lowerCamelCase__	:       int					=						None
             if self.use_labels:
                   lowerCamelCase__	:       Optional[int]					=						ids_tensor([self.batch_size]      ,   self.type_sequence_label_size )
                   lowerCamelCase__	:       str					=						ids_tensor([self.batch_size, self.seq_length]      ,   self.num_labels )
                   lowerCamelCase__	:       List[Any]					=						ids_tensor([self.batch_size]      ,   self.num_choices )

             lowerCamelCase__	:       Tuple					=						self.get_config()

             lowerCamelCase__	:       Dict					=						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							:    Optional[Any] ):


             '''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 		lowerCAmelCase  (     self							:    int      ,   __lowerCamelCase							:    List[Any]      ,   __lowerCamelCase							:    List[str]      ,   __lowerCamelCase							:    List[Any]      ,   __lowerCamelCase							:    Tuple      ,   __lowerCamelCase							:    Any      ,   *__lowerCamelCase							:    Tuple ):


             '''simple docstring'''



             lowerCamelCase__	:       Dict					=						CTRLModel(config=__lowerCamelCase )
             model.to(__lowerCamelCase )
             model.eval()

             model(__lowerCamelCase      ,   token_type_ids=__lowerCamelCase      ,   head_mask=__lowerCamelCase )
             model(__lowerCamelCase      ,   token_type_ids=__lowerCamelCase )
             lowerCamelCase__	:       Union[str, Any]					=						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 		lowerCAmelCase  (     self							:    Any      ,   __lowerCamelCase							:    Optional[Any]      ,   __lowerCamelCase							:    Dict      ,   __lowerCamelCase							:    Tuple      ,   __lowerCamelCase							:    Union[str, Any]      ,   __lowerCamelCase							:    Optional[int]      ,   *__lowerCamelCase							:    str ):


             '''simple docstring'''



             lowerCamelCase__	:       int					=						CTRLLMHeadModel(__lowerCamelCase )
             model.to(__lowerCamelCase )
             model.eval()

             lowerCamelCase__	:       Any					=						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 		lowerCAmelCase  (     self							:    str ):


             '''simple docstring'''



             lowerCamelCase__	:       Optional[Any]					=						self.prepare_config_and_inputs()

             (
                 (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     (
                 lowerCamelCase__
             )					,     
             )	:       Tuple					=						config_and_inputs

             lowerCamelCase__	:       Optional[Any]					=						{"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

             return config, inputs_dict





       def 		lowerCAmelCase  (     self							:    int      ,   __lowerCamelCase							:    List[str]      ,   __lowerCamelCase							:    str      ,   __lowerCamelCase							:    List[Any]      ,   __lowerCamelCase							:    Tuple      ,   *__lowerCamelCase							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       List[Any]					=						self.num_labels
             lowerCamelCase__	:       List[Any]					=						CTRLForSequenceClassification(__lowerCamelCase )
             model.to(__lowerCamelCase )
             model.eval()
             lowerCamelCase__	:       Tuple					=						ids_tensor([self.batch_size]      ,   self.type_sequence_label_size )
             lowerCamelCase__	:       Dict					=						model(__lowerCamelCase      ,   token_type_ids=__lowerCamelCase      ,   labels=__lowerCamelCase )
             self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.num_labels) )







@require_torch
class     _lowercase				(							lowercase__							,						lowercase__							,						lowercase__							,						unittest.TestCase):


       """simple docstring"""




       A__      		=    (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       A__      		=    (CTRLLMHeadModel,) if is_torch_available() else ()
       A__      		=    (
           {
               "feature-extraction": CTRLModel,
               "text-classification": CTRLForSequenceClassification,
               "text-generation": CTRLLMHeadModel,
               "zero-shot": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       A__      		=    True
       A__      		=    False
       A__      		=    False





       def 		lowerCAmelCase  (     self							:    Tuple      ,   __lowerCamelCase							:    Optional[Any]      ,   __lowerCamelCase							:    Optional[Any]      ,   __lowerCamelCase							:    Any      ,   __lowerCamelCase							:    int      ,   __lowerCamelCase							:    Union[str, Any] ):


             '''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							:    Any ):


             '''simple docstring'''



             lowerCamelCase__	:       Union[str, Any]					=						CTRLModelTester(self )
             lowerCamelCase__	:       str					=						ConfigTester(self      ,   config_class=__lowerCamelCase      ,   n_embd=37 )





       def 		lowerCAmelCase  (     self							:    Any ):


             '''simple docstring'''



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





       def 		lowerCAmelCase  (     self							:    Optional[Any] ):


             '''simple docstring'''



             self.config_tester.run_common_tests()





       def 		lowerCAmelCase  (     self							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       Tuple					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_ctrl_model(*__lowerCamelCase )





       def 		lowerCAmelCase  (     self							:    Dict ):


             '''simple docstring'''



             lowerCamelCase__	:       Dict					=						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 		lowerCAmelCase  (     self							:    Optional[Any] ):


             '''simple docstring'''



             pass





       @slow
       def 		lowerCAmelCase  (     self							:    Optional[int] ):


             '''simple docstring'''



             for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   lowerCamelCase__	:       str					=						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 		lowerCAmelCase  (     self							:    Optional[Any] ):


             '''simple docstring'''



             pass







@require_torch
class     _lowercase				(							unittest.TestCase):


       """simple docstring"""




       def 		lowerCAmelCase  (     self							:    Optional[int] ):


             '''simple docstring'''



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





       @slow
       def 		lowerCAmelCase  (     self							:    Tuple ):


             '''simple docstring'''



             lowerCamelCase__	:       int					=						CTRLLMHeadModel.from_pretrained("ctrl" )
             model.to(__lowerCamelCase )
             lowerCamelCase__	:       Optional[int]					=						torch.tensor(
                 [[11859, 0, 1611, 8]]      ,   dtype=torch.long      ,   device=__lowerCamelCase )  # Legal the president is
             lowerCamelCase__	:       Optional[Any]					=						[
                 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

             lowerCamelCase__	:       List[Any]					=						model.generate(__lowerCamelCase      ,   do_sample=__lowerCamelCase )
             self.assertListEqual(output_ids[0].tolist()      ,   __lowerCamelCase )






def    lowercase_	(	_A    :					int					,      _A    :					int		):

      """simple docstring"""




      if a < 0 or b < 0:
            raise ValueError("the value of both inputs must be positive"		)

      lowerCamelCase__	:       List[str]					=						str(bin(_A		)		)[2:]  # remove the leading "0b"
      lowerCamelCase__	:       List[Any]					=						str(bin(_A		)		)[2:]  # remove the leading "0b"

      lowerCamelCase__	:       List[Any]					=						max(len(_A		)					,      len(_A		)		)

      return "0b" + "".join(
          str(int(char_a != char_b		)		)
          for char_a, char_b in zip(a_binary.zfill(_A		)					,      b_binary.zfill(_A		)		)		)


if __name__ == "__main__":
    import doctest

    doctest.testmod()


